diff --git a/packages/database/src/lib/crossAppConverters.ts b/packages/database/src/lib/crossAppConverters.ts index d5c9dfc9f..5c34ea5ea 100644 --- a/packages/database/src/lib/crossAppConverters.ts +++ b/packages/database/src/lib/crossAppConverters.ts @@ -5,6 +5,9 @@ import { InlineCrossAppContent, CrossAppBase, CrossAppNode, + CrossAppNodeSchema, + CrossAppRelationTypeSchema, + CrossAppRelationTripleSchema, } from "../crossAppContracts"; import { LocalContentDataInput, LocalConceptDataInput } from "../inputTypes"; import { Enums, CompositeTypes } from "../dbTypes"; @@ -109,3 +112,92 @@ export const crossAppNodeToDbConcept = ( last_modified: node.modifiedAt?.toISOString(), }); }; + +export const crossAppNodeSchemaToDbConcept = ( + node: CrossAppNodeSchema, +): LocalConceptDataInput => { + const literalInfo = filterUndefined({ + template: node.templateTitle, + templateContent: node.template, + }); + return filterUndefined({ + ...decodeLocalRef(node, "source_local_id"), + name: node.label, + ...decodeRef(node.author, "author_id", "author_local_id"), + is_schema: true, + literal_content: + Object.keys(literalInfo).length > 0 ? literalInfo : undefined, + created: node.createdAt?.toISOString(), + last_modified: node.modifiedAt?.toISOString(), + }); +}; + +export const crossAppRelationTypeSchemaToDbConcept = ( + node: CrossAppRelationTypeSchema, +): LocalConceptDataInput => { + return filterUndefined({ + ...decodeLocalRef(node, "source_local_id"), + name: node.label, + ...decodeRef(node.author, "author_id", "author_local_id"), + is_schema: true, + literal_content: { + roles: ["source", "destination"], + label: node.label, + complement: node.complement, + }, + created: node.createdAt?.toISOString(), + last_modified: node.modifiedAt?.toISOString(), + }); +}; + +export const crossAppRelationTripleSchemaToDbConcept = ({ + node, + sourceNodeSchema, + destinationNodeSchema, + relationType, +}: { + node: CrossAppRelationTripleSchema; + sourceNodeSchema: CrossAppNodeSchema; + destinationNodeSchema: CrossAppNodeSchema; + relationType?: CrossAppRelationTypeSchema; +}): LocalConceptDataInput | undefined => { + // relationType must be provided if relation-based triple. + if (!("label" in node) && relationType === undefined) return undefined; + const label = "label" in node ? node.label : relationType!.label; + const complement = + "complement" in node ? node.complement : relationType!.complement; + + const relData = { + ...decodeRef( + "relation" in node ? node.relation : undefined, + "relation_type", + "relation_type_local_id", + ), + ...decodeRef(node.sourceType, "source", "source_local_id"), + ...decodeRef(node.destinationType, "destination", "destination_local_id"), + }; + const refLocalIds = Object.fromEntries( + Object.entries(relData) + .filter(([k]) => k.endsWith("_local_id")) + .map(([k, v]) => [k.substring(0, k.length - 9), v]), + ) as Record; + const refIds = Object.fromEntries( + Object.entries(relData).filter(([k]) => !k.endsWith("_local_id")), + ) as Record; + return filterUndefined({ + ...decodeLocalRef(node, "source_local_id"), + name: `${sourceNodeSchema.label} -${label}-> ${destinationNodeSchema.label}`, + ...decodeRef(node.author, "author_id", "author_local_id"), + is_schema: true, + literal_content: filterUndefined({ + roles: ["source", "destination"], + label, + complement, + }), + local_reference_content: + Object.keys(refLocalIds).length > 0 ? refLocalIds : undefined, + reference_content: Object.keys(refIds).length > 0 ? refIds : undefined, + created: node.createdAt?.toISOString(), + last_modified: node.modifiedAt?.toISOString(), + }); +};