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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
/**

 * Fast Neo4j Sync - Parallel batch sync with incremental support

 *

 * Much faster than sequential sync by:

 * 1. Using larger batches (500 instead of 100)

 * 2. Running multiple batches in parallel

 * 3. Using UNWIND for bulk operations

 * 4. Supporting incremental sync based on timestamps

 */

import neo4j, { Driver, Session } from 'neo4j-driver';
import { config } from 'dotenv';
import { resolve } from 'path';

// Load .env.production if it exists, otherwise fall back to .env
config({ path: resolve(process.cwd(), '.env.production') });
config({ path: resolve(process.cwd(), '.env') });

interface SyncConfig {
  localUri: string;
  localUser: string;
  localPassword: string;
  cloudUri: string;
  cloudUser: string;
  cloudPassword: string;
  batchSize: number;
  parallelBatches: number;
  incrementalOnly: boolean;
  lastSyncTime?: Date;
}

interface SyncStats {
  nodesCreated: number;
  nodesUpdated: number;
  relationshipsCreated: number;
  relationshipsUpdated: number;
  duration: number;
}

class FastNeo4jSync {
  private localDriver: Driver;
  private cloudDriver: Driver;
  private config: SyncConfig;
  private stats: SyncStats = {
    nodesCreated: 0,
    nodesUpdated: 0,
    relationshipsCreated: 0,
    relationshipsUpdated: 0,
    duration: 0
  };

  constructor(config: SyncConfig) {
    this.config = config;
    this.localDriver = neo4j.driver(
      config.localUri,
      neo4j.auth.basic(config.localUser, config.localPassword)
    );
    this.cloudDriver = neo4j.driver(
      config.cloudUri,
      neo4j.auth.basic(config.cloudUser, config.cloudPassword)
    );
  }

  async connect(): Promise<void> {
    console.log('πŸ“‘ Connecting to databases...');

    const localSession = this.localDriver.session();
    const cloudSession = this.cloudDriver.session();

    try {
      await localSession.run('RETURN 1');
      console.log('  βœ“ Local Neo4j connected');

      await cloudSession.run('RETURN 1');
      console.log('  βœ“ Cloud AuraDB connected');
    } finally {
      await localSession.close();
      await cloudSession.close();
    }
  }

  async getLastSyncTime(): Promise<Date | null> {
    const cloudSession = this.cloudDriver.session();
    try {
      const result = await cloudSession.run(`

        MATCH (s:_SyncMetadata {id: 'last_sync'})

        RETURN s.timestamp as timestamp

      `);
      if (result.records.length > 0) {
        const ts = result.records[0].get('timestamp');
        return ts ? new Date(ts) : null;
      }
      return null;
    } catch {
      return null;
    } finally {
      await cloudSession.close();
    }
  }

  async setLastSyncTime(timestamp: Date): Promise<void> {
    const cloudSession = this.cloudDriver.session();
    try {
      await cloudSession.run(`

        MERGE (s:_SyncMetadata {id: 'last_sync'})

        SET s.timestamp = $timestamp

      `, { timestamp: timestamp.toISOString() });
    } finally {
      await cloudSession.close();
    }
  }

  async countNodesToSync(since?: Date): Promise<number> {
    const localSession = this.localDriver.session();
    try {
      let query: string;
      let params: Record<string, unknown> = {};

      if (since) {
        query = `

          MATCH (n)

          WHERE n.updatedAt > $since OR n.createdAt > $since OR NOT exists(n.updatedAt)

          RETURN count(n) as count

        `;
        params = { since: since.toISOString() };
      } else {
        query = 'MATCH (n) RETURN count(n) as count';
      }

      const result = await localSession.run(query, params);
      return result.records[0].get('count').toNumber();
    } finally {
      await localSession.close();
    }
  }

  async countRelationshipsToSync(since?: Date): Promise<number> {
    const localSession = this.localDriver.session();
    try {
      let query: string;
      let params: Record<string, unknown> = {};

      if (since) {
        // For relationships, we sync if either end node was updated
        query = `

          MATCH (a)-[r]->(b)

          WHERE a.updatedAt > $since OR b.updatedAt > $since

            OR a.createdAt > $since OR b.createdAt > $since

            OR NOT exists(a.updatedAt)

          RETURN count(r) as count

        `;
        params = { since: since.toISOString() };
      } else {
        query = 'MATCH ()-[r]->() RETURN count(r) as count';
      }

      const result = await localSession.run(query, params);
      return result.records[0].get('count').toNumber();
    } finally {
      await localSession.close();
    }
  }

