-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson.ts
More file actions
563 lines (512 loc) · 21.1 KB
/
Copy pathjson.ts
File metadata and controls
563 lines (512 loc) · 21.1 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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
import { existsSync, mkdirSync, readdirSync, readFileSync, renameSync, rmSync, statSync, writeFileSync } from "fs";
import path from "path";
import { duplicateError, validationError } from "../domain/errors";
import { assertValidId, createId } from "../domain/ids";
import { parseMetadata, parseMetadataSchema } from "../domain/metadata";
import { materializeBootstrapTypes } from "../domain/seed";
import type {
DeleteResult,
EdgeTypeDefinition,
GraphContext,
GraphEdge,
GraphNode,
GraphSnapshot,
Metadata,
MetadataSchema,
MutationResult,
NodeTypeDefinition,
SearchResult,
} from "../domain/types";
import {
assertDirection,
assertEdgeTypeUnused,
assertName,
assertNodeTypeUnused,
findActiveEdge,
findActiveEdgeType,
findActiveNode,
findActiveNodeType,
nowIso,
validateEdgeMetadata,
validateNodeMetadata,
} from "../domain/validation";
import type { EdgeInput, GraphRepository, NodeInput, TypeInput } from "./repository";
type Collection = "nodeTypes" | "edgeTypes" | "nodes" | "edges";
type RecordTypeName = "node type" | "edge type" | "node" | "edge";
interface JsonRepositoryOptions {
seed?: boolean;
}
const collectionDirs: Record<Collection, string> = {
nodeTypes: "node-types",
edgeTypes: "edge-types",
nodes: "nodes",
edges: "edges",
};
const recordTypeNames: Record<Collection, RecordTypeName> = {
nodeTypes: "node type",
edgeTypes: "edge type",
nodes: "node",
edges: "edge",
};
function isRecord(value: unknown): value is Record<string, unknown> {
return typeof value === "object" && value !== null && !Array.isArray(value);
}
function requiredString(record: Record<string, unknown>, field: string, filePath: string): string {
const value = record[field];
if (typeof value !== "string") {
throw validationError(`Invalid graph data in ${filePath}: ${field} must be a string.`, { filePath, field, value });
}
return value;
}
function sortObject(value: unknown): unknown {
if (Array.isArray(value)) return value.map(sortObject);
if (isRecord(value)) {
return Object.fromEntries(Object.entries(value).sort(([left], [right]) => left.localeCompare(right)).map(([key, entryValue]) => [key, sortObject(entryValue)]));
}
return value;
}
function orderedRecord(record: NodeTypeDefinition | EdgeTypeDefinition | GraphNode | GraphEdge): Record<string, unknown> {
if ("sourceNodeId" in record) {
return {
id: record.id,
typeId: record.typeId,
sourceNodeId: record.sourceNodeId,
targetNodeId: record.targetNodeId,
direction: record.direction,
description: record.description,
metadata: sortObject(record.metadata),
createdAt: record.createdAt,
updatedAt: record.updatedAt,
};
}
if ("typeId" in record) {
return {
id: record.id,
name: record.name,
typeId: record.typeId,
description: record.description,
metadata: sortObject(record.metadata),
createdAt: record.createdAt,
updatedAt: record.updatedAt,
};
}
return {
id: record.id,
name: record.name,
description: record.description,
metadataSchema: sortObject(record.metadataSchema),
createdAt: record.createdAt,
updatedAt: record.updatedAt,
};
}
function canonicalJson(record: NodeTypeDefinition | EdgeTypeDefinition | GraphNode | GraphEdge): string {
return `${JSON.stringify(orderedRecord(record), null, 2)}\n`;
}
function fileErrorPrefix(filePath: string): string {
return `Invalid graph data in ${filePath}`;
}
function shouldIgnoreCollectionEntry(fileName: string): boolean {
const lowerName = fileName.toLowerCase();
return fileName.startsWith(".") || lowerName.endsWith(".tmp") || lowerName === "thumbs.db" || lowerName === "desktop.ini";
}
function parseJsonFile(filePath: string): Record<string, unknown> {
const text = readFileSync(filePath, "utf8");
if (text.includes("<<<<<<<") || text.includes("=======") || text.includes(">>>>>>>")) {
throw validationError(`Invalid JSON in ${filePath}: possible unresolved Git merge conflict markers.`);
}
try {
const value = JSON.parse(text) as unknown;
if (!isRecord(value)) throw validationError(`${fileErrorPrefix(filePath)}: record must be a JSON object.`);
return value;
} catch (error) {
if (error instanceof SyntaxError) {
throw validationError(`Invalid JSON in ${filePath}: ${error.message}`);
}
throw error;
}
}
function assertFileNameMatchesId(filePath: string, id: string): void {
const expected = `${id}.json`;
const actual = path.basename(filePath);
if (actual !== expected) {
throw validationError(`${fileErrorPrefix(filePath)}: file name must match record id "${id}".`, { filePath, expected, actual, id });
}
}
function parseTypeRecord(record: Record<string, unknown>, filePath: string): NodeTypeDefinition {
const id = requiredString(record, "id", filePath);
assertValidId(id);
assertFileNameMatchesId(filePath, id);
return {
id,
name: requiredString(record, "name", filePath),
description: requiredString(record, "description", filePath),
metadataSchema: parseMetadataSchema(record.metadataSchema),
createdAt: requiredString(record, "createdAt", filePath),
updatedAt: requiredString(record, "updatedAt", filePath),
};
}
function parseNodeRecord(record: Record<string, unknown>, filePath: string): GraphNode {
const id = requiredString(record, "id", filePath);
assertValidId(id);
assertFileNameMatchesId(filePath, id);
return {
id,
name: requiredString(record, "name", filePath),
typeId: requiredString(record, "typeId", filePath),
description: requiredString(record, "description", filePath),
metadata: parseMetadata(record.metadata),
createdAt: requiredString(record, "createdAt", filePath),
updatedAt: requiredString(record, "updatedAt", filePath),
};
}
function parseEdgeRecord(record: Record<string, unknown>, filePath: string): GraphEdge {
const id = requiredString(record, "id", filePath);
assertValidId(id);
assertFileNameMatchesId(filePath, id);
const direction = requiredString(record, "direction", filePath);
if (direction !== "directed" && direction !== "bidirectional") {
throw validationError(`${fileErrorPrefix(filePath)}: direction must be directed or bidirectional.`, { filePath, direction });
}
return {
id,
typeId: requiredString(record, "typeId", filePath),
sourceNodeId: requiredString(record, "sourceNodeId", filePath),
targetNodeId: requiredString(record, "targetNodeId", filePath),
direction,
description: requiredString(record, "description", filePath),
metadata: parseMetadata(record.metadata),
createdAt: requiredString(record, "createdAt", filePath),
updatedAt: requiredString(record, "updatedAt", filePath),
};
}
function sortByName<T extends { name: string; id: string }>(records: T[]): T[] {
return records.sort((left, right) => left.name.localeCompare(right.name) || left.id.localeCompare(right.id));
}
function sortById<T extends { id: string }>(records: T[]): T[] {
return records.sort((left, right) => left.id.localeCompare(right.id));
}
export class JsonGraphRepository implements GraphRepository {
constructor(
private readonly graphPath: string,
options: JsonRepositoryOptions = {},
) {
if (options.seed === true && this.isEmpty()) {
this.seedBootstrap();
} else {
this.getSnapshot();
}
}
close(): void {}
private collectionPath(collection: Collection): string {
return path.join(this.graphPath, collectionDirs[collection]);
}
private recordPath(collection: Collection, id: string): string {
return path.join(this.collectionPath(collection), `${id}.json`);
}
private ensureDirectories(): void {
mkdirSync(this.graphPath, { recursive: true });
for (const dir of Object.values(collectionDirs)) {
mkdirSync(path.join(this.graphPath, dir), { recursive: true });
}
}
private isEmpty(): boolean {
return (Object.keys(collectionDirs) as Collection[]).every(collection => {
const dir = this.collectionPath(collection);
return !existsSync(dir) || readdirSync(dir).filter(file => file.endsWith(".json")).length === 0;
});
}
private readCollection<T extends NodeTypeDefinition | EdgeTypeDefinition | GraphNode | GraphEdge>(collection: Collection, parse: (record: Record<string, unknown>, filePath: string) => T): T[] {
const dir = this.collectionPath(collection);
if (!existsSync(dir)) return [];
const records: T[] = [];
for (const fileName of readdirSync(dir).sort()) {
if (shouldIgnoreCollectionEntry(fileName)) continue;
const filePath = path.join(dir, fileName);
if (!statSync(filePath).isFile()) continue;
if (!fileName.endsWith(".json")) {
throw validationError(`${fileErrorPrefix(filePath)}: graph record files must use the .json extension.`, { filePath });
}
const record = parse(parseJsonFile(filePath), filePath);
const text = readFileSync(filePath, "utf8");
const canonical = canonicalJson(record);
if (text !== canonical) {
throw validationError(`${fileErrorPrefix(filePath)}: JSON is not in canonical format.`, { filePath });
}
records.push(record);
}
return records;
}
private assertUnique(collection: Collection, records: Array<{ id: string }>): void {
const seen = new Set<string>();
for (const record of records) {
if (seen.has(record.id)) {
throw duplicateError(`Duplicate ${recordTypeNames[collection]} ID in graph data.`, { collection, id: record.id });
}
seen.add(record.id);
}
}
private validateSnapshot(snapshot: GraphSnapshot): GraphSnapshot {
this.assertUnique("nodeTypes", snapshot.nodeTypes);
this.assertUnique("edgeTypes", snapshot.edgeTypes);
this.assertUnique("nodes", snapshot.nodes);
this.assertUnique("edges", snapshot.edges);
const nodeTypeIds = new Set(snapshot.nodeTypes.map(type => type.id));
const edgeTypeIds = new Set(snapshot.edgeTypes.map(type => type.id));
const nodeIds = new Set(snapshot.nodes.map(node => node.id));
for (const node of snapshot.nodes) {
if (!nodeTypeIds.has(node.typeId)) {
throw validationError(`Invalid graph data: node "${node.id}" typeId "${node.typeId}" does not reference an existing node type.`, { recordType: "node", recordId: node.id, missingId: node.typeId });
}
validateNodeMetadata(snapshot, node.typeId, node.metadata);
}
for (const edge of snapshot.edges) {
if (!edgeTypeIds.has(edge.typeId)) {
throw validationError(`Invalid graph data: edge "${edge.id}" typeId "${edge.typeId}" does not reference an existing edge type.`, { recordType: "edge", recordId: edge.id, missingId: edge.typeId });
}
if (!nodeIds.has(edge.sourceNodeId)) {
throw validationError(`Invalid graph data: edge "${edge.id}" sourceNodeId "${edge.sourceNodeId}" does not reference an existing node.`, { recordType: "edge", recordId: edge.id, missingId: edge.sourceNodeId });
}
if (!nodeIds.has(edge.targetNodeId)) {
throw validationError(`Invalid graph data: edge "${edge.id}" targetNodeId "${edge.targetNodeId}" does not reference an existing node.`, { recordType: "edge", recordId: edge.id, missingId: edge.targetNodeId });
}
validateEdgeMetadata(snapshot, edge.typeId, edge.metadata);
}
return snapshot;
}
getSnapshot(): GraphSnapshot {
const snapshot = {
nodeTypes: sortByName(this.readCollection("nodeTypes", parseTypeRecord)),
edgeTypes: sortByName(this.readCollection("edgeTypes", parseTypeRecord)),
nodes: sortByName(this.readCollection("nodes", parseNodeRecord)),
edges: sortById(this.readCollection("edges", parseEdgeRecord)),
};
return this.validateSnapshot(snapshot);
}
search(query: string): SearchResult {
const needle = query.trim().toLowerCase();
const includesNeedle = (value: unknown) => JSON.stringify(value).toLowerCase().includes(needle);
const snapshot = this.getSnapshot();
if (!needle) return { nodes: [], nodeTypes: [], edgeTypes: [], edges: [] };
return {
nodes: snapshot.nodes.filter(record => includesNeedle(record)),
nodeTypes: snapshot.nodeTypes.filter(record => includesNeedle(record)),
edgeTypes: snapshot.edgeTypes.filter(record => includesNeedle(record)),
edges: snapshot.edges.filter(record => includesNeedle(record)),
};
}
getContext(nodeId: string): GraphContext {
const snapshot = this.getSnapshot();
const node = findActiveNode(snapshot, nodeId);
const edges = snapshot.edges.filter(edge => edge.sourceNodeId === nodeId || edge.targetNodeId === nodeId);
const nodeIds = new Set(edges.flatMap(edge => [edge.sourceNodeId, edge.targetNodeId]).filter(id => id !== nodeId));
const nodes = snapshot.nodes.filter(candidate => nodeIds.has(candidate.id));
return { node, edges, nodes };
}
private writeRecord(collection: Collection, record: NodeTypeDefinition | EdgeTypeDefinition | GraphNode | GraphEdge): void {
this.ensureDirectories();
const filePath = this.recordPath(collection, record.id);
const tempPath = path.join(path.dirname(filePath), `.${path.basename(filePath)}.${process.pid}.${Date.now()}.tmp`);
writeFileSync(tempPath, canonicalJson(record), "utf8");
renameSync(tempPath, filePath);
}
private deleteRecord(collection: Collection, id: string): void {
const filePath = this.recordPath(collection, id);
rmSync(filePath);
}
private seedBootstrap(): void {
const seed = materializeBootstrapTypes();
for (const type of seed.nodeTypes) this.writeRecord("nodeTypes", type);
for (const type of seed.edgeTypes) this.writeRecord("edgeTypes", type);
}
private assertNoIdChange(id: string, inputId: string | undefined): void {
if (inputId !== undefined && inputId !== "" && inputId !== id) {
throw validationError("Record IDs are immutable and cannot be changed.", { id, inputId });
}
}
createNodeType(input: TypeInput): MutationResult<NodeTypeDefinition> {
assertName(input.name);
const snapshot = this.getSnapshot();
const timestamp = nowIso();
const record: NodeTypeDefinition = {
id: createId({ explicitId: input.id, name: input.name, existingIds: snapshot.nodeTypes.map(type => type.id) }),
name: input.name.trim(),
description: input.description?.trim() ?? "",
metadataSchema: parseMetadataSchema(input.metadataSchema),
createdAt: timestamp,
updatedAt: timestamp,
};
this.writeRecord("nodeTypes", record);
return { record };
}
updateNodeType(id: string, input: Partial<TypeInput>): MutationResult<NodeTypeDefinition> {
this.assertNoIdChange(id, input.id);
const before = findActiveNodeType(this.getSnapshot(), id);
const record: NodeTypeDefinition = {
...before,
name: input.name?.trim() ?? before.name,
description: input.description?.trim() ?? before.description,
metadataSchema: input.metadataSchema === undefined ? before.metadataSchema : parseMetadataSchema(input.metadataSchema),
updatedAt: nowIso(),
};
assertName(record.name);
this.writeRecord("nodeTypes", record);
return { record };
}
deleteNodeType(id: string): DeleteResult {
const snapshot = this.getSnapshot();
findActiveNodeType(snapshot, id);
assertNodeTypeUnused(snapshot, id);
this.deleteRecord("nodeTypes", id);
return { deletedId: id };
}
createEdgeType(input: TypeInput): MutationResult<EdgeTypeDefinition> {
assertName(input.name);
const snapshot = this.getSnapshot();
const timestamp = nowIso();
const record: EdgeTypeDefinition = {
id: createId({ explicitId: input.id, name: input.name, existingIds: snapshot.edgeTypes.map(type => type.id) }),
name: input.name.trim(),
description: input.description?.trim() ?? "",
metadataSchema: parseMetadataSchema(input.metadataSchema),
createdAt: timestamp,
updatedAt: timestamp,
};
this.writeRecord("edgeTypes", record);
return { record };
}
updateEdgeType(id: string, input: Partial<TypeInput>): MutationResult<EdgeTypeDefinition> {
this.assertNoIdChange(id, input.id);
const before = findActiveEdgeType(this.getSnapshot(), id);
const record: EdgeTypeDefinition = {
...before,
name: input.name?.trim() ?? before.name,
description: input.description?.trim() ?? before.description,
metadataSchema: input.metadataSchema === undefined ? before.metadataSchema : parseMetadataSchema(input.metadataSchema),
updatedAt: nowIso(),
};
assertName(record.name);
this.writeRecord("edgeTypes", record);
return { record };
}
deleteEdgeType(id: string): DeleteResult {
const snapshot = this.getSnapshot();
findActiveEdgeType(snapshot, id);
assertEdgeTypeUnused(snapshot, id);
this.deleteRecord("edgeTypes", id);
return { deletedId: id };
}
createNode(input: NodeInput): MutationResult<GraphNode> {
assertName(input.name);
const snapshot = this.getSnapshot();
findActiveNodeType(snapshot, input.typeId);
const metadata = validateNodeMetadata(snapshot, input.typeId, parseMetadata(input.metadata));
const timestamp = nowIso();
const record: GraphNode = {
id: createId({ explicitId: input.id, name: input.name, existingIds: snapshot.nodes.map(node => node.id) }),
name: input.name.trim(),
typeId: input.typeId,
description: input.description?.trim() ?? "",
metadata,
createdAt: timestamp,
updatedAt: timestamp,
};
this.writeRecord("nodes", record);
return { record };
}
updateNode(id: string, input: Partial<NodeInput>): MutationResult<GraphNode> {
this.assertNoIdChange(id, input.id);
const snapshot = this.getSnapshot();
const before = findActiveNode(snapshot, id);
const typeId = input.typeId ?? before.typeId;
findActiveNodeType(snapshot, typeId);
const metadata = validateNodeMetadata(snapshot, typeId, parseMetadata(input.metadata ?? before.metadata));
const record: GraphNode = {
...before,
name: input.name?.trim() ?? before.name,
typeId,
description: input.description?.trim() ?? before.description,
metadata,
updatedAt: nowIso(),
};
assertName(record.name);
this.writeRecord("nodes", record);
return { record };
}
deleteNode(id: string): DeleteResult {
const snapshot = this.getSnapshot();
findActiveNode(snapshot, id);
for (const edge of snapshot.edges.filter(candidate => candidate.sourceNodeId === id || candidate.targetNodeId === id)) {
this.deleteRecord("edges", edge.id);
}
this.deleteRecord("nodes", id);
return { deletedId: id };
}
createEdge(input: EdgeInput): MutationResult<GraphEdge> {
assertDirection(input.direction);
const snapshot = this.getSnapshot();
findActiveEdgeType(snapshot, input.typeId);
findActiveNode(snapshot, input.sourceNodeId);
findActiveNode(snapshot, input.targetNodeId);
const metadata = validateEdgeMetadata(snapshot, input.typeId, parseMetadata(input.metadata));
const timestamp = nowIso();
const fallbackName = `${input.sourceNodeId}-${input.typeId}-${input.targetNodeId}`;
const record: GraphEdge = {
id: createId({ explicitId: input.id, name: fallbackName, existingIds: snapshot.edges.map(edge => edge.id) }),
typeId: input.typeId,
sourceNodeId: input.sourceNodeId,
targetNodeId: input.targetNodeId,
direction: input.direction,
description: input.description?.trim() ?? "",
metadata,
createdAt: timestamp,
updatedAt: timestamp,
};
this.writeRecord("edges", record);
return { record };
}
updateEdge(id: string, input: Partial<EdgeInput>): MutationResult<GraphEdge> {
this.assertNoIdChange(id, input.id);
const snapshot = this.getSnapshot();
const before = findActiveEdge(snapshot, id);
const typeId = input.typeId ?? before.typeId;
const sourceNodeId = input.sourceNodeId ?? before.sourceNodeId;
const targetNodeId = input.targetNodeId ?? before.targetNodeId;
const direction = input.direction ?? before.direction;
assertDirection(direction);
findActiveEdgeType(snapshot, typeId);
findActiveNode(snapshot, sourceNodeId);
findActiveNode(snapshot, targetNodeId);
const metadata = validateEdgeMetadata(snapshot, typeId, parseMetadata(input.metadata ?? before.metadata));
const record: GraphEdge = {
...before,
typeId,
sourceNodeId,
targetNodeId,
direction,
description: input.description?.trim() ?? before.description,
metadata,
updatedAt: nowIso(),
};
this.writeRecord("edges", record);
return { record };
}
deleteEdge(id: string): DeleteResult {
findActiveEdge(this.getSnapshot(), id);
this.deleteRecord("edges", id);
return { deletedId: id };
}
}
export function validateGraphPath(graphPath: string): GraphSnapshot {
if (!existsSync(graphPath)) {
return { nodeTypes: [], edgeTypes: [], nodes: [], edges: [] };
}
for (const dir of Object.values(collectionDirs)) {
const dirPath = path.join(graphPath, dir);
if (!existsSync(dirPath)) continue;
if (!statSync(dirPath).isDirectory()) {
throw validationError(`Invalid graph data in ${dirPath}: expected a directory.`);
}
}
const repository = new JsonGraphRepository(graphPath);
return repository.getSnapshot();
}