@restura/core 0.1.0-alpha.35 → 0.1.0-alpha.37

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 CHANGED
@@ -124,15 +124,16 @@ interface DatabaseActionData {
124
124
  queryMetadata: QueryMetadata;
125
125
  }
126
126
  interface ActionRowInsertData<T = DynamicObject> extends DatabaseActionData {
127
- insertId: number;
127
+ insertedId: number;
128
128
  insertObject: T;
129
129
  }
130
130
  interface ActionRowDeleteData<T = DynamicObject> extends DatabaseActionData {
131
+ deletedId: number;
131
132
  deletedRow: T;
132
133
  }
133
134
  interface ActionColumnChangeData<T = DynamicObject> extends DatabaseActionData {
134
135
  tableName: string;
135
- rowId: number;
136
+ changedId: number;
136
137
  newData: T;
137
138
  oldData: T;
138
139
  }
@@ -148,8 +149,10 @@ interface ActionColumnChangeFilter {
148
149
  }
149
150
  type TriggerResult = {
150
151
  table: string;
151
- insertId?: number;
152
- query: string;
152
+ insertedId?: number;
153
+ changedId?: number;
154
+ deletedId?: number;
155
+ queryMetadata: QueryMetadata;
153
156
  record: DynamicObject;
154
157
  previousRecord: DynamicObject;
155
158
  requesterId: number;
@@ -1091,6 +1094,7 @@ declare const tableDataSchema: z.ZodObject<{
1091
1094
  check: string;
1092
1095
  }>, "many">;
1093
1096
  roles: z.ZodArray<z.ZodString, "many">;
1097
+ notify: z.ZodOptional<z.ZodUnion<[z.ZodLiteral<"ALL">, z.ZodArray<z.ZodString, "many">]>>;
1094
1098
  }, "strict", z.ZodTypeAny, {
1095
1099
  name: string;
1096
1100
  roles: string[];
@@ -1126,6 +1130,7 @@ declare const tableDataSchema: z.ZodObject<{
1126
1130
  name: string;
1127
1131
  check: string;
1128
1132
  }[];
1133
+ notify?: string[] | "ALL" | undefined;
1129
1134
  }, {
1130
1135
  name: string;
1131
1136
  roles: string[];
@@ -1161,6 +1166,7 @@ declare const tableDataSchema: z.ZodObject<{
1161
1166
  name: string;
1162
1167
  check: string;
1163
1168
  }[];
1169
+ notify?: string[] | "ALL" | undefined;
1164
1170
  }>;
1165
1171
  type TableData = z.infer<typeof tableDataSchema>;
1166
1172
  declare const resturaSchema: z.ZodObject<{
@@ -1255,6 +1261,7 @@ declare const resturaSchema: z.ZodObject<{
1255
1261
  check: string;
1256
1262
  }>, "many">;
1257
1263
  roles: z.ZodArray<z.ZodString, "many">;
1264
+ notify: z.ZodOptional<z.ZodUnion<[z.ZodLiteral<"ALL">, z.ZodArray<z.ZodString, "many">]>>;
1258
1265
  }, "strict", z.ZodTypeAny, {
1259
1266
  name: string;
1260
1267
  roles: string[];
@@ -1290,6 +1297,7 @@ declare const resturaSchema: z.ZodObject<{
1290
1297
  name: string;
1291
1298
  check: string;
1292
1299
  }[];
1300
+ notify?: string[] | "ALL" | undefined;
1293
1301
  }, {
1294
1302
  name: string;
1295
1303
  roles: string[];
@@ -1325,6 +1333,7 @@ declare const resturaSchema: z.ZodObject<{
1325
1333
  name: string;
1326
1334
  check: string;
1327
1335
  }[];
1336
+ notify?: string[] | "ALL" | undefined;
1328
1337
  }>, "many">;
1329
1338
  endpoints: z.ZodArray<z.ZodObject<{
1330
1339
  name: z.ZodString;
@@ -2143,6 +2152,7 @@ declare const resturaSchema: z.ZodObject<{
2143
2152
  name: string;
2144
2153
  check: string;
2145
2154
  }[];
2155
+ notify?: string[] | "ALL" | undefined;
2146
2156
  }[];
