@autofleet/sadot 1.0.0 → 1.0.1

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.
@@ -135,10 +135,7 @@ const validateModel = async (instance, options, scopeAttributes, modelOptions =
135
135
  validatorsPromise = ValidatorRepo.findAllByModelType(modelType, entityId, {
136
136
  transaction: options.transaction,
137
137
  attributes: CUSTOM_VALIDATOR_ATTRIBUTES_TO_PULL,
138
- ...(modelOptions.include && {
139
- include: modelOptions.include?.(entityId),
140
- }),
141
- raw: true,
138
+ modelOptions,
142
139
  });
143
140
  if (options.transaction) {
144
141
  options?.transaction?.validationsCache.set(cacheKey, validatorsPromise);
@@ -1,5 +1,6 @@
1
- import type { IncludeOptions, Transactionable } from 'sequelize';
1
+ import type { FindOptions, IncludeOptions, Transactionable } from 'sequelize';
2
2
  import { CustomValidator } from '../models';
3
+ import type { ModelOptions } from '../types';
3
4
  export interface FindValidatorOptions extends Transactionable {
4
5
  withDisabled?: boolean;
5
6
  attributes?: string[];
@@ -15,7 +16,13 @@ export interface ValidatorAttributes {
15
16
  [key: string]: unknown;
16
17
  }
17
18
  export declare const create: (validatorAttributes: ValidatorAttributes, options?: Transactionable) => Promise<CustomValidator>;
18
- export declare const findAll: (where?: {}, options?: FindValidatorOptions) => Promise<CustomValidator[]>;
19
- export declare const findAllByModelType: (modelType: string, entityId: string, options?: FindValidatorOptions) => Promise<CustomValidator[]>;
19
+ export declare const findAll: (where?: {}, options?: FindOptions & {
20
+ modelOptions?: ModelOptions;
21
+ withDisabled?: boolean;
22
+ }) => Promise<CustomValidator[]>;
23
+ export declare const findAllByModelType: (modelType: string, entityId: string, options?: FindOptions & {
24
+ modelOptions?: ModelOptions;
25
+ withDisabled?: boolean;
26
+ }) => Promise<CustomValidator[]>;
20
27
  export declare const update: (id: string, updates: Partial<ValidatorAttributes>, options?: Transactionable) => Promise<[number, CustomValidator[]]>;
21
28
  export declare const disable: (id: string, options?: Transactionable) => Promise<[number, CustomValidator[]]>;
@@ -13,9 +13,9 @@ const create = async (validatorAttributes, options = {}) => {
13
13
  return validator;
14
14
  };
15
15
  exports.create = create;
16
- const findAll = async (where = {}, options = { withDisabled: false }) => {
16
+ const findAll = async (where = {}, options = {}) => {
17
17
  logger_1.default.debug('custom-validator - find all validators');
18
- const { transaction, withDisabled } = options;
18
+ const { transaction, withDisabled, include, attributes, raw, } = options;
19
19
  let validators;
20
20
  if (withDisabled) {
21
21
  // If withDisabled is true, use unscoped to ignore the default scope that filters disabled items
@@ -23,6 +23,9 @@ const findAll = async (where = {}, options = { withDisabled: false }) => {
23
23
  validators = await models_1.CustomValidator.unscoped().scope('userScope').findAll({
24
24
  where,
25
25
  transaction,
26
+ include,
27
+ attributes,
28
+ raw,
26
29
  });
27
30
  }
28
31
  else {
@@ -31,19 +34,23 @@ const findAll = async (where = {}, options = { withDisabled: false }) => {
31
34
  validators = await models_1.CustomValidator.scope(['defaultScope', 'userScope']).findAll({
32
35
  where,
33
36
  transaction,
37
+ include,
38
+ attributes,
39
+ raw,
34
40
  });
35
41
  }
36
42
  return validators;
37
43
  };
38
44
  exports.findAll = findAll;
39
- const findAllByModelType = async (modelType, entityId, options = { withDisabled: false }) => {
45
+ const findAllByModelType = async (modelType, entityId, options = {}) => {
40
46
  logger_1.default.debug('custom-validator - find all validators by model type');
41
47
  return (0, exports.findAll)({
42
48
  modelType,
43
- ...(!options.include && {
44
- entityId,
45
- }),
46
- }, options);
49
+ ...(!options?.modelOptions?.useEntityIdFromInclude && { entityId }),
50
+ }, {
51
+ ...options,
52
+ include: options?.modelOptions?.include?.(entityId),
53
+ });
47
54
  };
48
55
  exports.findAllByModelType = findAllByModelType;
49
56
  const update = async (id, updates, options) => {
@@ -14,7 +14,7 @@ export type ModelOptions = {
14
14
  /**
15
15
  * Include options for the model
16
16
  */
17
- include?: (entityIds: string[]) => IncludeOptions[];
17
+ include?: (entityIds: string | string[]) => IncludeOptions[];
18
18
  /**
19
19
  * Custom association for the model
20
20
  * @param model - The model to associate with
@@ -106,6 +106,7 @@ exports.addScopes = addScopes;
106
106
  const applyCustomAssociation = (models) => {
107
107
  models.forEach(({ modelOptions }) => {
108
108
  modelOptions?.customAssociation?.(models_1.CustomFieldDefinition);
109
+ modelOptions?.customAssociation?.(models_1.CustomValidator);
109
110
  });
110
111
  };
111
112
  exports.applyCustomAssociation = applyCustomAssociation;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@autofleet/sadot",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -150,10 +150,7 @@ const validateModel = async (
150
150
  {
151
151
  transaction: options.transaction,
152
152
  attributes: CUSTOM_VALIDATOR_ATTRIBUTES_TO_PULL,
153
- ...(modelOptions.include && {
154
- include: modelOptions.include?.(entityId),
155
- }),
156
- raw: true,
153
+ modelOptions,
157
154
  },
158
155
  );
159
156
  if (options.transaction) {
@@ -1,6 +1,7 @@
1
- import type { IncludeOptions, Transactionable } from 'sequelize';
1
+ import type { FindOptions, IncludeOptions, Transactionable } from 'sequelize';
2
2
  import logger from '../utils/logger';
3
3
  import { CustomValidator } from '../models';
4
+ import type { ModelOptions } from '../types';
4
5
 
5
6
  export interface FindValidatorOptions extends Transactionable {
6
7
  withDisabled?: boolean;
@@ -33,11 +34,16 @@ export const create = async (
33
34
 
34
35
  export const findAll = async (
35
36
  where = {},
36
- options: FindValidatorOptions = { withDisabled: false },
37
+ options: FindOptions & {
38
+ modelOptions?: ModelOptions,
39
+ withDisabled?: boolean
40
+ } = {},
37
41
  ): Promise<CustomValidator[]> => {
38
42
  logger.debug('custom-validator - find all validators');
39
43
 
40
- const { transaction, withDisabled } = options;
44
+ const {
45
+ transaction, withDisabled, include, attributes, raw,
46
+ } = options;
41
47
 
42
48
  let validators;
43
49
  if (withDisabled) {
@@ -46,6 +52,9 @@ export const findAll = async (
46
52
  validators = await CustomValidator.unscoped().scope('userScope').findAll({
47
53
  where,
48
54
  transaction,
55
+ include,
56
+ attributes,
57
+ raw,
49
58
  });
50
59
  } else {
51
60
  // Use defaultScope and userScope to filter both disabled and by permissions
@@ -53,6 +62,9 @@ export const findAll = async (
53
62
  validators = await CustomValidator.scope(['defaultScope', 'userScope']).findAll({
54
63
  where,
55
64
  transaction,
65
+ include,
66
+ attributes,
67
+ raw,
56
68
  });
57
69
  }
58
70
 
@@ -62,18 +74,21 @@ export const findAll = async (
62
74
  export const findAllByModelType = async (
63
75
  modelType: string,
64
76
  entityId: string,
65
- options: FindValidatorOptions = { withDisabled: false },
77
+ options: FindOptions & {
78
+ modelOptions?: ModelOptions,
79
+ withDisabled?: boolean
80
+ } = {},
66
81
  ): Promise<CustomValidator[]> => {
67
82
  logger.debug('custom-validator - find all validators by model type');
68
-
69
83
  return findAll(
70
84
  {
71
85
  modelType,
72
- ...(!options.include && {
73
- entityId,
74
- }),
86
+ ...(!options?.modelOptions?.useEntityIdFromInclude && { entityId }),
87
+ },
88
+ {
89
+ ...options,
90
+ include: options?.modelOptions?.include?.(entityId),
75
91
  },
76
- options,
77
92
  );
78
93
  };
79
94
 
@@ -17,7 +17,7 @@ export type ModelOptions = {
17
17
  /**
18
18
  * Include options for the model
19
19
  */
20
- include?: (entityIds: string[]) => IncludeOptions[];
20
+ include?: (entityIds: string | string[]) => IncludeOptions[];
21
21
  /**
22
22
  * Custom association for the model
23
23
  * @param model - The model to associate with
package/src/utils/init.ts CHANGED
@@ -5,6 +5,7 @@ import { CUSTOM_FIELDS_SORT_SCOPE } from '@autofleet/common-types/lib/custom-fie
5
5
  import {
6
6
  CustomFieldDefinition,
7
7
  CustomFieldValue,
8
+ CustomValidator,
8
9
  } from '../models';
9
10
  import {
10
11
  beforeFind,
@@ -116,5 +117,6 @@ export const addScopes = (models: Models[], getModel: ModelFetcher, options: Pic
116
117
  export const applyCustomAssociation = (models: Models[]): void => {
117
118
  models.forEach(({ modelOptions }) => {
118
119
  modelOptions?.customAssociation?.(CustomFieldDefinition);
120
+ modelOptions?.customAssociation?.(CustomValidator);
119
121
  });
120
122
  };