  async syncNodesInParallel(since?: Date): Promise<void> {
    const localSession = this.localDriver.session();
    const totalNodes = await this.countNodesToSync(since);

    console.log(`\nπŸ“¦ Syncing ${totalNodes} nodes (batch: ${this.config.batchSize}, parallel: ${this.config.parallelBatches})...`);

    if (totalNodes === 0) {
      console.log('  βœ“ No nodes to sync');
      return;
    }

    let offset = 0;
    let processed = 0;
    const startTime = Date.now();

    try {
      while (offset < totalNodes) {
        // Fetch multiple batches worth of nodes
        const fetchSize = this.config.batchSize * this.config.parallelBatches;

        let query: string;
        let params: Record<string, unknown> = { skip: neo4j.int(offset), limit: neo4j.int(fetchSize) };

        if (since) {
          query = `

            MATCH (n)

            WHERE n.updatedAt > $since OR n.createdAt > $since OR NOT exists(n.updatedAt)

            RETURN n, labels(n) as labels, elementId(n) as elementId

            SKIP $skip LIMIT $limit

          `;
          params.since = since.toISOString();
        } else {
          query = `

            MATCH (n)

            RETURN n, labels(n) as labels, elementId(n) as elementId

            SKIP $skip LIMIT $limit

          `;
        }

        const result = await localSession.run(query, params);
        const nodes = result.records.map(r => ({
          properties: r.get('n').properties,
          labels: r.get('labels') as string[],
          elementId: r.get('elementId') as string
        }));

        if (nodes.length === 0) break;

        // Split into batches and process in parallel
        const batches: typeof nodes[] = [];
        for (let i = 0; i < nodes.length; i += this.config.batchSize) {
          batches.push(nodes.slice(i, i + this.config.batchSize));
        }

        // Process batches in parallel
        await Promise.all(batches.map(batch => this.importNodeBatch(batch)));

        processed += nodes.length;
        offset += fetchSize;

        const elapsed = (Date.now() - startTime) / 1000;
        const rate = Math.round(processed / elapsed);
        const eta = Math.round((totalNodes - processed) / rate);

        console.log(`  Progress: ${processed}/${totalNodes} (${rate} nodes/sec, ETA: ${eta}s)`);
      }

      this.stats.nodesCreated = processed;
      console.log(`  βœ“ Synced ${processed} nodes in ${((Date.now() - startTime) / 1000).toFixed(1)}s`);
    } finally {
      await localSession.close();
    }
  }

  private async importNodeBatch(nodes: Array<{ properties: Record<string, unknown>; labels: string[]; elementId: string }>): Promise<void> {
    const cloudSession = this.cloudDriver.session();
    try {
      // Group nodes by label combination for efficient UNWIND
      const nodesByLabels = new Map<string, typeof nodes>();

      for (const node of nodes) {
        const labelKey = node.labels.sort().join(':');
        if (!nodesByLabels.has(labelKey)) {
          nodesByLabels.set(labelKey, []);
        }
        nodesByLabels.get(labelKey)!.push(node);
      }

      // Import each label group
      for (const [labelKey, labelNodes] of nodesByLabels) {
        const labels = labelKey || 'Node';
        const nodeData = labelNodes.map(n => ({
          ...n.properties,
          _syncId: n.elementId
        }));

        // Use MERGE to handle both create and update
        await cloudSession.run(`

          UNWIND $nodes as nodeData

          MERGE (n:${labels} {_syncId: nodeData._syncId})

          SET n = nodeData

        `, { nodes: nodeData });
      }
    } finally {
      await cloudSession.close();
    }
  }

  async syncRelationshipsInParallel(since?: Date): Promise<void> {
    const localSession = this.localDriver.session();
    const totalRels = await this.countRelationshipsToSync(since);

    console.log(`\nπŸ”— Syncing ${totalRels} relationships...`);

    if (totalRels === 0) {
      console.log('  βœ“ No relationships to sync');
      return;
    }

    let offset = 0;
    let processed = 0;
    const startTime = Date.now();

    try {
      while (offset < totalRels) {
        const fetchSize = this.config.batchSize * this.config.parallelBatches;

        let query: string;
        let params: Record<string, unknown> = { skip: neo4j.int(offset), limit: neo4j.int(fetchSize) };

        if (since) {
          query = `

            MATCH (a)-[r]->(b)

            WHERE a.updatedAt > $since OR b.updatedAt > $since

              OR a.createdAt > $since OR b.createdAt > $since

              OR NOT exists(a.updatedAt)

            RETURN type(r) as type, properties(r) as props,

                   elementId(a) as startId, elementId(b) as endId

            SKIP $skip LIMIT $limit

          `;
          params.since = since.toISOString();
        } else {
          query = `

            MATCH (a)-[r]->(b)

            RETURN type(r) as type, properties(r) as props,

                   elementId(a) as startId, elementId(b) as endId

            SKIP $skip LIMIT $limit

          `;
        }

        const result = await localSession.run(query, params);
        const rels = result.records.map(r => ({
          type: r.get('type') as string,
          properties: r.get('props') as Record<string, unknown>,
          startId: r.get('startId') as string,
          endId: r.get('endId') as string
        }));

        if (rels.length === 0) break;

        // Split into batches and process in parallel
        const batches: typeof rels[] = [];
        for (let i = 0; i < rels.length; i += this.config.batchSize) {
          batches.push(rels.slice(i, i + this.config.batchSize));
        }

        // Process batches sequentially to avoid deadlocks
        for (const batch of batches) {
          await this.importRelationshipBatch(batch);
        }

        processed += rels.length;
        offset += fetchSize;

        const elapsed = (Date.now() - startTime) / 1000;
        const rate = Math.round(processed / elapsed);
        const eta = Math.round((totalRels - processed) / rate);

        console.log(`  Progress: ${processed}/${totalRels} (${rate} rels/sec, ETA: ${eta}s)`);
      }

      this.stats.relationshipsCreated = processed;
      console.log(`  βœ“ Synced ${processed} relationships in ${((Date.now() - startTime) / 1000).toFixed(1)}s`);
    } finally {
      await localSession.close();
    }
  }

