|
6 | 6 | InlineCrossAppContent, |
7 | 7 | CrossAppBase, |
8 | 8 | CrossAppNode, |
| 9 | + CrossAppNodeSchema, |
| 10 | + CrossAppRelationTypeSchema, |
| 11 | + CrossAppRelationTripleSchema, |
9 | 12 | CrossAppRelation, |
10 | 13 | } from "../crossAppContracts"; |
11 | 14 | import { LocalContentDataInput, LocalConceptDataInput } from "../inputTypes"; |
@@ -169,6 +172,95 @@ export const crossAppNodeToDbConcept = ( |
169 | 172 | }); |
170 | 173 | }; |
171 | 174 |
|
| 175 | +export const crossAppNodeSchemaToDbConcept = ( |
| 176 | + node: CrossAppNodeSchema, |
| 177 | +): LocalConceptDataInput => { |
| 178 | + const literalInfo = filterUndefined({ |
| 179 | + template: node.templateTitle, |
| 180 | + templateContent: node.template, |
| 181 | + }); |
| 182 | + return filterUndefined<LocalConceptDataInput>({ |
| 183 | + ...decodeLocalRef(node, "source_local_id"), |
| 184 | + name: node.label, |
| 185 | + ...decodeRef(node.author, "author_id", "author_local_id"), |
| 186 | + is_schema: true, |
| 187 | + literal_content: |
| 188 | + Object.keys(literalInfo).length > 0 ? literalInfo : undefined, |
| 189 | + created: node.createdAt?.toISOString(), |
| 190 | + last_modified: node.modifiedAt?.toISOString(), |
| 191 | + }); |
| 192 | +}; |
| 193 | + |
| 194 | +export const crossAppRelationTypeSchemaToDbConcept = ( |
| 195 | + node: CrossAppRelationTypeSchema, |
| 196 | +): LocalConceptDataInput => { |
| 197 | + return filterUndefined<LocalConceptDataInput>({ |
| 198 | + ...decodeLocalRef(node, "source_local_id"), |
| 199 | + name: node.label, |
| 200 | + ...decodeRef(node.author, "author_id", "author_local_id"), |
| 201 | + is_schema: true, |
| 202 | + literal_content: { |
| 203 | + roles: ["source", "destination"], |
| 204 | + label: node.label, |
| 205 | + complement: node.complement, |
| 206 | + }, |
| 207 | + created: node.createdAt?.toISOString(), |
| 208 | + last_modified: node.modifiedAt?.toISOString(), |
| 209 | + }); |
| 210 | +}; |
| 211 | + |
| 212 | +export const crossAppRelationTripleSchemaToDbConcept = ({ |
| 213 | + node, |
| 214 | + sourceNodeSchema, |
| 215 | + destinationNodeSchema, |
| 216 | + relationType, |
| 217 | +}: { |
| 218 | + node: CrossAppRelationTripleSchema; |
| 219 | + sourceNodeSchema: CrossAppNodeSchema; |
| 220 | + destinationNodeSchema: CrossAppNodeSchema; |
| 221 | + relationType?: CrossAppRelationTypeSchema; |
| 222 | +}): LocalConceptDataInput | undefined => { |
| 223 | + // relationType must be provided if relation-based triple. |
| 224 | + if (!("label" in node) && relationType === undefined) return undefined; |
| 225 | + const label = "label" in node ? node.label : relationType!.label; |
| 226 | + const complement = |
| 227 | + "complement" in node ? node.complement : relationType!.complement; |
| 228 | + |
| 229 | + const relData = { |
| 230 | + ...decodeRef( |
| 231 | + "relation" in node ? node.relation : undefined, |
| 232 | + "relation_type", |
| 233 | + "relation_type_local_id", |
| 234 | + ), |
| 235 | + ...decodeRef(node.sourceType, "source", "source_local_id"), |
| 236 | + ...decodeRef(node.destinationType, "destination", "destination_local_id"), |
| 237 | + }; |
| 238 | + const refLocalIds = Object.fromEntries( |
| 239 | + Object.entries(relData) |
| 240 | + .filter(([k]) => k.endsWith("_local_id")) |
| 241 | + .map(([k, v]) => [k.substring(0, k.length - 9), v]), |
| 242 | + ) as Record<string, string>; |
| 243 | + const refIds = Object.fromEntries( |
| 244 | + Object.entries(relData).filter(([k]) => !k.endsWith("_local_id")), |
| 245 | + ) as Record<string, number>; |
| 246 | + return filterUndefined<LocalConceptDataInput>({ |
| 247 | + ...decodeLocalRef(node, "source_local_id"), |
| 248 | + name: `${sourceNodeSchema.label} -${label}-> ${destinationNodeSchema.label}`, |
| 249 | + ...decodeRef(node.author, "author_id", "author_local_id"), |
| 250 | + is_schema: true, |
| 251 | + literal_content: filterUndefined({ |
| 252 | + roles: ["source", "destination"], |
| 253 | + label, |
| 254 | + complement, |
| 255 | + }), |
| 256 | + local_reference_content: |
| 257 | + Object.keys(refLocalIds).length > 0 ? refLocalIds : undefined, |
| 258 | + reference_content: Object.keys(refIds).length > 0 ? refIds : undefined, |
| 259 | + created: node.createdAt?.toISOString(), |
| 260 | + last_modified: node.modifiedAt?.toISOString(), |
| 261 | + }); |
| 262 | +}; |
| 263 | + |
172 | 264 | export const crossAppRelationToDbConcept = ( |
173 | 265 | node: CrossAppRelation, |
174 | 266 | currentSpaceUri: string, |
|
0 commit comments