@autofleet/sadot 0.13.2-beta.6 → 0.13.2

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.
@@ -222,10 +222,7 @@ const formatDates = (fieldDefinitions, instance) => {
222
222
  throw new errors_2.InvalidValueError(value, name, validationError);
223
223
  }
224
224
  // eslint-disable-next-line no-param-reassign
225
- const typeofjoi = typeof joiValue;
226
- logger_1.default.info('sadot - formatting date', { name, value, joiValue, type: typeof joiValue });
227
225
  instance.customFields[name] = joiValue.toISOString();
228
- new Date().toString;
229
226
  }
230
227
  }
231
228
  });
package/dist/index.js CHANGED
@@ -54,10 +54,7 @@ const useCustomFields = async (app, getModel, options) => {
54
54
  }
55
55
  // The order is important
56
56
  (0, init_1.addHooks)(models, getModel, { useCustomFieldsEntries });
57
- await (0, models_1.initTables)(sequelize, options.getUser, { useCustomFieldsEntries }).catch((e) => {
58
- logger_1.default.error('sadot - error while initializing custom fields tables', e);
59
- throw e;
60
- });
57
+ await (0, models_1.initTables)(sequelize, options.getUser, { useCustomFieldsEntries });
61
58
  (0, init_1.addScopes)(models, getModel, { useCustomFieldsEntries });
62
59
  (0, init_1.applyCustomAssociation)(models);
63
60
  logger_1.default.debug('sadot - custom fields finished initializing with models', models);
@@ -27,11 +27,7 @@ const productionModels = [CustomFieldDefinition_1.default, CustomFieldValue_1.de
27
27
  const testModels = [TestModel_1.default, AssociatedTestModel_1.default, ContextAwareTestModel_1.default, ContextTestModel_1.default];
28
28
  const SADOT_MIGRATION_PREFIX = 'sadot-migration';
29
29
  const SCHEMA_VERSION = '49c9dd1d-b1cc-445b-a911-fd349d783110';
30
- const initTables = async (sequelize, getUser, { schemaPrefix, schemaVersion, useCustomFieldsEntries, } = {
31
- schemaPrefix: SADOT_MIGRATION_PREFIX,
32
- schemaVersion: SCHEMA_VERSION,
33
- useCustomFieldsEntries: false,
34
- }) => {
30
+ const initTables = async (sequelize, getUser, { schemaPrefix = SADOT_MIGRATION_PREFIX, schemaVersion = SCHEMA_VERSION, useCustomFieldsEntries = false, } = {}) => {
35
31
  const CUSTOM_FIELDS_SCHEMA_VERSION = `${schemaPrefix}_${schemaVersion}${useCustomFieldsEntries ? '_withEntries' : ''}`;
36
32
  logger_1.default.info('custom-fields: initialize custom-fields tables');
37
33
  // Detect models and import them to the orm
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@autofleet/sadot",
3
- "version": "0.13.2-beta.6",
3
+ "version": "0.13.2",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -258,10 +258,7 @@ const formatDates = (fieldDefinitions: CustomFieldDefinition[], instance: any) =
258
258
  throw new InvalidValueError(value, name, validationError);
259
259
  }
260
260
  // eslint-disable-next-line no-param-reassign
261
- const typeofjoi = typeof joiValue;
262
- logger.info('sadot - formatting date', { name, value, joiValue, type: typeof joiValue });
263
261
  instance.customFields[name] = joiValue.toISOString();
264
- new Date().toString
265
262
  }
266
263
  }
267
264
  });
