@autofleet/sadot 1.1.4-beta → 1.1.5-beta

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.
@@ -90,7 +90,7 @@ const getCompleteCustomFields = async (instance, options) => {
90
90
  }
91
91
  return instance.customFields || {};
92
92
  };
93
- const buildBeforeFromInstance = (instance) => {
93
+ const buildPreChangeState = (instance) => {
94
94
  const beforeFull = { ...instance.dataValues };
95
95
  const changedKeys = instance.changed?.() || [];
96
96
  changedKeys.forEach((key) => {
@@ -150,10 +150,7 @@ const validateModel = async (instance, options, scopeAttributes, modelOptions =
150
150
  validatorsPromise = ValidatorRepo.findAllByModelType(modelType, entityId, {
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
  if (options.transaction) {
159
156
  options?.transaction?.validationsCache.set(cacheKey, validatorsPromise);
@@ -168,7 +165,7 @@ const validateModel = async (instance, options, scopeAttributes, modelOptions =
168
165
  // For updates, get the previous values
169
166
  let originalValues = null;
170
167
  if (!isCreate) {
171
- originalValues = buildBeforeFromInstance(instance);
168
+ originalValues = buildPreChangeState(instance);
172
169
  }
173
170
  // Get complete custom fields by merging DB values with update values
174
171
  // This is especially important for partial updates to ensure all related fields are available
@@ -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,12 @@ 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
+ }) => Promise<CustomValidator[]>;
20
26
  export declare const update: (id: string, updates: Partial<ValidatorAttributes>, options?: Transactionable) => Promise<[number, CustomValidator[]]>;
21
27
  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.1.4-beta",
3
+ "version": "1.1.5-beta",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -78,7 +78,7 @@ const getCompleteCustomFields = async (instance, options): Promise<Record<string
78
78
  return instance.customFields || {};
79
79
  };
80
80
 
81
- const buildBeforeFromInstance = (instance: any) => {
81
+ const buildPreChangeState = (instance: any) => {
82
82
  const beforeFull: any = { ...instance.dataValues };
83
83
 
84
84
  const changedKeys: string[] = instance.changed?.() || [];
@@ -191,7 +191,7 @@ const validateModel = async (
191
191
  // For updates, get the previous values
192
192
  let originalValues = null;
193
193
  if (!isCreate) {
194
- originalValues = buildBeforeFromInstance(instance);
194
+ originalValues = buildPreChangeState(instance);
195
195
  }
196
196
 
197
197
  // Get complete custom fields by merging DB values with update values