-
Notifications
You must be signed in to change notification settings - Fork 4
[ENG-1176] Sync DG node from Obsidian to database #665
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
Open
trangdoan982
wants to merge
7
commits into
main
Choose a base branch
from
01-03-sync_all_nodes_on_load
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,193
−8
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ac9076d
current progress
trangdoan982 c2fb3c8
redo the auth so that only one user acc is created
trangdoan982 bb29c87
sync all nodes on load
trangdoan982 c973f5c
current progress
trangdoan982 1c0fb71
revert changes
trangdoan982 b5adfb3
clean up sync nodes
trangdoan982 b7e4306
enable cors
trangdoan982 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
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,140 @@ | ||
| /* eslint-disable @typescript-eslint/naming-convention */ | ||
| import { TFile } from "obsidian"; | ||
| import { DiscourseNode } from "~/types"; | ||
| import { SupabaseContext } from "./supabaseContext"; | ||
| import { LocalConceptDataInput } from "@repo/database/inputTypes"; | ||
| import { ObsidianDiscourseNodeData } from "./syncDgNodesToSupabase"; | ||
| import { Json } from "@repo/database/dbTypes"; | ||
|
|
||
| /** | ||
| * Get extra data (author, timestamps) from file metadata | ||
| */ | ||
| const getNodeExtraData = ( | ||
| file: TFile, | ||
| accountLocalId: string, | ||
| ): { | ||
| author_local_id: string; | ||
| created: string; | ||
| last_modified: string; | ||
| } => { | ||
| return { | ||
| author_local_id: accountLocalId, | ||
| created: new Date(file.stat.ctime).toISOString(), | ||
| last_modified: new Date(file.stat.mtime).toISOString(), | ||
| }; | ||
| }; | ||
|
|
||
| export const discourseNodeSchemaToLocalConcept = ({ | ||
| context, | ||
| node, | ||
| accountLocalId, | ||
| }: { | ||
| context: SupabaseContext; | ||
| node: DiscourseNode; | ||
| accountLocalId: string; | ||
| }): LocalConceptDataInput => { | ||
| const now = new Date().toISOString(); | ||
| return { | ||
| space_id: context.spaceId, | ||
| name: node.name, | ||
| represented_by_local_id: node.id, | ||
| is_schema: true, | ||
| author_local_id: accountLocalId, | ||
| created: now, | ||
| // TODO: get the template or any other info to put into literal_content jsonb | ||
trangdoan982 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| last_modified: now, | ||
| }; | ||
| }; | ||
|
|
||
| /** | ||
| * Convert discourse node instance (file) to LocalConceptDataInput | ||
| */ | ||
| export const discourseNodeInstanceToLocalConcept = ({ | ||
| context, | ||
| nodeData, | ||
| accountLocalId, | ||
| }: { | ||
| context: SupabaseContext; | ||
| nodeData: ObsidianDiscourseNodeData; | ||
| accountLocalId: string; | ||
| }): LocalConceptDataInput => { | ||
| const extraData = getNodeExtraData(nodeData.file, accountLocalId); | ||
| console.log(nodeData.frontmatter); | ||
| const concept = { | ||
| space_id: context.spaceId, | ||
| name: nodeData.file.basename, | ||
| represented_by_local_id: nodeData.nodeInstanceId, | ||
| schema_represented_by_local_id: nodeData.nodeTypeId, | ||
| is_schema: false, | ||
| literal_content: { | ||
| ...nodeData.frontmatter, | ||
| } as unknown as Json, | ||
| ...extraData, | ||
| }; | ||
| console.log( | ||
| `[discourseNodeInstanceToLocalConcept] Converting concept: represented_by_local_id=${nodeData.nodeInstanceId}, name="${nodeData.file.basename}"`, | ||
| ); | ||
| return concept; | ||
| }; | ||
|
|
||
| export const relatedConcepts = (concept: LocalConceptDataInput): string[] => { | ||
| const relations = Object.values( | ||
| concept.local_reference_content || {}, | ||
| ).flat() as string[]; | ||
| if (concept.schema_represented_by_local_id) { | ||
| relations.push(concept.schema_represented_by_local_id); | ||
| } | ||
| // remove duplicates | ||
| return [...new Set(relations)]; | ||
| }; | ||
|
|
||
| /** | ||
| * Recursively order concepts by dependency | ||
| */ | ||
| const orderConceptsRec = ( | ||
| ordered: LocalConceptDataInput[], | ||
| concept: LocalConceptDataInput, | ||
| remainder: { [key: string]: LocalConceptDataInput }, | ||
| ): Set<string> => { | ||
| const relatedConceptIds = relatedConcepts(concept); | ||
| let missing: Set<string> = new Set(); | ||
| while (relatedConceptIds.length > 0) { | ||
| const relatedConceptId = relatedConceptIds.shift()!; | ||
| const relatedConcept = remainder[relatedConceptId]; | ||
| if (relatedConcept === undefined) { | ||
| missing.add(relatedConceptId); | ||
| } else { | ||
| missing = new Set([ | ||
| ...missing, | ||
| ...orderConceptsRec(ordered, relatedConcept, remainder), | ||
| ]); | ||
| delete remainder[relatedConceptId]; | ||
| } | ||
| } | ||
| ordered.push(concept); | ||
| delete remainder[concept.represented_by_local_id!]; | ||
| return missing; | ||
| }; | ||
|
|
||
| export const orderConceptsByDependency = ( | ||
| concepts: LocalConceptDataInput[], | ||
| ): { ordered: LocalConceptDataInput[]; missing: string[] } => { | ||
| if (concepts.length === 0) return { ordered: concepts, missing: [] }; | ||
| const conceptById: { [key: string]: LocalConceptDataInput } = | ||
| Object.fromEntries( | ||
| concepts | ||
| .filter((c) => c.represented_by_local_id) | ||
| .map((c) => [c.represented_by_local_id!, c]), | ||
| ); | ||
| const ordered: LocalConceptDataInput[] = []; | ||
| let missing: Set<string> = new Set(); | ||
| while (Object.keys(conceptById).length > 0) { | ||
| const first = Object.values(conceptById)[0]; | ||
| if (!first) break; | ||
| missing = new Set([ | ||
| ...missing, | ||
| ...orderConceptsRec(ordered, first, conceptById), | ||
| ]); | ||
| } | ||
| return { ordered, missing: Array.from(missing) }; | ||
| }; | ||
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
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.
Uh oh!
There was an error while loading. Please reload this page.