@punks/backend-entity-manager 0.0.427 → 0.0.429

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.
@@ -1,4 +1,5 @@
1
1
  /// <reference types="node" />
2
+ import { ExcelParseOptions } from "@punks/backend-core";
2
3
  export type EntryValidationError = {
3
4
  errorCode: string;
4
5
  value?: any;
@@ -50,8 +51,10 @@ export type EntitySerializerSheetOptions = {
50
51
  useTypeColumn?: boolean;
51
52
  };
52
53
  export type EntitySerializerSheetDefinition<TSheetItem> = {
54
+ autoColumns?: boolean;
53
55
  columns: EntitySerializerColumnDefinition<TSheetItem>[];
54
56
  sheet?: EntitySerializerSheetOptions;
57
+ options?: ExcelParseOptions;
55
58
  };
56
59
  export type EntityExportFile = {
57
60
  content: Buffer;
package/dist/esm/index.js CHANGED
@@ -263,24 +263,20 @@ class EntitySerializer {
263
263
  const records = excelParse(data, {
264
264
  keysTransform: ExcelKeyTransform.Lower,
265
265
  dateColumns: dateColumns.map((x) => x.name),
266
+ ...(definition?.options ? definition.options : {}),
266
267
  });
267
- return Array.isArray(records)
268
- ? records.map((x, i) => this.convertSheetRecord(x, definition, i))
269
- : Object.values(records)
270
- .flat()
271
- .map((x, i) => this.convertSheetRecord(x, definition, i));
268
+ return records.map((x, i) => this.convertSheetRecord(x, definition, i));
272
269
  }
273
270
  convertSheetRecord(record, definition, rowIndex) {
274
- console.log("record", record);
275
- console.log("definition", definition);
276
- console.log("rowIndex", rowIndex);
277
271
  if (definition.sheet?.useTypeColumn &&
278
272
  (!record._type || record._type !== this.entityName)) {
279
273
  throw new Error(`Invalid record type ${record._type} -> record: \n${JSON.stringify(record)}`);
280
274
  }
281
- const entity = {};
275
+ const entity = (definition.autoColumns ? { ...record } : {});
282
276
  for (const column of definition.columns) {
283
277
  const columnName = normalizeSheetColumn(column.name);
278
+ if (!(columnName in record))
279
+ continue;
284
280
  if (column.parseAction) {
285
281
  column.parseAction(record[columnName], entity);
286
282
  continue;
@@ -297,6 +293,8 @@ class EntitySerializer {
297
293
  }
298
294
  const validationErrors = [];
299
295
  for (const column of definition.columns) {
296
+ if (!(normalizeSheetColumn(column.name) in record))
297
+ continue;
300
298
  for (const validator of column.validators ?? []) {
301
299
  const result = validator.fn(entity);
302
300
  if (!result.isValid) {