@sdflc/backend-helpers 1.0.70 → 1.0.72

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/BaseCore.js CHANGED
@@ -719,7 +719,7 @@ class BaseCore {
719
719
  return __awaiter(this, void 0, void 0, function* () {
720
720
  const { req, space, authorization } = args;
721
721
  const token = ((_a = req.cookies) === null || _a === void 0 ? void 0 : _a[space.cookieName]) || authorization;
722
- const fingerprint = ((_b = req.cookies) === null || _b === void 0 ? void 0 : _b[space.fingerprintCookieName]) || '';
722
+ const fingerprint = ((_b = req.cookies) === null || _b === void 0 ? void 0 : _b[space.fingerprintCookieName]) || args.fingerprint || '';
723
723
  return {
724
724
  token,
725
725
  fingerprint,
@@ -737,7 +737,13 @@ class BaseCore {
737
737
  if (spaceResult !== true) {
738
738
  return [spaceResult, space];
739
739
  }
740
- const { token, fingerprint } = yield this.extractCredentials({ req, authorization, space, params });
740
+ const { token, fingerprint } = yield this.extractCredentials({
741
+ req,
742
+ authorization,
743
+ fingerprint: context.fingerprint,
744
+ space,
745
+ params,
746
+ });
741
747
  const { jwtSecret, verifyTokenArgs } = yield this.getJwtTokenSecretAndSettings(params);
742
748
  if (token && jwtSecret) {
743
749
  const verifyResult = (0, userToken_1.verifyUserToken)(token, jwtSecret, Object.assign(Object.assign({}, verifyTokenArgs), { algorithms: (verifyTokenArgs === null || verifyTokenArgs === void 0 ? void 0 : verifyTokenArgs.algorithms) || [(0, userToken_1.getDefaultJwtAlgorithm)()], audience: (verifyTokenArgs === null || verifyTokenArgs === void 0 ? void 0 : verifyTokenArgs.audience) || space.id, issuer: (verifyTokenArgs === null || verifyTokenArgs === void 0 ? void 0 : verifyTokenArgs.issuer) || (0, userToken_1.getDefaultJwtIssuer)(), fingerprint }));
@@ -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, 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));
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, (0, lodash_1.castArray)(filterParams === null || filterParams === void 0 ? void 0 : filterParams[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, (0, lodash_1.castArray)(filterParams === null || filterParams === void 0 ? void 0 : filterParams[this.getIdField()]));
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,9 +295,9 @@ 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();
300
- const query = this.buildListQuery({ filter, params, fieldsToUse });
298
+ const shouldUseCache = useCache != null ? useCache : !this.config.noCache;
299
+ const fieldsToUse = !shouldUseCache && Array.isArray(this.config.selectFields) ? this.config.selectFields : this.getIdField();
300
+ const query = this.buildListQuery({ filter, params, fieldsToUse, useCache: shouldUseCache });
301
301
  yield this.onListFilter(query, filter);
302
302
  if (limit && !isNaN(limit) && limit > 0) {
303
303
  query.offset(offset).limit(limit);
@@ -306,7 +306,8 @@ class BaseGateway {
306
306
  query.orderBy(sortBy);
307
307
  }
308
308
  const items = yield query;
309
- return shouldUseCache ? items : yield this.getMany(items.map((row) => row.id));
309
+ const result = shouldUseCache ? yield this.getMany(items.map((row) => row.id)) : items;
310
+ return result;
310
311
  });
311
312
  }
312
313
  create(args) {
@@ -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;
@@ -2,4 +2,5 @@ export interface BuildListQueryArgsInterface {
2
2
  filter: any;
3
3
  params: any;
4
4
  fieldsToUse: any;
5
+ useCache: boolean;
5
6
  }
@@ -5,6 +5,7 @@ export interface CacheInterface {
5
5
  context?: any;
6
6
  table?: string;
7
7
  idField?: string;
8
+ idFieldAs?: string;
8
9
  selectFields?: any[];
9
10
  readAll?: boolean;
10
11
  redisClient?: any;
@@ -11,6 +11,7 @@ export interface ExtractCredentialsArgs {
11
11
  * Authorization is a token from the Authorization header
12
12
  */
13
13
  authorization?: string;
14
+ fingerprint?: string;
14
15
  /**
15
16
  * An object with parameters passed to the authenticate methods
16
17
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sdflc/backend-helpers",
3
- "version": "1.0.70",
3
+ "version": "1.0.72",
4
4
  "description": "Set of helper functions and classes to simplify building backend apps within @sdflc ecosystem",
5
5
  "main": "build/index.js",
6
6
  "types": "build/index.d.ts",