@cadenza.io/service 1.8.0 → 1.8.2

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
@@ -2392,13 +2392,14 @@ var DatabaseController = class _DatabaseController {
2392
2392
  "Generates auto-tasks for database schema"
2393
2393
  ),
2394
2394
  CadenzaService.createMetaTask("Generate DDL from table", (ctx) => {
2395
+ var _a2;
2395
2396
  const { ddl, table, tableName, schema, options } = ctx;
2396
2397
  const fieldDefs = Object.entries(table.fields).map((value) => {
2397
- var _a2, _b2, _c2, _d2, _e, _f, _g;
2398
+ var _a3, _b2, _c2, _d2, _e, _f, _g;
2398
2399
  const [fieldName, field] = value;
2399
2400
  let def = `${fieldName} ${field.type.toUpperCase()}`;
2400
2401
  if (field.type === "varchar")
2401
- def += `(${(_b2 = (_a2 = field.constraints) == null ? void 0 : _a2.maxLength) != null ? _b2 : 255})`;
2402
+ def += `(${(_b2 = (_a3 = field.constraints) == null ? void 0 : _a3.maxLength) != null ? _b2 : 255})`;
2402
2403
  if (field.type === "decimal")
2403
2404
  def += `(${(_d2 = (_c2 = field.constraints) == null ? void 0 : _c2.precision) != null ? _d2 : 10},${(_f = (_e = field.constraints) == null ? void 0 : _e.scale) != null ? _f : 2})`;
2404
2405
  if (field.primary) def += " PRIMARY KEY";
@@ -2418,7 +2419,7 @@ var DatabaseController = class _DatabaseController {
2418
2419
  return def;
2419
2420
  }).join(", ");
2420
2421
  let sql = "";
2421
- if (schema.dropExisting) {
2422
+ if ((_a2 = schema.meta) == null ? void 0 : _a2.dropExisting) {
2422
2423
  sql = `DROP TABLE IF EXISTS ${tableName};`;
2423
2424
  ddl.push(sql);
2424
2425
  }
@@ -2438,53 +2439,49 @@ var DatabaseController = class _DatabaseController {
2438
2439
  return { ddl, table, tableName, schema, options };
2439
2440
  }).then(
2440
2441
  CadenzaService.createMetaTask(
2441
- "Generate unique index DDL",
2442
+ "Generate primary key ddl",
2442
2443
  (ctx) => {
2443
2444
  const { ddl, table, tableName, schema, options } = ctx;
2444
- if (table.uniqueConstraints) {
2445
- table.uniqueConstraints.forEach(
2446
- (fields) => {
2447
- ddl.push(
2448
- `ALTER TABLE ${tableName} DROP CONSTRAINT IF EXISTS unique_${tableName}_${fields.join("_")};`,
2449
- `ALTER TABLE ${tableName} ADD CONSTRAINT unique_${tableName}_${fields.join("_")} UNIQUE (${fields.join(", ")});`
2450
- );
2451
- }
2445
+ if (table.primaryKey) {
2446
+ ddl.push(
2447
+ `ALTER TABLE ${tableName} DROP CONSTRAINT IF EXISTS unique_${tableName}_${table.primaryKey.join("_")};`,
2448
+ `ALTER TABLE ${tableName} ADD CONSTRAINT unique_${tableName}_${table.primaryKey.join("_")} PRIMARY KEY (${table.primaryKey.join(", ")});`
2452
2449
  );
2453
2450
  }
2454
2451
  return { ddl, table, tableName, schema, options };
2455
2452
  }
2456
2453
  ).then(
2457
2454
  CadenzaService.createMetaTask(
2458
- "Generate foreign key DDL",
2455
+ "Generate unique index DDL",
2459
2456
  (ctx) => {
2460
2457
  const { ddl, table, tableName, schema, options } = ctx;
2461
- if (table.foreignKeys) {
2462
- for (const foreignKey of table.foreignKeys) {
2463
- const foreignKeyName = `fk_${tableName}_${foreignKey.fields.join("_")}`;
2464
- ddl.push(
2465
- `ALTER TABLE ${tableName} DROP CONSTRAINT IF EXISTS ${foreignKeyName};`,
2466
- `ALTER TABLE ${tableName} ADD CONSTRAINT ${foreignKeyName} FOREIGN KEY (${foreignKey.fields.join(
2467
- ", "
2468
- )}) REFERENCES ${foreignKey.tableName} (${foreignKey.referenceFields.join(
2469
- ", "
2470
- )});`
2471
- );
2472
- }
2458
+ if (table.uniqueConstraints) {
2459
+ table.uniqueConstraints.forEach(
2460
+ (fields) => {
2461
+ ddl.push(
2462
+ `ALTER TABLE ${tableName} DROP CONSTRAINT IF EXISTS unique_${tableName}_${fields.join("_")};`,
2463
+ `ALTER TABLE ${tableName} ADD CONSTRAINT unique_${tableName}_${fields.join("_")} UNIQUE (${fields.join(", ")});`
2464
+ );
2465
+ }
2466
+ );
2473
2467
  }
2474
2468
  return { ddl, table, tableName, schema, options };
2475
2469
  }
2476
2470
  ).then(
2477
2471
  CadenzaService.createMetaTask(
2478
- "Generate trigger DDL",
2472
+ "Generate foreign key DDL",
2479
2473
  (ctx) => {
2480
2474
  const { ddl, table, tableName, schema, options } = ctx;
2481
- if (table.triggers) {
2482
- for (const [
2483
- triggerName,
2484
- trigger
2485
- ] of Object.entries(table.triggers)) {
2475
+ if (table.foreignKeys) {
2476
+ for (const foreignKey of table.foreignKeys) {
2477
+ const foreignKeyName = `fk_${tableName}_${foreignKey.fields.join("_")}`;
2486
2478
  ddl.push(
2487
- `CREATE TRIGGER ${triggerName} ${trigger.when} ${trigger.event} ON ${tableName} FOR EACH STATEMENT EXECUTE FUNCTION ${trigger.function};`
2479
+ `ALTER TABLE ${tableName} DROP CONSTRAINT IF EXISTS ${foreignKeyName};`,
2480
+ `ALTER TABLE ${tableName} ADD CONSTRAINT ${foreignKeyName} FOREIGN KEY (${foreignKey.fields.join(
2481
+ ", "
2482
+ )}) REFERENCES ${foreignKey.tableName} (${foreignKey.referenceFields.join(
2483
+ ", "
2484
+ )});`
2488
2485
  );
2489
2486
  }
2490
2487
  }
@@ -2492,58 +2489,91 @@ var DatabaseController = class _DatabaseController {
2492
2489
  }
2493
2490
  ).then(
2494
2491
  CadenzaService.createMetaTask(
2495
- "Generate initial data DDL",
2492
+ "Generate trigger DDL",
2496
2493
  (ctx) => {
2497
2494
  const { ddl, table, tableName, schema, options } = ctx;
2498
- if (table.initialData) {
2499
- ddl.push(
2500
- `INSERT INTO ${tableName} (${table.initialData.fields.join(", ")}) VALUES ${table.initialData.data.map(
2501
- (row) => `(${row.map(
2502
- (value) => value === void 0 ? "NULL" : value.charAt(0) === "'" ? value : `'${value}'`
2503
- ).join(", ")})`
2504
- // TODO: handle non string data
2505
- ).join(", ")} ON CONFLICT DO NOTHING;`
2506
- );
2495
+ if (table.triggers) {
2496
+ for (const [
2497
+ triggerName,
2498
+ trigger
2499
+ ] of Object.entries(table.triggers)) {
2500
+ ddl.push(
2501
+ `CREATE TRIGGER ${triggerName} ${trigger.when} ${trigger.event} ON ${tableName} FOR EACH STATEMENT EXECUTE FUNCTION ${trigger.function};`
2502
+ );
2503
+ }
2507
2504
  }
2508
2505
  return { ddl, table, tableName, schema, options };
2509
2506
  }
2510
2507
  ).then(
2511
- CadenzaService.createUniqueMetaTask("Join DDL", (ctx) => {
2512
- const { joinedContexts } = ctx;
2513
- const ddl = [];
2514
- for (const joinedContext of joinedContexts) {
2515
- ddl.push(...joinedContext.ddl);
2508
+ CadenzaService.createMetaTask(
2509
+ "Generate initial data DDL",
2510
+ (ctx) => {
2511
+ const {
2512
+ ddl,
2513
+ table,
2514
+ tableName,
2515
+ schema,
2516
+ options
2517
+ } = ctx;
2518
+ if (table.initialData) {
2519
+ ddl.push(
2520
+ `INSERT INTO ${tableName} (${table.initialData.fields.join(", ")}) VALUES ${table.initialData.data.map(
2521
+ (row) => `(${row.map(
2522
+ (value) => value === void 0 ? "NULL" : value.charAt(0) === "'" ? value : `'${value}'`
2523
+ ).join(", ")})`
2524
+ // TODO: handle non string data
2525
+ ).join(", ")} ON CONFLICT DO NOTHING;`
2526
+ );
2527
+ }
2528
+ return {
2529
+ ddl,
2530
+ table,
2531
+ tableName,
2532
+ schema,
2533
+ options
2534
+ };
2516
2535
  }
2517
- ddl.flat();
2518
- return {
2519
- ddl,
2520
- schema: joinedContexts[0].schema,
2521
- options: joinedContexts[0].options
2522
- };
2523
- }).then(
2524
- CadenzaService.createMetaTask(
2525
- "meta.applyDatabaseChanges",
2526
- (ctx) => __async(this, null, function* () {
2527
- const { ddl } = ctx;
2528
- if (ddl && ddl.length > 0) {
2529
- try {
2530
- for (const sql of ddl) {
2531
- console.log("Applying SQL", sql);
2532
- yield this.dbClient.query(sql);
2536
+ ).then(
2537
+ CadenzaService.createUniqueMetaTask(
2538
+ "Join DDL",
2539
+ (ctx) => {
2540
+ const { joinedContexts } = ctx;
2541
+ const ddl = [];
2542
+ for (const joinedContext of joinedContexts) {
2543
+ ddl.push(...joinedContext.ddl);
2544
+ }
2545
+ ddl.flat();
2546
+ return {
2547
+ ddl,
2548
+ schema: joinedContexts[0].schema,
2549
+ options: joinedContexts[0].options
2550
+ };
2551
+ }
2552
+ ).then(
2553
+ CadenzaService.createMetaTask(
2554
+ "meta.applyDatabaseChanges",
2555
+ (ctx) => __async(this, null, function* () {
2556
+ const { ddl } = ctx;
2557
+ if (ddl && ddl.length > 0) {
2558
+ try {
2559
+ for (const sql of ddl) {
2560
+ console.log("Applying SQL", sql);
2561
+ yield this.dbClient.query(sql);
2562
+ }
2563
+ } catch (error) {
2564
+ console.error(
2565
+ "Error applying DDL",
2566
+ error
2567
+ );
2568
+ throw error;
2533
2569
  }
2534
- } catch (error) {
2535
- console.error(
2536
- "Error applying DDL",
2537
- error
2538
- );
2539
- throw error;
2540
2570
  }
2541
- }
2542
- console.log("DDL applied");
2543
- return ctx;
2544
- }),
2545
- "Applies generated DDL to the database"
2546
- ).emits("meta.database.setup_done")
2571
+ console.log("DDL applied");
2572
+ return ctx;
2573
+ }),
2574
+ "Applies generated DDL to the database"
2575
+ ).emits("meta.database.setup_done")
2576
+ )
2547
2577
  )
2548
2578
  )
2549
2579
  )