  private async importRelationshipBatch(rels: Array<{ type: string; properties: Record<string, unknown>; startId: string; endId: string }>): Promise<void> {
    const cloudSession = this.cloudDriver.session();
    try {
      // Group by relationship type
      const relsByType = new Map<string, typeof rels>();

      for (const rel of rels) {
        if (!relsByType.has(rel.type)) {
          relsByType.set(rel.type, []);
        }
        relsByType.get(rel.type)!.push(rel);
      }

      // Import each type
      for (const [relType, typeRels] of relsByType) {
        const relData = typeRels.map(r => ({
          startId: r.startId,
          endId: r.endId,
          props: r.properties
        }));

        await cloudSession.run(`

          UNWIND $rels as relData

          MATCH (a {_syncId: relData.startId})

          MATCH (b {_syncId: relData.endId})

          MERGE (a)-[r:${relType}]->(b)

          SET r = relData.props

        `, { rels: relData });
      }
    } finally {
      await cloudSession.close();
    }
  }

  async syncSchema(): Promise<void> {
    console.log('\nπŸ“‹ Syncing schema...');
    const localSession = this.localDriver.session();
    const cloudSession = this.cloudDriver.session();

    try {
      // Get constraints from local - build CREATE statements from available fields
      const constraintsResult = await localSession.run('SHOW CONSTRAINTS');

      for (const record of constraintsResult.records) {
        const name = record.get('name');
        const constraintType = record.get('type');
        const entityType = record.get('entityType');
        const labelsOrTypes = record.get('labelsOrTypes') as string[];
        const properties = record.get('properties') as string[];

        if (!name || name.startsWith('_')) continue;

        // Build CREATE CONSTRAINT statement
        let createStatement = '';
        const label = labelsOrTypes?.[0] || 'Node';
        const prop = properties?.[0] || 'id';

        if (constraintType === 'UNIQUENESS' && entityType === 'NODE') {
          createStatement = `CREATE CONSTRAINT ${name} IF NOT EXISTS FOR (n:${label}) REQUIRE n.${prop} IS UNIQUE`;
        } else if (constraintType === 'NODE_KEY') {
          createStatement = `CREATE CONSTRAINT ${name} IF NOT EXISTS FOR (n:${label}) REQUIRE n.${prop} IS NODE KEY`;
        }

        if (createStatement) {
          try {
            await cloudSession.run(createStatement);
            console.log(`  βœ“ Constraint: ${name}`);
          } catch (e: unknown) {
            const error = e as Error;
            if (!error.message?.includes('already exists') && !error.message?.includes('equivalent')) {
              console.log(`  ⚠ Constraint ${name}: ${error.message?.slice(0, 50)}`);
            }
          }
        }
      }

      // Get indexes - exclude LOOKUP and internal indexes
      const indexesResult = await localSession.run('SHOW INDEXES');

      for (const record of indexesResult.records) {
        const name = record.get('name');
        const indexType = record.get('type');
        const entityType = record.get('entityType');
        const labelsOrTypes = record.get('labelsOrTypes') as string[];
        const properties = record.get('properties') as string[];

        if (!name || name.startsWith('_') || indexType === 'LOOKUP') continue;

        const label = labelsOrTypes?.[0] || 'Node';
        const props = properties || [];

        let createStatement = '';

        if (indexType === 'RANGE' && entityType === 'NODE' && props.length > 0) {
          const propList = props.map(p => `n.${p}`).join(', ');
          createStatement = `CREATE INDEX ${name} IF NOT EXISTS FOR (n:${label}) ON (${propList})`;
        } else if (indexType === 'VECTOR' && props.length > 0) {
          // Skip vector indexes - they need special handling
          console.log(`  ⏭ Skipping vector index: ${name}`);
          continue;
        }

        if (createStatement) {
          try {
            await cloudSession.run(createStatement);
            console.log(`  βœ“ Index: ${name} (${indexType})`);
          } catch (e: unknown) {
            const error = e as Error;
            if (!error.message?.includes('already exists') && !error.message?.includes('equivalent')) {
              console.log(`  ⚠ Index ${name}: ${error.message?.slice(0, 50)}`);
            }
          }
        }
      }

      // Ensure _syncId index exists for fast lookups
      try {
        await cloudSession.run('CREATE INDEX sync_id_index IF NOT EXISTS FOR (n:Node) ON (n._syncId)');
      } catch {
        // Ignore
      }
    } finally {
      await localSession.close();
      await cloudSession.close();
    }
  }

