@cadenza.io/service 1.6.13 → 1.7.1
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.d.mts +5 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +79 -51
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +79 -51
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -267,6 +267,11 @@ interface TableDefinition {
|
|
|
267
267
|
uniqueConstraints?: string[][];
|
|
268
268
|
primaryKey?: string[];
|
|
269
269
|
fullTextIndexes?: string[][];
|
|
270
|
+
foreignKeys?: {
|
|
271
|
+
tableName: string;
|
|
272
|
+
fields: string[];
|
|
273
|
+
referenceFields: string[];
|
|
274
|
+
}[];
|
|
270
275
|
triggers?: Record<string, {
|
|
271
276
|
when: "before" | "after";
|
|
272
277
|
event: "insert" | "update" | "delete";
|
package/dist/index.d.ts
CHANGED
|
@@ -267,6 +267,11 @@ interface TableDefinition {
|
|
|
267
267
|
uniqueConstraints?: string[][];
|
|
268
268
|
primaryKey?: string[];
|
|
269
269
|
fullTextIndexes?: string[][];
|
|
270
|
+
foreignKeys?: {
|
|
271
|
+
tableName: string;
|
|
272
|
+
fields: string[];
|
|
273
|
+
referenceFields: string[];
|
|
274
|
+
}[];
|
|
270
275
|
triggers?: Record<string, {
|
|
271
276
|
when: "before" | "after";
|
|
272
277
|
event: "insert" | "update" | "delete";
|
package/dist/index.js
CHANGED
|
@@ -2449,69 +2449,97 @@ var DatabaseController = class _DatabaseController {
|
|
|
2449
2449
|
return { ddl, table, tableName, schema, options };
|
|
2450
2450
|
}
|
|
2451
2451
|
).then(
|
|
2452
|
-
CadenzaService.createMetaTask(
|
|
2453
|
-
|
|
2454
|
-
|
|
2455
|
-
|
|
2456
|
-
|
|
2457
|
-
|
|
2458
|
-
|
|
2459
|
-
|
|
2460
|
-
|
|
2452
|
+
CadenzaService.createMetaTask(
|
|
2453
|
+
"Generate foreign key DDL",
|
|
2454
|
+
(ctx) => {
|
|
2455
|
+
const { ddl, table, tableName, schema, options } = ctx;
|
|
2456
|
+
if (table.foreignKeys) {
|
|
2457
|
+
for (const foreignKey of table.foreignKeys) {
|
|
2458
|
+
const foreignKeyName = `fk_${tableName}_${foreignKey.fields.join("_")}`;
|
|
2459
|
+
ddl.push(
|
|
2460
|
+
`ALTER TABLE ${tableName} DROP CONSTRAINT IF EXISTS ${foreignKeyName};`,
|
|
2461
|
+
`ALTER TABLE ${tableName} ADD CONSTRAINT ${foreignKeyName} FOREIGN KEY (${foreignKey.fields.join(
|
|
2462
|
+
", "
|
|
2463
|
+
)}) REFERENCES ${foreignKey.tableName} (${foreignKey.referenceFields.join(
|
|
2464
|
+
", "
|
|
2465
|
+
)});`
|
|
2466
|
+
);
|
|
2467
|
+
}
|
|
2461
2468
|
}
|
|
2469
|
+
return { ddl, table, tableName, schema, options };
|
|
2462
2470
|
}
|
|
2463
|
-
|
|
2464
|
-
}).then(
|
|
2471
|
+
).then(
|
|
2465
2472
|
CadenzaService.createMetaTask(
|
|
2466
|
-
"Generate
|
|
2473
|
+
"Generate trigger DDL",
|
|
2467
2474
|
(ctx) => {
|
|
2468
2475
|
const { ddl, table, tableName, schema, options } = ctx;
|
|
2469
|
-
if (table.
|
|
2470
|
-
|
|
2471
|
-
|
|
2472
|
-
|
|
2473
|
-
|
|
2474
|
-
|
|
2475
|
-
|
|
2476
|
-
)
|
|
2477
|
-
|
|
2476
|
+
if (table.triggers) {
|
|
2477
|
+
for (const [
|
|
2478
|
+
triggerName,
|
|
2479
|
+
trigger
|
|
2480
|
+
] of Object.entries(table.triggers)) {
|
|
2481
|
+
ddl.push(
|
|
2482
|
+
`CREATE TRIGGER ${triggerName} ${trigger.when} ${trigger.event} ON ${tableName} FOR EACH STATEMENT EXECUTE FUNCTION ${trigger.function};`
|
|
2483
|
+
);
|
|
2484
|
+
}
|
|
2478
2485
|
}
|
|
2479
2486
|
return { ddl, table, tableName, schema, options };
|
|
2480
2487
|
}
|
|
2481
2488
|
).then(
|
|
2482
|
-
CadenzaService.
|
|
2483
|
-
|
|
2484
|
-
|
|
2485
|
-
|
|
2486
|
-
|
|
2489
|
+
CadenzaService.createMetaTask(
|
|
2490
|
+
"Generate initial data DDL",
|
|
2491
|
+
(ctx) => {
|
|
2492
|
+
const { ddl, table, tableName, schema, options } = ctx;
|
|
2493
|
+
if (table.initialData) {
|
|
2494
|
+
ddl.push(
|
|
2495
|
+
`INSERT INTO ${tableName} (${table.initialData.fields.join(", ")}) VALUES ${table.initialData.data.map(
|
|
2496
|
+
(row) => `(${row.map(
|
|
2497
|
+
(value) => value === void 0 ? "NULL" : value.charAt(0) === "'" ? value : `'${value}'`
|
|
2498
|
+
).join(", ")})`
|
|
2499
|
+
// TODO: handle non string data
|
|
2500
|
+
).join(", ")} ON CONFLICT DO NOTHING;`
|
|
2501
|
+
);
|
|
2502
|
+
}
|
|
2503
|
+
return { ddl, table, tableName, schema, options };
|
|
2487
2504
|
}
|
|
2488
|
-
|
|
2489
|
-
|
|
2490
|
-
|
|
2491
|
-
|
|
2492
|
-
|
|
2493
|
-
|
|
2494
|
-
|
|
2495
|
-
|
|
2496
|
-
|
|
2497
|
-
|
|
2498
|
-
|
|
2499
|
-
|
|
2500
|
-
|
|
2501
|
-
|
|
2502
|
-
|
|
2503
|
-
|
|
2505
|
+
).then(
|
|
2506
|
+
CadenzaService.createUniqueMetaTask("Join DDL", (ctx) => {
|
|
2507
|
+
const { joinedContexts } = ctx;
|
|
2508
|
+
const ddl = [];
|
|
2509
|
+
for (const joinedContext of joinedContexts) {
|
|
2510
|
+
ddl.push(...joinedContext.ddl);
|
|
2511
|
+
}
|
|
2512
|
+
ddl.flat();
|
|
2513
|
+
return {
|
|
2514
|
+
ddl,
|
|
2515
|
+
schema: joinedContexts[0].schema,
|
|
2516
|
+
options: joinedContexts[0].options
|
|
2517
|
+
};
|
|
2518
|
+
}).then(
|
|
2519
|
+
CadenzaService.createMetaTask(
|
|
2520
|
+
"meta.applyDatabaseChanges",
|
|
2521
|
+
(ctx) => __async(this, null, function* () {
|
|
2522
|
+
const { ddl } = ctx;
|
|
2523
|
+
if (ddl && ddl.length > 0) {
|
|
2524
|
+
try {
|
|
2525
|
+
for (const sql of ddl) {
|
|
2526
|
+
console.log("Applying SQL", sql);
|
|
2527
|
+
yield this.dbClient.query(sql);
|
|
2528
|
+
}
|
|
2529
|
+
} catch (error) {
|
|
2530
|
+
console.error(
|
|
2531
|
+
"Error applying DDL",
|
|
2532
|
+
error
|
|
2533
|
+
);
|
|
2534
|
+
throw error;
|
|
2504
2535
|
}
|
|
2505
|
-
} catch (error) {
|
|
2506
|
-
console.error("Error applying DDL", error);
|
|
2507
|
-
throw error;
|
|
2508
2536
|
}
|
|
2509
|
-
|
|
2510
|
-
|
|
2511
|
-
|
|
2512
|
-
|
|
2513
|
-
"
|
|
2514
|
-
)
|
|
2537
|
+
console.log("DDL applied");
|
|
2538
|
+
return ctx;
|
|
2539
|
+
}),
|
|
2540
|
+
"Applies generated DDL to the database"
|
|
2541
|
+
).emits("meta.database.setup_done")
|
|
2542
|
+
)
|
|
2515
2543
|
)
|
|
2516
2544
|
)
|
|
2517
2545
|
)
|