Skip to content

Commit bf2dd0c

Browse files
committed
eng-2018-add-converter-for-relations
1 parent f14ab44 commit bf2dd0c

1 file changed

Lines changed: 98 additions & 0 deletions

File tree

packages/database/src/lib/crossAppConverters.ts

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
import {
22
LocalRef,
33
Ref,
4+
LocalOrRemoteRef,
45
CrossAppEmbedding,
56
InlineCrossAppContent,
67
CrossAppBase,
78
CrossAppNode,
9+
CrossAppRelation,
810
} from "../crossAppContracts";
911
import { LocalContentDataInput, LocalConceptDataInput } from "../inputTypes";
1012
import { Enums, CompositeTypes } from "../dbTypes";
13+
import { spaceUriAndLocalIdToRid, ridToSpaceUriAndLocalId } from "./rid";
1114

1215
type InlineEmbeddingInput = CompositeTypes<"inline_embedding_input">;
1316
type InlineAbstractBase = Partial<CrossAppBase>;
@@ -39,6 +42,62 @@ const decodeRef = <DbVarName extends string, LocalVarName extends string>(
3942
return decodeLocalRef(ref, localVarName);
4043
};
4144

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+
42101
const crossAppEmbeddingToDbEmbedding = (
43102
embedding: CrossAppEmbedding | undefined,
44103
): InlineEmbeddingInput | undefined =>
@@ -109,3 +168,42 @@ export const crossAppNodeToDbConcept = (
109168
last_modified: node.modifiedAt?.toISOString(),
110169
});
111170
};
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

Comments
 (0)