@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
package/dist/index.esm.js CHANGED
@@ -3128,21 +3128,21 @@ var SqlExecutor = /** @class */ (function (_super) {
3128
3128
  case 3: return [4 /*yield*/, this.runQuery()];
3129
3129
  case 4:
3130
3130
  result = _b.sent();
3131
- return [4 /*yield*/, this.broadcastWebsocketIfConfigured()];
3131
+ return [4 /*yield*/, this.broadcastWebsocketIfConfigured(result)];
3132
3132
  case 5:
3133
3133
  _b.sent();
3134
3134
  return [2 /*return*/, result];
3135
3135
  case 6: return [4 /*yield*/, this.runQuery()];
3136
3136
  case 7:
3137
3137
  result = _b.sent();
3138
- return [4 /*yield*/, this.broadcastWebsocketIfConfigured()];
3138
+ return [4 /*yield*/, this.broadcastWebsocketIfConfigured(result)];
3139
3139
  case 8:
3140
3140
  _b.sent();
3141
3141
  return [2 /*return*/, result];
3142
3142
  case 9: return [4 /*yield*/, this.runQuery()];
3143
3143
  case 10:
3144
3144
  result = _b.sent();
3145
- return [4 /*yield*/, this.broadcastWebsocketIfConfigured()];
3145
+ return [4 /*yield*/, this.broadcastWebsocketIfConfigured(result)];
3146
3146
  case 11:
3147
3147
  _b.sent();
3148
3148
  return [2 /*return*/, result];
@@ -3251,8 +3251,9 @@ var SqlExecutor = /** @class */ (function (_super) {
3251
3251
  var _a, _b;
3252
3252
  var request = this.request;
3253
3253
  if (this.config.requestMethod === C6Constants.POST) {
3254
- if (Array.isArray(request.dataInsertMultipleRows) && request.dataInsertMultipleRows.length > 0) {
3255
- return request.dataInsertMultipleRows[0];
3254
+ var insertRows = request.dataInsertMultipleRows;
3255
+ if (Array.isArray(insertRows) && insertRows.length > 0) {
3256
+ return insertRows[0];
3256
3257
  }
3257
3258
  if (C6Constants.INSERT in request) {
3258
3259
  return (_a = request[C6Constants.INSERT]) !== null && _a !== void 0 ? _a : {};
@@ -3313,34 +3314,124 @@ var SqlExecutor = /** @class */ (function (_super) {
3313
3314
  }
3314
3315
  return Object.keys(pkValues).length > 0 ? pkValues : null;
3315
3316
  };
3316
- SqlExecutor.prototype.broadcastWebsocketIfConfigured = function () {
3317
+ SqlExecutor.prototype.extractPrimaryKeyValuesFromData = function (data) {
3318
+ if (!data)
3319
+ return null;
3320
+ var row = Array.isArray(data) ? data[0] : data;
3321
+ if (!row || typeof row !== "object")
3322
+ return null;
3323
+ var pkShorts = this.config.restModel.PRIMARY_SHORT;
3324
+ var columns = this.config.restModel.COLUMNS;
3325
+ var pkValues = {};
3326
+ var _loop_2 = function (pk) {
3327
+ if (pk in row) {
3328
+ pkValues[pk] = row[pk];
3329
+ return "continue";
3330
+ }
3331
+ var fullKey = Object.keys(columns).find(function (key) { return columns[key] === pk; });
3332
+ if (fullKey && fullKey in row) {
3333
+ pkValues[pk] = row[fullKey];
3334
+ }
3335
+ };
3336
+ for (var _i = 0, pkShorts_1 = pkShorts; _i < pkShorts_1.length; _i++) {
3337
+ var pk = pkShorts_1[_i];
3338
+ _loop_2(pk);
3339
+ }
3340
+ if (pkShorts.length > 0 && Object.keys(pkValues).length < pkShorts.length) {
3341
+ return null;
3342
+ }
3343
+ return Object.keys(pkValues).length > 0 ? pkValues : null;
3344
+ };
3345
+ SqlExecutor.prototype.broadcastWebsocketIfConfigured = function (response) {
3317
3346
  return __awaiter(this, void 0, void 0, function () {
3318
- var broadcast, payload, error_1;
3347
+ var broadcast, normalizedRequest, pkShorts, columns, validColumns, responseRest, responsePrimaryKey, insertId, synthesizedRequest, now, synthesized, payload, error_1;
3319
3348
  var _a, _b;
3320
- return __generator(this, function (_c) {
3321
- switch (_c.label) {
3349
+ var _c, _d, _e;
3350
+ return __generator(this, function (_f) {
3351
+ switch (_f.label) {
3322
3352
  case 0:
3323
3353
  broadcast = this.config.websocketBroadcast;
3324
- if (!broadcast || this.config.requestMethod === C6Constants.GET)
3354
+ this.config.verbose && console.log("[SQL EXECUTOR] 📣 broadcastWebsocketIfConfigured start", {
3355
+ method: this.config.requestMethod,
3356
+ hasBroadcast: Boolean(broadcast),
3357
+ });
3358
+ if (!broadcast || this.config.requestMethod === C6Constants.GET) {
3359
+ this.config.verbose && console.log("[SQL EXECUTOR] 📣 websocket broadcast skipped", {
3360
+ reason: !broadcast ? "no broadcast configured" : "GET request",
3361
+ });
3325
3362
  return [2 /*return*/];
3363
+ }
3364
+ normalizedRequest = this.normalizeRequestPayload(this.extractRequestBody());
3365
+ pkShorts = (_c = this.config.restModel.PRIMARY_SHORT) !== null && _c !== void 0 ? _c : [];
3366
+ columns = this.config.restModel.COLUMNS;
3367
+ validColumns = new Set(Object.values(columns));
3368
+ responseRest = response === null || response === void 0 ? void 0 : response.rest;
3369
+ responsePrimaryKey = this.extractPrimaryKeyValuesFromData(responseRest);
3370
+ this.config.verbose && console.log("[SQL EXECUTOR] 📣 websocket request payload", {
3371
+ normalizedRequest: normalizedRequest,
3372
+ requestPrimaryKey: this.extractPrimaryKeyValues(),
3373
+ pkShorts: pkShorts,
3374
+ });
3375
+ this.config.verbose && console.log("[SQL EXECUTOR] 📣 websocket response payload", {
3376
+ responseRest: responseRest,
3377
+ responsePrimaryKey: responsePrimaryKey,
3378
+ });
3379
+ if ((responseRest === null || (Array.isArray(responseRest) && responseRest.length === 0))
3380
+ && this.config.requestMethod === C6Constants.POST) {
3381
+ this.config.verbose && console.log("[SQL EXECUTOR] 📣 response rest empty, attempting synthesize", {
3382
+ responseRest: responseRest,
3383
+ });
3384
+ insertId = response === null || response === void 0 ? void 0 : response.insertId;
3385
+ this.config.verbose && console.log("[SQL EXECUTOR] 📣 POST insertId lookup", {
3386
+ insertId: insertId,
3387
+ });
3388
+ if (insertId !== undefined && pkShorts.length === 1) {
3389
+ synthesizedRequest = __assign({}, normalizedRequest);
3390
+ now = new Date().toISOString();
3391
+ if (validColumns.has("changed_at") && synthesizedRequest.changed_at === undefined) {
3392
+ synthesizedRequest.changed_at = now;
3393
+ }
3394
+ if (validColumns.has("created_at") && synthesizedRequest.created_at === undefined) {
3395
+ synthesizedRequest.created_at = now;
3396
+ }
3397
+ if (validColumns.has("updated_at") && synthesizedRequest.updated_at === undefined) {
3398
+ synthesizedRequest.updated_at = now;
3399
+ }
3400
+ synthesized = __assign(__assign({}, synthesizedRequest), (_a = {}, _a[pkShorts[0]] = insertId, _a));
3401
+ // @ts-ignore - todo
3402
+ responseRest = [synthesized];
3403
+ responsePrimaryKey = (_b = {},
3404
+ _b[pkShorts[0]] = insertId,
3405
+ _b);
3406
+ this.config.verbose && console.log("[SQL EXECUTOR] 📣 synthesized response payload", {
3407
+ synthesized: synthesized,
3408
+ responsePrimaryKey: responsePrimaryKey,
3409
+ });
3410
+ }
3411
+ }
3326
3412
  payload = {
3327
3413
  REST: {
3328
3414
  TABLE_NAME: this.config.restModel.TABLE_NAME,
3329
- TABLE_PREFIX: (_b = (_a = this.config.C6) === null || _a === void 0 ? void 0 : _a.PREFIX) !== null && _b !== void 0 ? _b : "",
3415
+ TABLE_PREFIX: (_e = (_d = this.config.C6) === null || _d === void 0 ? void 0 : _d.PREFIX) !== null && _e !== void 0 ? _e : "",
3330
3416
  METHOD: this.config.requestMethod,
3331
- REQUEST: this.normalizeRequestPayload(this.extractRequestBody()),
3417
+ REQUEST: normalizedRequest,
3332
3418
  REQUEST_PRIMARY_KEY: this.extractPrimaryKeyValues(),
3419
+ RESPONSE: responseRest,
3420
+ RESPONSE_PRIMARY_KEY: responsePrimaryKey,
3333
3421
  },
3334
3422
  };
3335
- _c.label = 1;
3423
+ this.config.verbose && console.log("[SQL EXECUTOR] 📣 websocket payload ready", payload);
3424
+ _f.label = 1;
3336
3425
  case 1:
3337
- _c.trys.push([1, 3, , 4]);
3426
+ _f.trys.push([1, 3, , 4]);
3427
+ this.config.verbose && console.log("[SQL EXECUTOR] 📣 websocket broadcast dispatch start");
3338
3428
  return [4 /*yield*/, broadcast(payload)];
3339
3429
  case 2:
3340
- _c.sent();
3430
+ _f.sent();
3431
+ this.config.verbose && console.log("[SQL EXECUTOR] 📣 websocket broadcast dispatch complete");
3341
3432
  return [3 /*break*/, 4];
3342
3433
  case 3:
3343
- error_1 = _c.sent();
3434
+ error_1 = _f.sent();
3344
3435
  if (this.config.verbose) {
3345
3436
  console.error("[SQL EXECUTOR] websocketBroadcast failed", error_1);
3346
3437
  }
@@ -3401,7 +3492,8 @@ var SqlExecutor = /** @class */ (function (_super) {
3401
3492
  this.config.verbose && console.log("[SQL EXECUTOR] \u270F\uFE0F Rows affected:", result.affectedRows);
3402
3493
  return [2 /*return*/, {
3403
3494
  affected: result.affectedRows,
3404
- rest: [],
3495
+ insertId: result.insertId,
3496
+ rest: [], // TODO - remove rest empty array from non-GET responses?
3405
3497
  sql: { sql: sql, values: values }
3406
3498
  }];
3407
3499
  }
@@ -3447,7 +3539,7 @@ var SqlExecutor$1 = /*#__PURE__*/Object.freeze({
3447
3539
  // note sure how it would help anyone actually...
3448
3540
  function ExpressHandler(_a) {
3449
3541
  var _this = this;
3450
- var C6 = _a.C6, mysqlPool = _a.mysqlPool, sqlAllowListPath = _a.sqlAllowListPath;
3542
+ var C6 = _a.C6, mysqlPool = _a.mysqlPool, sqlAllowListPath = _a.sqlAllowListPath, websocketBroadcast = _a.websocketBroadcast;
3451
3543
  return function (req, res, next) { return __awaiter(_this, void 0, void 0, function () {
3452
3544
  var incomingMethod, table, primary, methodOverrideRaw, methodOverride, treatAsGet, method, payload, restModel, primaryKeys_1, primaryShortKeys_1, columnMap_1, resolveShortKey_1, hasPrimaryKeyValues, primaryKeyName, response, err_1;
3453
3545
  var _a, _b, _c, _d, _e, _f, _g;
@@ -3534,6 +3626,7 @@ function ExpressHandler(_a) {
3534
3626
  C6: C6,
3535
3627
  mysqlPool: mysqlPool,
3536
3628
  sqlAllowListPath: sqlAllowListPath,
3629
+ websocketBroadcast: websocketBroadcast,
3537
3630
  requestMethod: method,
3538
3631
  restModel: C6.TABLES[table]
3539
3632
  })(payload)];