Skip to content

Commit 521741e

Browse files
committed
eng-2036-simplify-the-cross-app-node-contract
1 parent a12e86e commit 521741e

5 files changed

Lines changed: 26 additions & 70 deletions

File tree

apps/roam/src/utils/convertRoamNodeToFullContent.example.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,6 @@ export const roamClaimFullMarkdownExample: {
229229
full: {
230230
contentType: contentTypes.markdown,
231231
value: buildFullMarkdown({ title, blocks }),
232-
author: { localId: "someone" },
232+
authorId: "someone",
233233
},
234234
};

apps/roam/src/utils/convertRoamNodeToFullContent.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,11 @@ export const convertRoamNodeToFullContent = ({
5858
const blocks = getFullTreeByParentUid(node.source_local_id).children;
5959
const viewType = getPageViewType(title) || "bullet";
6060
const crossAppNode: CrossAppNode = {
61-
author: { localId: node.author_local_id },
61+
authorId: node.author_local_id,
6262
localId: node.source_local_id,
6363
createdAt: new Date(node.created || Date.now()),
6464
modifiedAt: new Date(node.last_modified || Date.now()),
65-
nodeType: { localId: node.node_type_id },
65+
nodeType: node.node_type_id,
6666
content: {
6767
direct: {
6868
localId: node.source_local_id,

packages/database/src/crossAppContracts.ts

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,15 @@
11
import type { ContentType } from "@repo/content-model";
22
import { Enums, type Json } from "./dbTypes";
33

4-
export type LocalRef = {
5-
// This localId is expected to be unique within the current space
6-
localId: string;
7-
};
8-
9-
type DbRef = {
10-
// Some operations will refer to objects through their database Id
11-
dbId: number;
12-
};
13-
14-
// Generalized reference
15-
export type Ref = LocalRef | DbRef;
4+
// An identifier for objects in the platform. Expected to be unique within the platform.
5+
export type LocalId = string;
166

177
// Common attributes for most types
18-
export type CrossAppBase = LocalRef & {
8+
export type CrossAppBase = {
9+
localId: LocalId;
1910
createdAt: Date;
2011
modifiedAt?: Date;
21-
author: Ref;
12+
authorId: LocalId;
2213
};
2314

2415
export type CrossAppSchemaBase = CrossAppBase & {
@@ -48,13 +39,13 @@ export type CrossAppRelationTripleSchema = CrossAppSchemaBase &
4839
relation?: never;
4940
}
5041
| {
51-
relation: Ref;
42+
relation: LocalId;
5243
label?: never;
5344
complement?: never;
5445
}
5546
) & {
56-
sourceType: Ref;
57-
destinationType: Ref;
47+
sourceType: LocalId;
48+
destinationType: LocalId;
5849
};
5950

6051
// An inline vector semantic embedding
@@ -79,7 +70,7 @@ type InlineCrossAppTypedContent = InlineCrossAppContent & {
7970

8071
// A node instance
8172
export type CrossAppNode = CrossAppBase & {
82-
nodeType: Ref;
73+
nodeType: LocalId;
8374
content: {
8475
direct: InlineCrossAppContent;
8576
full?: InlineCrossAppTypedContent;

packages/database/src/crossAppNodeContract.example.ts

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,21 @@ Multiple studies show that sleep after learning strengthens memory traces.
1313

1414
export const roamOriginNodeExample: CrossAppNode = {
1515
localId: ROAM_SOURCE_NODE_ID,
16-
nodeType: {
17-
localId: "rCLM0schema",
18-
},
16+
nodeType: "rCLM0schema",
1917
content: {
2018
direct: {
2119
value: "Sleep improves memory consolidation",
22-
author: { localId: "someone" },
20+
authorId: "someone",
2321
},
2422
full: {
2523
contentType: contentTypes.markdown,
2624
value: roamFullMarkdown,
27-
author: { localId: "someone" },
25+
authorId: "someone",
2826
},
2927
},
3028
createdAt: new Date("2026-06-12T14:00:00.000Z"),
3129
modifiedAt: new Date("2026-06-12T15:00:00.000Z"),
32-
author: { localId: "maparent" },
30+
authorId: "maparent",
3331
};
3432

3533
// const OBSIDIAN_SOURCE_SPACE_ID = "obsidian:9a8b7c6d5e4f3210";
@@ -48,21 +46,19 @@ Participants with more REM sleep showed better next-day recall.
4846

4947
export const obsidianOriginNodeExample: CrossAppNode = {
5048
localId: OBSIDIAN_SOURCE_NODE_ID,
51-
nodeType: {
52-
localId: OBSIDIAN_SOURCE_NODE_TYPE_ID,
53-
},
49+
nodeType: OBSIDIAN_SOURCE_NODE_TYPE_ID,
5450
content: {
5551
direct: {
5652
value: "EVD - REM sleep and recall",
57-
author: { localId: "someone" },
53+
authorId: "someone",
5854
},
5955
full: {
6056
contentType: contentTypes.markdown,
6157
value: obsidianFullMarkdown,
62-
author: { localId: "someone" },
58+
authorId: "someone",
6359
},
6460
},
6561
createdAt: new Date("2026-06-14T10:30:00.000Z"),
6662
modifiedAt: new Date("2026-06-14T15:00:00.000Z"),
67-
author: { localId: "maparent" },
63+
authorId: "maparent",
6864
};

packages/database/src/lib/crossAppConverters.ts

Lines changed: 6 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,12 @@
11
import {
2-
LocalRef,
3-
Ref,
42
CrossAppEmbedding,
53
InlineCrossAppContent,
6-
CrossAppBase,
74
CrossAppNode,
85
} from "../crossAppContracts";
96
import { LocalContentDataInput, LocalConceptDataInput } from "../inputTypes";
107
import { Enums, CompositeTypes } from "../dbTypes";
118

129
type InlineEmbeddingInput = CompositeTypes<"inline_embedding_input">;
13-
type InlineAbstractBase = Partial<CrossAppBase>;
14-
15-
const decodeLocalRef = <LocalVarName extends string>(
16-
ref: LocalRef | InlineAbstractBase | undefined,
17-
localVarName: LocalVarName,
18-
): Record<LocalVarName, string> | Record<string, never> => {
19-
if (ref === undefined) return {};
20-
if ("localId" in ref) {
21-
return {
22-
[localVarName]: ref.localId,
23-
} as Record<LocalVarName, string>;
24-
}
25-
return {};
26-
};
27-
28-
const decodeRef = <DbVarName extends string, LocalVarName extends string>(
29-
ref: Ref | undefined,
30-
dbVarName: DbVarName,
31-
localVarName: LocalVarName,
32-
):
33-
| Record<DbVarName, number>
34-
| Record<LocalVarName, string>
35-
| Record<string, never> => {
36-
if (ref === undefined) return {};
37-
if ("dbId" in ref)
38-
return { [dbVarName]: ref.dbId } as Record<DbVarName, number>;
39-
return decodeLocalRef(ref, localVarName);
40-
};
4110

4211
const crossAppEmbeddingToDbEmbedding = (
4312
embedding: CrossAppEmbedding | undefined,
@@ -64,14 +33,14 @@ const inlineCrossAppContentToDbContent = (
6433
): LocalContentDataInput | undefined => {
6534
if (content === undefined) return undefined;
6635
return filterUndefined<LocalContentDataInput>({
67-
...decodeLocalRef(content, "source_local_id"),
36+
source_local_id: content.localId,
6837
text: content.value,
6938
scale: content.scale || "document",
7039
content_type: content.contentType || "text/plain",
7140
variant,
7241
created: content.createdAt?.toISOString(),
7342
last_modified: content.modifiedAt?.toISOString(),
74-
...decodeRef(content.author, "author_id", "author_local_id"),
43+
author_local_id: content.authorId,
7544
embedding_inline: crossAppEmbeddingToDbEmbedding(content.embedding),
7645
});
7746
};
@@ -88,7 +57,7 @@ export const crossAppNodeToDbContent = (
8857
...content,
8958
createdAt: content.createdAt || node.createdAt,
9059
modifiedAt: content.modifiedAt || node.modifiedAt,
91-
author: content.author || node.author,
60+
authorId: content.authorId || node.authorId,
9261
},
9362
variant,
9463
);
@@ -98,10 +67,10 @@ export const crossAppNodeToDbConcept = (
9867
node: CrossAppNode,
9968
): LocalConceptDataInput => {
10069
return filterUndefined<LocalConceptDataInput>({
101-
...decodeLocalRef(node, "source_local_id"),
70+
source_local_id: node.localId,
10271
name: node.content.direct.value,
103-
...decodeRef(node.author, "author_id", "author_local_id"),
104-
...decodeRef(node.nodeType, "schema_id", "schema_represented_by_local_id"),
72+
author_local_id: node.authorId,
73+
schema_represented_by_local_id: node.nodeType,
10574
contents_inline: filterUndefinedArray([
10675
crossAppNodeToDbContent(node, "direct"),
10776
crossAppNodeToDbContent(node, "full"),

0 commit comments

Comments
 (0)