@@ -278,72 +275,72 @@ export const beforeCreate = (
278
275
  instance,
279
276
  options,
280
277
  ): Promise<void> => {
281
- logger.debug('sadot - before create hook');
282
- const { fields } = options;
283
- const modelType = instance.constructor.name;
278
+ logger.debug('sadot - before create hook');
279
+ const { fields } = options;
280
+ const modelType = instance.constructor.name;
284
281
 
285
- const identifiers = applyScopeToInstance(instance, scopeAttributes);
282
+ const identifiers = applyScopeToInstance(instance, scopeAttributes);
286
283
 
287
- // Step 1: Handle custom fields default values and required fields
284
+ // Step 1: Handle custom fields default values and required fields
288
285
 
289
- const fieldDefinitions = await getFieldDefinitions({
290
- modelType, modelOptions, identifiers, options,
291
- });
286
+ const fieldDefinitions = await getFieldDefinitions({
287
+ modelType, modelOptions, identifiers, options,
288
+ });
292
289
 
293
- // Apply default values
294
- const fieldsWithDefaultValue = fieldDefinitions.filter((def) => ![null, undefined].includes(def.defaultValue));
295
- if (fieldsWithDefaultValue.length) {
296
- // eslint-disable-next-line no-param-reassign
297
- instance.customFields ||= {};
298
- fieldsWithDefaultValue
299
- .filter((def) => (instance.customFields?.[def.name] === undefined))
300
- .forEach(({ name, defaultValue }) => {
301
- // eslint-disable-next-line no-param-reassign
302
- instance.customFields[name] = defaultValue;
303
- });
304
- }
290
+ // Apply default values
291
+ const fieldsWithDefaultValue = fieldDefinitions.filter((def) => ![null, undefined].includes(def.defaultValue));
292
+ if (fieldsWithDefaultValue.length) {
293
+ // eslint-disable-next-line no-param-reassign
294
+ instance.customFields ||= {};
295
+ fieldsWithDefaultValue
296
+ .filter((def) => (instance.customFields?.[def.name] === undefined))
297
+ .forEach(({ name, defaultValue }) => {
298
+ // eslint-disable-next-line no-param-reassign
299
+ instance.customFields[name] = defaultValue;
300
+ });
301
+ }
305
302
 
306
- // Check for required fields
307
- const requiredFieldsNames = Array.from(
308
- new Set(fieldDefinitions.filter(({ required }) => required).map(({ name }) => name)),
309
- );
310
- const { customFields } = instance;
311
- const fieldsNames = Object.keys(customFields ?? {});
312
- const missingFields = requiredFieldsNames.filter((name) => !fieldsNames.includes(name));
313
- if (missingFields?.length) {
314
- throw new MissingRequiredCustomFieldError(missingFields);
315
- }
303
+ // Check for required fields
304
+ const requiredFieldsNames = Array.from(
305
+ new Set(fieldDefinitions.filter(({ required }) => required).map(({ name }) => name)),
306
+ );
307
+ const { customFields } = instance;
308
+ const fieldsNames = Object.keys(customFields ?? {});
309
+ const missingFields = requiredFieldsNames.filter((name) => !fieldsNames.includes(name));
310
+ if (missingFields?.length) {
311
+ throw new MissingRequiredCustomFieldError(missingFields);
312
+ }
316
313
 
317
- // Step 2: Validate the model data (including custom fields)
318
- await validateModel(instance, options, scopeAttributes, true);
314
+ // Step 2: Validate the model data (including custom fields)
315
+ await validateModel(instance, options, scopeAttributes, true);
319
316
 
320
- // format date and datetime fields
321
- formatDates(fieldDefinitions, instance);
317
+ // format date and datetime fields
318
+ formatDates(fieldDefinitions, instance);
322
319
 
323
- // Step 3: Save custom field values if they exist
324
- const customFieldsIdx = fields.indexOf('customFields');
325
- if (customFieldsIdx === -1 || !customFields || !Object.keys(customFields).length) {
326
- // No custom fields to update
327
- return;
328
- }
320
+ // Step 3: Save custom field values if they exist
321
+ const customFieldsIdx = fields.indexOf('customFields');
322
+ if (customFieldsIdx === -1 || !customFields || !Object.keys(customFields).length) {
323
+ // No custom fields to update
324
+ return;
325
+ }
329
326
 
330
- // Save custom field values
331
- await updateInstanceValues({
332
- modelId: instance.id,
333
- modelType,
334
- identifiers,
335
- customFields,
336
- options: {
337
- useCustomFieldsEntries: sadotOptions.useCustomFieldsEntries,
338
- transaction: options.transaction,
339
- modelOptions,
340
- },
341
- });
327
+ // Save custom field values
328
+ await updateInstanceValues({
329
+ modelId: instance.id,
330
+ modelType,
331
+ identifiers,
332
+ customFields,
333
+ options: {
334
+ useCustomFieldsEntries: sadotOptions.useCustomFieldsEntries,
335
+ transaction: options.transaction,
336
+ modelOptions,
337
+ },
338
+ });
342
339
 
343
- // Remove customFields from fields array after handling
344
- // eslint-disable-next-line no-param-reassign
345
- fields.splice(customFieldsIdx, 1);
346
- };
340
+ // Remove customFields from fields array after handling
341
+ // eslint-disable-next-line no-param-reassign
342
+ fields.splice(customFieldsIdx, 1);
343
+ };
347
344
 
