File size: 3,227 Bytes
2d661b0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import type { FalkorClient, QueryParams } from "@engram/storage";
import type { BaseNode } from "./models/base";
import { createBitemporal, MAX_DATE, now } from "./utils/time";

/**
 * Regex pattern for valid Cypher labels and relationship types.
 * Labels/types must:
 * - Start with a letter (a-z, A-Z)
 * - Contain only letters, digits, and underscores
 * - Be at most 100 characters long
 */
const VALID_CYPHER_IDENTIFIER = /^[a-zA-Z][a-zA-Z0-9_]{0,99}$/;

/**
 * Validate a label or relationship type to prevent Cypher injection.
 * @param identifier The label or relationship type to validate
 * @param type Description for error message (e.g., "label", "relationship type")
 * @throws Error if the identifier is invalid
 */
function validateCypherIdentifier(identifier: string, type: string): void {
	if (!VALID_CYPHER_IDENTIFIER.test(identifier)) {
		throw new Error(
			`Invalid ${type}: "${identifier}". Must start with a letter and contain only letters, digits, and underscores (max 100 chars).`,
		);
	}
}

export class GraphWriter {
	constructor(private client: FalkorClient) {}

	async writeNode<T extends BaseNode>(
		label: string,
		data: Omit<T, "vt_start" | "vt_end" | "tt_start" | "tt_end">,
		validFrom: number = now(),
	): Promise<void> {
		// Validate label to prevent Cypher injection
		validateCypherIdentifier(label, "label");

		const temporal = createBitemporal(validFrom);
		const nodeData = { ...data, ...temporal };

		const propKeys = Object.keys(nodeData);
		const propsString = propKeys.map((k) => `${k}: $${k}`).join(", ");

		const query = `CREATE (n:${label} { ${propsString} })`;

		await this.client.query(query, nodeData as QueryParams);
	}

	async writeEdge(
		fromId: string,
		toId: string,
		relationType: string,
		props: Record<string, unknown> = {},
		validFrom: number = now(),
	): Promise<void> {
		// Validate relationship type to prevent Cypher injection
		validateCypherIdentifier(relationType, "relationship type");

		const temporal = createBitemporal(validFrom);
		const edgeData = { ...props, ...temporal };

		const propKeys = Object.keys(edgeData);
		const propsString = propKeys.map((k) => `${k}: $${k}`).join(", ");

		const query = `
      MATCH (a {id: $fromId}), (b {id: $toId})
      CREATE (a)-[:${relationType} { ${propsString} }]->(b)
    `;

		await this.client.query(query, { fromId, toId, ...edgeData });
	}

	// Transaction Time: Update (Append-Only / Replace)
	async updateNode<T extends BaseNode>(
		oldNodeId: string,
		label: string,
		newNodeData: Omit<T, "vt_start" | "vt_end" | "tt_start" | "tt_end">,
		validFrom: number = now(),
	): Promise<void> {
		// 1. Write the new node version
		await this.writeNode(label, newNodeData, validFrom);

		// 2. Link New -> Old via REPLACES
		// We expect newNodeData to contain the new 'id'
		await this.writeEdge(newNodeData.id, oldNodeId, "REPLACES", {}, validFrom);
	}

	// Transaction Time: Delete (Logical Delete)
	async deleteNode(id: string): Promise<void> {
		const t = now();
		// Close the transaction time interval for the current version
		const query = `
      MATCH (n {id: $id})
      WHERE n.tt_end = ${MAX_DATE}
      SET n.tt_end = $t
    `;
		await this.client.query(query, { id, t });
	}
}