@carbonorm/carbonnode 6.0.3 → 6.0.5

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.
Files changed (71) hide show
  1. package/dist/executors/SqlExecutor.d.ts +2 -15
  2. package/dist/handlers/ExpressHandler.d.ts +3 -2
  3. package/dist/index.cjs.js +111 -18
  4. package/dist/index.cjs.js.map +1 -1
  5. package/dist/index.esm.js +111 -18
  6. package/dist/index.esm.js.map +1 -1
  7. package/dist/types/ormInterfaces.d.ts +14 -3
  8. package/package.json +2 -2
  9. package/src/__tests__/sakila-db/C6.js +1 -1
  10. package/src/__tests__/sakila-db/C6.mysqldump.json +1 -1
  11. package/src/__tests__/sakila-db/C6.mysqldump.sql +1 -1
  12. package/src/__tests__/sakila-db/C6.ts +1 -1
  13. package/src/__tests__/sakila-db/sqlResponses/C6.actor.delete.json +1 -0
  14. package/src/__tests__/sakila-db/sqlResponses/C6.actor.post.json +4 -3
  15. package/src/__tests__/sakila-db/sqlResponses/C6.actor.post.latest.json +3 -3
  16. package/src/__tests__/sakila-db/sqlResponses/C6.actor.put.json +2 -1
  17. package/src/__tests__/sakila-db/sqlResponses/C6.actor.put.lookup.json +3 -3
  18. package/src/__tests__/sakila-db/sqlResponses/C6.address.delete.json +1 -0
  19. package/src/__tests__/sakila-db/sqlResponses/C6.address.post.json +6 -5
  20. package/src/__tests__/sakila-db/sqlResponses/C6.address.post.latest.json +5 -5
  21. package/src/__tests__/sakila-db/sqlResponses/C6.address.put.json +2 -1
  22. package/src/__tests__/sakila-db/sqlResponses/C6.address.put.lookup.json +5 -5
  23. package/src/__tests__/sakila-db/sqlResponses/C6.category.delete.json +1 -0
  24. package/src/__tests__/sakila-db/sqlResponses/C6.category.post.json +3 -2
  25. package/src/__tests__/sakila-db/sqlResponses/C6.category.post.latest.json +2 -2
  26. package/src/__tests__/sakila-db/sqlResponses/C6.category.put.json +2 -1
  27. package/src/__tests__/sakila-db/sqlResponses/C6.category.put.lookup.json +2 -2
  28. package/src/__tests__/sakila-db/sqlResponses/C6.city.delete.json +1 -0
  29. package/src/__tests__/sakila-db/sqlResponses/C6.city.post.json +3 -2
  30. package/src/__tests__/sakila-db/sqlResponses/C6.city.post.latest.json +2 -2
  31. package/src/__tests__/sakila-db/sqlResponses/C6.city.put.json +2 -1
  32. package/src/__tests__/sakila-db/sqlResponses/C6.city.put.lookup.json +2 -2
  33. package/src/__tests__/sakila-db/sqlResponses/C6.country.delete.json +1 -0
  34. package/src/__tests__/sakila-db/sqlResponses/C6.country.post.json +3 -2
  35. package/src/__tests__/sakila-db/sqlResponses/C6.country.post.latest.json +2 -2
  36. package/src/__tests__/sakila-db/sqlResponses/C6.country.put.json +2 -1
  37. package/src/__tests__/sakila-db/sqlResponses/C6.country.put.lookup.json +2 -2
  38. package/src/__tests__/sakila-db/sqlResponses/C6.customer.delete.json +1 -0
  39. package/src/__tests__/sakila-db/sqlResponses/C6.customer.post.json +6 -5
  40. package/src/__tests__/sakila-db/sqlResponses/C6.customer.post.latest.json +5 -5
  41. package/src/__tests__/sakila-db/sqlResponses/C6.customer.put.json +2 -1
  42. package/src/__tests__/sakila-db/sqlResponses/C6.customer.put.lookup.json +5 -5
  43. package/src/__tests__/sakila-db/sqlResponses/C6.film.delete.json +1 -0
  44. package/src/__tests__/sakila-db/sqlResponses/C6.film.post.json +3 -2
  45. package/src/__tests__/sakila-db/sqlResponses/C6.film.post.latest.json +2 -2
  46. package/src/__tests__/sakila-db/sqlResponses/C6.film.put.json +2 -1
  47. package/src/__tests__/sakila-db/sqlResponses/C6.film.put.lookup.json +2 -2
  48. package/src/__tests__/sakila-db/sqlResponses/C6.inventory.delete.json +1 -0
  49. package/src/__tests__/sakila-db/sqlResponses/C6.inventory.post.json +2 -1
  50. package/src/__tests__/sakila-db/sqlResponses/C6.inventory.post.latest.json +1 -1
  51. package/src/__tests__/sakila-db/sqlResponses/C6.inventory.put.json +2 -1
  52. package/src/__tests__/sakila-db/sqlResponses/C6.inventory.put.lookup.json +1 -1
  53. package/src/__tests__/sakila-db/sqlResponses/C6.language.delete.json +1 -0
  54. package/src/__tests__/sakila-db/sqlResponses/C6.language.post.json +3 -2
  55. package/src/__tests__/sakila-db/sqlResponses/C6.language.post.latest.json +2 -2
  56. package/src/__tests__/sakila-db/sqlResponses/C6.language.put.json +2 -1
  57. package/src/__tests__/sakila-db/sqlResponses/C6.language.put.lookup.json +2 -2
  58. package/src/__tests__/sakila-db/sqlResponses/C6.payment.delete.json +1 -0
  59. package/src/__tests__/sakila-db/sqlResponses/C6.payment.post.json +3 -2
  60. package/src/__tests__/sakila-db/sqlResponses/C6.payment.post.latest.json +2 -2
  61. package/src/__tests__/sakila-db/sqlResponses/C6.payment.put.json +1 -0
  62. package/src/__tests__/sakila-db/sqlResponses/C6.payment.put.lookup.json +2 -2
  63. package/src/__tests__/sakila-db/sqlResponses/C6.rental.delete.json +1 -0
  64. package/src/__tests__/sakila-db/sqlResponses/C6.rental.post.json +4 -3
  65. package/src/__tests__/sakila-db/sqlResponses/C6.rental.post.latest.json +3 -3
  66. package/src/__tests__/sakila-db/sqlResponses/C6.rental.put.json +2 -1
  67. package/src/__tests__/sakila-db/sqlResponses/C6.rental.put.lookup.json +3 -3
  68. package/src/executors/HttpExecutor.ts +2 -1
  69. package/src/executors/SqlExecutor.ts +132 -22
  70. package/src/handlers/ExpressHandler.ts +4 -1
  71. package/src/types/ormInterfaces.ts +13 -3
