@cadenza.io/service 1.8.1 → 1.8.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -2420,7 +2420,7 @@ var DatabaseController = class _DatabaseController {
2420
2420
  }).join(", ");
2421
2421
  let sql = "";
2422
2422
  if ((_a2 = schema.meta) == null ? void 0 : _a2.dropExisting) {
2423
- sql = `DROP TABLE IF EXISTS ${tableName};`;
2423
+ sql = `DROP TABLE IF EXISTS ${tableName} CASCADE;`;
2424
2424
  ddl.push(sql);
2425
2425
  }
2426
2426
  sql = `CREATE TABLE IF NOT EXISTS ${tableName} (${fieldDefs})`;
@@ -2439,53 +2439,52 @@ var DatabaseController = class _DatabaseController {
2439
2439
  return { ddl, table, tableName, schema, options };
2440
2440
  }).then(
2441
2441
  CadenzaService.createMetaTask(
2442
- "Generate unique index DDL",
2442
+ "Generate primary key ddl",
2443
2443
  (ctx) => {
2444
2444
  const { ddl, table, tableName, schema, options } = ctx;
2445
- if (table.uniqueConstraints) {
2446
- table.uniqueConstraints.forEach(
2447
- (fields) => {
2448
- ddl.push(
2449
- `ALTER TABLE ${tableName} DROP CONSTRAINT IF EXISTS unique_${tableName}_${fields.join("_")};`,
2450
- `ALTER TABLE ${tableName} ADD CONSTRAINT unique_${tableName}_${fields.join("_")} UNIQUE (${fields.join(", ")});`
2451
- );
2452
- }
2445
+ if (table.primaryKey) {
2446
+ ddl.push(
2447
+ `ALTER TABLE ${tableName} DROP CONSTRAINT IF EXISTS unique_${tableName}_${table.primaryKey.join("_")} CASCADE;`,
2448
+ // TODO: should be cascade?
2449
+ `ALTER TABLE ${tableName} ADD CONSTRAINT unique_${tableName}_${table.primaryKey.join("_")} PRIMARY KEY (${table.primaryKey.join(", ")});`
2453
2450
  );
2454
2451
  }
2455
2452
  return { ddl, table, tableName, schema, options };
2456
2453
  }
2457
2454
  ).then(
2458
2455
  CadenzaService.createMetaTask(
2459
- "Generate foreign key DDL",
2456
+ "Generate unique index DDL",
2460
2457
  (ctx) => {
2461
2458
  const { ddl, table, tableName, schema, options } = ctx;
2462
- if (table.foreignKeys) {
2463
- for (const foreignKey of table.foreignKeys) {
2464
- const foreignKeyName = `fk_${tableName}_${foreignKey.fields.join("_")}`;
2465
- ddl.push(
2466
- `ALTER TABLE ${tableName} DROP CONSTRAINT IF EXISTS ${foreignKeyName};`,
2467
- `ALTER TABLE ${tableName} ADD CONSTRAINT ${foreignKeyName} FOREIGN KEY (${foreignKey.fields.join(
2468
- ", "
2469
- )}) REFERENCES ${foreignKey.tableName} (${foreignKey.referenceFields.join(
2470
- ", "
2471
- )});`
2472
- );
2473
- }
2459
+ if (table.uniqueConstraints) {
2460
+ table.uniqueConstraints.forEach(
2461
+ (fields) => {
2462
+ ddl.push(
2463
+ `ALTER TABLE ${tableName} DROP CONSTRAINT IF EXISTS unique_${tableName}_${fields.join("_")}; CASCADE`,
2464
+ // TODO: should be cascade?
2465
+ `ALTER TABLE ${tableName} ADD CONSTRAINT unique_${tableName}_${fields.join("_")} UNIQUE (${fields.join(", ")});`
2466
+ );
2467
+ }
2468
+ );
2474
2469
  }
2475
2470
  return { ddl, table, tableName, schema, options };
2476
2471
  }
2477
2472
  ).then(
2478
2473
  CadenzaService.createMetaTask(
2479
- "Generate trigger DDL",
2474
+ "Generate foreign key DDL",
2480
2475
  (ctx) => {
2481
2476
  const { ddl, table, tableName, schema, options } = ctx;
2482
- if (table.triggers) {
2483
- for (const [
2484
- triggerName,
2485
- trigger
2486
- ] of Object.entries(table.triggers)) {
2477
+ if (table.foreignKeys) {
2478
+ for (const foreignKey of table.foreignKeys) {
2479
+ const foreignKeyName = `fk_${tableName}_${foreignKey.fields.join("_")}`;
2487
2480
  ddl.push(
2488
- `CREATE TRIGGER ${triggerName} ${trigger.when} ${trigger.event} ON ${tableName} FOR EACH STATEMENT EXECUTE FUNCTION ${trigger.function};`
2481
+ `ALTER TABLE ${tableName} DROP CONSTRAINT IF EXISTS ${foreignKeyName} CASCADE;`,
2482
+ // TODO: should be cascade?
2483
+ `ALTER TABLE ${tableName} ADD CONSTRAINT ${foreignKeyName} FOREIGN KEY (${foreignKey.fields.join(
2484
+ ", "
2485
+ )}) REFERENCES ${foreignKey.tableName} (${foreignKey.referenceFields.join(
2486
+ ", "
2487
+ )});`
2489
2488
  );
2490
2489
  }
2491
2490
  }
@@ -2493,58 +2492,90 @@ var DatabaseController = class _DatabaseController {
2493
2492
  }
2494
2493
  ).then(
2495
2494
  CadenzaService.createMetaTask(
2496
- "Generate initial data DDL",
2495
+ "Generate trigger DDL",
2497
2496
  (ctx) => {
2498
2497
  const { ddl, table, tableName, schema, options } = ctx;
2499
- if (table.initialData) {
2500
- ddl.push(
2501
- `INSERT INTO ${tableName} (${table.initialData.fields.join(", ")}) VALUES ${table.initialData.data.map(
2502
- (row) => `(${row.map(
2503
- (value) => value === void 0 ? "NULL" : value.charAt(0) === "'" ? value : `'${value}'`
2504
- ).join(", ")})`
2505
- // TODO: handle non string data
2506
- ).join(", ")} ON CONFLICT DO NOTHING;`
2507
- );
2498
+ if (table.triggers) {
2499
+ for (const [
2500
+ triggerName,
2501
+ trigger
2502
+ ] of Object.entries(table.triggers)) {
2503
+ ddl.push(
2504
+ `CREATE TRIGGER ${triggerName} ${trigger.when} ${trigger.event} ON ${tableName} FOR EACH STATEMENT EXECUTE FUNCTION ${trigger.function};`
2505
+ );
2506
+ }
2508
2507
  }
2509
2508
  return { ddl, table, tableName, schema, options };
2510
2509
  }
2511
2510
  ).then(
2512
- CadenzaService.createUniqueMetaTask("Join DDL", (ctx) => {
2513
- const { joinedContexts } = ctx;
2514
- const ddl = [];
2515
- for (const joinedContext of joinedContexts) {
2516
- ddl.push(...joinedContext.ddl);
2511
+ CadenzaService.createMetaTask(
2512
+ "Generate initial data DDL",
2513
+ (ctx) => {
2514
+ const {
2515
+ ddl,
2516
+ table,
2517
+ tableName,
2518
+ schema,
2519
+ options
2520
+ } = ctx;
2521
+ if (table.initialData) {
2522
+ ddl.push(
2523
+ `INSERT INTO ${tableName} (${table.initialData.fields.join(", ")}) VALUES ${table.initialData.data.map(
2524
+ (row) => `(${row.map(
2525
+ (value) => value === void 0 ? "NULL" : value.charAt(0) === "'" ? value : `'${value}'`
2526
+ ).join(", ")})`
2527
+ // TODO: handle non string data
2528
+ ).join(", ")} ON CONFLICT DO NOTHING;`
2529
+ );
2530
+ }
2531
+ return {
2532
+ ddl,
2533
+ table,
2534
+ tableName,
2535
+ schema,
2536
+ options
2537
+ };
2517
2538
  }
2518
- ddl.flat();
2519
- return {
2520
- ddl,
2521
- schema: joinedContexts[0].schema,
2522
- options: joinedContexts[0].options
2523
- };
2524
- }).then(
2525
- CadenzaService.createMetaTask(
2526
- "meta.applyDatabaseChanges",
2527
- (ctx) => __async(this, null, function* () {
2528
- const { ddl } = ctx;
2529
- if (ddl && ddl.length > 0) {
2530
- try {
2531
- for (const sql of ddl) {
2532
- console.log("Applying SQL", sql);
2533
- yield this.dbClient.query(sql);
2539
+ ).then(
2540
+ CadenzaService.createUniqueMetaTask(
2541
+ "Join DDL",
2542
+ (ctx) => {
2543
+ const { joinedContexts } = ctx;
2544
+ const ddl = [];
2545
+ for (const joinedContext of joinedContexts) {
2546
+ ddl.push(...joinedContext.ddl);
2547
+ }
2548
+ ddl.flat();
2549
+ return {
2550
+ ddl,
2551
+ schema: joinedContexts[0].schema,
2552
+ options: joinedContexts[0].options
2553
+ };
2554
+ }
2555
+ ).then(
2556
+ CadenzaService.createMetaTask(
2557
+ "Apply Database Changes",
2558
+ (ctx) => __async(this, null, function* () {
2559
+ const { ddl } = ctx;
2560
+ if (ddl && ddl.length > 0) {
2561
+ try {
2562
+ for (const sql of ddl) {
2563
+ console.log("Applying SQL", sql);
2564
+ yield this.dbClient.query(sql);
2565
+ }
2566
+ } catch (error) {
2567
+ console.error(
2568
+ "Error applying DDL",
2569
+ error
2570
+ );
2534
2571
  }
2535
- } catch (error) {
2536
- console.error(
2537
- "Error applying DDL",
2538
- error
2539
- );
2540
- throw error;
2541
2572
  }
2542
- }
2543
- console.log("DDL applied");
2544
- return ctx;
2545
- }),
2546
- "Applies generated DDL to the database"
2547
- ).emits("meta.database.setup_done")
2573
+ console.log("DDL applied");
2574
+ return ctx;
2575
+ }),
2576
+ "Applies generated DDL to the database"
2577
+ ).emits("meta.database.setup_done")
2578
+ )
2548
2579
  )
2549
2580
  )
2550
2581
  )