Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 92 additions & 0 deletions packages/database/src/lib/crossAppConverters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import {
InlineCrossAppContent,
CrossAppBase,
CrossAppNode,
CrossAppNodeSchema,
CrossAppRelationTypeSchema,
CrossAppRelationTripleSchema,
} from "../crossAppContracts";
import { LocalContentDataInput, LocalConceptDataInput } from "../inputTypes";
import { Enums, CompositeTypes } from "../dbTypes";
Expand Down Expand Up @@ -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<LocalConceptDataInput>({
...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<LocalConceptDataInput>({
...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<string, string>;
const refIds = Object.fromEntries(
Object.entries(relData).filter(([k]) => !k.endsWith("_local_id")),
) as Record<string, number>;
Comment thread
maparent marked this conversation as resolved.
return filterUndefined<LocalConceptDataInput>({
...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(),
});
};