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