@punks/backend-entity-manager 0.0.364 → 0.0.366

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.
@@ -24,6 +24,7 @@ export type EntitySerializerColumnValidator<TSheetItem> = {
24
24
  key: string;
25
25
  fn: (item: TSheetItem) => EntryValidationResult;
26
26
  };
27
+ export type EntitySerializerSheetCustomParser = "date";
27
28
  export type EntitySerializerColumnDefinition<TSheetItem> = {
28
29
  name: string;
29
30
  key: string;
@@ -35,6 +36,7 @@ export type EntitySerializerColumnDefinition<TSheetItem> = {
35
36
  converter?: EntitySerializerColumnConverter<TSheetItem>;
36
37
  validators?: EntitySerializerColumnValidator<TSheetItem>[];
37
38
  idColumn?: boolean;
39
+ sheetParser?: EntitySerializerSheetCustomParser;
38
40
  };
39
41
  export type EntitySerializerSheetDefinition<TSheetItem> = {
40
42
  columns: EntitySerializerColumnDefinition<TSheetItem>[];
package/dist/esm/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { Log, csvParse, excelParse, excelBuild, csvBuild, mapAsync, isNullOrUndefined, addTime, newUuid as newUuid$1, buildObject, toDict, sleep, sort, byField, toArrayDict, toItemsDict, ensureTailingSlash, ensureStartSlash, removeUndefinedProps } from '@punks/backend-core';
1
+ import { Log, csvParse, excelParse, ExcelKeyTransform, excelBuild, csvBuild, mapAsync, isNullOrUndefined, addTime, newUuid as newUuid$1, buildObject, toDict, sleep, sort, byField, toArrayDict, toItemsDict, ensureTailingSlash, ensureStartSlash, removeUndefinedProps } from '@punks/backend-core';
2
2
  import { QueryCommand, ScanCommand, GetItemCommand, PutItemCommand, DeleteItemCommand, DynamoDBClient } from '@aws-sdk/client-dynamodb';
3
3
  import { unmarshall, marshall } from '@aws-sdk/util-dynamodb';
4
4
  import { Module, applyDecorators, Injectable, SetMetadata, createParamDecorator, Global, Scope, Inject, Logger, StreamableFile, HttpException, HttpStatus } from '@nestjs/common';
@@ -177,6 +177,7 @@ class EntitySeeder {
177
177
  }
178
178
  }
179
179
 
180
+ const normalizeSheetColumn = (column) => column.replace(/ /g, "").toLowerCase();
180
181
  const DEFAULT_DELIMITER = ";";
181
182
  class EntitySerializer {
182
183
  constructor(services, options) {
@@ -215,7 +216,11 @@ class EntitySerializer {
215
216
  return records.map((x, i) => this.convertSheetRecord(x, definition, i));
216
217
  }
217
218
  parseXlsx(data, definition) {
218
- const records = excelParse(data);
219
+ const dateColumns = definition.columns.filter((x) => x.sheetParser === "date");
220
+ const records = excelParse(data, {
221
+ keysTransform: ExcelKeyTransform.Lower,
222
+ dateColumns: dateColumns.map((x) => x.name),
223
+ });
219
224
  return records.map((x, i) => this.convertSheetRecord(x, definition, i));
220
225
  }
221
226
  convertSheetRecord(record, definition, rowIndex) {
@@ -225,8 +230,9 @@ class EntitySerializer {
225
230
  }
226
231
  const entity = {};
227
232
  for (const column of definition.columns) {
233
+ const columnName = normalizeSheetColumn(column.name);
228
234
  if (column.parseAction) {
229
- column.parseAction(record[column.name], entity);
235
+ column.parseAction(record[columnName], entity);
230
236
  continue;
231
237
  }
232
238
  if (typeof column.selector === "function") {
@@ -263,7 +269,8 @@ class EntitySerializer {
263
269
  };
264
270
  }
265
271
  parseColumnValue(row, definition) {
266
- const rawValue = row[definition.name];
272
+ const columnName = normalizeSheetColumn(definition.name);
273
+ const rawValue = row[columnName];
267
274
  return definition.parser ? definition.parser(rawValue) : rawValue;
268
275
  }
269
276
  async createSample(format) {
@@ -22215,26 +22222,6 @@ MediaLibraryService = __decorate([
22215
22222
  __metadata("design:paramtypes", [EntityManagerRegistry])
22216
22223
  ], MediaLibraryService);
22217
22224
 
22218
- function toInteger(dirtyNumber) {
22219
- if (dirtyNumber === null || dirtyNumber === true || dirtyNumber === false) {
22220
- return NaN;
22221
- }
22222
-
22223
- var number = Number(dirtyNumber);
22224
-
22225
- if (isNaN(number)) {
22226
- return number;
22227
- }
22228
-
22229
- return number < 0 ? Math.ceil(number) : Math.floor(number);
22230
- }
22231
-
22232
- function requiredArgs(required, args) {
22233
- if (args.length < required) {
22234
- throw new TypeError(required + ' argument' + (required > 1 ? 's' : '') + ' required, but only ' + args.length + ' present');
22235
- }
22236
- }
22237
-
22238
22225
  /**
22239
22226
  * @name toDate
22240
22227
  * @category Common Helpers
@@ -22251,9 +22238,11 @@ function requiredArgs(required, args) {
22251
22238
  *
22252
22239
  * **Note**: *all* Date arguments passed to any *date-fns* function is processed by `toDate`.
22253
22240
  *
22254
- * @param {Date|Number} argument - the value to convert
22255
- * @returns {Date} the parsed date in the local time zone
22256
- * @throws {TypeError} 1 argument required
22241
+ * @typeParam DateType - The `Date` type, the function operates on. Gets inferred from passed arguments. Allows to use extensions like [`UTCDate`](https://github.com/date-fns/utc).
22242
+ *
22243
+ * @param argument - The value to convert
22244
+ *
22245
+ * @returns The parsed date in the local time zone
22257
22246
  *
22258
22247
  * @example
22259
22248
  * // Clone the date:
@@ -22265,28 +22254,68 @@ function requiredArgs(required, args) {
22265
22254
  * const result = toDate(1392098430000)
22266
22255
  * //=> Tue Feb 11 2014 11:30:30
22267
22256
  */
22268
-
22269
22257
  function toDate(argument) {
22270
- requiredArgs(1, arguments);
22271
- var argStr = Object.prototype.toString.call(argument); // Clone the date
22258
+ const argStr = Object.prototype.toString.call(argument);
22272
22259
 
22273
- if (argument instanceof Date || typeof argument === 'object' && argStr === '[object Date]') {
22260
+ // Clone the date
22261
+ if (
22262
+ argument instanceof Date ||
22263
+ (typeof argument === "object" && argStr === "[object Date]")
22264
+ ) {
22274
22265
  // Prevent the date to lose the milliseconds when passed to new Date() in IE10
22275
- return new Date(argument.getTime());
22276
- } else if (typeof argument === 'number' || argStr === '[object Number]') {
22266
+ return new argument.constructor(+argument);
22267
+ } else if (
22268
+ typeof argument === "number" ||
22269
+ argStr === "[object Number]" ||
22270
+ typeof argument === "string" ||
22271
+ argStr === "[object String]"
22272
+ ) {
22273
+ // TODO: Can we get rid of as?
22277
22274
  return new Date(argument);
22278
22275
  } else {
22279
- if ((typeof argument === 'string' || argStr === '[object String]') && typeof console !== 'undefined') {
22280
- // eslint-disable-next-line no-console
22281
- console.warn("Starting with v2.0.0-beta.1 date-fns doesn't accept strings as date arguments. Please use `parseISO` to parse strings. See: https://git.io/fjule"); // eslint-disable-next-line no-console
22282
-
22283
- console.warn(new Error().stack);
22284
- }
22285
-
22276
+ // TODO: Can we get rid of as?
22286
22277
  return new Date(NaN);
22287
22278
  }
22288
22279
  }
22289
22280
 
22281
+ /**
22282
+ * @name constructFrom
22283
+ * @category Generic Helpers
22284
+ * @summary Constructs a date using the reference date and the value
22285
+ *
22286
+ * @description
22287
+ * The function constructs a new date using the constructor from the reference
22288
+ * date and the given value. It helps to build generic functions that accept
22289
+ * date extensions.
22290
+ *
22291
+ * It defaults to `Date` if the passed reference date is a number or a string.
22292
+ *
22293
+ * @typeParam DateType - The `Date` type, the function operates on. Gets inferred from passed arguments. Allows to use extensions like [`UTCDate`](https://github.com/date-fns/utc).
22294
+ *
22295
+ * @param date - The reference date to take constructor from
22296
+ * @param value - The value to create the date
22297
+ *
22298
+ * @returns Date initialized using the given date and value
22299
+ *
22300
+ * @example
22301
+ * import { constructFrom } from 'date-fns'
22302
+ *
22303
+ * // A function that clones a date preserving the original type
22304
+ * function cloneDate<DateType extends Date(date: DateType): DateType {
22305
+ * return constructFrom(
22306
+ * date, // Use contrustor from the given date
22307
+ * date.getTime() // Use the date value to create a new date
22308
+ * )
22309
+ * }
22310
+ */
22311
+ function constructFrom(date, value) {
22312
+ if (date instanceof Date) {
22313
+ return new date.constructor(value);
22314
+ } else {
22315
+ return new Date(value);
22316
+ }
22317
+ }
22318
+
22290
22319
  /**
22291
22320
  * @name addDays
22292
22321
  * @category Day Helpers
@@ -22295,37 +22324,60 @@ function toDate(argument) {
22295
22324
  * @description
22296
22325
  * Add the specified number of days to the given date.
22297
22326
  *
22298
- * ### v2.0.0 breaking changes:
22327
+ * @typeParam DateType - The `Date` type, the function operates on. Gets inferred from passed arguments. Allows to use extensions like [`UTCDate`](https://github.com/date-fns/utc).
22299
22328
  *
22300
- * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes).
22329
+ * @param date - The date to be changed
22330
+ * @param amount - The amount of days to be added.
22301
22331
  *
22302
- * @param {Date|Number} date - the date to be changed
22303
- * @param {Number} amount - the amount of days to be added. Positive decimals will be rounded using `Math.floor`, decimals less than zero will be rounded using `Math.ceil`.
22304
- * @returns {Date} the new date with the days added
22305
- * @throws {TypeError} 2 arguments required
22332
+ * @returns The new date with the days added
22306
22333
  *
22307
22334
  * @example
22308
22335
  * // Add 10 days to 1 September 2014:
22309
- * var result = addDays(new Date(2014, 8, 1), 10)
22336
+ * const result = addDays(new Date(2014, 8, 1), 10)
22310
22337
  * //=> Thu Sep 11 2014 00:00:00
22311
22338
  */
22312
-
22313
- function addDays(dirtyDate, dirtyAmount) {
22314
- requiredArgs(2, arguments);
22315
- var date = toDate(dirtyDate);
22316
- var amount = toInteger(dirtyAmount);
22317
-
22318
- if (isNaN(amount)) {
22319
- return new Date(NaN);
22320
- }
22321
-
22339
+ function addDays(date, amount) {
22340
+ const _date = toDate(date);
22341
+ if (isNaN(amount)) return constructFrom(date, NaN);
22322
22342
  if (!amount) {
22323
22343
  // If 0 days, no-op to avoid changing times in the hour before end of DST
22324
- return date;
22344
+ return _date;
22325
22345
  }
22346
+ _date.setDate(_date.getDate() + amount);
22347
+ return _date;
22348
+ }
22349
+
22350
+ /**
22351
+ * @module constants
22352
+ * @summary Useful constants
22353
+ * @description
22354
+ * Collection of useful date constants.
22355
+ *
22356
+ * The constants could be imported from `date-fns/constants`:
22357
+ *
22358
+ * ```ts
22359
+ * import { maxTime, minTime } from "./constants/date-fns/constants";
22360
+ *
22361
+ * function isAllowedTime(time) {
22362
+ * return time <= maxTime && time >= minTime;
22363
+ * }
22364
+ * ```
22365
+ */
22326
22366
 
22327
- date.setDate(date.getDate() + amount);
22328
- return date;
22367
+ /**
22368
+ * @constant
22369
+ * @name millisecondsInMinute
22370
+ * @summary Milliseconds in 1 minute
22371
+ */
22372
+ const millisecondsInMinute = 60000;
22373
+
22374
+ function getRoundingMethod(method) {
22375
+ return (number) => {
22376
+ const round = method ? Math[method] : Math.trunc;
22377
+ const result = round(number);
22378
+ // Prevent negative zero
22379
+ return result === 0 ? 0 : result;
22380
+ };
22329
22381
  }
22330
22382
 
22331
22383
  /**
@@ -22336,33 +22388,30 @@ function addDays(dirtyDate, dirtyAmount) {
22336
22388
  * @description
22337
22389
  * Get the number of milliseconds between the given dates.
22338
22390
  *
22339
- * ### v2.0.0 breaking changes:
22391
+ * @typeParam DateType - The `Date` type, the function operates on. Gets inferred from passed arguments. Allows to use extensions like [`UTCDate`](https://github.com/date-fns/utc).
22340
22392
  *
22341
- * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes).
22393
+ * @param dateLeft - The later date
22394
+ * @param dateRight - The earlier date
22342
22395
  *
22343
- * @param {Date|Number} dateLeft - the later date
22344
- * @param {Date|Number} dateRight - the earlier date
22345
- * @returns {Number} the number of milliseconds
22346
- * @throws {TypeError} 2 arguments required
22396
+ * @returns The number of milliseconds
22347
22397
  *
22348
22398
  * @example
22349
22399
  * // How many milliseconds are between
22350
22400
  * // 2 July 2014 12:30:20.600 and 2 July 2014 12:30:21.700?
22351
- * var result = differenceInMilliseconds(
22401
+ * const result = differenceInMilliseconds(
22352
22402
  * new Date(2014, 6, 2, 12, 30, 21, 700),
22353
22403
  * new Date(2014, 6, 2, 12, 30, 20, 600)
22354
22404
  * )
22355
22405
  * //=> 1100
22356
22406
  */
22357
-
22358
- function differenceInMilliseconds(dirtyDateLeft, dirtyDateRight) {
22359
- requiredArgs(2, arguments);
22360
- var dateLeft = toDate(dirtyDateLeft);
22361
- var dateRight = toDate(dirtyDateRight);
22362
- return dateLeft.getTime() - dateRight.getTime();
22407
+ function differenceInMilliseconds(dateLeft, dateRight) {
22408
+ return +toDate(dateLeft) - +toDate(dateRight);
22363
22409
  }
22364
22410
 
22365
- var MILLISECONDS_IN_MINUTE = 60000;
22411
+ /**
22412
+ * The {@link differenceInMinutes} function options.
22413
+ */
22414
+
22366
22415
  /**
22367
22416
  * @name differenceInMinutes
22368
22417
  * @category Minute Helpers
@@ -22371,36 +22420,34 @@ var MILLISECONDS_IN_MINUTE = 60000;
22371
22420
  * @description
22372
22421
  * Get the signed number of full (rounded towards 0) minutes between the given dates.
22373
22422
  *
22374
- * ### v2.0.0 breaking changes:
22423
+ * @typeParam DateType - The `Date` type, the function operates on. Gets inferred from passed arguments. Allows to use extensions like [`UTCDate`](https://github.com/date-fns/utc).
22375
22424
  *
22376
- * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes).
22425
+ * @param dateLeft - The later date
22426
+ * @param dateRight - The earlier date
22427
+ * @param options - An object with options.
22377
22428
  *
22378
- * @param {Date|Number} dateLeft - the later date
22379
- * @param {Date|Number} dateRight - the earlier date
22380
- * @returns {Number} the number of minutes
22381
- * @throws {TypeError} 2 arguments required
22429
+ * @returns The number of minutes
22382
22430
  *
22383
22431
  * @example
22384
22432
  * // How many minutes are between 2 July 2014 12:07:59 and 2 July 2014 12:20:00?
22385
- * var result = differenceInMinutes(
22433
+ * const result = differenceInMinutes(
22386
22434
  * new Date(2014, 6, 2, 12, 20, 0),
22387
22435
  * new Date(2014, 6, 2, 12, 7, 59)
22388
22436
  * )
22389
22437
  * //=> 12
22390
22438
  *
22391
22439
  * @example
22392
- * // How many minutes are from 10:01:59 to 10:00:00
22393
- * var result = differenceInMinutes(
22440
+ * // How many minutes are between 10:01:59 and 10:00:00
22441
+ * const result = differenceInMinutes(
22394
22442
  * new Date(2000, 0, 1, 10, 0, 0),
22395
22443
  * new Date(2000, 0, 1, 10, 1, 59)
22396
22444
  * )
22397
22445
  * //=> -1
22398
22446
  */
22399
-
22400
- function differenceInMinutes(dirtyDateLeft, dirtyDateRight) {
22401
- requiredArgs(2, arguments);
22402
- var diff = differenceInMilliseconds(dirtyDateLeft, dirtyDateRight) / MILLISECONDS_IN_MINUTE;
22403
- return diff > 0 ? Math.floor(diff) : Math.ceil(diff);
22447
+ function differenceInMinutes(dateLeft, dateRight, options) {
22448
+ const diff =
22449
+ differenceInMilliseconds(dateLeft, dateRight) / millisecondsInMinute;
22450
+ return getRoundingMethod(options?.roundingMethod)(diff);
22404
22451
  }
22405
22452
 
22406
22453
  /**
@@ -22411,25 +22458,20 @@ function differenceInMinutes(dirtyDateLeft, dirtyDateRight) {
22411
22458
  * @description
22412
22459
  * Subtract the specified number of days from the given date.
22413
22460
  *
22414
- * ### v2.0.0 breaking changes:
22461
+ * @typeParam DateType - The `Date` type, the function operates on. Gets inferred from passed arguments. Allows to use extensions like [`UTCDate`](https://github.com/date-fns/utc).
22415
22462
  *
22416
- * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes).
22463
+ * @param date - The date to be changed
22464
+ * @param amount - The amount of days to be subtracted.
22417
22465
  *
22418
- * @param {Date|Number} date - the date to be changed
22419
- * @param {Number} amount - the amount of days to be subtracted. Positive decimals will be rounded using `Math.floor`, decimals less than zero will be rounded using `Math.ceil`.
22420
- * @returns {Date} the new date with the days subtracted
22421
- * @throws {TypeError} 2 arguments required
22466
+ * @returns The new date with the days subtracted
22422
22467
  *
22423
22468
  * @example
22424
22469
  * // Subtract 10 days from 1 September 2014:
22425
- * var result = subDays(new Date(2014, 8, 1), 10)
22470
+ * const result = subDays(new Date(2014, 8, 1), 10)
22426
22471
  * //=> Fri Aug 22 2014 00:00:00
22427
22472
  */
22428
-
22429
- function subDays(dirtyDate, dirtyAmount) {
22430
- requiredArgs(2, arguments);
22431
- var amount = toInteger(dirtyAmount);
22432
- return addDays(dirtyDate, -amount);
22473
+ function subDays(date, amount) {
22474
+ return addDays(date, -amount);
22433
22475
  }
22434
22476
 
22435
22477
  const WpOperationLockService = (props = {}) => applyDecorators(Injectable(), SetMetadata(EntityManagerSymbols.OperationLockService, {