@sdflc/backend-helpers 1.0.119 → 1.0.121
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/build/BaseGateway.d.ts +1 -1
- package/build/BaseGateway.js +6 -4
- package/build/dbTableHelpers.d.ts +19 -0
- package/build/dbTableHelpers.js +36 -1
- package/build/esm/BaseGateway.d.ts +1 -1
- package/build/esm/BaseGateway.js +6 -4
- package/build/esm/dbTableHelpers.d.ts +19 -0
- package/build/esm/dbTableHelpers.js +34 -0
- package/build/esm/interfaces/CacheInterface.d.ts +8 -1
- package/build/interfaces/CacheInterface.d.ts +8 -1
- package/package.json +1 -1
package/build/BaseGateway.d.ts
CHANGED
|
@@ -285,7 +285,7 @@ declare class BaseGateway {
|
|
|
285
285
|
* Returns the Database instance from config.
|
|
286
286
|
* All query builders are derived from this instance.
|
|
287
287
|
*/
|
|
288
|
-
getDb(): Database;
|
|
288
|
+
getDb(): Database | undefined;
|
|
289
289
|
/**
|
|
290
290
|
* Returns a Knex query builder scoped to this gateway's table.
|
|
291
291
|
* The builder is a fresh instance each time — it does not carry state
|
package/build/BaseGateway.js
CHANGED
|
@@ -482,10 +482,11 @@ class BaseGateway {
|
|
|
482
482
|
obj[constants_1.FIELDS.REMOVED_BY] = this.getContext()?.userId;
|
|
483
483
|
}
|
|
484
484
|
}
|
|
485
|
-
|
|
485
|
+
const db = this.getDb();
|
|
486
|
+
if (hasLang && localizationForField && db) {
|
|
486
487
|
// Append removal timestamp to the id to free the original localisation key
|
|
487
488
|
// for future re-use (e.g. if a new record with the same lang + slug is created).
|
|
488
|
-
obj[this.getIdField()] =
|
|
489
|
+
obj[this.getIdField()] = db.raw(`CONCAT(id, '-${removalTimeStr}')`);
|
|
489
490
|
}
|
|
490
491
|
return obj;
|
|
491
492
|
}
|
|
@@ -634,10 +635,11 @@ class BaseGateway {
|
|
|
634
635
|
*/
|
|
635
636
|
async list(args) {
|
|
636
637
|
const { filter, params, useCache } = args || {};
|
|
637
|
-
const
|
|
638
|
-
if (!this.config.table) {
|
|
638
|
+
const db = this.getDb();
|
|
639
|
+
if (!this.config.table || !db) {
|
|
639
640
|
return [];
|
|
640
641
|
}
|
|
642
|
+
const { limit, offset, sortBy } = db.createLimitOffsetSortBy(params, this.config.defaultSorting || []);
|
|
641
643
|
const shouldUseCache = useCache != null ? useCache : !this.config.noCache;
|
|
642
644
|
const fieldsToUse = !shouldUseCache && Array.isArray(this.config.selectFields) ? this.config.selectFields : this.getIdField();
|
|
643
645
|
const { query } = await this.buildListQuery({ filter, params, fieldsToUse, useCache: shouldUseCache });
|
|
@@ -129,3 +129,22 @@ export declare const addLang: (table: any, opt?: any) => any;
|
|
|
129
129
|
* @returns Plain object with default field values
|
|
130
130
|
*/
|
|
131
131
|
export declare const defaultFields: (opt?: any) => Record<string, any>;
|
|
132
|
+
/**
|
|
133
|
+
* Utility function to remove standard system fields from an object.
|
|
134
|
+
*
|
|
135
|
+
* Useful for preparing database records for output or comparison by stripping
|
|
136
|
+
* out fields that are automatically managed by the system and not relevant to
|
|
137
|
+
* business logic.
|
|
138
|
+
*
|
|
139
|
+
* Fields removed:
|
|
140
|
+
* - createdAt, createdBy, updatedAt, updatedBy, removedAt, removedBy,
|
|
141
|
+
* version, removedAtStr
|
|
142
|
+
*
|
|
143
|
+
* @param obj Input object (e.g. a database record)
|
|
144
|
+
* @returns New object with system fields omitted
|
|
145
|
+
*
|
|
146
|
+
* Note: this function does not modify the input object; it returns a new
|
|
147
|
+
* object with the specified fields removed. This is important for avoiding
|
|
148
|
+
* unintended side effects when working with database records.
|
|
149
|
+
* */
|
|
150
|
+
export declare const removeSystemTableFields: (obj: any) => any;
|
package/build/dbTableHelpers.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
// ./src/dbFieldHelpers.ts
|
|
3
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
-
exports.defaultFields = exports.addLang = exports.addAccountId = exports.addUserId = exports.addSpaceId = exports.addAmount = exports.addOrderNoNum = exports.addOrderNo = exports.dbFieldAddDefaults = void 0;
|
|
4
|
+
exports.removeSystemTableFields = exports.defaultFields = exports.addLang = exports.addAccountId = exports.addUserId = exports.addSpaceId = exports.addAmount = exports.addOrderNoNum = exports.addOrderNo = exports.dbFieldAddDefaults = void 0;
|
|
5
|
+
const lodash_1 = require("lodash");
|
|
5
6
|
const utils_1 = require("@sdflc/utils");
|
|
6
7
|
const constants_1 = require("./constants");
|
|
7
8
|
/**
|
|
@@ -254,3 +255,37 @@ const defaultFields = (opt) => {
|
|
|
254
255
|
return obj;
|
|
255
256
|
};
|
|
256
257
|
exports.defaultFields = defaultFields;
|
|
258
|
+
/**
|
|
259
|
+
* Utility function to remove standard system fields from an object.
|
|
260
|
+
*
|
|
261
|
+
* Useful for preparing database records for output or comparison by stripping
|
|
262
|
+
* out fields that are automatically managed by the system and not relevant to
|
|
263
|
+
* business logic.
|
|
264
|
+
*
|
|
265
|
+
* Fields removed:
|
|
266
|
+
* - createdAt, createdBy, updatedAt, updatedBy, removedAt, removedBy,
|
|
267
|
+
* version, removedAtStr
|
|
268
|
+
*
|
|
269
|
+
* @param obj Input object (e.g. a database record)
|
|
270
|
+
* @returns New object with system fields omitted
|
|
271
|
+
*
|
|
272
|
+
* Note: this function does not modify the input object; it returns a new
|
|
273
|
+
* object with the specified fields removed. This is important for avoiding
|
|
274
|
+
* unintended side effects when working with database records.
|
|
275
|
+
* */
|
|
276
|
+
const removeSystemTableFields = (obj) => {
|
|
277
|
+
if (!obj) {
|
|
278
|
+
return obj;
|
|
279
|
+
}
|
|
280
|
+
return (0, lodash_1.omit)(obj, [
|
|
281
|
+
'createdAt',
|
|
282
|
+
'createdBy',
|
|
283
|
+
'updatedAt',
|
|
284
|
+
'updatedBy',
|
|
285
|
+
'removedAt',
|
|
286
|
+
'removedBy',
|
|
287
|
+
'version',
|
|
288
|
+
'removedAtStr',
|
|
289
|
+
]);
|
|
290
|
+
};
|
|
291
|
+
exports.removeSystemTableFields = removeSystemTableFields;
|
|
@@ -285,7 +285,7 @@ declare class BaseGateway {
|
|
|
285
285
|
* Returns the Database instance from config.
|
|
286
286
|
* All query builders are derived from this instance.
|
|
287
287
|
*/
|
|
288
|
-
getDb(): Database;
|
|
288
|
+
getDb(): Database | undefined;
|
|
289
289
|
/**
|
|
290
290
|
* Returns a Knex query builder scoped to this gateway's table.
|
|
291
291
|
* The builder is a fresh instance each time — it does not carry state
|
package/build/esm/BaseGateway.js
CHANGED
|
@@ -476,10 +476,11 @@ class BaseGateway {
|
|
|
476
476
|
obj[FIELDS.REMOVED_BY] = this.getContext()?.userId;
|
|
477
477
|
}
|
|
478
478
|
}
|
|
479
|
-
|
|
479
|
+
const db = this.getDb();
|
|
480
|
+
if (hasLang && localizationForField && db) {
|
|
480
481
|
// Append removal timestamp to the id to free the original localisation key
|
|
481
482
|
// for future re-use (e.g. if a new record with the same lang + slug is created).
|
|
482
|
-
obj[this.getIdField()] =
|
|
483
|
+
obj[this.getIdField()] = db.raw(`CONCAT(id, '-${removalTimeStr}')`);
|
|
483
484
|
}
|
|
484
485
|
return obj;
|
|
485
486
|
}
|
|
@@ -628,10 +629,11 @@ class BaseGateway {
|
|
|
628
629
|
*/
|
|
629
630
|
async list(args) {
|
|
630
631
|
const { filter, params, useCache } = args || {};
|
|
631
|
-
const
|
|
632
|
-
if (!this.config.table) {
|
|
632
|
+
const db = this.getDb();
|
|
633
|
+
if (!this.config.table || !db) {
|
|
633
634
|
return [];
|
|
634
635
|
}
|
|
636
|
+
const { limit, offset, sortBy } = db.createLimitOffsetSortBy(params, this.config.defaultSorting || []);
|
|
635
637
|
const shouldUseCache = useCache != null ? useCache : !this.config.noCache;
|
|
636
638
|
const fieldsToUse = !shouldUseCache && Array.isArray(this.config.selectFields) ? this.config.selectFields : this.getIdField();
|
|
637
639
|
const { query } = await this.buildListQuery({ filter, params, fieldsToUse, useCache: shouldUseCache });
|
|
@@ -129,3 +129,22 @@ export declare const addLang: (table: any, opt?: any) => any;
|
|
|
129
129
|
* @returns Plain object with default field values
|
|
130
130
|
*/
|
|
131
131
|
export declare const defaultFields: (opt?: any) => Record<string, any>;
|
|
132
|
+
/**
|
|
133
|
+
* Utility function to remove standard system fields from an object.
|
|
134
|
+
*
|
|
135
|
+
* Useful for preparing database records for output or comparison by stripping
|
|
136
|
+
* out fields that are automatically managed by the system and not relevant to
|
|
137
|
+
* business logic.
|
|
138
|
+
*
|
|
139
|
+
* Fields removed:
|
|
140
|
+
* - createdAt, createdBy, updatedAt, updatedBy, removedAt, removedBy,
|
|
141
|
+
* version, removedAtStr
|
|
142
|
+
*
|
|
143
|
+
* @param obj Input object (e.g. a database record)
|
|
144
|
+
* @returns New object with system fields omitted
|
|
145
|
+
*
|
|
146
|
+
* Note: this function does not modify the input object; it returns a new
|
|
147
|
+
* object with the specified fields removed. This is important for avoiding
|
|
148
|
+
* unintended side effects when working with database records.
|
|
149
|
+
* */
|
|
150
|
+
export declare const removeSystemTableFields: (obj: any) => any;
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
// ./src/dbFieldHelpers.ts
|
|
2
|
+
import { omit } from 'lodash';
|
|
2
3
|
import { STATUSES, UUID_EMPTY } from '@sdflc/utils';
|
|
3
4
|
import { DEFAULT_ORDER_NO, FIELDS } from './constants';
|
|
4
5
|
/**
|
|
@@ -242,3 +243,36 @@ export const defaultFields = (opt) => {
|
|
|
242
243
|
}
|
|
243
244
|
return obj;
|
|
244
245
|
};
|
|
246
|
+
/**
|
|
247
|
+
* Utility function to remove standard system fields from an object.
|
|
248
|
+
*
|
|
249
|
+
* Useful for preparing database records for output or comparison by stripping
|
|
250
|
+
* out fields that are automatically managed by the system and not relevant to
|
|
251
|
+
* business logic.
|
|
252
|
+
*
|
|
253
|
+
* Fields removed:
|
|
254
|
+
* - createdAt, createdBy, updatedAt, updatedBy, removedAt, removedBy,
|
|
255
|
+
* version, removedAtStr
|
|
256
|
+
*
|
|
257
|
+
* @param obj Input object (e.g. a database record)
|
|
258
|
+
* @returns New object with system fields omitted
|
|
259
|
+
*
|
|
260
|
+
* Note: this function does not modify the input object; it returns a new
|
|
261
|
+
* object with the specified fields removed. This is important for avoiding
|
|
262
|
+
* unintended side effects when working with database records.
|
|
263
|
+
* */
|
|
264
|
+
export const removeSystemTableFields = (obj) => {
|
|
265
|
+
if (!obj) {
|
|
266
|
+
return obj;
|
|
267
|
+
}
|
|
268
|
+
return omit(obj, [
|
|
269
|
+
'createdAt',
|
|
270
|
+
'createdBy',
|
|
271
|
+
'updatedAt',
|
|
272
|
+
'updatedBy',
|
|
273
|
+
'removedAt',
|
|
274
|
+
'removedBy',
|
|
275
|
+
'version',
|
|
276
|
+
'removedAtStr',
|
|
277
|
+
]);
|
|
278
|
+
};
|
|
@@ -1,6 +1,13 @@
|
|
|
1
1
|
import { Database } from '../Database';
|
|
2
2
|
export interface CacheInterface {
|
|
3
|
-
|
|
3
|
+
/**
|
|
4
|
+
* Database instance used by the default batch loader (batchLoaderDefault).
|
|
5
|
+
* Optional when a custom `loaderFn` is provided — in that case the loader
|
|
6
|
+
* function is responsible for its own data fetching and `db` is not used.
|
|
7
|
+
* Runtime safety is enforced in DataCache.batchLoaderDefault(), which throws
|
|
8
|
+
* if `db` is missing and no `loaderFn` was supplied.
|
|
9
|
+
*/
|
|
10
|
+
db?: Database;
|
|
4
11
|
/**
|
|
5
12
|
* Namespace prefix for DataLoader cache keys.
|
|
6
13
|
* Resolved in BaseGateway as: context.keyPrefix → config.keyPrefix → table name.
|
|
@@ -1,6 +1,13 @@
|
|
|
1
1
|
import { Database } from '../Database';
|
|
2
2
|
export interface CacheInterface {
|
|
3
|
-
|
|
3
|
+
/**
|
|
4
|
+
* Database instance used by the default batch loader (batchLoaderDefault).
|
|
5
|
+
* Optional when a custom `loaderFn` is provided — in that case the loader
|
|
6
|
+
* function is responsible for its own data fetching and `db` is not used.
|
|
7
|
+
* Runtime safety is enforced in DataCache.batchLoaderDefault(), which throws
|
|
8
|
+
* if `db` is missing and no `loaderFn` was supplied.
|
|
9
|
+
*/
|
|
10
|
+
db?: Database;
|
|
4
11
|
/**
|
|
5
12
|
* Namespace prefix for DataLoader cache keys.
|
|
6
13
|
* Resolved in BaseGateway as: context.keyPrefix → config.keyPrefix → table name.
|