@sdflc/backend-helpers 1.0.133 → 1.0.135
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/README.md +151 -58
- package/build/BaseCore.d.ts +34 -0
- package/build/BaseCore.js +93 -11
- package/build/BaseGateway.d.ts +25 -0
- package/build/BaseGateway.js +114 -2
- package/build/esm/BaseCore.d.ts +34 -0
- package/build/esm/BaseCore.js +93 -11
- package/build/esm/BaseGateway.d.ts +25 -0
- package/build/esm/BaseGateway.js +115 -3
- package/build/esm/interfaces/BaseGatewayPropsInterface.d.ts +33 -0
- package/build/interfaces/BaseGatewayPropsInterface.d.ts +33 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -778,40 +778,50 @@ export { NoteGw };
|
|
|
778
778
|
|
|
779
779
|
#### Gateway config flags
|
|
780
780
|
|
|
781
|
-
| Flag | Type | Description
|
|
782
|
-
| ---------------------- | --------------------- |
|
|
783
|
-
| `table` | `string` | Database table name
|
|
784
|
-
| `idField` | `string` | Primary key for SELECT/loader — table-qualify for joined queries (default: `id`)
|
|
785
|
-
| `idFieldAs` | `string` | Alias for the id column when it differs from the lookup key
|
|
786
|
-
| `idFieldUpdateRemove` | `string` | Primary key for UPDATE/DELETE WHERE — must be unqualified
|
|
787
|
-
| `
|
|
788
|
-
| `
|
|
789
|
-
| `
|
|
790
|
-
| `
|
|
791
|
-
| `
|
|
792
|
-
| `
|
|
793
|
-
| `
|
|
794
|
-
| `
|
|
795
|
-
| `
|
|
796
|
-
| `
|
|
797
|
-
| `
|
|
798
|
-
| `
|
|
799
|
-
| `
|
|
800
|
-
| `
|
|
801
|
-
| `
|
|
802
|
-
| `
|
|
803
|
-
| `
|
|
804
|
-
| `
|
|
805
|
-
| `
|
|
806
|
-
| `
|
|
807
|
-
| `
|
|
808
|
-
| `
|
|
809
|
-
| `
|
|
810
|
-
| `
|
|
811
|
-
| `
|
|
812
|
-
| `
|
|
813
|
-
| `
|
|
814
|
-
| `
|
|
781
|
+
| Flag | Type | Description |
|
|
782
|
+
| ---------------------- | --------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
783
|
+
| `table` | `string` | Database table name |
|
|
784
|
+
| `idField` | `string` | Primary key for SELECT/loader — table-qualify for joined queries (default: `id`) |
|
|
785
|
+
| `idFieldAs` | `string` | Alias for the id column when it differs from the lookup key |
|
|
786
|
+
| `idFieldUpdateRemove` | `string` | Primary key for UPDATE/DELETE WHERE — must be unqualified |
|
|
787
|
+
| `statusField` | `string` | Column name for the status field (default: `'status'`). Set to `'Status_ID'` etc. on legacy tables — affects all status WHERE clauses and the soft-delete patch |
|
|
788
|
+
| `setFields` | `string \| string[]` | Conflict target field(s) for the `set()` upsert operation |
|
|
789
|
+
| `mergeFields` | `string[]` | Explicit field list for the ON CONFLICT MERGE clause |
|
|
790
|
+
| `hasStatus` | `boolean` | Status filtering and REMOVED guard |
|
|
791
|
+
| `hasCreatedAt` | `boolean` | Auto-set `created_at` on insert |
|
|
792
|
+
| `hasUpdatedAt` | `boolean` | Auto-set `updated_at` on update |
|
|
793
|
+
| `hasRemovedAt` | `boolean` | Soft-delete via `removed_at` timestamp |
|
|
794
|
+
| `hasRemovedAtStr` | `boolean` | Also writes a formatted string timestamp to `removed_at_str` |
|
|
795
|
+
| `hasCreatedBy` | `boolean` | Auto-set `created_by` from `context.userId` on insert |
|
|
796
|
+
| `hasUpdatedBy` | `boolean` | Auto-set `updated_by` from `context.userId` on update |
|
|
797
|
+
| `hasRemovedBy` | `boolean` | Auto-set `removed_by` from `context.userId` on soft-delete |
|
|
798
|
+
| `hasSpace` | `boolean` | Scopes reads/writes to `context.spaceId` |
|
|
799
|
+
| `hasUserId` | `boolean` | Sets `userId` from context on create |
|
|
800
|
+
| `hasVersion` | `boolean` | Optimistic locking via version field |
|
|
801
|
+
| `hasLang` | `boolean` | Language-scoped records |
|
|
802
|
+
| `localizationForField` | `string` | Field combined with `lang` to build a composite localisation id |
|
|
803
|
+
| `hardRemove` | `boolean` | Hard DELETE instead of soft-delete |
|
|
804
|
+
| `noCache` | `boolean` | Bypass DataLoader cache |
|
|
805
|
+
| `returnFields` | `string[]` | Columns in RETURNING clause (default: `['*']`) |
|
|
806
|
+
| `defaultSorting` | `SortItemInterface[]` | Default ORDER BY when none provided |
|
|
807
|
+
| `filterByUserField` | `string` | Auto-scope list queries to `context.userId` |
|
|
808
|
+
| `fieldForKeyword` | `string` | Column to apply keyword ILIKE/LIKE search against |
|
|
809
|
+
| `selectFields` | `string[]` | Columns selected when `useCache: false` (default: `['*']`) |
|
|
810
|
+
| `activeStatuses` | `number[]` | Status values considered active for list filtering |
|
|
811
|
+
| `insertChunkSize` | `number` | Records per INSERT batch — prevents DB parameter limit errors (default: `2500`) |
|
|
812
|
+
| `listFilters` | `GatewayFilterRule[]` | Declarative WHERE rules for `list()` — see [Declarative filter rules](#declarative-filter-rules) |
|
|
813
|
+
| `updateFilters` | `GatewayFilterRule[]` | Declarative WHERE rules for `update()` |
|
|
814
|
+
| `removeFilters` | `GatewayFilterRule[]` | Declarative WHERE rules for `remove()` |
|
|
815
|
+
| `countFilters` | `GatewayFilterRule[]` | Declarative WHERE rules for `count()` — falls back to `listFilters` when not set |
|
|
816
|
+
| `nonCreatableFields` | `string[]` | Field names callers cannot set on create — stripped before INSERT. snake_case or camelCase both accepted |
|
|
817
|
+
| `nonUpdatableFields` | `string[]` | Field names callers cannot change on update — stripped before UPDATE. snake_case or camelCase both accepted |
|
|
818
|
+
|
|
819
|
+
> **Implicit write protection.** In addition to `nonCreatableFields` / `nonUpdatableFields`, the framework always strips a baseline set of fields the framework owns:
|
|
820
|
+
>
|
|
821
|
+
> - **On create** — `id`, `removedAt`, `removedBy` (the framework generates `id` via `generateId()`; removal markers only belong on the remove path)
|
|
822
|
+
> - **On update** — `id`, `createdAt`, `createdBy`, `removedAt`, `removedBy` (write-once and remove-only markers)
|
|
823
|
+
>
|
|
824
|
+
> This implicit stripping happens regardless of whether `nonCreatableFields` / `nonUpdatableFields` are set. Use those arrays only for additional resource-specific immutables (e.g. `partyId` on a junction table whose identity is fixed at creation time). |
|
|
815
825
|
|
|
816
826
|
#### Filter hooks
|
|
817
827
|
|
|
@@ -825,6 +835,52 @@ export { NoteGw };
|
|
|
825
835
|
|
|
826
836
|
Always call `super.onListFilter()` / `super.onUpdateFilter()` / `super.onRemoveFilter()` at the top of any override so base-class scoping (soft-delete, space, user) runs first.
|
|
827
837
|
|
|
838
|
+
**Soft-delete guards on writes.** When `hasRemovedAt: true`, the base implementations of `onUpdateFilter` and `onRemoveFilter` automatically add `WHERE table.removed_at IS NULL`. This guard is independent of `hasStatus` — it fires on tables that use `Removed_At` without the framework's standard `status` column (a common shape for PartyLabz tables, where `hasStatus` is `false` and the row's lifecycle is tracked through `Status_ID` + `Removed_At` instead).
|
|
839
|
+
|
|
840
|
+
The guard prevents two failure modes silently breaking writes:
|
|
841
|
+
|
|
842
|
+
- An `update()` on an already-removed row would otherwise produce a half-mutated record (new `updated_at` and `updated_by`, original `removed_at` still set).
|
|
843
|
+
- A `remove()` on an already-removed row would otherwise re-write `removed_at` and `removed_by` with a fresh timestamp, masking the fact that the row was removed earlier.
|
|
844
|
+
**Configurable status column.** When `statusField` is set in the config (e.g. `statusField: 'Status_ID'`), every status-related WHERE clause in `onListFilter`, `onUpdateFilter`, `onRemoveFilter`, and the soft-delete patch in `transformOnRemove` uses that column name. Set this once on legacy tables that do not use the framework's default `status` column.
|
|
845
|
+
|
|
846
|
+
#### Field-level write protection
|
|
847
|
+
|
|
848
|
+
`BaseGateway` strips fields that callers should not be able to set from `params` before the INSERT or UPDATE statement runs. Two layers cooperate:
|
|
849
|
+
|
|
850
|
+
**Implicit framework-managed fields** are always stripped, regardless of configuration. These are columns the framework owns and either generates itself or sets only on a specific path.
|
|
851
|
+
|
|
852
|
+
| Path | Always stripped |
|
|
853
|
+
| -------- | -------------------------------------------------------- |
|
|
854
|
+
| `create` | `id`, `removedAt`, `removedBy` |
|
|
855
|
+
| `update` | `id`, `createdAt`, `createdBy`, `removedAt`, `removedBy` |
|
|
856
|
+
|
|
857
|
+
`removedAt` and `removedBy` are only included when their `has*` flags are set on the gateway. `id` is always stripped — `BaseGateway.generateId()` produces it.
|
|
858
|
+
|
|
859
|
+
**Gateway-configured immutables** are declared per-gateway via `nonCreatableFields` and `nonUpdatableFields`. Names can be supplied in `snake_case` (DB column form) or `camelCase` (VM/API form) — both spellings are stripped from the params object so callers can be consistent with whichever convention they prefer.
|
|
860
|
+
|
|
861
|
+
```typescript
|
|
862
|
+
class PartyGuestGw extends BaseGateway {
|
|
863
|
+
constructor(props: BaseGatewayPropsInterface) {
|
|
864
|
+
super({
|
|
865
|
+
...props,
|
|
866
|
+
table: 'Party_Guest',
|
|
867
|
+
hasStatus: false, // PartyLabz uses Status_ID
|
|
868
|
+
statusField: 'Status_ID',
|
|
869
|
+
|
|
870
|
+
// partyId / personId define the row's identity. Once a guest record
|
|
871
|
+
// exists in a party, those values must never change — only the RSVP
|
|
872
|
+
// state, notes, and audit fields evolve. Stripping them on update
|
|
873
|
+
// ensures a malformed client payload cannot reassign a guest.
|
|
874
|
+
nonUpdatableFields: ['partyId', 'personId'],
|
|
875
|
+
});
|
|
876
|
+
}
|
|
877
|
+
}
|
|
878
|
+
```
|
|
879
|
+
|
|
880
|
+
**Logging.** Every stripped field is logged at `debug` level with the table name and field name, so it is easy to confirm during troubleshooting that a "ignored" client value was, in fact, dropped by the framework rather than silently overwritten.
|
|
881
|
+
|
|
882
|
+
**Why this matters.** Without this protection, a request like `update(where, { id: 'evil', createdBy: 'attacker', myField: 'ok' })` would attempt to rewrite framework-owned columns. The framework strips `id` and `createdBy` automatically; the gateway author can extend the strip-list to cover any additional columns whose values must come from the server, not the client.
|
|
883
|
+
|
|
828
884
|
---
|
|
829
885
|
|
|
830
886
|
#### Declarative filter rules
|
|
@@ -889,6 +945,13 @@ const total = await this.gateway.count({ partyId: args.partyId, rsvpStatus: [1,
|
|
|
889
945
|
|
|
890
946
|
Gateways that already declare `listFilters` get `count()` for free with no additional configuration. Set `countFilters` explicitly only when count needs different scoping than list.
|
|
891
947
|
|
|
948
|
+
**Soft-delete guard order.** `count()` adds the soft-delete guard automatically based on the gateway's flags:
|
|
949
|
+
|
|
950
|
+
1. When `hasRemovedBy: true` → `WHERE table.removed_by IS NULL`
|
|
951
|
+
2. Otherwise, when `hasRemovedAt: true` → `WHERE table.removed_at IS NULL`
|
|
952
|
+
3. Otherwise — no soft-delete guard
|
|
953
|
+
`Removed_By IS NULL` is preferred because, in this codebase, `Removed_By` and `Removed_At` are always set together on the remove path and `Removed_By` is the primary sentinel. The fallback to `Removed_At` covers tables that have a timestamp column but no author column (uncommon but valid).
|
|
954
|
+
|
|
892
955
|
To add custom WHERE logic beyond what declarative rules express, override `onCountFilter()`:
|
|
893
956
|
|
|
894
957
|
```typescript
|
|
@@ -1324,37 +1387,67 @@ try {
|
|
|
1324
1387
|
`DataCache` is the default per-request DataLoader-backed cache. You can provide a **custom loader** when you need non-standard fetching — for example enriching records with a JOIN.
|
|
1325
1388
|
|
|
1326
1389
|
```typescript
|
|
1327
|
-
// src/gateways/NoteGw.ts
|
|
1328
|
-
import { BaseGateway, BaseGatewayPropsInterface
|
|
1390
|
+
// src/gateways/NoteGw.ts
|
|
1391
|
+
import { BaseGateway, BaseGatewayPropsInterface } from '@sdflc/backend-helpers';
|
|
1329
1392
|
|
|
1330
1393
|
class NoteGw extends BaseGateway {
|
|
1331
1394
|
constructor(props: BaseGatewayPropsInterface) {
|
|
1332
|
-
super({
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
|
|
1395
|
+
super({
|
|
1396
|
+
...props,
|
|
1397
|
+
table: 'note',
|
|
1398
|
+
keyPrefix: 'note',
|
|
1399
|
+
hasStatus: true,
|
|
1400
|
+
// statusField: 'Status_ID', // ← uncomment for legacy tables that don't use 'status'
|
|
1401
|
+
hasCreatedAt: true,
|
|
1402
|
+
hasUpdatedAt: true,
|
|
1403
|
+
hasRemovedAt: true, // soft-delete
|
|
1404
|
+
hasCreatedBy: true,
|
|
1405
|
+
hasUpdatedBy: true,
|
|
1406
|
+
hasUserId: true,
|
|
1407
|
+
selectFields: ['note.*'],
|
|
1408
|
+
idField: 'note.id',
|
|
1409
|
+
idFieldUpdateRemove: 'id',
|
|
1410
|
+
defaultSorting: [{ name: 'created_at', order: 'desc' }],
|
|
1411
|
+
|
|
1412
|
+
// Declarative write protection — these fields are stripped from
|
|
1413
|
+
// the params object before INSERT / UPDATE runs. id, audit, and
|
|
1414
|
+
// removal fields are stripped automatically by the framework, so
|
|
1415
|
+
// these arrays are for additional resource-specific immutables only.
|
|
1416
|
+
nonCreatableFields: ['accountId'], // server-derived, never client-supplied
|
|
1417
|
+
nonUpdatableFields: ['userId'], // ownership cannot change after creation
|
|
1418
|
+
});
|
|
1344
1419
|
}
|
|
1345
1420
|
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
.
|
|
1421
|
+
async onListFilter(query: any, filterParams: any): Promise<void> {
|
|
1422
|
+
await super.onListFilter(query, filterParams);
|
|
1423
|
+
|
|
1424
|
+
const { userId, title } = filterParams || {};
|
|
1425
|
+
|
|
1426
|
+
if (userId) {
|
|
1427
|
+
query.where('note.user_id', userId);
|
|
1428
|
+
}
|
|
1353
1429
|
|
|
1354
|
-
|
|
1355
|
-
|
|
1430
|
+
if (title) {
|
|
1431
|
+
query.whereILike('note.title', `%${title}%`);
|
|
1432
|
+
}
|
|
1433
|
+
}
|
|
1434
|
+
|
|
1435
|
+
// BaseGateway.onUpdateFilter already adds the soft-delete guard
|
|
1436
|
+
// (WHERE removed_at IS NULL) when hasRemovedAt is true, so we don't
|
|
1437
|
+
// need to add it here. Just call super and add resource-specific scoping.
|
|
1438
|
+
async onUpdateFilter(query: any, whereParams: any): Promise<number> {
|
|
1439
|
+
let count = await super.onUpdateFilter(query, whereParams);
|
|
1440
|
+
|
|
1441
|
+
if (whereParams?.userId != null) {
|
|
1442
|
+
count++;
|
|
1443
|
+
query.where({ user_id: whereParams.userId });
|
|
1444
|
+
}
|
|
1445
|
+
|
|
1446
|
+
return count;
|
|
1356
1447
|
}
|
|
1357
1448
|
}
|
|
1449
|
+
|
|
1450
|
+
export { NoteGw };
|
|
1358
1451
|
```
|
|
1359
1452
|
|
|
1360
1453
|
You can also swap loaders at runtime:
|
|
@@ -1938,7 +2031,7 @@ ORDER_BYS.DESC; // 'desc'
|
|
|
1938
2031
|
| `ApiContextInterface` | Arguments for `createApiContext()` |
|
|
1939
2032
|
| `BaseCorePropsInterface` | Constructor props for `BaseCore` subclasses |
|
|
1940
2033
|
| `BaseCoreRateLimitOnActionInterface` | Per-action rate-limit rule map used in `BaseCorePropsInterface` |
|
|
1941
|
-
| `BaseGatewayPropsInterface` | Constructor props for `BaseGateway` subclasses
|
|
2034
|
+
| `BaseGatewayPropsInterface` | Constructor props for `BaseGateway` subclasses — includes `statusField`, `nonCreatableFields`, `nonUpdatableFields`, and the filter rule arrays |
|
|
1942
2035
|
| `BaseExternalApiGatewayPropsInterface` | Constructor props for `BaseExternalApiGateway` |
|
|
1943
2036
|
| `BaseRestApiCrudControllerPropsInterface` | Constructor props for `BaseRestApiCrudController` |
|
|
1944
2037
|
| `BuildDefaultResolvers` | Arguments for `buildDefaultResolvers()` |
|
package/build/BaseCore.d.ts
CHANGED
|
@@ -659,6 +659,40 @@ declare class BaseCore {
|
|
|
659
659
|
* @param args.gateway Gateway instance to query for the current max orderNo
|
|
660
660
|
*/
|
|
661
661
|
setOrderNo(args: any): Promise<void>;
|
|
662
|
+
/**
|
|
663
|
+
* Lifts an optimistic-concurrency `version` value from update params into
|
|
664
|
+
* the where filter.
|
|
665
|
+
*
|
|
666
|
+
* GraphQL clients typically send `version` inside the params payload (the
|
|
667
|
+
* UI reads it on get/list, then sends it back on update so concurrent
|
|
668
|
+
* edits are detected). The gateway, however, expects `version` in the
|
|
669
|
+
* where clause so it can be added to the UPDATE's WHERE. This helper
|
|
670
|
+
* bridges the two conventions:
|
|
671
|
+
*
|
|
672
|
+
* - When `params.version` is present and `where.version` is absent,
|
|
673
|
+
* copy params.version → where.version.
|
|
674
|
+
* - The original params.version is intentionally LEFT IN PLACE.
|
|
675
|
+
* transformOnUpdate() in the gateway only increments version when it
|
|
676
|
+
* is already present in params; stripping it would skip the bump and
|
|
677
|
+
* leave the version column stale.
|
|
678
|
+
* - When `where.version` is already populated, params.version is
|
|
679
|
+
* ignored with a debug log. The where value wins so older code paths
|
|
680
|
+
* that populate version explicitly are preserved unchanged.
|
|
681
|
+
* - When neither is set, the helper is a no-op.
|
|
682
|
+
*
|
|
683
|
+
* The pair operates as expected end-to-end:
|
|
684
|
+
*
|
|
685
|
+
* client { where: {id}, params: {..., version: 7} }
|
|
686
|
+
* → after lift: where = {id, version: 7}, params = {..., version: 7}
|
|
687
|
+
* → transformOnUpdate increments params.version to 8
|
|
688
|
+
* → onUpdateFilter adds WHERE version = 7
|
|
689
|
+
* → UPDATE ... SET version = 8 WHERE id = ... AND version = 7
|
|
690
|
+
*
|
|
691
|
+
* @param args The action args object — typically { where, params, ... }
|
|
692
|
+
* Mutated in place: where is initialised to {} if absent so
|
|
693
|
+
* the lift target always exists.
|
|
694
|
+
*/
|
|
695
|
+
private liftVersionFromParamsToWhere;
|
|
662
696
|
/**
|
|
663
697
|
* Stores a typed value in the action-scoped request store.
|
|
664
698
|
*
|
package/build/BaseCore.js
CHANGED
|
@@ -743,7 +743,7 @@ class BaseCore {
|
|
|
743
743
|
action: async (args, opt) => {
|
|
744
744
|
const object = await this.beforeList(args, opt);
|
|
745
745
|
if ((0, api_helpers_1.isOpResult)(object)) {
|
|
746
|
-
return object;
|
|
746
|
+
return object.toJS();
|
|
747
747
|
}
|
|
748
748
|
const items = await this.getGateway()?.list(object);
|
|
749
749
|
const result = await this.afterList(items, opt);
|
|
@@ -773,15 +773,20 @@ class BaseCore {
|
|
|
773
773
|
action: async (args, opt) => {
|
|
774
774
|
const args2use = await this.beforeGet(args, opt);
|
|
775
775
|
if ((0, api_helpers_1.isOpResult)(args2use)) {
|
|
776
|
-
return args2use;
|
|
776
|
+
return args2use.toJS();
|
|
777
777
|
}
|
|
778
778
|
if (args2use?.id == null) {
|
|
779
779
|
return this.failure(api_helpers_1.OP_RESULT_CODES.NOT_FOUND, '', []);
|
|
780
780
|
}
|
|
781
781
|
const item = await this.getGateway()?.get(args2use.id);
|
|
782
|
+
if (this.getGateway()?.getConfig()?.hasRemovedAt === true) {
|
|
783
|
+
if (!item || item.removedAt) {
|
|
784
|
+
return this.failure(api_helpers_1.OP_RESULT_CODES.NOT_FOUND, `${this.config.name} not found`, []);
|
|
785
|
+
}
|
|
786
|
+
}
|
|
782
787
|
const result = await this.afterGet(item, opt);
|
|
783
788
|
if (!result) {
|
|
784
|
-
return this.failure(api_helpers_1.OP_RESULT_CODES.NOT_FOUND,
|
|
789
|
+
return this.failure(api_helpers_1.OP_RESULT_CODES.NOT_FOUND, `${this.config.name} not found`, []);
|
|
785
790
|
}
|
|
786
791
|
return this.success([result]);
|
|
787
792
|
},
|
|
@@ -809,12 +814,19 @@ class BaseCore {
|
|
|
809
814
|
action: async (args, opt) => {
|
|
810
815
|
const args2use = await this.beforeGetMany(args, opt);
|
|
811
816
|
if ((0, api_helpers_1.isOpResult)(args2use)) {
|
|
812
|
-
return args2use;
|
|
817
|
+
return args2use.toJS();
|
|
813
818
|
}
|
|
814
819
|
if (!Array.isArray(args2use?.ids)) {
|
|
815
820
|
return this.failure(api_helpers_1.OP_RESULT_CODES.NOT_FOUND, 'Expected ids array but received invalid input', []);
|
|
816
821
|
}
|
|
817
822
|
const items = await this.getGateway()?.getMany(args2use.ids);
|
|
823
|
+
if (this.getGateway()?.getConfig()?.hasRemovedAt === true) {
|
|
824
|
+
items.forEach((item, index) => {
|
|
825
|
+
if (!item || item.removedAt) {
|
|
826
|
+
items[index] = null;
|
|
827
|
+
}
|
|
828
|
+
});
|
|
829
|
+
}
|
|
818
830
|
const result = await this.afterGetMany(items, opt);
|
|
819
831
|
return this.success(result);
|
|
820
832
|
},
|
|
@@ -861,7 +873,7 @@ class BaseCore {
|
|
|
861
873
|
}
|
|
862
874
|
const object = await this.beforeCreate(params, opt);
|
|
863
875
|
if ((0, api_helpers_1.isOpResult)(object)) {
|
|
864
|
-
return object;
|
|
876
|
+
return object.toJS();
|
|
865
877
|
}
|
|
866
878
|
const items = await this.getGateway()?.create(object);
|
|
867
879
|
const result = await this.afterCreate(items, opt);
|
|
@@ -889,7 +901,7 @@ class BaseCore {
|
|
|
889
901
|
const { params } = args || {};
|
|
890
902
|
const objects = await this.beforeCreateMany(params, opt);
|
|
891
903
|
if ((0, api_helpers_1.isOpResult)(objects)) {
|
|
892
|
-
return objects;
|
|
904
|
+
return objects.toJS();
|
|
893
905
|
}
|
|
894
906
|
const items = await this.getGateway()?.create(objects);
|
|
895
907
|
const result = await this.afterCreateMany(items, opt);
|
|
@@ -917,10 +929,14 @@ class BaseCore {
|
|
|
917
929
|
validate: this.getValidators().update,
|
|
918
930
|
accessRights: [utils_1.ACCESS_RIGHTS.UPDATE],
|
|
919
931
|
action: async (args, opt) => {
|
|
932
|
+
// Lift version from params into where for optimistic-concurrency.
|
|
933
|
+
// Mutates args in place — done BEFORE destructuring so the
|
|
934
|
+
// local where binding picks up the lifted value.
|
|
935
|
+
this.liftVersionFromParamsToWhere(args);
|
|
920
936
|
const { where, params } = args || {};
|
|
921
937
|
const object = await this.beforeUpdate(params, opt);
|
|
922
938
|
if ((0, api_helpers_1.isOpResult)(object)) {
|
|
923
|
-
return object;
|
|
939
|
+
return object.toJS();
|
|
924
940
|
}
|
|
925
941
|
const items = await this.getGateway()?.update(where, object);
|
|
926
942
|
const result = await this.afterUpdate(items, opt);
|
|
@@ -946,9 +962,18 @@ class BaseCore {
|
|
|
946
962
|
accessRights: [utils_1.ACCESS_RIGHTS.UPDATE],
|
|
947
963
|
action: async (args, opt) => {
|
|
948
964
|
const { params } = args || {};
|
|
965
|
+
// Lift version from params into where for optimistic-concurrency,
|
|
966
|
+
// per pair. Mutates each pair in place. Tolerates a non-array
|
|
967
|
+
// params input — beforeUpdateMany handles the normalisation
|
|
968
|
+
// and a non-array shape will simply be skipped here.
|
|
969
|
+
if (Array.isArray(params)) {
|
|
970
|
+
for (const pair of params) {
|
|
971
|
+
this.liftVersionFromParamsToWhere(pair);
|
|
972
|
+
}
|
|
973
|
+
}
|
|
949
974
|
const object = await this.beforeUpdateMany(params, opt);
|
|
950
975
|
if ((0, api_helpers_1.isOpResult)(object)) {
|
|
951
|
-
return object;
|
|
976
|
+
return object.toJS();
|
|
952
977
|
}
|
|
953
978
|
const items = await this.getGateway()?.updateMany(object);
|
|
954
979
|
const result = await this.afterUpdateMany(items, opt);
|
|
@@ -977,7 +1002,7 @@ class BaseCore {
|
|
|
977
1002
|
const { params } = args || {};
|
|
978
1003
|
const object = await this.beforeSet(params, opt);
|
|
979
1004
|
if ((0, api_helpers_1.isOpResult)(object)) {
|
|
980
|
-
return object;
|
|
1005
|
+
return object.toJS();
|
|
981
1006
|
}
|
|
982
1007
|
const items = await this.getGateway()?.set(object);
|
|
983
1008
|
const result = await this.afterSet(items, opt);
|
|
@@ -1005,7 +1030,7 @@ class BaseCore {
|
|
|
1005
1030
|
const { where } = args || {};
|
|
1006
1031
|
const object = await this.beforeRemove(where, opt);
|
|
1007
1032
|
if ((0, api_helpers_1.isOpResult)(object)) {
|
|
1008
|
-
return object;
|
|
1033
|
+
return object.toJS();
|
|
1009
1034
|
}
|
|
1010
1035
|
const items = await this.getGateway()?.remove(object);
|
|
1011
1036
|
const result = await this.afterRemove(items, opt);
|
|
@@ -1033,7 +1058,7 @@ class BaseCore {
|
|
|
1033
1058
|
const { where } = args || {};
|
|
1034
1059
|
const object = await this.beforeRemoveMany(where, opt);
|
|
1035
1060
|
if ((0, api_helpers_1.isOpResult)(object)) {
|
|
1036
|
-
return object;
|
|
1061
|
+
return object.toJS();
|
|
1037
1062
|
}
|
|
1038
1063
|
const items = await this.getGateway()?.remove(object);
|
|
1039
1064
|
const result = await this.afterRemoveMany(items, opt);
|
|
@@ -1487,6 +1512,63 @@ class BaseCore {
|
|
|
1487
1512
|
}
|
|
1488
1513
|
}
|
|
1489
1514
|
}
|
|
1515
|
+
// ─── Update-version helpers ────────────────────────────────────────────────
|
|
1516
|
+
/**
|
|
1517
|
+
* Lifts an optimistic-concurrency `version` value from update params into
|
|
1518
|
+
* the where filter.
|
|
1519
|
+
*
|
|
1520
|
+
* GraphQL clients typically send `version` inside the params payload (the
|
|
1521
|
+
* UI reads it on get/list, then sends it back on update so concurrent
|
|
1522
|
+
* edits are detected). The gateway, however, expects `version` in the
|
|
1523
|
+
* where clause so it can be added to the UPDATE's WHERE. This helper
|
|
1524
|
+
* bridges the two conventions:
|
|
1525
|
+
*
|
|
1526
|
+
* - When `params.version` is present and `where.version` is absent,
|
|
1527
|
+
* copy params.version → where.version.
|
|
1528
|
+
* - The original params.version is intentionally LEFT IN PLACE.
|
|
1529
|
+
* transformOnUpdate() in the gateway only increments version when it
|
|
1530
|
+
* is already present in params; stripping it would skip the bump and
|
|
1531
|
+
* leave the version column stale.
|
|
1532
|
+
* - When `where.version` is already populated, params.version is
|
|
1533
|
+
* ignored with a debug log. The where value wins so older code paths
|
|
1534
|
+
* that populate version explicitly are preserved unchanged.
|
|
1535
|
+
* - When neither is set, the helper is a no-op.
|
|
1536
|
+
*
|
|
1537
|
+
* The pair operates as expected end-to-end:
|
|
1538
|
+
*
|
|
1539
|
+
* client { where: {id}, params: {..., version: 7} }
|
|
1540
|
+
* → after lift: where = {id, version: 7}, params = {..., version: 7}
|
|
1541
|
+
* → transformOnUpdate increments params.version to 8
|
|
1542
|
+
* → onUpdateFilter adds WHERE version = 7
|
|
1543
|
+
* → UPDATE ... SET version = 8 WHERE id = ... AND version = 7
|
|
1544
|
+
*
|
|
1545
|
+
* @param args The action args object — typically { where, params, ... }
|
|
1546
|
+
* Mutated in place: where is initialised to {} if absent so
|
|
1547
|
+
* the lift target always exists.
|
|
1548
|
+
*/
|
|
1549
|
+
liftVersionFromParamsToWhere(args) {
|
|
1550
|
+
if (!args || typeof args !== 'object')
|
|
1551
|
+
return;
|
|
1552
|
+
const params = args.params;
|
|
1553
|
+
if (!params || typeof params !== 'object')
|
|
1554
|
+
return;
|
|
1555
|
+
const versionInParams = params.version;
|
|
1556
|
+
if (versionInParams == null)
|
|
1557
|
+
return;
|
|
1558
|
+
// Initialise where if absent — gives the lift somewhere to land.
|
|
1559
|
+
if (!args.where || typeof args.where !== 'object') {
|
|
1560
|
+
args.where = {};
|
|
1561
|
+
}
|
|
1562
|
+
if (args.where.version != null) {
|
|
1563
|
+
// Where wins. params.version is left in place — transformOnUpdate
|
|
1564
|
+
// will still see it and bump it, which is the desired outcome.
|
|
1565
|
+
this.logger.debug(`Update — both where.version (${args.where.version}) and params.version (${versionInParams}) ` +
|
|
1566
|
+
`were supplied; where.version wins (params.version left in payload for the increment).`);
|
|
1567
|
+
return;
|
|
1568
|
+
}
|
|
1569
|
+
args.where.version = versionInParams;
|
|
1570
|
+
this.logger.debug(`Update — lifted version=${versionInParams} from params into where for optimistic-concurrency check.`);
|
|
1571
|
+
}
|
|
1490
1572
|
// ─── Action-scoped request store helpers ──────────────────────────────────
|
|
1491
1573
|
// These helpers provide typed access to the per-action requestStore carried
|
|
1492
1574
|
// in BaseCoreActionsInterface.opt. Use them in any before*, after*, validate,
|
package/build/BaseGateway.d.ts
CHANGED
|
@@ -311,6 +311,31 @@ declare class BaseGateway {
|
|
|
311
311
|
* path — they are skipped with a warning log.
|
|
312
312
|
*/
|
|
313
313
|
private applyDeclarativeRemoveFilters;
|
|
314
|
+
/**
|
|
315
|
+
* Returns the full set of field names (camelCase + snake_case) that
|
|
316
|
+
* should be stripped from a params object on the given write path.
|
|
317
|
+
*
|
|
318
|
+
* Combines two sources:
|
|
319
|
+
* 1. The framework's implicit immutables, derived from has* flags:
|
|
320
|
+
* - 'create' → id, removedAt, removedBy
|
|
321
|
+
* - 'update' → id, createdAt, createdBy, removedAt, removedBy
|
|
322
|
+
* 2. The gateway's own nonCreatableFields / nonUpdatableFields config.
|
|
323
|
+
*
|
|
324
|
+
* For each source name, both snake_case (matches DB / FIELDS constants)
|
|
325
|
+
* and camelCase (matches VM / API params) are added so callers do not
|
|
326
|
+
* have to be consistent with one convention.
|
|
327
|
+
*
|
|
328
|
+
* Returns a Set for O(1) lookup inside the strip loop.
|
|
329
|
+
*/
|
|
330
|
+
private buildNonWritableFieldSet;
|
|
331
|
+
/**
|
|
332
|
+
* Strips every key whose name appears in `nonWritable` from `params`.
|
|
333
|
+
* Logs each strip at debug level so audit / troubleshooting can see
|
|
334
|
+
* exactly which client-supplied keys were ignored.
|
|
335
|
+
*
|
|
336
|
+
* Mutates and returns `params` for chaining.
|
|
337
|
+
*/
|
|
338
|
+
private stripNonWritableFields;
|
|
314
339
|
/**
|
|
315
340
|
* Generates a unique ID for a new record.
|
|
316
341
|
*
|
package/build/BaseGateway.js
CHANGED
|
@@ -575,6 +575,87 @@ class BaseGateway {
|
|
|
575
575
|
}
|
|
576
576
|
return count;
|
|
577
577
|
}
|
|
578
|
+
// ─── Write-protection helpers ──────────────────────────────────────────────
|
|
579
|
+
/**
|
|
580
|
+
* Returns the full set of field names (camelCase + snake_case) that
|
|
581
|
+
* should be stripped from a params object on the given write path.
|
|
582
|
+
*
|
|
583
|
+
* Combines two sources:
|
|
584
|
+
* 1. The framework's implicit immutables, derived from has* flags:
|
|
585
|
+
* - 'create' → id, removedAt, removedBy
|
|
586
|
+
* - 'update' → id, createdAt, createdBy, removedAt, removedBy
|
|
587
|
+
* 2. The gateway's own nonCreatableFields / nonUpdatableFields config.
|
|
588
|
+
*
|
|
589
|
+
* For each source name, both snake_case (matches DB / FIELDS constants)
|
|
590
|
+
* and camelCase (matches VM / API params) are added so callers do not
|
|
591
|
+
* have to be consistent with one convention.
|
|
592
|
+
*
|
|
593
|
+
* Returns a Set for O(1) lookup inside the strip loop.
|
|
594
|
+
*/
|
|
595
|
+
buildNonWritableFieldSet(path) {
|
|
596
|
+
const { hasCreatedAt, hasCreatedBy, hasRemovedAt, hasRemovedBy, nonCreatableFields, nonUpdatableFields } = this.config;
|
|
597
|
+
const fields = [];
|
|
598
|
+
// ── Framework-implicit immutables (always stripped) ──────────────────────
|
|
599
|
+
// id is always framework-managed via generateId(), so it must never be
|
|
600
|
+
// honoured from caller input on create OR update.
|
|
601
|
+
fields.push(this.getIdField().split('.').pop());
|
|
602
|
+
if (path === 'create') {
|
|
603
|
+
// Soft-delete markers are only set on the remove path. A caller
|
|
604
|
+
// sending them on create would record a "born already removed" row.
|
|
605
|
+
if (hasRemovedAt)
|
|
606
|
+
fields.push(constants_1.FIELDS.REMOVED_AT);
|
|
607
|
+
if (hasRemovedBy)
|
|
608
|
+
fields.push(constants_1.FIELDS.REMOVED_BY);
|
|
609
|
+
}
|
|
610
|
+
else {
|
|
611
|
+
// On update, creation-time markers are write-once and removal-time
|
|
612
|
+
// markers are managed by the remove path.
|
|
613
|
+
if (hasCreatedAt)
|
|
614
|
+
fields.push(constants_1.FIELDS.CREATED_AT);
|
|
615
|
+
if (hasCreatedBy)
|
|
616
|
+
fields.push(constants_1.FIELDS.CREATED_BY);
|
|
617
|
+
if (hasRemovedAt)
|
|
618
|
+
fields.push(constants_1.FIELDS.REMOVED_AT);
|
|
619
|
+
if (hasRemovedBy)
|
|
620
|
+
fields.push(constants_1.FIELDS.REMOVED_BY);
|
|
621
|
+
}
|
|
622
|
+
// ── Gateway-configured immutables ────────────────────────────────────────
|
|
623
|
+
const configured = path === 'create' ? nonCreatableFields : nonUpdatableFields;
|
|
624
|
+
if (Array.isArray(configured)) {
|
|
625
|
+
fields.push(...configured);
|
|
626
|
+
}
|
|
627
|
+
// ── Expand each entry to both snake_case and camelCase ───────────────────
|
|
628
|
+
// This way the gateway author can write columns the way they think of
|
|
629
|
+
// them (snake_case for DB-minded folks, camelCase for VM-minded folks)
|
|
630
|
+
// and both forms are stripped from incoming params.
|
|
631
|
+
const out = new Set();
|
|
632
|
+
for (const name of fields) {
|
|
633
|
+
if (typeof name !== 'string' || name.length === 0)
|
|
634
|
+
continue;
|
|
635
|
+
out.add(name);
|
|
636
|
+
out.add((0, lodash_1.camelCase)(name));
|
|
637
|
+
out.add((0, lodash_1.snakeCase)(name));
|
|
638
|
+
}
|
|
639
|
+
return out;
|
|
640
|
+
}
|
|
641
|
+
/**
|
|
642
|
+
* Strips every key whose name appears in `nonWritable` from `params`.
|
|
643
|
+
* Logs each strip at debug level so audit / troubleshooting can see
|
|
644
|
+
* exactly which client-supplied keys were ignored.
|
|
645
|
+
*
|
|
646
|
+
* Mutates and returns `params` for chaining.
|
|
647
|
+
*/
|
|
648
|
+
stripNonWritableFields(params, nonWritable, path) {
|
|
649
|
+
if (!params || typeof params !== 'object')
|
|
650
|
+
return params;
|
|
651
|
+
for (const key of Object.keys(params)) {
|
|
652
|
+
if (nonWritable.has(key)) {
|
|
653
|
+
this.logger.debug(`${this.config.table}: field "${key}" is not writable on ${path} and was stripped from the payload`);
|
|
654
|
+
delete params[key];
|
|
655
|
+
}
|
|
656
|
+
}
|
|
657
|
+
return params;
|
|
658
|
+
}
|
|
578
659
|
// ─── ID generation ─────────────────────────────────────────────────────────
|
|
579
660
|
/**
|
|
580
661
|
* Generates a unique ID for a new record.
|
|
@@ -663,7 +744,7 @@ class BaseGateway {
|
|
|
663
744
|
* - `updateFilters` — applies declarative GatewayFilterRule array (new)
|
|
664
745
|
*/
|
|
665
746
|
async onUpdateFilter(query, filterParams) {
|
|
666
|
-
const { hasStatus, hasVersion } = this.config;
|
|
747
|
+
const { table, hasStatus, hasVersion, hasRemovedAt } = this.config;
|
|
667
748
|
const idName = this.getIdFieldUpdateRemove();
|
|
668
749
|
let filtersAppliedQty = 0;
|
|
669
750
|
if (filterParams[idName]) {
|
|
@@ -686,6 +767,16 @@ class BaseGateway {
|
|
|
686
767
|
query.where(statusField, '!=', utils_1.STATUSES.REMOVED);
|
|
687
768
|
}
|
|
688
769
|
}
|
|
770
|
+
// Skip already-removed rows on soft-delete tables. Without this guard,
|
|
771
|
+
// a regular update call on a soft-deleted row would silently bring it
|
|
772
|
+
// back into a half-mutated state (new updatedAt / updatedBy, old
|
|
773
|
+
// Removed_At still set). For tables where hasStatus is true, the
|
|
774
|
+
// status-based REMOVED guard above already covers this; for PartyLabz
|
|
775
|
+
// tables (Status_Id, hasStatus: false) this is the only line of defence.
|
|
776
|
+
if (hasRemovedAt === true) {
|
|
777
|
+
filtersAppliedQty++;
|
|
778
|
+
query.whereNull(`${table}.${constants_1.FIELDS.REMOVED_AT}`);
|
|
779
|
+
}
|
|
689
780
|
// Declarative update filter rules — applied after base logic.
|
|
690
781
|
filtersAppliedQty += this.applyDeclarativeUpdateFilters(query, filterParams);
|
|
691
782
|
return filtersAppliedQty;
|
|
@@ -709,7 +800,7 @@ class BaseGateway {
|
|
|
709
800
|
* - `removeFilters` — applies declarative GatewayFilterRule array (new)
|
|
710
801
|
*/
|
|
711
802
|
async onRemoveFilter(query, filterParams) {
|
|
712
|
-
const { hasStatus } = this.config;
|
|
803
|
+
const { table, hasStatus, hasRemovedAt } = this.config;
|
|
713
804
|
const arr = filterParams ? (0, lodash_1.castArray)(filterParams) : [];
|
|
714
805
|
let filtersAppliedQty = 0;
|
|
715
806
|
const idName = this.getIdFieldUpdateRemove();
|
|
@@ -727,6 +818,17 @@ class BaseGateway {
|
|
|
727
818
|
filtersAppliedQty++;
|
|
728
819
|
query.where(this.getStatusField(), '!=', utils_1.STATUSES.REMOVED);
|
|
729
820
|
}
|
|
821
|
+
// Skip already-removed rows on soft-delete tables. Without this guard,
|
|
822
|
+
// a "remove" call on an already-removed row would re-write Removed_At
|
|
823
|
+
// and Removed_By with a new timestamp / user and return the row as if
|
|
824
|
+
// it was just removed — masking the fact that it was removed earlier.
|
|
825
|
+
// This guard is independent of hasStatus because PartyLabz tables use
|
|
826
|
+
// Status_Id (not "status"), so hasStatus is false on those tables and
|
|
827
|
+
// the status-based REMOVED guard above does not fire.
|
|
828
|
+
if (hasRemovedAt === true) {
|
|
829
|
+
filtersAppliedQty++;
|
|
830
|
+
query.whereNull(`${table}.${constants_1.FIELDS.REMOVED_AT}`);
|
|
831
|
+
}
|
|
730
832
|
// Declarative remove filter rules — applied after base logic.
|
|
731
833
|
filtersAppliedQty += this.applyDeclarativeRemoveFilters(query, filterParams);
|
|
732
834
|
return filtersAppliedQty;
|
|
@@ -771,6 +873,11 @@ class BaseGateway {
|
|
|
771
873
|
*/
|
|
772
874
|
async transformOnCreate(params) {
|
|
773
875
|
const { hasCreatedAt, hasCreatedBy, hasSpace, hasVersion, hasLang, localizationForField, hasUserId } = this.config;
|
|
876
|
+
// Strip client-supplied values for fields the framework manages or
|
|
877
|
+
// the gateway explicitly forbids from being set on create. Runs FIRST
|
|
878
|
+
// so the auto-set logic for createdAt / createdBy / spaceId / version
|
|
879
|
+
// below operates on a clean payload.
|
|
880
|
+
this.stripNonWritableFields(params, this.buildNonWritableFieldSet('create'), 'create');
|
|
774
881
|
if (hasCreatedAt) {
|
|
775
882
|
params[(0, lodash_1.camelCase)(constants_1.FIELDS.CREATED_AT)] = this.dateTime.dbFormat(this.dateTimeNow);
|
|
776
883
|
}
|
|
@@ -822,6 +929,11 @@ class BaseGateway {
|
|
|
822
929
|
*/
|
|
823
930
|
async transformOnUpdate(params, where) {
|
|
824
931
|
const { hasUpdatedAt, hasUpdatedBy, hasVersion, hasLang, localizationForField } = this.config;
|
|
932
|
+
// Strip client-supplied values for fields the framework manages or
|
|
933
|
+
// the gateway explicitly forbids from being changed. Runs FIRST so
|
|
934
|
+
// the auto-set logic for updatedAt / updatedBy / version below
|
|
935
|
+
// operates on a clean payload.
|
|
936
|
+
this.stripNonWritableFields(params, this.buildNonWritableFieldSet('update'), 'update');
|
|
825
937
|
if (hasUpdatedAt) {
|
|
826
938
|
params[(0, lodash_1.camelCase)(constants_1.FIELDS.UPDATED_AT)] = this.dateTime.dbFormat(this.dateTimeNow);
|
|
827
939
|
}
|
package/build/esm/BaseCore.d.ts
CHANGED
|
@@ -659,6 +659,40 @@ declare class BaseCore {
|
|
|
659
659
|
* @param args.gateway Gateway instance to query for the current max orderNo
|
|
660
660
|
*/
|
|
661
661
|
setOrderNo(args: any): Promise<void>;
|
|
662
|
+
/**
|
|
663
|
+
* Lifts an optimistic-concurrency `version` value from update params into
|
|
664
|
+
* the where filter.
|
|
665
|
+
*
|
|
666
|
+
* GraphQL clients typically send `version` inside the params payload (the
|
|
667
|
+
* UI reads it on get/list, then sends it back on update so concurrent
|
|
668
|
+
* edits are detected). The gateway, however, expects `version` in the
|
|
669
|
+
* where clause so it can be added to the UPDATE's WHERE. This helper
|
|
670
|
+
* bridges the two conventions:
|
|
671
|
+
*
|
|
672
|
+
* - When `params.version` is present and `where.version` is absent,
|
|
673
|
+
* copy params.version → where.version.
|
|
674
|
+
* - The original params.version is intentionally LEFT IN PLACE.
|
|
675
|
+
* transformOnUpdate() in the gateway only increments version when it
|
|
676
|
+
* is already present in params; stripping it would skip the bump and
|
|
677
|
+
* leave the version column stale.
|
|
678
|
+
* - When `where.version` is already populated, params.version is
|
|
679
|
+
* ignored with a debug log. The where value wins so older code paths
|
|
680
|
+
* that populate version explicitly are preserved unchanged.
|
|
681
|
+
* - When neither is set, the helper is a no-op.
|
|
682
|
+
*
|
|
683
|
+
* The pair operates as expected end-to-end:
|
|
684
|
+
*
|
|
685
|
+
* client { where: {id}, params: {..., version: 7} }
|
|
686
|
+
* → after lift: where = {id, version: 7}, params = {..., version: 7}
|
|
687
|
+
* → transformOnUpdate increments params.version to 8
|
|
688
|
+
* → onUpdateFilter adds WHERE version = 7
|
|
689
|
+
* → UPDATE ... SET version = 8 WHERE id = ... AND version = 7
|
|
690
|
+
*
|
|
691
|
+
* @param args The action args object — typically { where, params, ... }
|
|
692
|
+
* Mutated in place: where is initialised to {} if absent so
|
|
693
|
+
* the lift target always exists.
|
|
694
|
+
*/
|
|
695
|
+
private liftVersionFromParamsToWhere;
|
|
662
696
|
/**
|
|
663
697
|
* Stores a typed value in the action-scoped request store.
|
|
664
698
|
*
|
package/build/esm/BaseCore.js
CHANGED
|
@@ -737,7 +737,7 @@ class BaseCore {
|
|
|
737
737
|
action: async (args, opt) => {
|
|
738
738
|
const object = await this.beforeList(args, opt);
|
|
739
739
|
if (isOpResult(object)) {
|
|
740
|
-
return object;
|
|
740
|
+
return object.toJS();
|
|
741
741
|
}
|
|
742
742
|
const items = await this.getGateway()?.list(object);
|
|
743
743
|
const result = await this.afterList(items, opt);
|
|
@@ -767,15 +767,20 @@ class BaseCore {
|
|
|
767
767
|
action: async (args, opt) => {
|
|
768
768
|
const args2use = await this.beforeGet(args, opt);
|
|
769
769
|
if (isOpResult(args2use)) {
|
|
770
|
-
return args2use;
|
|
770
|
+
return args2use.toJS();
|
|
771
771
|
}
|
|
772
772
|
if (args2use?.id == null) {
|
|
773
773
|
return this.failure(OP_RESULT_CODES.NOT_FOUND, '', []);
|
|
774
774
|
}
|
|
775
775
|
const item = await this.getGateway()?.get(args2use.id);
|
|
776
|
+
if (this.getGateway()?.getConfig()?.hasRemovedAt === true) {
|
|
777
|
+
if (!item || item.removedAt) {
|
|
778
|
+
return this.failure(OP_RESULT_CODES.NOT_FOUND, `${this.config.name} not found`, []);
|
|
779
|
+
}
|
|
780
|
+
}
|
|
776
781
|
const result = await this.afterGet(item, opt);
|
|
777
782
|
if (!result) {
|
|
778
|
-
return this.failure(OP_RESULT_CODES.NOT_FOUND,
|
|
783
|
+
return this.failure(OP_RESULT_CODES.NOT_FOUND, `${this.config.name} not found`, []);
|
|
779
784
|
}
|
|
780
785
|
return this.success([result]);
|
|
781
786
|
},
|
|
@@ -803,12 +808,19 @@ class BaseCore {
|
|
|
803
808
|
action: async (args, opt) => {
|
|
804
809
|
const args2use = await this.beforeGetMany(args, opt);
|
|
805
810
|
if (isOpResult(args2use)) {
|
|
806
|
-
return args2use;
|
|
811
|
+
return args2use.toJS();
|
|
807
812
|
}
|
|
808
813
|
if (!Array.isArray(args2use?.ids)) {
|
|
809
814
|
return this.failure(OP_RESULT_CODES.NOT_FOUND, 'Expected ids array but received invalid input', []);
|
|
810
815
|
}
|
|
811
816
|
const items = await this.getGateway()?.getMany(args2use.ids);
|
|
817
|
+
if (this.getGateway()?.getConfig()?.hasRemovedAt === true) {
|
|
818
|
+
items.forEach((item, index) => {
|
|
819
|
+
if (!item || item.removedAt) {
|
|
820
|
+
items[index] = null;
|
|
821
|
+
}
|
|
822
|
+
});
|
|
823
|
+
}
|
|
812
824
|
const result = await this.afterGetMany(items, opt);
|
|
813
825
|
return this.success(result);
|
|
814
826
|
},
|
|
@@ -855,7 +867,7 @@ class BaseCore {
|
|
|
855
867
|
}
|
|
856
868
|
const object = await this.beforeCreate(params, opt);
|
|
857
869
|
if (isOpResult(object)) {
|
|
858
|
-
return object;
|
|
870
|
+
return object.toJS();
|
|
859
871
|
}
|
|
860
872
|
const items = await this.getGateway()?.create(object);
|
|
861
873
|
const result = await this.afterCreate(items, opt);
|
|
@@ -883,7 +895,7 @@ class BaseCore {
|
|
|
883
895
|
const { params } = args || {};
|
|
884
896
|
const objects = await this.beforeCreateMany(params, opt);
|
|
885
897
|
if (isOpResult(objects)) {
|
|
886
|
-
return objects;
|
|
898
|
+
return objects.toJS();
|
|
887
899
|
}
|
|
888
900
|
const items = await this.getGateway()?.create(objects);
|
|
889
901
|
const result = await this.afterCreateMany(items, opt);
|
|
@@ -911,10 +923,14 @@ class BaseCore {
|
|
|
911
923
|
validate: this.getValidators().update,
|
|
912
924
|
accessRights: [ACCESS_RIGHTS.UPDATE],
|
|
913
925
|
action: async (args, opt) => {
|
|
926
|
+
// Lift version from params into where for optimistic-concurrency.
|
|
927
|
+
// Mutates args in place — done BEFORE destructuring so the
|
|
928
|
+
// local where binding picks up the lifted value.
|
|
929
|
+
this.liftVersionFromParamsToWhere(args);
|
|
914
930
|
const { where, params } = args || {};
|
|
915
931
|
const object = await this.beforeUpdate(params, opt);
|
|
916
932
|
if (isOpResult(object)) {
|
|
917
|
-
return object;
|
|
933
|
+
return object.toJS();
|
|
918
934
|
}
|
|
919
935
|
const items = await this.getGateway()?.update(where, object);
|
|
920
936
|
const result = await this.afterUpdate(items, opt);
|
|
@@ -940,9 +956,18 @@ class BaseCore {
|
|
|
940
956
|
accessRights: [ACCESS_RIGHTS.UPDATE],
|
|
941
957
|
action: async (args, opt) => {
|
|
942
958
|
const { params } = args || {};
|
|
959
|
+
// Lift version from params into where for optimistic-concurrency,
|
|
960
|
+
// per pair. Mutates each pair in place. Tolerates a non-array
|
|
961
|
+
// params input — beforeUpdateMany handles the normalisation
|
|
962
|
+
// and a non-array shape will simply be skipped here.
|
|
963
|
+
if (Array.isArray(params)) {
|
|
964
|
+
for (const pair of params) {
|
|
965
|
+
this.liftVersionFromParamsToWhere(pair);
|
|
966
|
+
}
|
|
967
|
+
}
|
|
943
968
|
const object = await this.beforeUpdateMany(params, opt);
|
|
944
969
|
if (isOpResult(object)) {
|
|
945
|
-
return object;
|
|
970
|
+
return object.toJS();
|
|
946
971
|
}
|
|
947
972
|
const items = await this.getGateway()?.updateMany(object);
|
|
948
973
|
const result = await this.afterUpdateMany(items, opt);
|
|
@@ -971,7 +996,7 @@ class BaseCore {
|
|
|
971
996
|
const { params } = args || {};
|
|
972
997
|
const object = await this.beforeSet(params, opt);
|
|
973
998
|
if (isOpResult(object)) {
|
|
974
|
-
return object;
|
|
999
|
+
return object.toJS();
|
|
975
1000
|
}
|
|
976
1001
|
const items = await this.getGateway()?.set(object);
|
|
977
1002
|
const result = await this.afterSet(items, opt);
|
|
@@ -999,7 +1024,7 @@ class BaseCore {
|
|
|
999
1024
|
const { where } = args || {};
|
|
1000
1025
|
const object = await this.beforeRemove(where, opt);
|
|
1001
1026
|
if (isOpResult(object)) {
|
|
1002
|
-
return object;
|
|
1027
|
+
return object.toJS();
|
|
1003
1028
|
}
|
|
1004
1029
|
const items = await this.getGateway()?.remove(object);
|
|
1005
1030
|
const result = await this.afterRemove(items, opt);
|
|
@@ -1027,7 +1052,7 @@ class BaseCore {
|
|
|
1027
1052
|
const { where } = args || {};
|
|
1028
1053
|
const object = await this.beforeRemoveMany(where, opt);
|
|
1029
1054
|
if (isOpResult(object)) {
|
|
1030
|
-
return object;
|
|
1055
|
+
return object.toJS();
|
|
1031
1056
|
}
|
|
1032
1057
|
const items = await this.getGateway()?.remove(object);
|
|
1033
1058
|
const result = await this.afterRemoveMany(items, opt);
|
|
@@ -1481,6 +1506,63 @@ class BaseCore {
|
|
|
1481
1506
|
}
|
|
1482
1507
|
}
|
|
1483
1508
|
}
|
|
1509
|
+
// ─── Update-version helpers ────────────────────────────────────────────────
|
|
1510
|
+
/**
|
|
1511
|
+
* Lifts an optimistic-concurrency `version` value from update params into
|
|
1512
|
+
* the where filter.
|
|
1513
|
+
*
|
|
1514
|
+
* GraphQL clients typically send `version` inside the params payload (the
|
|
1515
|
+
* UI reads it on get/list, then sends it back on update so concurrent
|
|
1516
|
+
* edits are detected). The gateway, however, expects `version` in the
|
|
1517
|
+
* where clause so it can be added to the UPDATE's WHERE. This helper
|
|
1518
|
+
* bridges the two conventions:
|
|
1519
|
+
*
|
|
1520
|
+
* - When `params.version` is present and `where.version` is absent,
|
|
1521
|
+
* copy params.version → where.version.
|
|
1522
|
+
* - The original params.version is intentionally LEFT IN PLACE.
|
|
1523
|
+
* transformOnUpdate() in the gateway only increments version when it
|
|
1524
|
+
* is already present in params; stripping it would skip the bump and
|
|
1525
|
+
* leave the version column stale.
|
|
1526
|
+
* - When `where.version` is already populated, params.version is
|
|
1527
|
+
* ignored with a debug log. The where value wins so older code paths
|
|
1528
|
+
* that populate version explicitly are preserved unchanged.
|
|
1529
|
+
* - When neither is set, the helper is a no-op.
|
|
1530
|
+
*
|
|
1531
|
+
* The pair operates as expected end-to-end:
|
|
1532
|
+
*
|
|
1533
|
+
* client { where: {id}, params: {..., version: 7} }
|
|
1534
|
+
* → after lift: where = {id, version: 7}, params = {..., version: 7}
|
|
1535
|
+
* → transformOnUpdate increments params.version to 8
|
|
1536
|
+
* → onUpdateFilter adds WHERE version = 7
|
|
1537
|
+
* → UPDATE ... SET version = 8 WHERE id = ... AND version = 7
|
|
1538
|
+
*
|
|
1539
|
+
* @param args The action args object — typically { where, params, ... }
|
|
1540
|
+
* Mutated in place: where is initialised to {} if absent so
|
|
1541
|
+
* the lift target always exists.
|
|
1542
|
+
*/
|
|
1543
|
+
liftVersionFromParamsToWhere(args) {
|
|
1544
|
+
if (!args || typeof args !== 'object')
|
|
1545
|
+
return;
|
|
1546
|
+
const params = args.params;
|
|
1547
|
+
if (!params || typeof params !== 'object')
|
|
1548
|
+
return;
|
|
1549
|
+
const versionInParams = params.version;
|
|
1550
|
+
if (versionInParams == null)
|
|
1551
|
+
return;
|
|
1552
|
+
// Initialise where if absent — gives the lift somewhere to land.
|
|
1553
|
+
if (!args.where || typeof args.where !== 'object') {
|
|
1554
|
+
args.where = {};
|
|
1555
|
+
}
|
|
1556
|
+
if (args.where.version != null) {
|
|
1557
|
+
// Where wins. params.version is left in place — transformOnUpdate
|
|
1558
|
+
// will still see it and bump it, which is the desired outcome.
|
|
1559
|
+
this.logger.debug(`Update — both where.version (${args.where.version}) and params.version (${versionInParams}) ` +
|
|
1560
|
+
`were supplied; where.version wins (params.version left in payload for the increment).`);
|
|
1561
|
+
return;
|
|
1562
|
+
}
|
|
1563
|
+
args.where.version = versionInParams;
|
|
1564
|
+
this.logger.debug(`Update — lifted version=${versionInParams} from params into where for optimistic-concurrency check.`);
|
|
1565
|
+
}
|
|
1484
1566
|
// ─── Action-scoped request store helpers ──────────────────────────────────
|
|
1485
1567
|
// These helpers provide typed access to the per-action requestStore carried
|
|
1486
1568
|
// in BaseCoreActionsInterface.opt. Use them in any before*, after*, validate,
|
|
@@ -311,6 +311,31 @@ declare class BaseGateway {
|
|
|
311
311
|
* path — they are skipped with a warning log.
|
|
312
312
|
*/
|
|
313
313
|
private applyDeclarativeRemoveFilters;
|
|
314
|
+
/**
|
|
315
|
+
* Returns the full set of field names (camelCase + snake_case) that
|
|
316
|
+
* should be stripped from a params object on the given write path.
|
|
317
|
+
*
|
|
318
|
+
* Combines two sources:
|
|
319
|
+
* 1. The framework's implicit immutables, derived from has* flags:
|
|
320
|
+
* - 'create' → id, removedAt, removedBy
|
|
321
|
+
* - 'update' → id, createdAt, createdBy, removedAt, removedBy
|
|
322
|
+
* 2. The gateway's own nonCreatableFields / nonUpdatableFields config.
|
|
323
|
+
*
|
|
324
|
+
* For each source name, both snake_case (matches DB / FIELDS constants)
|
|
325
|
+
* and camelCase (matches VM / API params) are added so callers do not
|
|
326
|
+
* have to be consistent with one convention.
|
|
327
|
+
*
|
|
328
|
+
* Returns a Set for O(1) lookup inside the strip loop.
|
|
329
|
+
*/
|
|
330
|
+
private buildNonWritableFieldSet;
|
|
331
|
+
/**
|
|
332
|
+
* Strips every key whose name appears in `nonWritable` from `params`.
|
|
333
|
+
* Logs each strip at debug level so audit / troubleshooting can see
|
|
334
|
+
* exactly which client-supplied keys were ignored.
|
|
335
|
+
*
|
|
336
|
+
* Mutates and returns `params` for chaining.
|
|
337
|
+
*/
|
|
338
|
+
private stripNonWritableFields;
|
|
314
339
|
/**
|
|
315
340
|
* Generates a unique ID for a new record.
|
|
316
341
|
*
|
package/build/esm/BaseGateway.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { v4 } from 'uuid';
|
|
2
2
|
import dayjs from 'dayjs';
|
|
3
3
|
import check from 'check-types';
|
|
4
|
-
import { camelCase, castArray, pullAll } from 'lodash';
|
|
4
|
+
import { camelCase, castArray, pullAll, snakeCase } from 'lodash';
|
|
5
5
|
import { arrToChunks, STATUSES, DateTimeHelpers, buildKey, Logger, LoggerLevels, isLogger, camelResponse, } from '@sdflc/utils';
|
|
6
6
|
import { DataCache } from './DataCache';
|
|
7
7
|
import { FIELDS, ID_FIELD } from './constants';
|
|
@@ -569,6 +569,87 @@ class BaseGateway {
|
|
|
569
569
|
}
|
|
570
570
|
return count;
|
|
571
571
|
}
|
|
572
|
+
// ─── Write-protection helpers ──────────────────────────────────────────────
|
|
573
|
+
/**
|
|
574
|
+
* Returns the full set of field names (camelCase + snake_case) that
|
|
575
|
+
* should be stripped from a params object on the given write path.
|
|
576
|
+
*
|
|
577
|
+
* Combines two sources:
|
|
578
|
+
* 1. The framework's implicit immutables, derived from has* flags:
|
|
579
|
+
* - 'create' → id, removedAt, removedBy
|
|
580
|
+
* - 'update' → id, createdAt, createdBy, removedAt, removedBy
|
|
581
|
+
* 2. The gateway's own nonCreatableFields / nonUpdatableFields config.
|
|
582
|
+
*
|
|
583
|
+
* For each source name, both snake_case (matches DB / FIELDS constants)
|
|
584
|
+
* and camelCase (matches VM / API params) are added so callers do not
|
|
585
|
+
* have to be consistent with one convention.
|
|
586
|
+
*
|
|
587
|
+
* Returns a Set for O(1) lookup inside the strip loop.
|
|
588
|
+
*/
|
|
589
|
+
buildNonWritableFieldSet(path) {
|
|
590
|
+
const { hasCreatedAt, hasCreatedBy, hasRemovedAt, hasRemovedBy, nonCreatableFields, nonUpdatableFields } = this.config;
|
|
591
|
+
const fields = [];
|
|
592
|
+
// ── Framework-implicit immutables (always stripped) ──────────────────────
|
|
593
|
+
// id is always framework-managed via generateId(), so it must never be
|
|
594
|
+
// honoured from caller input on create OR update.
|
|
595
|
+
fields.push(this.getIdField().split('.').pop());
|
|
596
|
+
if (path === 'create') {
|
|
597
|
+
// Soft-delete markers are only set on the remove path. A caller
|
|
598
|
+
// sending them on create would record a "born already removed" row.
|
|
599
|
+
if (hasRemovedAt)
|
|
600
|
+
fields.push(FIELDS.REMOVED_AT);
|
|
601
|
+
if (hasRemovedBy)
|
|
602
|
+
fields.push(FIELDS.REMOVED_BY);
|
|
603
|
+
}
|
|
604
|
+
else {
|
|
605
|
+
// On update, creation-time markers are write-once and removal-time
|
|
606
|
+
// markers are managed by the remove path.
|
|
607
|
+
if (hasCreatedAt)
|
|
608
|
+
fields.push(FIELDS.CREATED_AT);
|
|
609
|
+
if (hasCreatedBy)
|
|
610
|
+
fields.push(FIELDS.CREATED_BY);
|
|
611
|
+
if (hasRemovedAt)
|
|
612
|
+
fields.push(FIELDS.REMOVED_AT);
|
|
613
|
+
if (hasRemovedBy)
|
|
614
|
+
fields.push(FIELDS.REMOVED_BY);
|
|
615
|
+
}
|
|
616
|
+
// ── Gateway-configured immutables ────────────────────────────────────────
|
|
617
|
+
const configured = path === 'create' ? nonCreatableFields : nonUpdatableFields;
|
|
618
|
+
if (Array.isArray(configured)) {
|
|
619
|
+
fields.push(...configured);
|
|
620
|
+
}
|
|
621
|
+
// ── Expand each entry to both snake_case and camelCase ───────────────────
|
|
622
|
+
// This way the gateway author can write columns the way they think of
|
|
623
|
+
// them (snake_case for DB-minded folks, camelCase for VM-minded folks)
|
|
624
|
+
// and both forms are stripped from incoming params.
|
|
625
|
+
const out = new Set();
|
|
626
|
+
for (const name of fields) {
|
|
627
|
+
if (typeof name !== 'string' || name.length === 0)
|
|
628
|
+
continue;
|
|
629
|
+
out.add(name);
|
|
630
|
+
out.add(camelCase(name));
|
|
631
|
+
out.add(snakeCase(name));
|
|
632
|
+
}
|
|
633
|
+
return out;
|
|
634
|
+
}
|
|
635
|
+
/**
|
|
636
|
+
* Strips every key whose name appears in `nonWritable` from `params`.
|
|
637
|
+
* Logs each strip at debug level so audit / troubleshooting can see
|
|
638
|
+
* exactly which client-supplied keys were ignored.
|
|
639
|
+
*
|
|
640
|
+
* Mutates and returns `params` for chaining.
|
|
641
|
+
*/
|
|
642
|
+
stripNonWritableFields(params, nonWritable, path) {
|
|
643
|
+
if (!params || typeof params !== 'object')
|
|
644
|
+
return params;
|
|
645
|
+
for (const key of Object.keys(params)) {
|
|
646
|
+
if (nonWritable.has(key)) {
|
|
647
|
+
this.logger.debug(`${this.config.table}: field "${key}" is not writable on ${path} and was stripped from the payload`);
|
|
648
|
+
delete params[key];
|
|
649
|
+
}
|
|
650
|
+
}
|
|
651
|
+
return params;
|
|
652
|
+
}
|
|
572
653
|
// ─── ID generation ─────────────────────────────────────────────────────────
|
|
573
654
|
/**
|
|
574
655
|
* Generates a unique ID for a new record.
|
|
@@ -657,7 +738,7 @@ class BaseGateway {
|
|
|
657
738
|
* - `updateFilters` — applies declarative GatewayFilterRule array (new)
|
|
658
739
|
*/
|
|
659
740
|
async onUpdateFilter(query, filterParams) {
|
|
660
|
-
const { hasStatus, hasVersion } = this.config;
|
|
741
|
+
const { table, hasStatus, hasVersion, hasRemovedAt } = this.config;
|
|
661
742
|
const idName = this.getIdFieldUpdateRemove();
|
|
662
743
|
let filtersAppliedQty = 0;
|
|
663
744
|
if (filterParams[idName]) {
|
|
@@ -680,6 +761,16 @@ class BaseGateway {
|
|
|
680
761
|
query.where(statusField, '!=', STATUSES.REMOVED);
|
|
681
762
|
}
|
|
682
763
|
}
|
|
764
|
+
// Skip already-removed rows on soft-delete tables. Without this guard,
|
|
765
|
+
// a regular update call on a soft-deleted row would silently bring it
|
|
766
|
+
// back into a half-mutated state (new updatedAt / updatedBy, old
|
|
767
|
+
// Removed_At still set). For tables where hasStatus is true, the
|
|
768
|
+
// status-based REMOVED guard above already covers this; for PartyLabz
|
|
769
|
+
// tables (Status_Id, hasStatus: false) this is the only line of defence.
|
|
770
|
+
if (hasRemovedAt === true) {
|
|
771
|
+
filtersAppliedQty++;
|
|
772
|
+
query.whereNull(`${table}.${FIELDS.REMOVED_AT}`);
|
|
773
|
+
}
|
|
683
774
|
// Declarative update filter rules — applied after base logic.
|
|
684
775
|
filtersAppliedQty += this.applyDeclarativeUpdateFilters(query, filterParams);
|
|
685
776
|
return filtersAppliedQty;
|
|
@@ -703,7 +794,7 @@ class BaseGateway {
|
|
|
703
794
|
* - `removeFilters` — applies declarative GatewayFilterRule array (new)
|
|
704
795
|
*/
|
|
705
796
|
async onRemoveFilter(query, filterParams) {
|
|
706
|
-
const { hasStatus } = this.config;
|
|
797
|
+
const { table, hasStatus, hasRemovedAt } = this.config;
|
|
707
798
|
const arr = filterParams ? castArray(filterParams) : [];
|
|
708
799
|
let filtersAppliedQty = 0;
|
|
709
800
|
const idName = this.getIdFieldUpdateRemove();
|
|
@@ -721,6 +812,17 @@ class BaseGateway {
|
|
|
721
812
|
filtersAppliedQty++;
|
|
722
813
|
query.where(this.getStatusField(), '!=', STATUSES.REMOVED);
|
|
723
814
|
}
|
|
815
|
+
// Skip already-removed rows on soft-delete tables. Without this guard,
|
|
816
|
+
// a "remove" call on an already-removed row would re-write Removed_At
|
|
817
|
+
// and Removed_By with a new timestamp / user and return the row as if
|
|
818
|
+
// it was just removed — masking the fact that it was removed earlier.
|
|
819
|
+
// This guard is independent of hasStatus because PartyLabz tables use
|
|
820
|
+
// Status_Id (not "status"), so hasStatus is false on those tables and
|
|
821
|
+
// the status-based REMOVED guard above does not fire.
|
|
822
|
+
if (hasRemovedAt === true) {
|
|
823
|
+
filtersAppliedQty++;
|
|
824
|
+
query.whereNull(`${table}.${FIELDS.REMOVED_AT}`);
|
|
825
|
+
}
|
|
724
826
|
// Declarative remove filter rules — applied after base logic.
|
|
725
827
|
filtersAppliedQty += this.applyDeclarativeRemoveFilters(query, filterParams);
|
|
726
828
|
return filtersAppliedQty;
|
|
@@ -765,6 +867,11 @@ class BaseGateway {
|
|
|
765
867
|
*/
|
|
766
868
|
async transformOnCreate(params) {
|
|
767
869
|
const { hasCreatedAt, hasCreatedBy, hasSpace, hasVersion, hasLang, localizationForField, hasUserId } = this.config;
|
|
870
|
+
// Strip client-supplied values for fields the framework manages or
|
|
871
|
+
// the gateway explicitly forbids from being set on create. Runs FIRST
|
|
872
|
+
// so the auto-set logic for createdAt / createdBy / spaceId / version
|
|
873
|
+
// below operates on a clean payload.
|
|
874
|
+
this.stripNonWritableFields(params, this.buildNonWritableFieldSet('create'), 'create');
|
|
768
875
|
if (hasCreatedAt) {
|
|
769
876
|
params[camelCase(FIELDS.CREATED_AT)] = this.dateTime.dbFormat(this.dateTimeNow);
|
|
770
877
|
}
|
|
@@ -816,6 +923,11 @@ class BaseGateway {
|
|
|
816
923
|
*/
|
|
817
924
|
async transformOnUpdate(params, where) {
|
|
818
925
|
const { hasUpdatedAt, hasUpdatedBy, hasVersion, hasLang, localizationForField } = this.config;
|
|
926
|
+
// Strip client-supplied values for fields the framework manages or
|
|
927
|
+
// the gateway explicitly forbids from being changed. Runs FIRST so
|
|
928
|
+
// the auto-set logic for updatedAt / updatedBy / version below
|
|
929
|
+
// operates on a clean payload.
|
|
930
|
+
this.stripNonWritableFields(params, this.buildNonWritableFieldSet('update'), 'update');
|
|
819
931
|
if (hasUpdatedAt) {
|
|
820
932
|
params[camelCase(FIELDS.UPDATED_AT)] = this.dateTime.dbFormat(this.dateTimeNow);
|
|
821
933
|
}
|
|
@@ -190,4 +190,37 @@ export interface BaseGatewayPropsInterface extends CacheInterface {
|
|
|
190
190
|
* ]
|
|
191
191
|
*/
|
|
192
192
|
countFilters?: GatewayFilterRule[];
|
|
193
|
+
/**
|
|
194
|
+
* Field names that callers cannot set when creating a new record.
|
|
195
|
+
* Stripped from the params object inside transformOnCreate() before
|
|
196
|
+
* the INSERT statement is built.
|
|
197
|
+
*
|
|
198
|
+
* Names can be in snake_case or camelCase — both forms are stripped.
|
|
199
|
+
*
|
|
200
|
+
* The framework already implicitly strips id, removedAt, and removedBy
|
|
201
|
+
* on create (those are managed by the framework / the remove path).
|
|
202
|
+
* Use this array for additional resource-specific immutables — e.g.
|
|
203
|
+
* a foreign key that should be derived from session context, or a
|
|
204
|
+
* computed column that should never come from the client.
|
|
205
|
+
*
|
|
206
|
+
* Example:
|
|
207
|
+
* nonCreatableFields: ['accountId', 'sentAt']
|
|
208
|
+
*/
|
|
209
|
+
nonCreatableFields?: string[];
|
|
210
|
+
/**
|
|
211
|
+
* Field names that callers cannot change after a record is created.
|
|
212
|
+
* Stripped from the params object inside transformOnUpdate() before
|
|
213
|
+
* the UPDATE statement is built.
|
|
214
|
+
*
|
|
215
|
+
* Names can be in snake_case or camelCase — both forms are stripped.
|
|
216
|
+
*
|
|
217
|
+
* The framework already implicitly strips id, createdAt, createdBy,
|
|
218
|
+
* removedAt, and removedBy on update. Use this array for additional
|
|
219
|
+
* resource-specific immutables — for example partyId on a junction
|
|
220
|
+
* table whose (party, child) identity is fixed at creation time.
|
|
221
|
+
*
|
|
222
|
+
* Example:
|
|
223
|
+
* nonUpdatableFields: ['partyId', 'offeringId']
|
|
224
|
+
*/
|
|
225
|
+
nonUpdatableFields?: string[];
|
|
193
226
|
}
|
|
@@ -190,4 +190,37 @@ export interface BaseGatewayPropsInterface extends CacheInterface {
|
|
|
190
190
|
* ]
|
|
191
191
|
*/
|
|
192
192
|
countFilters?: GatewayFilterRule[];
|
|
193
|
+
/**
|
|
194
|
+
* Field names that callers cannot set when creating a new record.
|
|
195
|
+
* Stripped from the params object inside transformOnCreate() before
|
|
196
|
+
* the INSERT statement is built.
|
|
197
|
+
*
|
|
198
|
+
* Names can be in snake_case or camelCase — both forms are stripped.
|
|
199
|
+
*
|
|
200
|
+
* The framework already implicitly strips id, removedAt, and removedBy
|
|
201
|
+
* on create (those are managed by the framework / the remove path).
|
|
202
|
+
* Use this array for additional resource-specific immutables — e.g.
|
|
203
|
+
* a foreign key that should be derived from session context, or a
|
|
204
|
+
* computed column that should never come from the client.
|
|
205
|
+
*
|
|
206
|
+
* Example:
|
|
207
|
+
* nonCreatableFields: ['accountId', 'sentAt']
|
|
208
|
+
*/
|
|
209
|
+
nonCreatableFields?: string[];
|
|
210
|
+
/**
|
|
211
|
+
* Field names that callers cannot change after a record is created.
|
|
212
|
+
* Stripped from the params object inside transformOnUpdate() before
|
|
213
|
+
* the UPDATE statement is built.
|
|
214
|
+
*
|
|
215
|
+
* Names can be in snake_case or camelCase — both forms are stripped.
|
|
216
|
+
*
|
|
217
|
+
* The framework already implicitly strips id, createdAt, createdBy,
|
|
218
|
+
* removedAt, and removedBy on update. Use this array for additional
|
|
219
|
+
* resource-specific immutables — for example partyId on a junction
|
|
220
|
+
* table whose (party, child) identity is fixed at creation time.
|
|
221
|
+
*
|
|
222
|
+
* Example:
|
|
223
|
+
* nonUpdatableFields: ['partyId', 'offeringId']
|
|
224
|
+
*/
|
|
225
|
+
nonUpdatableFields?: string[];
|
|
193
226
|
}
|