@@ -15,21 +15,8 @@ export declare class SqlExecutor<G extends OrmGenerics> extends Executor<G> {
15
15
  private normalizeRequestPayload;
16
16
  private extractRequestBody;
17
17
  private extractPrimaryKeyValues;
18
+ private extractPrimaryKeyValuesFromData;
18
19
  private broadcastWebsocketIfConfigured;
19
- runQuery(): Promise<{
20
- rest: any;
21
- sql: {
22
- sql: any;
23
- values: any;
24
- };
25
- affected?: undefined;
26
- } | {
27
- affected: any;
28
- rest: never[];
29
- sql: {
30
- sql: any;
31
- values: any;
32
- };
33
- }>;
20
+ runQuery(): Promise<DetermineResponseDataType<G['RequestMethod'], G['RestTableInterface']>>;
34
21
  private validateSqlAllowList;
35
22
  }
@@ -1,8 +1,9 @@
1
1
  import type { Request, Response, NextFunction } from "express";
2
2
  import type { Pool } from "mysql2/promise";
3
- import type { iC6Object } from "../types/ormInterfaces";
4
- export declare function ExpressHandler({ C6, mysqlPool, sqlAllowListPath, }: {
3
+ import type { iC6Object, tWebsocketBroadcast } from "../types/ormInterfaces";
4
+ export declare function ExpressHandler({ C6, mysqlPool, sqlAllowListPath, websocketBroadcast, }: {
5
5
  C6: iC6Object;
6
6
  mysqlPool: Pool;
7
7
  sqlAllowListPath?: string;
8
+ websocketBroadcast?: tWebsocketBroadcast;
8
9
  }): (req: Request, res: Response, next: NextFunction) => Promise<void>;
package/dist/index.cjs.js CHANGED
@@ -3131,21 +3131,21 @@ var SqlExecutor = /** @class */ (function (_super) {
3131
3131
  case 3: return [4 /*yield*/, this.runQuery()];
3132
3132
  case 4:
3133
3133
  result = _b.sent();
3134
- return [4 /*yield*/, this.broadcastWebsocketIfConfigured()];
3134
+ return [4 /*yield*/, this.broadcastWebsocketIfConfigured(result)];
3135
3135
  case 5:
3136
3136
  _b.sent();
3137
3137
  return [2 /*return*/, result];
3138
3138
  case 6: return [4 /*yield*/, this.runQuery()];
3139
3139
  case 7:
3140
3140
  result = _b.sent();
3141
- return [4 /*yield*/, this.broadcastWebsocketIfConfigured()];
3141
+ return [4 /*yield*/, this.broadcastWebsocketIfConfigured(result)];
3142
3142
  case 8:
3143
3143
  _b.sent();
3144
3144
  return [2 /*return*/, result];
3145
3145
  case 9: return [4 /*yield*/, this.runQuery()];
3146
3146
  case 10:
3147
3147
  result = _b.sent();
3148
- return [4 /*yield*/, this.broadcastWebsocketIfConfigured()];
3148
+ return [4 /*yield*/, this.broadcastWebsocketIfConfigured(result)];
3149
3149
  case 11:
3150
3150
  _b.sent();
3151
3151
  return [2 /*return*/, result];
@@ -3254,8 +3254,9 @@ var SqlExecutor = /** @class */ (function (_super) {
3254
3254
  var _a, _b;
3255
3255
  var request = this.request;
3256
3256
  if (this.config.requestMethod === C6Constants.POST) {
3257
- if (Array.isArray(request.dataInsertMultipleRows) && request.dataInsertMultipleRows.length > 0) {
3258
- return request.dataInsertMultipleRows[0];
3257
+ var insertRows = request.dataInsertMultipleRows;
3258
+ if (Array.isArray(insertRows) && insertRows.length > 0) {
3259
+ return insertRows[0];
3259
3260
  }
3260
3261
  if (C6Constants.INSERT in request) {
3261
3262
  return (_a = request[C6Constants.INSERT]) !== null && _a !== void 0 ? _a : {};
@@ -3316,34 +3317,124 @@ var SqlExecutor = /** @class */ (function (_super) {
3316
3317
  }
3317
3318
  return Object.keys(pkValues).length > 0 ? pkValues : null;
3318
3319
  };
3319
- SqlExecutor.prototype.broadcastWebsocketIfConfigured = function () {
3320
+ SqlExecutor.prototype.extractPrimaryKeyValuesFromData = function (data) {
3321
+ if (!data)
3322
+ return null;
3323
+ var row = Array.isArray(data) ? data[0] : data;
3324
+ if (!row || typeof row !== "object")
3325
+ return null;
3326
+ var pkShorts = this.config.restModel.PRIMARY_SHORT;
3327
+ var columns = this.config.restModel.COLUMNS;
3328
+ var pkValues = {};
3329
+ var _loop_2 = function (pk) {
3330
+ if (pk in row) {
3331
+ pkValues[pk] = row[pk];
3332
+ return "continue";
3333
+ }
3334
+ var fullKey = Object.keys(columns).find(function (key) { return columns[key] === pk; });
3335
+ if (fullKey && fullKey in row) {
3336
+ pkValues[pk] = row[fullKey];
3337
+ }
3338
+ };
3339
+ for (var _i = 0, pkShorts_1 = pkShorts; _i < pkShorts_1.length; _i++) {
3340
+ var pk = pkShorts_1[_i];
3341
+ _loop_2(pk);
3342
+ }
3343
+ if (pkShorts.length > 0 && Object.keys(pkValues).length < pkShorts.length) {
3344
+ return null;
3345
+ }
3346
+ return Object.keys(pkValues).length > 0 ? pkValues : null;
3347
+ };
3348
+ SqlExecutor.prototype.broadcastWebsocketIfConfigured = function (response) {
3320
3349
  return tslib.__awaiter(this, void 0, void 0, function () {
3321
- var broadcast, payload, error_1;
3350
+ var broadcast, normalizedRequest, pkShorts, columns, validColumns, responseRest, responsePrimaryKey, insertId, synthesizedRequest, now, synthesized, payload, error_1;
3322
3351
  var _a, _b;
3323
- return tslib.__generator(this, function (_c) {
3324
- switch (_c.label) {
3352
+ var _c, _d, _e;
3353
+ return tslib.__generator(this, function (_f) {
3354
+ switch (_f.label) {
3325
3355
  case 0:
3326
3356
  broadcast = this.config.websocketBroadcast;
3327
- if (!broadcast || this.config.requestMethod === C6Constants.GET)
3357
+ this.config.verbose && console.log("[SQL EXECUTOR] 📣 broadcastWebsocketIfConfigured start", {
3358
+ method: this.config.requestMethod,
3359
+ hasBroadcast: Boolean(broadcast),
3360
+ });
3361
+ if (!broadcast || this.config.requestMethod === C6Constants.GET) {
3362
+ this.config.verbose && console.log("[SQL EXECUTOR] 📣 websocket broadcast skipped", {
3363
+ reason: !broadcast ? "no broadcast configured" : "GET request",
3364
+ });
3328
3365
  return [2 /*return*/];
3366
+ }
3367
+ normalizedRequest = this.normalizeRequestPayload(this.extractRequestBody());
3368
+ pkShorts = (_c = this.config.restModel.PRIMARY_SHORT) !== null && _c !== void 0 ? _c : [];
3369
+ columns = this.config.restModel.COLUMNS;
3370
+ validColumns = new Set(Object.values(columns));
3371
+ responseRest = response === null || response === void 0 ? void 0 : response.rest;
3372
+ responsePrimaryKey = this.extractPrimaryKeyValuesFromData(responseRest);
3373
+ this.config.verbose && console.log("[SQL EXECUTOR] 📣 websocket request payload", {
3374
+ normalizedRequest: normalizedRequest,
3375
+ requestPrimaryKey: this.extractPrimaryKeyValues(),
3376
+ pkShorts: pkShorts,
3377
+ });
3378
+ this.config.verbose && console.log("[SQL EXECUTOR] 📣 websocket response payload", {
3379
+ responseRest: responseRest,
3380
+ responsePrimaryKey: responsePrimaryKey,
3381
+ });
3382
+ if ((responseRest === null || (Array.isArray(responseRest) && responseRest.length === 0))
3383
+ && this.config.requestMethod === C6Constants.POST) {
3384
+ this.config.verbose && console.log("[SQL EXECUTOR] 📣 response rest empty, attempting synthesize", {
3385
+ responseRest: responseRest,
3386
+ });
3387
+ insertId = response === null || response === void 0 ? void 0 : response.insertId;
3388
+ this.config.verbose && console.log("[SQL EXECUTOR] 📣 POST insertId lookup", {
3389
+ insertId: insertId,
3390
+ });
3391
+ if (insertId !== undefined && pkShorts.length === 1) {
3392
+ synthesizedRequest = tslib.__assign({}, normalizedRequest);
3393
+ now = new Date().toISOString();
3394
+ if (validColumns.has("changed_at") && synthesizedRequest.changed_at === undefined) {
3395
+ synthesizedRequest.changed_at = now;
3396
+ }
3397
+ if (validColumns.has("created_at") && synthesizedRequest.created_at === undefined) {
3398
+ synthesizedRequest.created_at = now;
3399
+ }
3400
+ if (validColumns.has("updated_at") && synthesizedRequest.updated_at === undefined) {
3401
+ synthesizedRequest.updated_at = now;
3402
+ }
3403
+ synthesized = tslib.__assign(tslib.__assign({}, synthesizedRequest), (_a = {}, _a[pkShorts[0]] = insertId, _a));
3404
+ // @ts-ignore - todo
3405
+ responseRest = [synthesized];
3406
+ responsePrimaryKey = (_b = {},
3407
+ _b[pkShorts[0]] = insertId,
3408
+ _b);
3409
+ this.config.verbose && console.log("[SQL EXECUTOR] 📣 synthesized response payload", {
3410
+ synthesized: synthesized,
3411
+ responsePrimaryKey: responsePrimaryKey,
3412
+ });
3413
+ }
3414
+ }
3329
3415
  payload = {
3330
3416
  REST: {
3331
3417
  TABLE_NAME: this.config.restModel.TABLE_NAME,
3332
- TABLE_PREFIX: (_b = (_a = this.config.C6) === null || _a === void 0 ? void 0 : _a.PREFIX) !== null && _b !== void 0 ? _b : "",
3418
+ TABLE_PREFIX: (_e = (_d = this.config.C6) === null || _d === void 0 ? void 0 : _d.PREFIX) !== null && _e !== void 0 ? _e : "",
3333
3419
  METHOD: this.config.requestMethod,
3334
- REQUEST: this.normalizeRequestPayload(this.extractRequestBody()),
3420
+ REQUEST: normalizedRequest,
3335
3421
  REQUEST_PRIMARY_KEY: this.extractPrimaryKeyValues(),
3422
+ RESPONSE: responseRest,
3423
+ RESPONSE_PRIMARY_KEY: responsePrimaryKey,
3336
3424
  },
3337
3425
  };
3338
- _c.label = 1;
3426
+ this.config.verbose && console.log("[SQL EXECUTOR] 📣 websocket payload ready", payload);
3427
+ _f.label = 1;
3339
3428
  case 1:
3340
- _c.trys.push([1, 3, , 4]);
3429
+ _f.trys.push([1, 3, , 4]);
3430
+ this.config.verbose && console.log("[SQL EXECUTOR] 📣 websocket broadcast dispatch start");
3341
3431
  return [4 /*yield*/, broadcast(payload)];
3342
3432
  case 2:
3343
- _c.sent();
3433
+ _f.sent();
3434
+ this.config.verbose && console.log("[SQL EXECUTOR] 📣 websocket broadcast dispatch complete");
3344
3435
  return [3 /*break*/, 4];
3345
3436
  case 3:
3346
- error_1 = _c.sent();
3437
+ error_1 = _f.sent();
3347
3438
  if (this.config.verbose) {
3348
3439
  console.error("[SQL EXECUTOR] websocketBroadcast failed", error_1);
3349
3440
  }
@@ -3404,7 +3495,8 @@ var SqlExecutor = /** @class */ (function (_super) {
3404
3495
  this.config.verbose && console.log("[SQL EXECUTOR] \u270F\uFE0F Rows affected:", result.affectedRows);
3405
3496
  return [2 /*return*/, {
3406
3497
  affected: result.affectedRows,
3407
- rest: [],
3498
+ insertId: result.insertId,
3499
+ rest: [], // TODO - remove rest empty array from non-GET responses?
3408
3500
  sql: { sql: sql, values: values }
3409
3501
  }];
3410
3502
  }
@@ -3450,7 +3542,7 @@ var SqlExecutor$1 = /*#__PURE__*/Object.freeze({
3450
3542
  // note sure how it would help anyone actually...
3451
3543
  function ExpressHandler(_a) {
3452
3544
  var _this = this;
3453
- var C6 = _a.C6, mysqlPool = _a.mysqlPool, sqlAllowListPath = _a.sqlAllowListPath;
3545
+ var C6 = _a.C6, mysqlPool = _a.mysqlPool, sqlAllowListPath = _a.sqlAllowListPath, websocketBroadcast = _a.websocketBroadcast;
3454
3546
  return function (req, res, next) { return tslib.__awaiter(_this, void 0, void 0, function () {
3455
3547
  var incomingMethod, table, primary, methodOverrideRaw, methodOverride, treatAsGet, method, payload, restModel, primaryKeys_1, primaryShortKeys_1, columnMap_1, resolveShortKey_1, hasPrimaryKeyValues, primaryKeyName, response, err_1;
3456
3548
  var _a, _b, _c, _d, _e, _f, _g;
@@ -3537,6 +3629,7 @@ function ExpressHandler(_a) {
3537
3629
  C6: C6,
3538
3630
  mysqlPool: mysqlPool,
3539
3631
  sqlAllowListPath: sqlAllowListPath,
3632
+ websocketBroadcast: websocketBroadcast,
3540
3633
  requestMethod: method,
3541
3634
  restModel: C6.TABLES[table]
3542
3635
  })(payload)];