348
345
  /**
349
346
  * Hook to handle validation and custom fields during update
@@ -356,48 +353,48 @@ export const beforeUpdate = (
356
353
  instance,
357
354
  options,
358
355
  ): Promise<void> => {
359
- logger.debug('sadot - before update hook');
360
- const { fields } = options;
361
- const modelType = instance.constructor.name;
362
- const identifiers = applyScopeToInstance(instance, scopeAttributes);
356
+ logger.debug('sadot - before update hook');
357
+ const { fields } = options;
358
+ const modelType = instance.constructor.name;
359
+ const identifiers = applyScopeToInstance(instance, scopeAttributes);
363
360
 
364
- const fieldDefinitions = await getFieldDefinitions({
365
- modelType, modelOptions, identifiers, options,
366
- });
361
+ const fieldDefinitions = await getFieldDefinitions({
362
+ modelType, modelOptions, identifiers, options,
363
+ });
367
364
 
368
- // Step 1: Validate the model data (including custom fields)
369
- await validateModel(instance, options, scopeAttributes, false);
365
+ // Step 1: Validate the model data (including custom fields)
366
+ await validateModel(instance, options, scopeAttributes, false);
370
367
 
371
- // format date and datetime fields
372
- formatDates(fieldDefinitions, instance);
368
+ // format date and datetime fields
369
+ formatDates(fieldDefinitions, instance);
373
370
 
374
- // Step 2: Update custom field values if they exist
375
- const customFieldsIdx = fields.indexOf('customFields');
376
- if (customFieldsIdx > -1) {
377
- const { customFields } = instance;
371
+ // Step 2: Update custom field values if they exist
372
+ const customFieldsIdx = fields.indexOf('customFields');
373
+ if (customFieldsIdx > -1) {
374
+ const { customFields } = instance;
378
375
 
379
- if (!Object.keys(customFields).length) {
380
- return;
381
- }
376
+ if (!Object.keys(customFields).length) {
377
+ return;
378
+ }
382
379
 
383
- // Save custom field values
384
- await updateInstanceValues({
385
- modelId: instance.id,
386
- modelType,
387
- identifiers,
388
- customFields,
389
- options: {
390
- useCustomFieldsEntries: sadotOptions.useCustomFieldsEntries,
391
- transaction: options.transaction,
392
- modelOptions,
393
- },
394
- });
380
+ // Save custom field values
381
+ await updateInstanceValues({
382
+ modelId: instance.id,
383
+ modelType,
384
+ identifiers,
385
+ customFields,
386
+ options: {
387
+ useCustomFieldsEntries: sadotOptions.useCustomFieldsEntries,
388
+ transaction: options.transaction,
389
+ modelOptions,
390
+ },
391
+ });
395
392
 
396
- // Remove customFields from fields array after handling
397
- // eslint-disable-next-line no-param-reassign
398
- fields.splice(customFieldsIdx, 1);
399
- }
400
- };
393
+ // Remove customFields from fields array after handling
394
+ // eslint-disable-next-line no-param-reassign
395
+ fields.splice(customFieldsIdx, 1);
396
+ }
397
+ };
401
398
 
402
399
  /**
403
400
  * Hook to enable individual hooks for bulk create operations
package/src/index.ts CHANGED
@@ -37,10 +37,7 @@ const useCustomFields = async (
37
37
  }
38
38
  // The order is important
39
39
  addHooks(models, getModel, { useCustomFieldsEntries });
40
- await initTables(sequelize, options.getUser, { useCustomFieldsEntries }).catch((e) => {
41
- logger.error('sadot - error while initializing custom fields tables', e);
42
- throw e;
43
- });
40
+ await initTables(sequelize, options.getUser, { useCustomFieldsEntries });
44
41
  addScopes(models, getModel, { useCustomFieldsEntries });
45
42
  applyCustomAssociation(models);
46
43
 
@@ -32,7 +32,7 @@ const initTables = async (
32
32
  schemaPrefix = SADOT_MIGRATION_PREFIX,
33
33
  schemaVersion = SCHEMA_VERSION,
34
34
  useCustomFieldsEntries = false,
35
- }: InitTablesOptions,
35
+ }: InitTablesOptions = {},
36
36
  ): Promise<void> => {
37
37
  const CUSTOM_FIELDS_SCHEMA_VERSION = `${schemaPrefix}_${schemaVersion}${useCustomFieldsEntries ? '_withEntries' : ''}`;
38
38
  logger.info('custom-fields: initialize custom-fields tables');
@@ -1,9 +0,0 @@
1
- const SADOT_MIGRATION_PREFIX = 'sadot-migration';
2
- const SCHEMA_VERSION = '49c9dd1d-b1cc-445b-a911-fd349d783110';
3
- const initTables = async ({ schemaPrefix = SADOT_MIGRATION_PREFIX, schemaVersion = SCHEMA_VERSION, useCustomFieldsEntries = false }) => {
4
- console.log('custom-fields: initialize custom-fields tables', {
5
- schemaPrefix, schemaVersion, useCustomFieldsEntries,
6
- });
7
- }
8
-
9
- initTables({useCustomFieldsEntries: true});