@sdflc/backend-helpers 1.0.70 → 1.0.71
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.js
CHANGED
|
@@ -54,24 +54,24 @@ class BaseGateway {
|
|
|
54
54
|
}
|
|
55
55
|
onListFilter(query, filterParams) {
|
|
56
56
|
return __awaiter(this, void 0, void 0, function* () {
|
|
57
|
-
const { hasStatus, hasRemovedAt, hasSpace, activeStatuses, hasLang, fieldForKeyword, filterByUserField } = this.config || {};
|
|
57
|
+
const { table, hasStatus, hasRemovedAt, hasSpace, activeStatuses, hasLang, fieldForKeyword, filterByUserField } = this.config || {};
|
|
58
58
|
// This function should be overriden
|
|
59
59
|
if (hasStatus) {
|
|
60
|
-
query.whereIn(constants_1.FIELDS.STATUS
|
|
60
|
+
query.whereIn(`${table}.${constants_1.FIELDS.STATUS}`, Array.isArray(filterParams === null || filterParams === void 0 ? void 0 : filterParams[constants_1.FIELDS.STATUS]) ? filterParams === null || filterParams === void 0 ? void 0 : filterParams[constants_1.FIELDS.STATUS] : (0, lodash_1.castArray)(activeStatuses));
|
|
61
61
|
}
|
|
62
62
|
if (hasRemovedAt) {
|
|
63
|
-
query.whereNull(constants_1.FIELDS.REMOVED_AT);
|
|
63
|
+
query.whereNull(`${table}.${constants_1.FIELDS.REMOVED_AT}`);
|
|
64
64
|
}
|
|
65
65
|
if (hasSpace && ((filterParams === null || filterParams === void 0 ? void 0 : filterParams[constants_1.FIELDS.SPACE_ID]) != undefined || this.config.context.spaceId)) {
|
|
66
66
|
query.where({
|
|
67
|
-
[constants_1.FIELDS.SPACE_ID]: (filterParams === null || filterParams === void 0 ? void 0 : filterParams[constants_1.FIELDS.SPACE_ID]) != undefined ? filterParams === null || filterParams === void 0 ? void 0 : filterParams[constants_1.FIELDS.SPACE_ID] : this.config.context.spaceId,
|
|
67
|
+
[`${table}.${constants_1.FIELDS.SPACE_ID}`]: (filterParams === null || filterParams === void 0 ? void 0 : filterParams[constants_1.FIELDS.SPACE_ID]) != undefined ? filterParams === null || filterParams === void 0 ? void 0 : filterParams[constants_1.FIELDS.SPACE_ID] : this.config.context.spaceId,
|
|
68
68
|
});
|
|
69
69
|
}
|
|
70
70
|
if (hasLang && (filterParams === null || filterParams === void 0 ? void 0 : filterParams[constants_1.FIELDS.LANG]) != undefined) {
|
|
71
|
-
query.whereIn(constants_1.FIELDS.LANG
|
|
71
|
+
query.whereIn(`${table}.${constants_1.FIELDS.LANG}`, (0, lodash_1.castArray)(filterParams === null || filterParams === void 0 ? void 0 : filterParams[constants_1.FIELDS.LANG]));
|
|
72
72
|
}
|
|
73
73
|
if (filterParams === null || filterParams === void 0 ? void 0 : filterParams[this.getIdField()]) {
|
|
74
|
-
query.whereIn(constants_1.FIELDS.ID
|
|
74
|
+
query.whereIn(`${table}.${constants_1.FIELDS.ID}`, (0, lodash_1.castArray)(filterParams === null || filterParams === void 0 ? void 0 : filterParams[this.getIdField()]));
|
|
75
75
|
}
|
|
76
76
|
if (fieldForKeyword && filterParams.keyword) {
|
|
77
77
|
query.whereRaw(`${fieldForKeyword} ILIKE ?`, `%${filterParams.keyword}%`);
|
|
@@ -295,8 +295,8 @@ class BaseGateway {
|
|
|
295
295
|
if (!this.config.table) {
|
|
296
296
|
return [];
|
|
297
297
|
}
|
|
298
|
-
const shouldUseCache = useCache != null ? useCache : this.config.noCache;
|
|
299
|
-
const fieldsToUse = shouldUseCache && Array.isArray(this.config.selectFields) ? this.config.selectFields : this.getIdField();
|
|
298
|
+
const shouldUseCache = useCache != null ? useCache : !this.config.noCache;
|
|
299
|
+
const fieldsToUse = !shouldUseCache && Array.isArray(this.config.selectFields) ? this.config.selectFields : this.getIdField();
|
|
300
300
|
const query = this.buildListQuery({ filter, params, fieldsToUse });
|
|
301
301
|
yield this.onListFilter(query, filter);
|
|
302
302
|
if (limit && !isNaN(limit) && limit > 0) {
|
|
@@ -306,7 +306,8 @@ class BaseGateway {
|
|
|
306
306
|
query.orderBy(sortBy);
|
|
307
307
|
}
|
|
308
308
|
const items = yield query;
|
|
309
|
-
|
|
309
|
+
const result = shouldUseCache ? yield this.getMany(items.map((row) => row.id)) : items;
|
|
310
|
+
return result;
|
|
310
311
|
});
|
|
311
312
|
}
|
|
312
313
|
create(args) {
|
package/build/DataCache.js
CHANGED
|
@@ -24,6 +24,9 @@ class DataCache {
|
|
|
24
24
|
if (!(props === null || props === void 0 ? void 0 : props.idField)) {
|
|
25
25
|
props.idField = _1.FIELDS.ID;
|
|
26
26
|
}
|
|
27
|
+
if (!(props === null || props === void 0 ? void 0 : props.idFieldAs)) {
|
|
28
|
+
props.idFieldAs = _1.FIELDS.ID;
|
|
29
|
+
}
|
|
27
30
|
this.config = props;
|
|
28
31
|
this.redisClient = redisClient;
|
|
29
32
|
this.buildKey = typeof buildKey === 'function' ? buildKey : utils_1.buildKey;
|
|
@@ -33,7 +36,7 @@ class DataCache {
|
|
|
33
36
|
this.init(fn, prefixToUse, context);
|
|
34
37
|
}
|
|
35
38
|
batchLoaderDefault(props) {
|
|
36
|
-
const { table, idField, readAll, db } = props;
|
|
39
|
+
const { table, idField, idFieldAs, readAll, db } = props;
|
|
37
40
|
if (!table) {
|
|
38
41
|
throw Error(`DataCache requires 'table' property to be setup`);
|
|
39
42
|
}
|
|
@@ -48,7 +51,7 @@ class DataCache {
|
|
|
48
51
|
const readCfg = Object.assign(Object.assign({}, props), { buildKey: this.buildKey });
|
|
49
52
|
const idsToUse = (0, lodash_1.castArray)(ids);
|
|
50
53
|
if (!readAll) {
|
|
51
|
-
readCfg.mapBy = idField;
|
|
54
|
+
readCfg.mapBy = idFieldAs || idField;
|
|
52
55
|
readCfg.whereIn = {
|
|
53
56
|
field: idField,
|
|
54
57
|
values: idsToUse.filter((id) => id),
|
|
@@ -5,6 +5,9 @@ import { LoaderInterface } from './LoaderInterface';
|
|
|
5
5
|
import { SortItemInterface } from './SortItemInterface';
|
|
6
6
|
export interface BaseGatewayPropsInterface extends CacheInterface {
|
|
7
7
|
context: ApiContext;
|
|
8
|
+
table?: string;
|
|
9
|
+
idField?: string;
|
|
10
|
+
idFieldAs?: string;
|
|
8
11
|
setFields?: string[] | string;
|
|
9
12
|
mergeFields?: string[];
|
|
10
13
|
hasStatus?: boolean;
|
package/package.json
CHANGED