-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathconceptConversion.ts
More file actions
311 lines (294 loc) · 10.3 KB
/
Copy pathconceptConversion.ts
File metadata and controls
311 lines (294 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
import { InputTextNode } from "roamjs-components/types";
import getBlockProps from "./getBlockProps";
import { DiscourseNode } from "./getDiscourseNodes";
import getDiscourseRelations from "./getDiscourseRelations";
import type { DiscourseRelation } from "./getDiscourseRelations";
import type { SupabaseContext } from "~/utils/supabaseContext";
import { DISCOURSE_GRAPH_PROP_NAME } from "~/utils/createReifiedBlock";
import type { LocalConceptDataInput } from "@repo/database/inputTypes";
import type { Json } from "@repo/database/dbTypes";
import getPageTitleByPageUid from "roamjs-components/queries/getPageTitleByPageUid";
const getNodeExtraData = (
node_uid: string,
): {
author_uid: string;
created: string;
last_modified: string;
page_uid: string;
/* eslint-enable @typescript-eslint/naming-convention */
} => {
const result = window.roamAlphaAPI.q(
`[
:find
?author_uid
?page_uid
?created
?last_modified
:in $ ?block_uid
:where
[?block :block/uid ?block_uid]
[?block :create/user ?author_id]
[?author_id :user/uid ?author_uid]
[?block :create/time ?created]
[(get-else $ ?block :edit/time ?created) ?last_modified]
[(get-else $ ?block :block/page ?block) ?page_id]
[?page_id :block/uid ?page_uid]
]`,
node_uid,
);
if (result.length !== 1 || result[0].length !== 4)
throw new Error("Invalid result from Roam query");
const [author_uid, page_uid, created_t, last_modified_t] = result[0] as [
string,
string,
number,
number,
];
const created = new Date(created_t).toISOString();
const last_modified = new Date(last_modified_t).toISOString();
return {
author_uid,
created,
last_modified,
page_uid,
};
/* eslint-enable @typescript-eslint/naming-convention */
};
const indent = (s: string): string =>
s
.split("\n")
.map((l) => " " + l)
.join("\n") + "\n";
const templateToText = (template: InputTextNode[]): string =>
template
.filter((itn) => !itn.text.startsWith("{{"))
.map(
(itn) =>
`* ${itn.text}\n${itn.children?.length ? indent(templateToText(itn.children)) : ""}`,
)
.join("");
export const discourseNodeSchemaToLocalConcept = (
context: SupabaseContext,
node: DiscourseNode,
): LocalConceptDataInput => {
const titleParts = node.text.split("/");
const label = titleParts[titleParts.length - 1] ?? node.text;
const result: LocalConceptDataInput = {
space_id: context.spaceId,
name: node.text,
source_local_id: node.type,
is_schema: true,
literal_content: {
label,
},
/* eslint-enable @typescript-eslint/naming-convention */
...getNodeExtraData(node.type),
};
if (node.template !== undefined)
result.literal_content = {
label,
template: templateToText(node.template),
};
return result;
};
export const discourseNodeBlockToLocalConcept = (
context: SupabaseContext,
{
nodeUid,
schemaUid,
text,
}: {
nodeUid: string;
schemaUid: string;
text: string;
},
): LocalConceptDataInput => {
return {
space_id: context.spaceId,
name: text,
source_local_id: nodeUid,
schema_represented_by_local_id: schemaUid,
is_schema: false,
/* eslint-enable @typescript-eslint/naming-convention */
...getNodeExtraData(nodeUid),
};
};
const STANDARD_ROLES = ["source", "destination"];
export const discourseRelationSchemaToLocalConcept = (
context: SupabaseContext,
relation: DiscourseRelation,
): LocalConceptDataInput => {
const { id, label, complement, source, destination, ...otherData } = relation;
return {
space_id: context.spaceId,
source_local_id: id,
name: getPageTitleByPageUid(id),
is_schema: true,
local_reference_content: {
source,
destination,
},
literal_content: {
source_data: otherData as unknown as Json,
/* eslint-enable @typescript-eslint/naming-convention */
roles: STANDARD_ROLES,
label: label,
complement: complement,
},
...getNodeExtraData(id),
};
};
export const discourseRelationDataToLocalConcept = (
context: SupabaseContext,
relationUid: string,
): LocalConceptDataInput => {
// assuming reified
const relationProps = getBlockProps(relationUid);
const relationSchemaData = relationProps[DISCOURSE_GRAPH_PROP_NAME] as Record<
string,
string
>;
if (!relationSchemaData) {
throw new Error(`Missing relation data for ${relationUid}`);
}
const relationSchemaUid = relationSchemaData.hasSchema;
if (!relationSchemaUid) {
throw new Error(`Missing relation schema uid for ${relationUid}`);
}
const roamRelation = getDiscourseRelations().find(
(r) => r.id === relationSchemaUid,
);
if (roamRelation === undefined) {
throw new Error(`Invalid roam relation id ${relationSchemaUid}`);
}
const relation = discourseRelationSchemaToLocalConcept(context, roamRelation);
const litContent = (
relation.literal_content ? relation.literal_content : {}
) as { [key: string]: Json };
const roles = (litContent["roles"] as string[] | undefined) || STANDARD_ROLES;
const casting = Object.fromEntries(
roles
.map((role) => [role, relationSchemaData[role + "Uid"]])
.filter(([, uid]) => uid !== undefined),
) as { [role: string]: string };
if (Object.keys(casting).length === 0) {
throw new Error(
`No valid node UIDs supplied for roles ${roles.join(", ")}`,
);
}
// TODO: Also get the nodes from the representation, using QueryBuilder. That will likely give me the relation object
const nodeData = Object.values(casting).map((v) => getNodeExtraData(v));
// roundabout way to do a max from stringified dates
const last_modified = new Date(
Math.max(...nodeData.map((nd) => new Date(nd.last_modified).getTime())),
).toISOString();
// creation is actually creation of the relation node, not the rest of the cast, but this will do as a first approximation.
// Still using max, since the relation cannot be created before its cast
const created = new Date(
Math.max(...nodeData.map((nd) => new Date(nd.created).getTime())),
).toISOString();
const author_local_id: string = nodeData[0].author_uid; // take any one; again until I get the relation object
return {
space_id: context.spaceId,
source_local_id: relationUid,
author_local_id,
created,
last_modified,
name: relationUid,
is_schema: false,
schema_represented_by_local_id: relationSchemaUid,
local_reference_content: casting,
};
/* eslint-enable @typescript-eslint/naming-convention */
};
export const relatedConcepts = (concept: LocalConceptDataInput): string[] => {
const relations = Object.values(
concept.local_reference_content || {},
).flat() as string[];
if (concept.schema_represented_by_local_id) {
relations.push(concept.schema_represented_by_local_id);
}
// remove duplicates
return [...new Set(relations)];
};
const orderConceptsRec = ({
ordered,
concept,
remainder,
processed,
}: {
ordered: LocalConceptDataInput[];
concept: LocalConceptDataInput;
remainder: { [key: string]: LocalConceptDataInput };
processed: Set<string>;
}): Set<string> => {
// Add to processed at the start to prevent cycles
processed.add(concept.source_local_id!);
const relatedConceptIds = relatedConcepts(concept);
let missing: Set<string> = new Set();
while (relatedConceptIds.length > 0) {
const relatedConceptId = relatedConceptIds.shift()!;
if (processed.has(relatedConceptId)) continue;
const relatedConcept = remainder[relatedConceptId];
if (relatedConcept === undefined) {
missing.add(relatedConceptId);
} else {
missing = new Set([
...missing,
...orderConceptsRec({
ordered,
concept: relatedConcept,
remainder,
processed,
}),
]);
delete remainder[relatedConceptId];
}
}
ordered.push(concept);
delete remainder[concept.source_local_id!];
return missing;
};
/*
If writing a concept upsert method, you want to insure that
a node's dependencies are defined before the node itself is upserted.
The dependencies are as defined in relatedConcepts.
If you upsert in the following order: [node schemas, relation schemas, nodes, relations]
then the depencies will be implicitly respected.
(It will be tricker when we have recursive relations.)
If you are starting from a random stream of nodes, you would want to order them with this function.
It assumes all input has defined source_local_id,
and that nodes that are not in the upsert set are already in the database.
the Id of those nodes is returned and can be used to check that assumption.
We also assume that there are no dependency cycles.
*/
export const orderConceptsByDependency = (
concepts: LocalConceptDataInput[],
): { ordered: LocalConceptDataInput[]; missing: string[] } => {
if (concepts.length === 0) return { ordered: concepts, missing: [] };
const conceptById = Object.fromEntries(
concepts.map((c) => [c.source_local_id, c]),
) as { [key: string]: LocalConceptDataInput };
const ordered: LocalConceptDataInput[] = [];
let missing: Set<string> = new Set();
const processed: Set<string> = new Set();
while (Object.keys(conceptById).length > 0) {
const first = Object.values(conceptById)[0];
missing = new Set([
...missing,
...orderConceptsRec({
ordered,
concept: first,
remainder: conceptById,
processed,
}),
]);
}
return { ordered, missing: [...missing] };
};
// the input to the upsert method would look like this:
// const idata: LocalConceptDataInput[] = [
// { "name": "Claim", "author_local_id": "sR22zZ470dNPkIf9PpjQXXdTBjG2", "source_local_id": "a_roam_uid", "created": "2000/01/01", "last_modified": "2001/01/02", "is_schema": true },
// { "name": "A Claim", "author_local_id": "sR22zZ470dNPkIf9PpjQXXdTBjG2", "source_local_id": "a_roam_uid2", "created": "2000/01/03", "last_modified": "2001/01/04", "is_schema": false, "schema_represented_by_local_id": "a_roam_uid" },
// { "name": "test2", "author_local_id": "sR22zZ470dNPkIf9PpjQXXdTBjG2", "created": "2000/01/04", "last_modified": "2001/01/05", "is_schema": false, "literal_content": { "source": "a_roam_uid", "target": ["a_roam_uid", "a_roam_uid2"] }, "local_reference_content": { "source": "a_roam_uid", "target": ["a_roam_uid", "a_roam_uid2"] } }]
// const { data, error } = await supabase_client.rpc("upsert_concepts", { v_space_id: 12, data: idata });