-
Notifications
You must be signed in to change notification settings - Fork 476
refactor(desktop): store organizations as markdown files with YAML frontmatter #2793
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
yujonglee
merged 1 commit into
main
from
devin/1767511753-refactor-organization-persister
Jan 4, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
55 changes: 55 additions & 0 deletions
55
apps/desktop/src/store/tinybase/persister/organization/collect.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| import type { MergeableStore, OptionalSchemas } from "tinybase/with-schemas"; | ||
|
|
||
| import type { FrontmatterInput, JsonValue } from "@hypr/plugin-export"; | ||
| import type { OrganizationStorage } from "@hypr/store"; | ||
|
|
||
| import type { CollectorResult, TablesContent } from "../utils"; | ||
| import { getOrganizationDir, getOrganizationFilePath } from "./utils"; | ||
|
|
||
| export interface OrganizationCollectorResult extends CollectorResult { | ||
| validOrgIds: Set<string>; | ||
| } | ||
|
|
||
| type OrganizationsTable = Record<string, OrganizationStorage>; | ||
|
|
||
| export function collectOrganizationWriteOps<Schemas extends OptionalSchemas>( | ||
| _store: MergeableStore<Schemas>, | ||
| tables: TablesContent, | ||
| dataDir: string, | ||
| ): OrganizationCollectorResult { | ||
| const dirs = new Set<string>(); | ||
| const operations: CollectorResult["operations"] = []; | ||
| const validOrgIds = new Set<string>(); | ||
|
|
||
| const organizationsDir = getOrganizationDir(dataDir); | ||
| dirs.add(organizationsDir); | ||
|
|
||
| const organizations = | ||
| (tables as { organizations?: OrganizationsTable }).organizations ?? {}; | ||
|
|
||
| const frontmatterItems: [FrontmatterInput, string][] = []; | ||
|
|
||
| for (const [orgId, org] of Object.entries(organizations)) { | ||
| validOrgIds.add(orgId); | ||
|
|
||
| const frontmatter: Record<string, JsonValue> = { | ||
| created_at: org.created_at ?? "", | ||
| name: org.name ?? "", | ||
| user_id: org.user_id ?? "", | ||
| }; | ||
|
|
||
| const body = ""; | ||
| const filePath = getOrganizationFilePath(dataDir, orgId); | ||
|
|
||
| frontmatterItems.push([{ frontmatter, content: body }, filePath]); | ||
| } | ||
|
|
||
| if (frontmatterItems.length > 0) { | ||
| operations.push({ | ||
| type: "frontmatter-batch", | ||
| items: frontmatterItems, | ||
| }); | ||
| } | ||
|
|
||
| return { dirs, operations, validOrgIds }; | ||
| } |
99 changes: 99 additions & 0 deletions
99
apps/desktop/src/store/tinybase/persister/organization/load.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| import { readDir, readTextFile, remove } from "@tauri-apps/plugin-fs"; | ||
|
|
||
| import type { OrganizationStorage } from "@hypr/store"; | ||
|
|
||
| import { isFileNotFoundError, isUUID } from "../utils"; | ||
| import { | ||
| getOrganizationDir, | ||
| getOrganizationFilePath, | ||
| parseMarkdownWithFrontmatter, | ||
| } from "./utils"; | ||
|
|
||
| export async function loadAllOrganizations( | ||
| dataDir: string, | ||
| ): Promise<Record<string, OrganizationStorage>> { | ||
| const result: Record<string, OrganizationStorage> = {}; | ||
| const organizationsDir = getOrganizationDir(dataDir); | ||
|
|
||
| let entries: { name: string; isDirectory: boolean }[]; | ||
| try { | ||
| entries = await readDir(organizationsDir); | ||
| } catch (error) { | ||
| if (isFileNotFoundError(error)) { | ||
| return result; | ||
| } | ||
| throw error; | ||
| } | ||
|
|
||
| for (const entry of entries) { | ||
| if (entry.isDirectory) continue; | ||
| if (!entry.name.endsWith(".md")) continue; | ||
|
|
||
| const orgId = entry.name.replace(/\.md$/, ""); | ||
| if (!isUUID(orgId)) { | ||
| console.warn( | ||
| `[OrganizationPersister] Skipping non-UUID file: ${entry.name}`, | ||
| ); | ||
| continue; | ||
| } | ||
|
|
||
| try { | ||
| const filePath = getOrganizationFilePath(dataDir, orgId); | ||
| const content = await readTextFile(filePath); | ||
| const { frontmatter } = await parseMarkdownWithFrontmatter(content); | ||
|
|
||
| result[orgId] = { | ||
| user_id: String(frontmatter.user_id ?? ""), | ||
| created_at: String(frontmatter.created_at ?? ""), | ||
| name: String(frontmatter.name ?? ""), | ||
| }; | ||
| } catch (error) { | ||
| console.error( | ||
| `[OrganizationPersister] Failed to load organization ${orgId}:`, | ||
| error, | ||
| ); | ||
| continue; | ||
| } | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| export async function cleanupOrphanOrganizationFiles( | ||
| dataDir: string, | ||
| validOrgIds: Set<string>, | ||
| ): Promise<void> { | ||
| const organizationsDir = getOrganizationDir(dataDir); | ||
|
|
||
| let entries: { name: string; isDirectory: boolean }[]; | ||
| try { | ||
| entries = await readDir(organizationsDir); | ||
| } catch (error) { | ||
| if (isFileNotFoundError(error)) { | ||
| return; | ||
| } | ||
| throw error; | ||
| } | ||
|
|
||
| for (const entry of entries) { | ||
| if (entry.isDirectory) continue; | ||
| if (!entry.name.endsWith(".md")) continue; | ||
|
|
||
| const orgId = entry.name.replace(/\.md$/, ""); | ||
| if (!isUUID(orgId)) continue; | ||
|
|
||
| if (!validOrgIds.has(orgId)) { | ||
| try { | ||
| const filePath = getOrganizationFilePath(dataDir, orgId); | ||
| await remove(filePath); | ||
| } catch (error) { | ||
| if (!isFileNotFoundError(error)) { | ||
| console.error( | ||
| `[OrganizationPersister] Failed to remove orphan file ${entry.name}:`, | ||
| error, | ||
| ); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } |
77 changes: 77 additions & 0 deletions
77
apps/desktop/src/store/tinybase/persister/organization/migrate.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| import { sep } from "@tauri-apps/api/path"; | ||
| import { exists, mkdir, readTextFile, remove } from "@tauri-apps/plugin-fs"; | ||
|
|
||
| import { | ||
| commands as exportCommands, | ||
| type FrontmatterInput, | ||
| type JsonValue, | ||
| } from "@hypr/plugin-export"; | ||
| import type { OrganizationStorage } from "@hypr/store"; | ||
|
|
||
| import { isFileNotFoundError } from "../utils"; | ||
| import { getOrganizationDir, getOrganizationFilePath } from "./utils"; | ||
|
|
||
| export async function migrateOrganizationsJsonIfNeeded( | ||
| dataDir: string, | ||
| ): Promise<void> { | ||
| const organizationsJsonPath = [dataDir, "organizations.json"].join(sep()); | ||
| const organizationsDir = getOrganizationDir(dataDir); | ||
|
|
||
| const jsonExists = await exists(organizationsJsonPath); | ||
| if (!jsonExists) { | ||
| return; | ||
| } | ||
|
|
||
| const dirExists = await exists(organizationsDir); | ||
| if (dirExists) { | ||
| return; | ||
| } | ||
|
|
||
| console.log( | ||
| "[OrganizationPersister] Migrating from organizations.json to organizations/*.md", | ||
| ); | ||
|
|
||
| try { | ||
| const content = await readTextFile(organizationsJsonPath); | ||
| const organizations = JSON.parse(content) as Record< | ||
| string, | ||
| OrganizationStorage | ||
| >; | ||
|
|
||
| await mkdir(organizationsDir, { recursive: true }); | ||
|
|
||
| const batchItems: [FrontmatterInput, string][] = []; | ||
|
|
||
| for (const [orgId, org] of Object.entries(organizations)) { | ||
| const frontmatter: Record<string, JsonValue> = { | ||
| created_at: org.created_at ?? "", | ||
| name: org.name ?? "", | ||
| user_id: org.user_id ?? "", | ||
| }; | ||
|
|
||
| const body = ""; | ||
| const filePath = getOrganizationFilePath(dataDir, orgId); | ||
|
|
||
| batchItems.push([{ frontmatter, content: body }, filePath]); | ||
| } | ||
|
|
||
| if (batchItems.length > 0) { | ||
| const result = await exportCommands.exportFrontmatterBatch(batchItems); | ||
| if (result.status === "error") { | ||
| throw new Error( | ||
| `Failed to export migrated organizations: ${result.error}`, | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| await remove(organizationsJsonPath); | ||
|
|
||
| console.log( | ||
| `[OrganizationPersister] Migration complete: ${Object.keys(organizations).length} organizations migrated`, | ||
| ); | ||
| } catch (error) { | ||
| if (!isFileNotFoundError(error)) { | ||
| console.error("[OrganizationPersister] Migration failed:", error); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Critical: Potential data loss during migration
The migration skips if the
organizationsdirectory exists, even if it's empty or incomplete. This prevents re-migration if a previous migration failed partway through.Scenario that breaks:
organizations.jsonwith dataorganizationsdirectoryorganizations.jsonis never migratedFix:
Check if the directory is non-empty instead of just checking existence:
Spotted by Graphite Agent

Is this helpful? React 👍 or 👎 to let us know.