  async run(): Promise<SyncStats> {
    const startTime = Date.now();

    console.log('═'.repeat(60));
    console.log('⚑ Fast Neo4j Sync');
    console.log('═'.repeat(60));
    console.log(`\nLocal:  ${this.config.localUri}`);
    console.log(`Cloud:  ${this.config.cloudUri}`);
    console.log(`Mode:   ${this.config.incrementalOnly ? 'Incremental' : 'Full'}`);
    console.log(`Batch:  ${this.config.batchSize} nodes Γ— ${this.config.parallelBatches} parallel`);

    await this.connect();

    let since: Date | undefined;

    if (this.config.incrementalOnly) {
      const lastSync = await this.getLastSyncTime();
      if (lastSync) {
        since = lastSync;
        console.log(`\nπŸ“… Last sync: ${lastSync.toISOString()}`);
        console.log('   Only syncing changes since then...');
      } else {
        console.log('\n⚠️  No previous sync found, doing full sync...');
      }
    }

    await this.syncSchema();
    await this.syncNodesInParallel(since);
    await this.syncRelationshipsInParallel(since);

    // Record sync time
    await this.setLastSyncTime(new Date());

    this.stats.duration = Date.now() - startTime;

    console.log('\n' + '═'.repeat(60));
    console.log('βœ… Sync Complete!');
    console.log('═'.repeat(60));
    console.log(`   Nodes: ${this.stats.nodesCreated}`);
    console.log(`   Relationships: ${this.stats.relationshipsCreated}`);
    console.log(`   Duration: ${(this.stats.duration / 1000).toFixed(1)}s`);
    console.log(`   Rate: ${Math.round((this.stats.nodesCreated + this.stats.relationshipsCreated) / (this.stats.duration / 1000))} items/sec`);

    return this.stats;
  }

  async close(): Promise<void> {
    await this.localDriver.close();
    await this.cloudDriver.close();
  }
}

// CLI
async function main() {
  const args = process.argv.slice(2);
  const incremental = args.includes('--incremental') || args.includes('-i');
  const help = args.includes('--help') || args.includes('-h');

  if (help) {
    console.log(`

Fast Neo4j Sync - Parallel batch sync with incremental support



Usage: npx tsx src/scripts/fast-neo4j-sync.ts [options]



Options:

  --incremental, -i  Only sync changes since last sync

  --help, -h         Show this help



Environment variables:

  NEO4J_LOCAL_URI       Local Neo4j URI (default: bolt://localhost:7687)

  NEO4J_LOCAL_USER      Local Neo4j user (default: neo4j)

  NEO4J_LOCAL_PASSWORD  Local Neo4j password

  NEO4J_CLOUD_URI       Cloud AuraDB URI

  NEO4J_CLOUD_USER      Cloud user (default: neo4j)

  NEO4J_CLOUD_PASSWORD  Cloud password

`);
    process.exit(0);
  }

  // Cloud URI from .env.production (NEO4J_URI contains the cloud AuraDB)
  const cloudUri = process.env.NEO4J_URI || '';
  const cloudPassword = process.env.NEO4J_PASSWORD || '';

  const syncConfig: SyncConfig = {
    localUri: process.env.NEO4J_LOCAL_URI || 'bolt://localhost:7687',
    localUser: process.env.NEO4J_LOCAL_USER || 'neo4j',
    localPassword: process.env.NEO4J_LOCAL_PASSWORD || 'password',  // Local default
    cloudUri: cloudUri,
    cloudUser: process.env.NEO4J_USER || 'neo4j',
    cloudPassword: cloudPassword,
    batchSize: 500,           // Larger batches
    parallelBatches: 4,       // 4 parallel operations
    incrementalOnly: incremental
  };

  if (!syncConfig.cloudUri) {
    console.error('❌ NEO4J_URI (cloud) is required in .env.production');
    process.exit(1);
  }

  const sync = new FastNeo4jSync(syncConfig);

  try {
    await sync.run();
  } finally {
    await sync.close();
  }
}

main().catch(console.error);