@sdflc/backend-helpers 1.0.135 → 1.0.137

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.
@@ -646,14 +646,26 @@ declare class BaseGateway {
646
646
  * no second pass over the results here.
647
647
  */
648
648
  updateMany(params: any): Promise<any[]>;
649
+ /**
650
+ * Builds the audit fields (updatedAt / updatedBy) to SET on an increment /
651
+ * decrement. These must be applied as normal assignments — NOT fed through the
652
+ * counter, which would try to do `updated_at = updated_at + <ts>` /
653
+ * `updated_by = updated_by + <uuid>`. Returns only the fields the gateway is
654
+ * configured to manage; an empty object when the table has no audit columns
655
+ * (so counter-only tables behave exactly as a bare increment).
656
+ */
657
+ buildAuditFieldsForCounter(): Record<string, any>;
649
658
  /**
650
659
  * Increments numeric field(s) on records matching the `where` filter.
651
660
  * Uses the database's native INCREMENT operation rather than a read-modify-write,
652
661
  * making it safe for concurrent requests.
653
662
  *
654
663
  * `params` should be an object mapping field names to the increment amounts
655
- * (e.g. `{ viewCount: 1 }`). `transformOnUpdate()` is still called, so
656
- * audit fields (updatedAt, updatedBy) and version are updated as normal.
664
+ * (e.g. `{ viewCount: 1 }`). ONLY those caller fields are incremented; audit
665
+ * fields (updatedAt / updatedBy) are applied as normal SET assignments so
666
+ * timestamp / uuid columns are never fed through the counter (which would fail
667
+ * with `operator does not exist`). Knex compiles the chained increment + update
668
+ * into a single UPDATE ... SET.
657
669
  *
658
670
  * Throws if no filter conditions are applied (same safety guard as `update()`).
659
671
  * Clears affected records from the loader cache after the operation.
@@ -663,7 +675,9 @@ declare class BaseGateway {
663
675
  * Decrements numeric field(s) on records matching the `where` filter.
664
676
  * Mirror of `increment()` using the database's native DECREMENT operation.
665
677
  *
666
- * Same behaviour, safety guards, and cache invalidation as `increment()`.
678
+ * Same behaviour, safety guards, and cache invalidation as `increment()` —
679
+ * caller fields only are decremented; audit fields are SET, not fed through the
680
+ * counter.
667
681
  */
668
682
  decrement(where: any, params: any): Promise<any[]>;
669
683
  /**
@@ -1279,21 +1279,50 @@ class BaseGateway {
1279
1279
  }
1280
1280
  return results.map((item) => this.transformOnOut(item));
1281
1281
  }
1282
+ /**
1283
+ * Builds the audit fields (updatedAt / updatedBy) to SET on an increment /
1284
+ * decrement. These must be applied as normal assignments — NOT fed through the
1285
+ * counter, which would try to do `updated_at = updated_at + <ts>` /
1286
+ * `updated_by = updated_by + <uuid>`. Returns only the fields the gateway is
1287
+ * configured to manage; an empty object when the table has no audit columns
1288
+ * (so counter-only tables behave exactly as a bare increment).
1289
+ */
1290
+ buildAuditFieldsForCounter() {
1291
+ const { hasUpdatedAt, hasUpdatedBy } = this.config;
1292
+ const out = {};
1293
+ if (hasUpdatedAt) {
1294
+ out[(0, lodash_1.camelCase)(constants_1.FIELDS.UPDATED_AT)] = this.dateTime.dbFormat(this.dateTimeNow);
1295
+ }
1296
+ if (hasUpdatedBy) {
1297
+ const userId = this.getContext()?.userId;
1298
+ if (userId != null) {
1299
+ out[(0, lodash_1.camelCase)(constants_1.FIELDS.UPDATED_BY)] = userId;
1300
+ }
1301
+ }
1302
+ return out;
1303
+ }
1282
1304
  /**
1283
1305
  * Increments numeric field(s) on records matching the `where` filter.
1284
1306
  * Uses the database's native INCREMENT operation rather than a read-modify-write,
1285
1307
  * making it safe for concurrent requests.
1286
1308
  *
1287
1309
  * `params` should be an object mapping field names to the increment amounts
1288
- * (e.g. `{ viewCount: 1 }`). `transformOnUpdate()` is still called, so
1289
- * audit fields (updatedAt, updatedBy) and version are updated as normal.
1310
+ * (e.g. `{ viewCount: 1 }`). ONLY those caller fields are incremented; audit
1311
+ * fields (updatedAt / updatedBy) are applied as normal SET assignments so
1312
+ * timestamp / uuid columns are never fed through the counter (which would fail
1313
+ * with `operator does not exist`). Knex compiles the chained increment + update
1314
+ * into a single UPDATE ... SET.
1290
1315
  *
1291
1316
  * Throws if no filter conditions are applied (same safety guard as `update()`).
1292
1317
  * Clears affected records from the loader cache after the operation.
1293
1318
  */
1294
1319
  async increment(where, params) {
1295
- const object = await this.transformOnUpdate(params, where);
1296
- const query = this.getBuilder().increment(object).returning(this.config.returnFields);
1320
+ let query = this.getBuilder().increment({ ...params });
1321
+ const audit = this.buildAuditFieldsForCounter();
1322
+ if (Object.keys(audit).length > 0) {
1323
+ query = query.update(audit);
1324
+ }
1325
+ query = query.returning(this.config.returnFields);
1297
1326
  const filtersAppliedQty = await this.onUpdateFilter(query, where);
1298
1327
  if (filtersAppliedQty == null || filtersAppliedQty <= 0) {
1299
1328
  throw new Error(`Failed to increment in table '${this.config.table}': no filters applied. ` +
@@ -1314,11 +1343,17 @@ class BaseGateway {
1314
1343
  * Decrements numeric field(s) on records matching the `where` filter.
1315
1344
  * Mirror of `increment()` using the database's native DECREMENT operation.
1316
1345
  *
1317
- * Same behaviour, safety guards, and cache invalidation as `increment()`.
1346
+ * Same behaviour, safety guards, and cache invalidation as `increment()` —
1347
+ * caller fields only are decremented; audit fields are SET, not fed through the
1348
+ * counter.
1318
1349
  */
1319
1350
  async decrement(where, params) {
1320
- const object = await this.transformOnUpdate(params, where);
1321
- const query = this.getBuilder().decrement(object).returning(this.config.returnFields);
1351
+ let query = this.getBuilder().decrement({ ...params });
1352
+ const audit = this.buildAuditFieldsForCounter();
1353
+ if (Object.keys(audit).length > 0) {
1354
+ query = query.update(audit);
1355
+ }
1356
+ query = query.returning(this.config.returnFields);
1322
1357
  const filtersAppliedQty = await this.onUpdateFilter(query, where);
1323
1358
  if (filtersAppliedQty == null || filtersAppliedQty <= 0) {
1324
1359
  throw new Error(`Failed to decrement in table '${this.config.table}': no filters applied. ` +
@@ -90,6 +90,11 @@ const createContextDefault = async (args) => {
90
90
  authorization,
91
91
  fingerprint,
92
92
  authorizationToken,
93
+ // Parsed request cookies must be exposed on the context so callers can read
94
+ // `context.cookies` (e.g. cookie-based flows that don't use the JWT). This
95
+ // was dropped from sharedContext in an earlier refactor, leaving
96
+ // `context.cookies` undefined even though the cookie was parsed.
97
+ cookies,
93
98
  isExternalDb: !!inDb,
94
99
  // Rate limiting — undefined values are omitted so context stays clean when
95
100
  // neither is configured.
@@ -646,14 +646,26 @@ declare class BaseGateway {
646
646
  * no second pass over the results here.
647
647
  */
648
648
  updateMany(params: any): Promise<any[]>;
649
+ /**
650
+ * Builds the audit fields (updatedAt / updatedBy) to SET on an increment /
651
+ * decrement. These must be applied as normal assignments — NOT fed through the
652
+ * counter, which would try to do `updated_at = updated_at + <ts>` /
653
+ * `updated_by = updated_by + <uuid>`. Returns only the fields the gateway is
654
+ * configured to manage; an empty object when the table has no audit columns
655
+ * (so counter-only tables behave exactly as a bare increment).
656
+ */
657
+ buildAuditFieldsForCounter(): Record<string, any>;
649
658
  /**
650
659
  * Increments numeric field(s) on records matching the `where` filter.
651
660
  * Uses the database's native INCREMENT operation rather than a read-modify-write,
652
661
  * making it safe for concurrent requests.
653
662
  *
654
663
  * `params` should be an object mapping field names to the increment amounts
655
- * (e.g. `{ viewCount: 1 }`). `transformOnUpdate()` is still called, so
656
- * audit fields (updatedAt, updatedBy) and version are updated as normal.
664
+ * (e.g. `{ viewCount: 1 }`). ONLY those caller fields are incremented; audit
665
+ * fields (updatedAt / updatedBy) are applied as normal SET assignments so
666
+ * timestamp / uuid columns are never fed through the counter (which would fail
667
+ * with `operator does not exist`). Knex compiles the chained increment + update
668
+ * into a single UPDATE ... SET.
657
669
  *
658
670
  * Throws if no filter conditions are applied (same safety guard as `update()`).
659
671
  * Clears affected records from the loader cache after the operation.
@@ -663,7 +675,9 @@ declare class BaseGateway {
663
675
  * Decrements numeric field(s) on records matching the `where` filter.
664
676
  * Mirror of `increment()` using the database's native DECREMENT operation.
665
677
  *
666
- * Same behaviour, safety guards, and cache invalidation as `increment()`.
678
+ * Same behaviour, safety guards, and cache invalidation as `increment()` —
679
+ * caller fields only are decremented; audit fields are SET, not fed through the
680
+ * counter.
667
681
  */
668
682
  decrement(where: any, params: any): Promise<any[]>;
669
683
  /**
@@ -1273,21 +1273,50 @@ class BaseGateway {
1273
1273
  }
1274
1274
  return results.map((item) => this.transformOnOut(item));
1275
1275
  }
1276
+ /**
1277
+ * Builds the audit fields (updatedAt / updatedBy) to SET on an increment /
1278
+ * decrement. These must be applied as normal assignments — NOT fed through the
1279
+ * counter, which would try to do `updated_at = updated_at + <ts>` /
1280
+ * `updated_by = updated_by + <uuid>`. Returns only the fields the gateway is
1281
+ * configured to manage; an empty object when the table has no audit columns
1282
+ * (so counter-only tables behave exactly as a bare increment).
1283
+ */
1284
+ buildAuditFieldsForCounter() {
1285
+ const { hasUpdatedAt, hasUpdatedBy } = this.config;
1286
+ const out = {};
1287
+ if (hasUpdatedAt) {
1288
+ out[camelCase(FIELDS.UPDATED_AT)] = this.dateTime.dbFormat(this.dateTimeNow);
1289
+ }
1290
+ if (hasUpdatedBy) {
1291
+ const userId = this.getContext()?.userId;
1292
+ if (userId != null) {
1293
+ out[camelCase(FIELDS.UPDATED_BY)] = userId;
1294
+ }
1295
+ }
1296
+ return out;
1297
+ }
1276
1298
  /**
1277
1299
  * Increments numeric field(s) on records matching the `where` filter.
1278
1300
  * Uses the database's native INCREMENT operation rather than a read-modify-write,
1279
1301
  * making it safe for concurrent requests.
1280
1302
  *
1281
1303
  * `params` should be an object mapping field names to the increment amounts
1282
- * (e.g. `{ viewCount: 1 }`). `transformOnUpdate()` is still called, so
1283
- * audit fields (updatedAt, updatedBy) and version are updated as normal.
1304
+ * (e.g. `{ viewCount: 1 }`). ONLY those caller fields are incremented; audit
1305
+ * fields (updatedAt / updatedBy) are applied as normal SET assignments so
1306
+ * timestamp / uuid columns are never fed through the counter (which would fail
1307
+ * with `operator does not exist`). Knex compiles the chained increment + update
1308
+ * into a single UPDATE ... SET.
1284
1309
  *
1285
1310
  * Throws if no filter conditions are applied (same safety guard as `update()`).
1286
1311
  * Clears affected records from the loader cache after the operation.
1287
1312
  */
1288
1313
  async increment(where, params) {
1289
- const object = await this.transformOnUpdate(params, where);
1290
- const query = this.getBuilder().increment(object).returning(this.config.returnFields);
1314
+ let query = this.getBuilder().increment({ ...params });
1315
+ const audit = this.buildAuditFieldsForCounter();
1316
+ if (Object.keys(audit).length > 0) {
1317
+ query = query.update(audit);
1318
+ }
1319
+ query = query.returning(this.config.returnFields);
1291
1320
  const filtersAppliedQty = await this.onUpdateFilter(query, where);
1292
1321
  if (filtersAppliedQty == null || filtersAppliedQty <= 0) {
1293
1322
  throw new Error(`Failed to increment in table '${this.config.table}': no filters applied. ` +
@@ -1308,11 +1337,17 @@ class BaseGateway {
1308
1337
  * Decrements numeric field(s) on records matching the `where` filter.
1309
1338
  * Mirror of `increment()` using the database's native DECREMENT operation.
1310
1339
  *
1311
- * Same behaviour, safety guards, and cache invalidation as `increment()`.
1340
+ * Same behaviour, safety guards, and cache invalidation as `increment()` —
1341
+ * caller fields only are decremented; audit fields are SET, not fed through the
1342
+ * counter.
1312
1343
  */
1313
1344
  async decrement(where, params) {
1314
- const object = await this.transformOnUpdate(params, where);
1315
- const query = this.getBuilder().decrement(object).returning(this.config.returnFields);
1345
+ let query = this.getBuilder().decrement({ ...params });
1346
+ const audit = this.buildAuditFieldsForCounter();
1347
+ if (Object.keys(audit).length > 0) {
1348
+ query = query.update(audit);
1349
+ }
1350
+ query = query.returning(this.config.returnFields);
1316
1351
  const filtersAppliedQty = await this.onUpdateFilter(query, where);
1317
1352
  if (filtersAppliedQty == null || filtersAppliedQty <= 0) {
1318
1353
  throw new Error(`Failed to decrement in table '${this.config.table}': no filters applied. ` +
@@ -83,6 +83,11 @@ export const createContextDefault = async (args) => {
83
83
  authorization,
84
84
  fingerprint,
85
85
  authorizationToken,
86
+ // Parsed request cookies must be exposed on the context so callers can read
87
+ // `context.cookies` (e.g. cookie-based flows that don't use the JWT). This
88
+ // was dropped from sharedContext in an earlier refactor, leaving
89
+ // `context.cookies` undefined even though the cookie was parsed.
90
+ cookies,
86
91
  isExternalDb: !!inDb,
87
92
  // Rate limiting — undefined values are omitted so context stays clean when
88
93
  // neither is configured.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sdflc/backend-helpers",
3
- "version": "1.0.135",
3
+ "version": "1.0.137",
4
4
  "description": "Set of helper functions and classes to simplify building backend apps within @sdflc ecosystem",
5
5
  "type": "commonjs",
6
6
  "exports": {