|
1 | 1 | import { |
2 | 2 | LocalRef, |
3 | 3 | Ref, |
| 4 | + LocalOrRemoteRef, |
4 | 5 | CrossAppEmbedding, |
5 | 6 | InlineCrossAppContent, |
6 | 7 | CrossAppBase, |
7 | 8 | CrossAppNode, |
| 9 | + CrossAppRelation, |
8 | 10 | } from "../crossAppContracts"; |
9 | 11 | import { LocalContentDataInput, LocalConceptDataInput } from "../inputTypes"; |
10 | 12 | import { Enums, CompositeTypes } from "../dbTypes"; |
| 13 | +import { spaceUriAndLocalIdToRid, ridToSpaceUriAndLocalId } from "./rid"; |
11 | 14 |
|
12 | 15 | type InlineEmbeddingInput = CompositeTypes<"inline_embedding_input">; |
13 | 16 | type InlineAbstractBase = Partial<CrossAppBase>; |
@@ -39,6 +42,62 @@ const decodeRef = <DbVarName extends string, LocalVarName extends string>( |
39 | 42 | return decodeLocalRef(ref, localVarName); |
40 | 43 | }; |
41 | 44 |
|
| 45 | +const decodeLocalOrRemoteRef = < |
| 46 | + LocalVarName extends string, |
| 47 | + DbVarName extends string, |
| 48 | +>({ |
| 49 | + ref, |
| 50 | + dbVarName, |
| 51 | + localVarName, |
| 52 | + currentSpaceUri, |
| 53 | +}: { |
| 54 | + ref: LocalOrRemoteRef | InlineAbstractBase | undefined; |
| 55 | + dbVarName: DbVarName; |
| 56 | + localVarName: LocalVarName; |
| 57 | + currentSpaceUri: string; |
| 58 | +}): |
| 59 | + | Record<LocalVarName, string> |
| 60 | + | Record<DbVarName, number> |
| 61 | + | Record<string, never> => { |
| 62 | + if (ref === undefined) return {}; |
| 63 | + if ("dbId" in ref) |
| 64 | + return { [dbVarName]: ref.dbId } as Record<DbVarName, number>; |
| 65 | + if ("rid" in ref && ref.rid !== undefined) { |
| 66 | + const { sourceLocalId, spaceUri } = ridToSpaceUriAndLocalId(ref.rid); |
| 67 | + if (spaceUri === currentSpaceUri) |
| 68 | + return { |
| 69 | + [localVarName]: sourceLocalId, |
| 70 | + } as Record<LocalVarName, string>; |
| 71 | + return { |
| 72 | + [localVarName]: ref.rid, |
| 73 | + } as Record<LocalVarName, string>; |
| 74 | + } |
| 75 | + if ("space" in ref && ref.space !== undefined) { |
| 76 | + if ("dbId" in ref.space) { |
| 77 | + // not sure how to handle this |
| 78 | + return { |
| 79 | + [localVarName]: ref.localId, |
| 80 | + } as Record<LocalVarName, string>; |
| 81 | + } |
| 82 | + if ("url" in ref.space) { |
| 83 | + if (ref.space.url === currentSpaceUri) { |
| 84 | + return { |
| 85 | + [localVarName]: ref.localId, |
| 86 | + } as Record<LocalVarName, string>; |
| 87 | + } else |
| 88 | + return { |
| 89 | + [localVarName]: spaceUriAndLocalIdToRid(ref.space.url, ref.localId), |
| 90 | + } as Record<LocalVarName, string>; |
| 91 | + } |
| 92 | + } |
| 93 | + if ("localId" in ref) { |
| 94 | + return { |
| 95 | + [localVarName]: ref.localId, |
| 96 | + } as Record<LocalVarName, string>; |
| 97 | + } |
| 98 | + return {}; |
| 99 | +}; |
| 100 | + |
42 | 101 | const crossAppEmbeddingToDbEmbedding = ( |
43 | 102 | embedding: CrossAppEmbedding | undefined, |
44 | 103 | ): InlineEmbeddingInput | undefined => |
@@ -109,3 +168,42 @@ export const crossAppNodeToDbConcept = ( |
109 | 168 | last_modified: node.modifiedAt?.toISOString(), |
110 | 169 | }); |
111 | 170 | }; |
| 171 | + |
| 172 | +export const crossAppRelationToDbConcept = ( |
| 173 | + node: CrossAppRelation, |
| 174 | + currentSpaceUri: string, |
| 175 | +): LocalConceptDataInput => { |
| 176 | + const relData = { |
| 177 | + ...decodeLocalOrRemoteRef({ |
| 178 | + ref: node.source, |
| 179 | + dbVarName: "source", |
| 180 | + localVarName: "source_local_id", |
| 181 | + currentSpaceUri, |
| 182 | + }), |
| 183 | + ...decodeLocalOrRemoteRef({ |
| 184 | + ref: node.destination, |
| 185 | + dbVarName: "destination", |
| 186 | + localVarName: "destination_local_id", |
| 187 | + currentSpaceUri, |
| 188 | + }), |
| 189 | + }; |
| 190 | + const refLocalIds = Object.fromEntries( |
| 191 | + Object.entries(relData) |
| 192 | + .filter(([k]) => k.endsWith("_local_id")) |
| 193 | + .map(([k, v]) => [k.substring(0, k.length - 9), v]), |
| 194 | + ) as Record<string, string>; |
| 195 | + const refIds = Object.fromEntries( |
| 196 | + Object.entries(relData).filter( |
| 197 | + ([k]) => k.endsWith("_id") && !k.endsWith("_local_id"), |
| 198 | + ), |
| 199 | + ) as Record<string, number>; |
| 200 | + |
| 201 | + return filterUndefined<LocalConceptDataInput>({ |
| 202 | + ...decodeLocalRef(node, "source_local_id"), |
| 203 | + ...decodeRef(node.author, "author_id", "author_local_id"), |
| 204 | + local_reference_content: refLocalIds, |
| 205 | + reference_content: refIds, |
| 206 | + created: node.createdAt?.toISOString(), |
| 207 | + last_modified: node.modifiedAt?.toISOString(), |
| 208 | + }); |
| 209 | +}; |
0 commit comments