@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 +105 -75
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +105 -75
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
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
|
|
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 = (
|
|
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
|
|
2442
|
+
"Generate primary key ddl",
|
|
2442
2443
|
(ctx) => {
|
|
2443
2444
|
const { ddl, table, tableName, schema, options } = ctx;
|
|
2444
|
-
if (table.
|
|
2445
|
-
|
|
2446
|
-
|
|
2447
|
-
|
|
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
|
|
2455
|
+
"Generate unique index DDL",
|
|
2459
2456
|
(ctx) => {
|
|
2460
2457
|
const { ddl, table, tableName, schema, options } = ctx;
|
|
2461
|
-
if (table.
|
|
2462
|
-
|
|
2463
|
-
|
|
2464
|
-
|
|
2465
|
-
|
|
2466
|
-
|
|
2467
|
-
|
|
2468
|
-
|
|
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
|
|
2472
|
+
"Generate foreign key DDL",
|
|
2479
2473
|
(ctx) => {
|
|
2480
2474
|
const { ddl, table, tableName, schema, options } = ctx;
|
|
2481
|
-
if (table.
|
|
2482
|
-
for (const
|
|
2483
|
-
|
|
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
|
-
`
|
|
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
|
|
2492
|
+
"Generate trigger DDL",
|
|
2496
2493
|
(ctx) => {
|
|
2497
2494
|
const { ddl, table, tableName, schema, options } = ctx;
|
|
2498
|
-
if (table.
|
|
2499
|
-
|
|
2500
|
-
|
|
2501
|
-
|
|
2502
|
-
|
|
2503
|
-
|
|
2504
|
-
|
|
2505
|
-
)
|
|
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.
|
|
2512
|
-
|
|
2513
|
-
|
|
2514
|
-
|
|
2515
|
-
|
|
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
|
-
|
|
2518
|
-
|
|
2519
|
-
|
|
2520
|
-
|
|
2521
|
-
|
|
2522
|
-
|
|
2523
|
-
|
|
2524
|
-
|
|
2525
|
-
|
|
2526
|
-
|
|
2527
|
-
|
|
2528
|
-
|
|
2529
|
-
|
|
2530
|
-
|
|
2531
|
-
|
|
2532
|
-
|
|
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
|
-
|
|
2543
|
-
|
|
2544
|
-
|
|
2545
|
-
"
|
|
2546
|
-
)
|
|
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
|
)
|