swadhinbiswas feat: add src/db/adapter/neo4j.ts · 2/18/2026
7952467
191 lines · 5.9 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
import {
  BaseDatabaseAdapter,
  ColumnType,
  DatabaseConfig,
  Migration,
  MigrationStatus,
  QueryOptions,
  QueryResult,
  TableAlterCommand,
  TableSchema,
  WhereClause,
} from "./types";

const dynamicImport = new Function("m", "return import(m)") as (m: string) => Promise<any>;

function toWhereCypher(where?: WhereClause[]): { clause: string; params: Record<string, any> } {
  if (!where?.length) return { clause: "", params: {} };
  const params: Record<string, any> = {};
  const parts: string[] = [];
  where.forEach((w, i) => {
    const key = `p${i}`;
    params[key] = w.value;
    const field = `n.${w.field}`;
    switch (w.operator) {
      case "=":
      case "eq":
        parts.push(`${field} = $${key}`);
        break;
      case "!=":
      case "ne":
        parts.push(`${field} <> $${key}`);
        break;
      case ">":
      case "gt":
        parts.push(`${field} > $${key}`);
        break;
      case ">=":
      case "gte":
        parts.push(`${field} >= $${key}`);
        break;
      case "<":
      case "lt":
        parts.push(`${field} < $${key}`);
        break;
      case "<=":
      case "lte":
        parts.push(`${field} <= $${key}`);
        break;
      default:
        break;
    }
  });
  return { clause: parts.length ? `WHERE ${parts.join(" AND ")}` : "", params };
}

export class Neo4jAdapter extends BaseDatabaseAdapter {
  private driver: any;

  async connect(): Promise<void> {
    if (this.connected) return;
    const neo4j = await dynamicImport("neo4j-driver");
    this.driver = neo4j.driver(
      this.config.url,
      neo4j.auth.basic(this.config.user || "neo4j", this.config.password || "")
    );
    await this.driver.verifyConnectivity();
    this.connected = true;
  }

  async disconnect(): Promise<void> {
    if (!this.connected) return;
    await this.driver?.close();
    this.connected = false;
  }

  private async run(query: string, params?: Record<string, any>) {
    const session = this.driver.session();
    try {
      return await session.run(query, params || {});
    } finally {
      await session.close();
    }
  }

  async findOne<T>(table: string, options?: QueryOptions): Promise<T | null> {
    const rows = await this.findMany<T>(table, { ...options, limit: 1 });
    return rows[0] || null;
  }

  async findMany<T>(table: string, options?: QueryOptions): Promise<T[]> {
    const where = toWhereCypher(options?.where);
    const limit = options?.limit ? `LIMIT ${options.limit}` : "";
    const result = await this.run(
      `MATCH (n:${table}) ${where.clause} RETURN n ${limit}`,
      where.params
    );
    return result.records.map((r: any) => r.get("n").properties) as T[];
  }

  async create<T>(table: string, data: Partial<T>): Promise<T> {
    const props = { ...(data as any) };
    if (!props.id) props.id = `${Date.now()}_${Math.random().toString(36).slice(2)}`;
    await this.run(`CREATE (n:${table} $props)`, { props });
    return props as T;
  }

  async createMany<T>(table: string, data: Partial<T>[]): Promise<T[]> {
    const out: T[] = [];
    for (const item of data) out.push(await this.create<T>(table, item));
    return out;
  }

  async update<T>(table: string, id: string | number, data: Partial<T>): Promise<T | null> {
    const props = { ...(data as any) };
    const result = await this.run(
      `MATCH (n:${table} {id: $id}) SET n += $props RETURN n`,
      { id: String(id), props }
    );
    if (!result.records.length) return null;
    return result.records[0].get("n").properties as T;
  }

  async updateMany<T>(table: string, options: QueryOptions, data: Partial<T>): Promise<number> {
    const where = toWhereCypher(options?.where);
    const result = await this.run(
      `MATCH (n:${table}) ${where.clause} SET n += $props RETURN count(n) as c`,
      { ...where.params, props: data }
    );
    return Number(result.records[0]?.get("c") || 0);
  }

  async delete(table: string, id: string | number): Promise<boolean> {
    const result = await this.run(
      `MATCH (n:${table} {id: $id}) DETACH DELETE n RETURN count(n) as c`,
      { id: String(id) }
    );
    return Number(result.records[0]?.get("c") || 0) > 0;
  }

  async deleteMany(table: string, options?: QueryOptions): Promise<number> {
    const where = toWhereCypher(options?.where);
    const result = await this.run(
      `MATCH (n:${table}) ${where.clause} WITH n, count(n) as c DETACH DELETE n RETURN c`,
      where.params
    );
    return Number(result.records[0]?.get("c") || 0);
  }

  async count(table: string, options?: QueryOptions): Promise<number> {
    const where = toWhereCypher(options?.where);
    const result = await this.run(`MATCH (n:${table}) ${where.clause} RETURN count(n) as c`, where.params);
    return Number(result.records[0]?.get("c") || 0);
  }

  async rawQuery<T>(query: string, params?: unknown[]): Promise<QueryResult<T>> {
    const result = await this.run(query, (params as any) || {});
    return {
      rows: result.records.map((r: any) => r.toObject()) as T[],
      rowCount: result.records.length,
    };
  }

  async beginTransaction(): Promise<void> {}
  async commitTransaction(): Promise<void> {}
  async rollbackTransaction(): Promise<void> {}

  async tableExists(table: string): Promise<boolean> {
    const result = await this.run("CALL db.labels() YIELD label RETURN collect(label) as labels");
    const labels = result.records[0]?.get("labels") || [];
    return labels.includes(table);
  }

  async createTable(table: string, schema: TableSchema): Promise<void> {}
  async dropTable(table: string): Promise<void> {
    await this.run(`MATCH (n:${table}) DETACH DELETE n`);
  }
  async alterTable(table: string, changes: TableAlterCommand[]): Promise<void> {}

  async runMigration(migration: Migration): Promise<void> {
    await migration.up(this);
  }

  async getMigrationStatus(): Promise<MigrationStatus[]> {
    return [];
  }

  protected mapColumnType(type: ColumnType): string {
    return type;
  }
}