Skip to content

Commit 6cf480a

Browse files
authored
eng-2017-add-converters-for-schemas (#1213)
1 parent 7f26dd0 commit 6cf480a

1 file changed

Lines changed: 76 additions & 0 deletions

File tree

packages/database/src/lib/crossAppConverters.ts

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ import {
22
CrossAppEmbedding,
33
InlineCrossAppContent,
44
CrossAppNode,
5+
CrossAppNodeSchema,
6+
CrossAppRelationTypeSchema,
7+
CrossAppRelationTripleSchema,
58
} from "../crossAppContracts";
69
import { LocalContentDataInput, LocalConceptDataInput } from "../inputTypes";
710
import { Enums, CompositeTypes } from "../dbTypes";
@@ -79,3 +82,76 @@ export const crossAppNodeToDbConcept = (
7982
last_modified: node.modifiedAt?.toISOString(),
8083
});
8184
};
85+
86+
export const crossAppNodeSchemaToDbConcept = (
87+
node: CrossAppNodeSchema,
88+
): LocalConceptDataInput => {
89+
const literalInfo = filterUndefined({
90+
template: node.templateTitle,
91+
template_content: node.template,
92+
});
93+
return filterUndefined<LocalConceptDataInput>({
94+
source_local_id: node.localId,
95+
name: node.label,
96+
author_local_id: node.authorId,
97+
is_schema: true,
98+
literal_content:
99+
Object.keys(literalInfo).length > 0 ? literalInfo : undefined,
100+
created: node.createdAt?.toISOString(),
101+
last_modified: node.modifiedAt?.toISOString(),
102+
});
103+
};
104+
105+
export const crossAppRelationTypeSchemaToDbConcept = (
106+
node: CrossAppRelationTypeSchema,
107+
): LocalConceptDataInput => {
108+
return filterUndefined<LocalConceptDataInput>({
109+
source_local_id: node.localId,
110+
name: node.label,
111+
author_local_id: node.authorId,
112+
is_schema: true,
113+
literal_content: {
114+
roles: ["source", "destination"],
115+
label: node.label,
116+
complement: node.complement,
117+
},
118+
created: node.createdAt?.toISOString(),
119+
last_modified: node.modifiedAt?.toISOString(),
120+
});
121+
};
122+
123+
export const crossAppRelationTripleSchemaToDbConcept = ({
124+
node,
125+
sourceNodeSchema,
126+
destinationNodeSchema,
127+
relationType,
128+
}: {
129+
node: CrossAppRelationTripleSchema;
130+
sourceNodeSchema: CrossAppNodeSchema;
131+
destinationNodeSchema: CrossAppNodeSchema;
132+
// used in Obsidian, not yet in Roam.
133+
relationType?: CrossAppRelationTypeSchema;
134+
}): LocalConceptDataInput | undefined => {
135+
if (!("label" in node) && relationType === undefined) return undefined;
136+
const label = "label" in node ? node.label : relationType!.label;
137+
const complement =
138+
"complement" in node ? node.complement : relationType!.complement;
139+
return filterUndefined<LocalConceptDataInput>({
140+
source_local_id: node.localId,
141+
name: `${sourceNodeSchema.label} -${label}-> ${destinationNodeSchema.label}`,
142+
author_local_id: node.authorId,
143+
is_schema: true,
144+
literal_content: filterUndefined({
145+
roles: ["source", "destination"],
146+
label,
147+
complement,
148+
}),
149+
local_reference_content: {
150+
relation_type: node.relation,
151+
source: node.sourceType,
152+
destination: node.destinationType,
153+
},
154+
created: node.createdAt?.toISOString(),
155+
last_modified: node.modifiedAt?.toISOString(),
156+
});
157+
};

0 commit comments

Comments
 (0)