2147
2157
  endpoints: {
2148
2158
  name: string;
@@ -2290,6 +2300,7 @@ declare const resturaSchema: z.ZodObject<{
2290
2300
  name: string;
2291
2301
  check: string;
2292
2302
  }[];
2303
+ notify?: string[] | "ALL" | undefined;
2293
2304
  }[];
2294
2305
  endpoints: {
2295
2306
  name: string;
package/dist/index.d.ts CHANGED
@@ -124,15 +124,16 @@ interface DatabaseActionData {
124
124
  queryMetadata: QueryMetadata;
125
125
  }
126
126
  interface ActionRowInsertData<T = DynamicObject> extends DatabaseActionData {
127
- insertId: number;
127
+ insertedId: number;
128
128
  insertObject: T;
129
129
  }
130
130
  interface ActionRowDeleteData<T = DynamicObject> extends DatabaseActionData {
131
+ deletedId: number;
131
132
  deletedRow: T;
132
133
  }
133
134
  interface ActionColumnChangeData<T = DynamicObject> extends DatabaseActionData {
134
135
  tableName: string;
135
- rowId: number;
136
+ changedId: number;
136
137
  newData: T;
137
138
  oldData: T;
138
139
  }
@@ -148,8 +149,10 @@ interface ActionColumnChangeFilter {
148
149
  }
149
150
  type TriggerResult = {
150
151
  table: string;
151
- insertId?: number;
152
- query: string;
152
+ insertedId?: number;
153
+ changedId?: number;
154
+ deletedId?: number;
155
+ queryMetadata: QueryMetadata;
153
156
  record: DynamicObject;
154
157
  previousRecord: DynamicObject;
155
158
  requesterId: number;
@@ -1091,6 +1094,7 @@ declare const tableDataSchema: z.ZodObject<{
1091
1094
  check: string;
1092
1095
  }>, "many">;
1093
1096
  roles: z.ZodArray<z.ZodString, "many">;
1097
+ notify: z.ZodOptional<z.ZodUnion<[z.ZodLiteral<"ALL">, z.ZodArray<z.ZodString, "many">]>>;
1094
1098
  }, "strict", z.ZodTypeAny, {
1095
1099
  name: string;
1096
1100
  roles: string[];
@@ -1126,6 +1130,7 @@ declare const tableDataSchema: z.ZodObject<{
1126
1130
  name: string;
1127
1131
  check: string;
1128
1132
  }[];
1133
+ notify?: string[] | "ALL" | undefined;
1129
1134
  }, {
1130
1135
  name: string;
1131
1136
  roles: string[];
@@ -1161,6 +1166,7 @@ declare const tableDataSchema: z.ZodObject<{
1161
1166
  name: string;
1162
1167
  check: string;
1163
1168
  }[];
1169
+ notify?: string[] | "ALL" | undefined;
1164
1170
  }>;
1165
1171
  type TableData = z.infer<typeof tableDataSchema>;
1166
1172
  declare const resturaSchema: z.ZodObject<{
@@ -1255,6 +1261,7 @@ declare const resturaSchema: z.ZodObject<{
1255
1261
  check: string;
1256
1262
  }>, "many">;
1257
1263
  roles: z.ZodArray<z.ZodString, "many">;
1264
+ notify: z.ZodOptional<z.ZodUnion<[z.ZodLiteral<"ALL">, z.ZodArray<z.ZodString, "many">]>>;
1258
1265
  }, "strict", z.ZodTypeAny, {
1259
1266
  name: string;
1260
1267
  roles: string[];
@@ -1290,6 +1297,7 @@ declare const resturaSchema: z.ZodObject<{
1290
1297
  name: string;
1291
1298
  check: string;
1292
1299
  }[];
1300
+ notify?: string[] | "ALL" | undefined;
1293
1301
  }, {
1294
1302
  name: string;
1295
1303
  roles: string[];
@@ -1325,6 +1333,7 @@ declare const resturaSchema: z.ZodObject<{
1325
1333
  name: string;
1326
1334
  check: string;
1327
1335
  }[];
1336
+ notify?: string[] | "ALL" | undefined;
1328
1337
  }>, "many">;
1329
1338
  endpoints: z.ZodArray<z.ZodObject<{
1330
1339
  name: z.ZodString;
@@ -2143,6 +2152,7 @@ declare const resturaSchema: z.ZodObject<{
2143
2152
  name: string;
2144
2153
  check: string;
2145
2154
  }[];
2155
+ notify?: string[] | "ALL" | undefined;
2146
2156
  }[];
2147
2157
  endpoints: {
2148
2158
  name: string;
@@ -2290,6 +2300,7 @@ declare const resturaSchema: z.ZodObject<{
2290
2300
  name: string;
2291
2301
  check: string;
2292
2302
  }[];
2303
+ notify?: string[] | "ALL" | undefined;
2293
2304
  }[];
2294
2305
  endpoints: {
2295
2306
  name: string;
package/dist/index.js CHANGED
@@ -176,7 +176,7 @@ var EventManager = class {
176
176
  if (!this.hasHandlersForEventType("DATABASE_ROW_INSERT", filter, triggerResult)) return;
177
177
  const insertData = {
178
178
  tableName: triggerResult.table,
179
- insertId: triggerResult.record.id,
179
+ insertedId: triggerResult.insertedId || 0,
180
180
  insertObject: triggerResult.record,
181
181
  queryMetadata: data.queryMetadata
182
182
  };
@@ -192,6 +192,7 @@ var EventManager = class {
192
192
  if (!this.hasHandlersForEventType("DATABASE_ROW_DELETE", filter, triggerResult)) return;
193
193
  const deleteData = {
194
194
  tableName: triggerResult.table,
195
+ deletedId: triggerResult.deletedId || 0,
195
196
  deletedRow: triggerResult.previousRecord,
196
197
  queryMetadata: data.queryMetadata
197
198
  };
@@ -207,7 +208,7 @@ var EventManager = class {
207
208
  if (!this.hasHandlersForEventType("DATABASE_COLUMN_UPDATE", filter, triggerResult)) return;
208
209
  const columnChangeData = {
209
210
  tableName: triggerResult.table,
210
- rowId: triggerResult.record.id,
211
+ changedId: triggerResult.changedId || 0,
211
212
  newData: triggerResult.record,
212
213
  oldData: triggerResult.previousRecord,
213
214
  queryMetadata: data.queryMetadata
@@ -551,7 +552,7 @@ var SqlUtils = class _SqlUtils {
551
552
  static convertDatabaseTypeToTypescript(type, value) {
552
553
  type = type.toLocaleLowerCase();
553
554
  if (type.startsWith("tinyint") || type.startsWith("boolean")) return "boolean";
554
- if (type.indexOf("int") > -1 || type.startsWith("decimal") || type.startsWith("double") || type.startsWith("float"))
555
+ if (type.indexOf("int") > -1 || type.startsWith("decimal") || type.startsWith("double") || type.startsWith("float") || type.indexOf("serial") > -1 || type.startsWith("decimal") || type.startsWith("real") || type.startsWith("double precision") || type.startsWith("numeric"))
555
556
  return "number";
556
557
  if (type === "json") {
557
558
  if (!value) return "object";
@@ -833,21 +834,28 @@ var ApiTree = class _ApiTree {
833
834
  return `export type Res = CustomTypes.${route.responseType}[]`;
834
835
  else return `export type Res = CustomTypes.${route.responseType}`;
835
836
  }
836
- return `export interface Res ${this.getFields(route.response)}`;
837
+ return `export interface Res ${this.getFields(route.response, route.table, route.joins)}`;
837
838
  }
838
- getFields(fields) {
839
- const nameFields = fields.map((f) => this.getNameAndType(f));
839
+ getFields(fields, routeBaseTable, joins) {
840
+ const nameFields = fields.map((f) => this.getNameAndType(f, routeBaseTable, joins));
840
841
  const nested = `{
841
842
  ${nameFields.join(";\n ")}${import_core_utils.ObjectUtils.isArrayWithData(nameFields) ? ";" : ""}
842
843
  }`;
843
844
  return nested;
844
845
  }
845
- getNameAndType(p) {
846
+ getNameAndType(p, routeBaseTable, joins) {
846
847
  let responseType = "any", isNullable = false, array = false;
847
848
  if (p.selector) {
848
849
  ({ responseType, isNullable } = this.getTypeFromTable(p.selector, p.name));
850
+ const selectorKey = p.selector.split(".")[0];
851
+ if (selectorKey !== routeBaseTable) {
852
+ const join = joins.find((j) => j.alias === selectorKey);
853
+ if (join && join.type !== "INNER") {
854
+ isNullable = true;
855
+ }
856
+ }
849
857
  } else if (p.subquery) {
850
- responseType = this.getFields(p.subquery.properties);
858
+ responseType = this.getFields(p.subquery.properties, p.subquery.table, p.subquery.joins);
851
859
  array = true;
852
860
  }
853
861
  return `${p.name}:${responseType}${array ? "[]" : ""}${isNullable ? " | null" : ""}`;
@@ -1313,7 +1321,8 @@ var tableDataSchema = import_zod3.z.object({
1313
1321
  indexes: import_zod3.z.array(indexDataSchema),
1314
1322
  foreignKeys: import_zod3.z.array(foreignKeyDataSchema),
1315
1323
  checkConstraints: import_zod3.z.array(checkConstraintDataSchema),
1316
- roles: import_zod3.z.array(import_zod3.z.string())
1324
+ roles: import_zod3.z.array(import_zod3.z.string()),
1325
+ notify: import_zod3.z.union([import_zod3.z.literal("ALL"), import_zod3.z.array(import_zod3.z.string())]).optional()
1317
1326
  }).strict();
1318
1327
  var endpointDataSchema = import_zod3.z.object({
1319
1328
  name: import_zod3.z.string(),
@@ -1607,10 +1616,11 @@ function insertObjectQuery(table, obj) {
1607
1616
  const params = Object.values(obj);
1608
1617
  const columns = keys.map((column) => escapeColumnName(column)).join(", ");
1609
1618
  const values = params.map((value) => SQL`${value}`).join(", ");
1610
- const query = `
1619
+ let query = `
1611
1620
  INSERT INTO "${table}" (${columns})
1612
1621
  VALUES (${values})
1613
1622
  RETURNING *`;
1623
+ query = query.replace(/'(\?)'/, "?");
1614
1624
  return query;
1615
1625
  }
1616
1626
  function updateObjectQuery(table, obj, whereStatement) {
@@ -1986,16 +1996,8 @@ var PsqlEngine = class extends SqlEngine {
1986
1996
  });
1987
1997
  }
1988
1998
  async handleTrigger(payload, mutationType) {
1989
- const findRequesterDetailsRegex = /^--QUERY_METADATA\(\{.*\}\)/;
1990
- const match = payload.query.match(findRequesterDetailsRegex);
1991
- if (match) {
1992
- const jsonString = match[0].slice(match[0].indexOf("{"), match[0].lastIndexOf("}") + 1);
1993
- const queryMetadata = import_core_utils5.ObjectUtils.safeParse(jsonString);
1994
- const triggerFromThisInstance = queryMetadata.connectionInstanceId === this.psqlConnectionPool.instanceId;
1995
- if (!triggerFromThisInstance) {
1996
- return;
1997
- }
1998
- await eventManager_default.fireActionFromDbTrigger({ queryMetadata, mutationType }, payload);
1999
+ if (payload.queryMetadata && payload.queryMetadata.connectionInstanceId === this.psqlConnectionPool.instanceId) {
2000
+ await eventManager_default.fireActionFromDbTrigger({ queryMetadata: payload.queryMetadata, mutationType }, payload);
1999
2001
  }
2000
2002
  }
2001
2003
  async createDatabaseFromSchema(schema, connection) {
@@ -2008,9 +2010,11 @@ var PsqlEngine = class extends SqlEngine {
2008
2010
  const indexes = [];
2009
2011
  const triggers = [];
2010
2012
  for (const table of schema.database) {
2011
- triggers.push(this.createInsertTriggers(table.name));
2012
- triggers.push(this.createUpdateTrigger(table.name));
2013
- triggers.push(this.createDeleteTrigger(table.name));
2013
+ if (table.notify) {
2014
+ triggers.push(this.createInsertTriggers(table.name, table.notify));
2015
+ triggers.push(this.createUpdateTrigger(table.name, table.notify));
2016
+ triggers.push(this.createDeleteTrigger(table.name, table.notify));
2017
+ }
2014
2018
  let sql = `CREATE TABLE "${table.name}"
2015
2019
  ( `;
2016
2020
  const tableColumns = [];
@@ -2437,53 +2441,216 @@ DELETE FROM "${routeData.table}" ${joinStatement} ${whereClause}`;
2437
2441
  }
2438
2442
  return whereClause;
2439
2443
  }
2440
- createUpdateTrigger(tableName) {
2441
- return `
2444
+ createUpdateTrigger(tableName, notify) {
2445
+ if (!notify) return "";
2446
+ if (notify === "ALL") {
2447
+ return `
2442
2448
  CREATE OR REPLACE FUNCTION notify_${tableName}_update()
2443
- RETURNS TRIGGER AS $$
2449
+ RETURNS TRIGGER AS $$
2450
+ DECLARE
2451
+ query_metadata JSON;
2444
2452
  BEGIN
2445
- PERFORM pg_notify('update', JSON_BUILD_OBJECT('table', '${tableName}', 'query', current_query(), 'record', NEW, 'previousRecord', OLD)::text);
2446
- RETURN NEW;
2453
+ SELECT INTO query_metadata
2454
+ (regexp_match(
2455
+ current_query(),
2456
+ '^--QUERY_METADATA\\(({.*})', 'n'
2457
+ ))[1]::json;
2458
+
2459
+ PERFORM pg_notify(
2460
+ 'update',
2461
+ json_build_object(
2462
+ 'table', '${tableName}',
2463
+ 'queryMetadata', query_metadata,
2464
+ 'changedId', NEW.id,
2465
+ 'record', NEW,
2466
+ 'previousRecord', OLD
2467
+ )::text
2468
+ );
2469
+ RETURN NEW;
2447
2470
  END;
2448
2471
  $$ LANGUAGE plpgsql;
2449
2472
 
2450
2473
  CREATE OR REPLACE TRIGGER ${tableName}_update
2451
- AFTER UPDATE ON "${tableName}"
2452
- FOR EACH ROW
2474
+ AFTER UPDATE ON "${tableName}"
2475
+ FOR EACH ROW
2476
+ EXECUTE FUNCTION notify_${tableName}_update();
2477
+ `;
2478
+ }
2479
+ const notifyColumnNewBuildString = notify.map((column) => `'${column}', NEW."${column}"`).join(",\n");
2480
+ const notifyColumnOldBuildString = notify.map((column) => `'${column}', OLD."${column}"`).join(",\n");
2481
+ return `
2482
+ CREATE OR REPLACE FUNCTION notify_${tableName}_update()
2483
+ RETURNS TRIGGER AS $$
2484
+ DECLARE
2485
+ query_metadata JSON;
2486
+ BEGIN
2487
+ SELECT INTO query_metadata
2488
+ (regexp_match(
2489
+ current_query(),
2490
+ '^--QUERY_METADATA\\(({.*})', 'n'
2491
+ ))[1]::json;
2492
+
2493
+ PERFORM pg_notify(
2494
+ 'update',
2495
+ json_build_object(
2496
+ 'table', '${tableName}',
2497
+ 'queryMetadata', query_metadata,
2498
+ 'changedId', NEW.id,
2499
+ 'record', json_build_object(
2500
+ ${notifyColumnNewBuildString}
2501
+ ),
2502
+ 'previousRecord', json_build_object(
2503
+ ${notifyColumnOldBuildString}
2504
+ )
2505
+ )::text
2506
+ );
2507
+ RETURN NEW;
2508
+ END;
2509
+ $$ LANGUAGE plpgsql;
2510
+
2511
+ CREATE OR REPLACE TRIGGER ${tableName}_update
2512
+ AFTER UPDATE ON "${tableName}"
2513
+ FOR EACH ROW
2453
2514
  EXECUTE FUNCTION notify_${tableName}_update();
2454
2515
  `;
2455
2516
  }
2456
- createDeleteTrigger(tableName) {
2517
+ createDeleteTrigger(tableName, notify) {
2518
+ if (!notify) return "";
2519
+ if (notify === "ALL") {
2520
+ return `
2521
+ CREATE OR REPLACE FUNCTION notify_${tableName}_delete()
2522
+ RETURNS TRIGGER AS $$
2523
+ DECLARE
2524
+ query_metadata JSON;
2525
+ BEGIN
2526
+ SELECT INTO query_metadata
2527
+ (regexp_match(
2528
+ current_query(),
2529
+ '^--QUERY_METADATA\\(({.*})', 'n'
2530
+ ))[1]::json;
2531
+
2532
+ PERFORM pg_notify(
2533
+ 'delete',
2534
+ json_build_object(
2535
+ 'table', '${tableName}',
2536
+ 'queryMetadata', query_metadata,
2537
+ 'deletedId', OLD.id,
2538
+ 'previousRecord', OLD
2539
+ )::text
2540
+ );
2541
+ RETURN NEW;
2542
+ END;
2543
+ $$ LANGUAGE plpgsql;
2544
+
2545
+ CREATE OR REPLACE TRIGGER "${tableName}_delete"
2546
+ AFTER DELETE ON "${tableName}"
2547
+ FOR EACH ROW
2548
+ EXECUTE FUNCTION notify_${tableName}_delete();
2549
+ `;
2550
+ }
2551
+ const notifyColumnOldBuildString = notify.map((column) => `'${column}', OLD."${column}"`).join(",\n");
2457
2552
  return `
2458
2553
  CREATE OR REPLACE FUNCTION notify_${tableName}_delete()
2459
- RETURNS TRIGGER AS $$
2554
+ RETURNS TRIGGER AS $$
2555
+ DECLARE
2556
+ query_metadata JSON;
2460
2557
  BEGIN
2461
- PERFORM pg_notify('delete', JSON_BUILD_OBJECT('table', '${tableName}', 'query', current_query(), 'record', NEW, 'previousRecord', OLD)::text);
2462
- RETURN NEW;
2558
+ SELECT INTO query_metadata
2559
+ (regexp_match(
2560
+ current_query(),
2561
+ '^--QUERY_METADATA\\(({.*})', 'n'
2562
+ ))[1]::json;
2563
+
2564
+ PERFORM pg_notify(
2565
+ 'delete',
2566
+ json_build_object(
2567
+ 'table', '${tableName}',
2568
+ 'queryMetadata', query_metadata,
2569
+ 'deletedId', OLD.id,
2570
+ 'previousRecord', json_build_object(
2571
+ ${notifyColumnOldBuildString}
2572
+ )
2573
+ )::text
2574
+ );
2575
+ RETURN NEW;
2463
2576
  END;
2464
2577
  $$ LANGUAGE plpgsql;
2465
2578
 
2466
2579
  CREATE OR REPLACE TRIGGER "${tableName}_delete"
2467
- AFTER DELETE ON "${tableName}"
2468
- FOR EACH ROW
2580
+ AFTER DELETE ON "${tableName}"
2581
+ FOR EACH ROW
2469
2582
  EXECUTE FUNCTION notify_${tableName}_delete();
2470
2583
  `;
2471
2584
  }
2472
- createInsertTriggers(tableName) {
2585
+ createInsertTriggers(tableName, notify) {
2586
+ if (!notify) return "";
2587
+ if (notify === "ALL") {
2588
+ return `
2589
+ CREATE OR REPLACE FUNCTION notify_${tableName}_insert()
2590
+ RETURNS TRIGGER AS $$
2591
+ DECLARE
2592
+ query_metadata JSON;
2593
+ BEGIN
2594
+ SELECT INTO query_metadata
2595
+ (regexp_match(
2596
+ current_query(),
2597
+ '^--QUERY_METADATA\\(({.*})', 'n'
2598
+ ))[1]::json;
2599
+
2600
+ PERFORM pg_notify(
2601
+ 'insert',
2602
+ json_build_object(
2603
+ 'table', '${tableName}',
2604
+ 'queryMetadata', query_metadata,
2605
+ 'insertedId', NEW.id,
2606
+ 'record', NEW
2607
+ )::text
2608
+ );
2609
+
2610
+ RETURN NEW;
2611
+ END;
2612
+ $$ LANGUAGE plpgsql;
2613
+
2614
+ CREATE OR REPLACE TRIGGER "${tableName}_insert"
2615
+ AFTER INSERT ON "${tableName}"
2616
+ FOR EACH ROW
2617
+ EXECUTE FUNCTION notify_${tableName}_insert();
2618
+ `;
2619
+ }
2620
+ const notifyColumnNewBuildString = notify.map((column) => `'${column}', NEW."${column}"`).join(",\n");
2473
2621
  return `
2474
2622
  CREATE OR REPLACE FUNCTION notify_${tableName}_insert()
2475
- RETURNS TRIGGER AS $$
2623
+ RETURNS TRIGGER AS $$
2624
+ DECLARE
2625
+ query_metadata JSON;
2476
2626
  BEGIN
2477
- PERFORM pg_notify('insert', JSON_BUILD_OBJECT('table', '${tableName}', 'query', current_query(), 'record', NEW, 'previousRecord', OLD)::text);
2478
- RETURN NEW;
2627
+ SELECT INTO query_metadata
2628
+ (regexp_match(
2629
+ current_query(),
2630
+ '^--QUERY_METADATA\\(({.*})', 'n'
2631
+ ))[1]::json;
2632
+
2633
+ PERFORM pg_notify(
2634
+ 'insert',
2635
+ json_build_object(
2636
+ 'table', '${tableName}',
2637
+ 'queryMetadata', query_metadata,
2638
+ 'insertedId', NEW.id,
2639
+ 'record', json_build_object(
2640
+ ${notifyColumnNewBuildString}
2641
+ )
2642
+ )::text
2643
+ );
2644
+
2645
+ RETURN NEW;
2479
2646
  END;
2480
2647
  $$ LANGUAGE plpgsql;
2481
2648
 
2482
- CREATE TRIGGER "${tableName}_insert"
2483
- AFTER INSERT ON "${tableName}"
2484
- FOR EACH ROW
2649
+ CREATE OR REPLACE TRIGGER "${tableName}_insert"
2650
+ AFTER INSERT ON "${tableName}"
2651
+ FOR EACH ROW
2485
2652
  EXECUTE FUNCTION notify_${tableName}_insert();
2486
- `;
2653
+ `;
2487
2654
  }
2488
2655
  schemaToPsqlType(column) {
2489
2656
  if (column.hasAutoIncrement) return "BIGSERIAL";
@@ -2493,15 +2660,6 @@ EXECUTE FUNCTION notify_${tableName}_insert();
2493
2660
  return column.type;
2494
2661
  }
2495
2662
  };
2496
- __decorateClass([
2497
- boundMethod
2498
- ], PsqlEngine.prototype, "createUpdateTrigger", 1);
2499
- __decorateClass([
2500
- boundMethod
2501
- ], PsqlEngine.prototype, "createDeleteTrigger", 1);
2502
- __decorateClass([
2503
- boundMethod
2504
- ], PsqlEngine.prototype, "createInsertTriggers", 1);
2505
2663
 
2506
2664
  // src/restura/utils/TempCache.ts
2507
2665
  var import_fs3 = __toESM(require("fs"));