@nhtio/lucid-resourceful 1.20250718.1 → 1.20250718.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.
package/index.cjs CHANGED
@@ -12,7 +12,7 @@ var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "
12
12
  var __privateMethod = (obj, member, method) => (__accessCheck(obj, member, "access private method"), method);
13
13
  var _a, _hookHandlers, _cleanupHandlers, _state, _handlersToIgnore, _skipAllHooks, _Runner_instances, filter_fn, exec_fn, _b, _hooks, _c, _d;
14
14
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
15
- const decorator_utils = require("./decorator_utils-1yWqd_Gg.cjs");
15
+ const decorator_utils = require("./decorator_utils-U_rZo8tv.cjs");
16
16
  const index = require("./index-Cv6KC1rC.cjs");
17
17
  const liqe = require("liqe");
18
18
  const errors = require("./errors-D8jb9VxY.cjs");
@@ -589,46 +589,9 @@ class l {
589
589
  ) : this.e[s] = []), this;
590
590
  }
591
591
  }
592
- var CRUDOperationsEnum = /* @__PURE__ */ ((CRUDOperationsEnum2) => {
593
- CRUDOperationsEnum2["CREATE"] = "create";
594
- CRUDOperationsEnum2["READ"] = "read";
595
- CRUDOperationsEnum2["LIST"] = "list";
596
- CRUDOperationsEnum2["UPDATE"] = "update";
597
- CRUDOperationsEnum2["DELETE"] = "delete";
598
- return CRUDOperationsEnum2;
599
- })(CRUDOperationsEnum || {});
600
- var ACLOperationsEnum = /* @__PURE__ */ ((ACLOperationsEnum2) => {
601
- ACLOperationsEnum2["WRITE"] = "write";
602
- ACLOperationsEnum2["READ"] = "read";
603
- return ACLOperationsEnum2;
604
- })(ACLOperationsEnum || {});
605
- const operationCRUDToACL = (operation) => {
606
- if (operation === CRUDOperationsEnum.READ || operation === CRUDOperationsEnum.LIST) {
607
- return ACLOperationsEnum.READ;
608
- }
609
- if (operation === CRUDOperationsEnum.CREATE || operation === CRUDOperationsEnum.UPDATE || operation === CRUDOperationsEnum.DELETE) {
610
- return ACLOperationsEnum.WRITE;
611
- }
612
- };
613
- const isString$1 = (value) => {
614
- return typeof value === "string";
615
- };
616
- const isFunction = (value) => {
617
- return typeof value === "function";
618
- };
619
- const prepareFields = (fields, primaryKey) => {
620
- const defaultValue = [primaryKey];
621
- if (Array.isArray(fields)) {
622
- return fields.length ? fields : defaultValue;
623
- }
624
- if (isString$1(fields)) {
625
- return fields.length ? [fields] : defaultValue;
626
- }
627
- return defaultValue;
628
- };
629
592
  const ResourcefulErrorHandlerMethod = ["bubble", "pass", "fail"];
630
593
  const getFieldKey = (key, definition) => {
631
- if (isString$1(definition.serializeAs)) {
594
+ if (decorator_utils.isString(definition.serializeAs)) {
632
595
  return definition.serializeAs;
633
596
  }
634
597
  return key;
@@ -1332,11 +1295,11 @@ function withResourceful(options = {}) {
1332
1295
  };
1333
1296
  }
1334
1297
  }
1335
- if (operation === CRUDOperationsEnum.DELETE) {
1298
+ if (operation === decorator_utils.CRUDOperationsEnum.DELETE) {
1336
1299
  return { isForbidden: false, allowedFieldsMap, allowedColumnsMap };
1337
1300
  }
1338
1301
  const columnsOptions = this.$resourcefulColumns.values();
1339
- const aclOperation = operationCRUDToACL(operation);
1302
+ const aclOperation = decorator_utils.operationCRUDToACL(operation);
1340
1303
  const addColumnOptionToAllowedFieldsMap = (propertyKey) => {
1341
1304
  const serializedName = this.$keys.attributesToSerialized.resolve(propertyKey);
1342
1305
  const columnName = this.$keys.serializedToColumns.resolve(serializedName);
@@ -1348,7 +1311,7 @@ function withResourceful(options = {}) {
1348
1311
  }
1349
1312
  };
1350
1313
  for (const columnOptions of columnsOptions) {
1351
- const propertyACLFilters = aclOperation === ACLOperationsEnum.READ ? columnOptions.readAccessControlFilters : columnOptions.writeAccessControlFilters;
1314
+ const propertyACLFilters = aclOperation === decorator_utils.ACLOperationsEnum.READ ? columnOptions.readAccessControlFilters : columnOptions.writeAccessControlFilters;
1352
1315
  if (!propertyACLFilters) {
1353
1316
  addColumnOptionToAllowedFieldsMap(columnOptions.propertyKey);
1354
1317
  continue;
@@ -1451,7 +1414,7 @@ function withResourceful(options = {}) {
1451
1414
  if (!opts) {
1452
1415
  continue;
1453
1416
  }
1454
- const preparedValue = isFunction(opts.consume) ? opts.consume(value, attribute, this) : value;
1417
+ const preparedValue = decorator_utils.isFunction(opts.consume) ? opts.consume(value, attribute, this) : value;
1455
1418
  result[serializable] = preparedValue;
1456
1419
  }
1457
1420
  return result;
@@ -1486,14 +1449,14 @@ function withResourceful(options = {}) {
1486
1449
  if (!primaryKey) {
1487
1450
  throw new errors.E_MISSING_PRIMARY_KEY_EXCEPTION(this.$resourcefulName);
1488
1451
  }
1489
- if (!isString$1(filter)) {
1452
+ if (!decorator_utils.isString(filter)) {
1490
1453
  filter = "";
1491
1454
  }
1492
- fields = prepareFields(fields, primaryKey);
1455
+ fields = decorator_utils.prepareFields(fields, primaryKey);
1493
1456
  const { isForbidden, allowedFieldsMap, message } = await this.$resourcefulCheckAccess({
1494
1457
  ctx,
1495
1458
  app,
1496
- operation: CRUDOperationsEnum.LIST
1459
+ operation: decorator_utils.CRUDOperationsEnum.LIST
1497
1460
  });
1498
1461
  if (isForbidden) {
1499
1462
  throw new errors.E_FORBIDDEN(message);
@@ -1601,7 +1564,7 @@ function withResourceful(options = {}) {
1601
1564
  ctx,
1602
1565
  app,
1603
1566
  instance: record,
1604
- operation: CRUDOperationsEnum.READ
1567
+ operation: decorator_utils.CRUDOperationsEnum.READ
1605
1568
  });
1606
1569
  if (isForbidden) {
1607
1570
  throw new errors.E_FORBIDDEN(message);
@@ -1657,7 +1620,7 @@ function withResourceful(options = {}) {
1657
1620
  const { isForbidden, allowedFieldsMap, message } = await this.$resourcefulCheckAccess({
1658
1621
  ctx,
1659
1622
  app,
1660
- operation: CRUDOperationsEnum.CREATE
1623
+ operation: decorator_utils.CRUDOperationsEnum.CREATE
1661
1624
  });
1662
1625
  if (isForbidden) {
1663
1626
  throw new errors.E_FORBIDDEN(message);
@@ -1734,7 +1697,7 @@ function withResourceful(options = {}) {
1734
1697
  ctx,
1735
1698
  app,
1736
1699
  instance: record,
1737
- operation: CRUDOperationsEnum.UPDATE
1700
+ operation: decorator_utils.CRUDOperationsEnum.UPDATE
1738
1701
  });
1739
1702
  if (isForbidden) {
1740
1703
  throw new errors.E_FORBIDDEN(message);
@@ -1797,7 +1760,7 @@ function withResourceful(options = {}) {
1797
1760
  ctx,
1798
1761
  app,
1799
1762
  instance: record,
1800
- operation: CRUDOperationsEnum.DELETE
1763
+ operation: decorator_utils.CRUDOperationsEnum.DELETE
1801
1764
  });
1802
1765
  if (isForbidden) {
1803
1766
  throw new errors.E_FORBIDDEN(message);
@@ -1859,6 +1822,56 @@ function withResourceful(options = {}) {
1859
1822
  return ResourcefulModel;
1860
1823
  };
1861
1824
  }
1825
+ class PrepareConsumeChainBuilder {
1826
+ /**
1827
+ * Creates a chained prepare function that calls default first, then custom.
1828
+ *
1829
+ * @param propertyKey - The property name for error context
1830
+ * @param nullable - Whether the field accepts null values
1831
+ * @param defaultPrepare - The default prepare function from decorator
1832
+ * @param customPrepare - The custom prepare function from options
1833
+ * @returns A chained prepare function or undefined if no functions provided
1834
+ */
1835
+ static chainPrepare(propertyKey, nullable, defaultPrepare, customPrepare) {
1836
+ if (!defaultPrepare && !customPrepare) {
1837
+ return void 0;
1838
+ }
1839
+ if (!defaultPrepare && customPrepare) {
1840
+ return customPrepare;
1841
+ }
1842
+ if (defaultPrepare && !customPrepare) {
1843
+ return (value) => defaultPrepare(propertyKey, value, nullable);
1844
+ }
1845
+ return (value) => {
1846
+ const defaultResult = defaultPrepare(propertyKey, value, nullable);
1847
+ return customPrepare(defaultResult);
1848
+ };
1849
+ }
1850
+ /**
1851
+ * Creates a chained consume function that calls custom first, then default.
1852
+ *
1853
+ * @param propertyKey - The property name for error context
1854
+ * @param nullable - Whether the field accepts null values
1855
+ * @param defaultConsume - The default consume function from decorator
1856
+ * @param customConsume - The custom consume function from options
1857
+ * @returns A chained consume function or undefined if no functions provided
1858
+ */
1859
+ static chainConsume(propertyKey, nullable, defaultConsume, customConsume) {
1860
+ if (!defaultConsume && !customConsume) {
1861
+ return void 0;
1862
+ }
1863
+ if (!defaultConsume && customConsume) {
1864
+ return customConsume;
1865
+ }
1866
+ if (defaultConsume && !customConsume) {
1867
+ return (value) => defaultConsume(propertyKey, value, nullable);
1868
+ }
1869
+ return (value) => {
1870
+ const customResult = customConsume(value);
1871
+ return defaultConsume(propertyKey, customResult, nullable);
1872
+ };
1873
+ }
1874
+ }
1862
1875
  const E_INVALID_DATE_COLUMN_VALUE = errors.createError('Invalid value for "%s". %s', "E_INVALID_DATE_COLUMN_VALUE", 500);
1863
1876
  const E_MISSING_MODEL_ATTRIBUTE = errors.createError('Relation "%s" expects "%s" to exist on "%s" model, but is missing. Did you forget to define the column?', "E_MISSING_MODEL_ATTRIBUTE", 500);
1864
1877
  const E_MODEL_DELETED = errors.createError("Cannot mutate delete model instance", "E_MODEL_DELETED", 500);
@@ -12261,16 +12274,37 @@ const handleResourcefulColumn = (config) => {
12261
12274
  propertyKey,
12262
12275
  decoratorOption,
12263
12276
  decoratorOptionsValidationSchema,
12264
- overrides,
12265
- lucidDecoratorFunction
12277
+ overrides = {},
12278
+ lucidDecoratorFunction,
12279
+ defaultPrepare,
12280
+ defaultConsume
12266
12281
  } = config;
12267
12282
  const opts = validateNormalizeAndCastDecoratorOptions(propertyKey, decoratorName, decoratorOptionsValidationSchema, {
12268
12283
  ...decoratorOption,
12269
12284
  propertyKey
12270
12285
  });
12286
+ const customPrepare = opts.prepare;
12287
+ const customConsume = opts.consume;
12288
+ const nullable = opts.nullable || false;
12289
+ const chainedPrepare = PrepareConsumeChainBuilder.chainPrepare(
12290
+ propertyKey,
12291
+ nullable,
12292
+ defaultPrepare,
12293
+ customPrepare
12294
+ );
12295
+ const chainedConsume = PrepareConsumeChainBuilder.chainConsume(
12296
+ propertyKey,
12297
+ nullable,
12298
+ defaultConsume,
12299
+ customConsume
12300
+ );
12271
12301
  const lucidOptions = extractLucidOptionsForLucidColumnDecorator(
12272
12302
  opts,
12273
- overrides
12303
+ {
12304
+ ...overrides,
12305
+ ...chainedPrepare && { prepare: chainedPrepare },
12306
+ ...chainedConsume && { consume: chainedConsume }
12307
+ }
12274
12308
  );
12275
12309
  lucidDecoratorFunction(lucidOptions)(target, propertyKey);
12276
12310
  const map = ensureOwnResourcefulMap(target, "$resourcefulColumns");
@@ -12318,10 +12352,8 @@ resourcefulColumn.string = function(options = {}) {
12318
12352
  decoratorOption: options,
12319
12353
  decoratorOptionsValidationSchema,
12320
12354
  lucidDecoratorFunction: column,
12321
- overrides: {
12322
- prepare: (value) => decorator_utils.prepareString(propertyKey, value, options.nullable),
12323
- consume: (value) => decorator_utils.consumeString(propertyKey, value, options.nullable)
12324
- }
12355
+ defaultPrepare: decorator_utils.prepareString,
12356
+ defaultConsume: decorator_utils.consumeString
12325
12357
  });
12326
12358
  };
12327
12359
  };
@@ -12364,10 +12396,8 @@ resourcefulColumn.binary = function(options = {}) {
12364
12396
  decoratorOption: options,
12365
12397
  decoratorOptionsValidationSchema,
12366
12398
  lucidDecoratorFunction: column,
12367
- overrides: {
12368
- prepare: (value) => decorator_utils.prepareBinary(propertyKey, value, options.nullable),
12369
- consume: (value) => decorator_utils.consumeBinary(propertyKey, value, options.nullable)
12370
- }
12399
+ defaultPrepare: decorator_utils.prepareBinary,
12400
+ defaultConsume: decorator_utils.consumeBinary
12371
12401
  });
12372
12402
  };
12373
12403
  };
@@ -12382,10 +12412,8 @@ resourcefulColumn.number = function(options = {}) {
12382
12412
  decoratorOption: options,
12383
12413
  decoratorOptionsValidationSchema,
12384
12414
  lucidDecoratorFunction: column,
12385
- overrides: {
12386
- prepare: (value) => decorator_utils.prepareNumber(propertyKey, value, options.nullable),
12387
- consume: (value) => decorator_utils.consumeNumber(propertyKey, value, options.nullable)
12388
- }
12415
+ defaultPrepare: decorator_utils.prepareNumber,
12416
+ defaultConsume: decorator_utils.consumeNumber
12389
12417
  });
12390
12418
  };
12391
12419
  };
@@ -12400,10 +12428,8 @@ resourcefulColumn.integer = function(options = {}) {
12400
12428
  decoratorOption: options,
12401
12429
  decoratorOptionsValidationSchema,
12402
12430
  lucidDecoratorFunction: column,
12403
- overrides: {
12404
- prepare: (value) => decorator_utils.prepareInteger(propertyKey, value, options.nullable),
12405
- consume: (value) => decorator_utils.consumeInteger(propertyKey, value, options.nullable)
12406
- }
12431
+ defaultPrepare: decorator_utils.prepareInteger,
12432
+ defaultConsume: decorator_utils.consumeInteger
12407
12433
  });
12408
12434
  };
12409
12435
  };
@@ -12418,10 +12444,8 @@ resourcefulColumn.bigint = function(options = {}) {
12418
12444
  decoratorOption: options,
12419
12445
  decoratorOptionsValidationSchema,
12420
12446
  lucidDecoratorFunction: column,
12421
- overrides: {
12422
- prepare: (value) => decorator_utils.prepareBigint(propertyKey, value, options.nullable),
12423
- consume: (value) => decorator_utils.consumeBigint(propertyKey, value, options.nullable)
12424
- }
12447
+ defaultPrepare: decorator_utils.prepareBigint,
12448
+ defaultConsume: decorator_utils.consumeBigint
12425
12449
  });
12426
12450
  };
12427
12451
  };
@@ -12436,10 +12460,8 @@ resourcefulColumn.unsignedint = function(options = {}) {
12436
12460
  decoratorOption: options,
12437
12461
  decoratorOptionsValidationSchema,
12438
12462
  lucidDecoratorFunction: column,
12439
- overrides: {
12440
- prepare: (value) => decorator_utils.prepareUnsignedint(propertyKey, value, options.nullable),
12441
- consume: (value) => decorator_utils.consumeUnsignedint(propertyKey, value, options.nullable)
12442
- }
12463
+ defaultPrepare: decorator_utils.prepareUnsignedint,
12464
+ defaultConsume: decorator_utils.consumeUnsignedint
12443
12465
  });
12444
12466
  };
12445
12467
  };
@@ -12454,10 +12476,8 @@ resourcefulColumn.boolean = function(options = {}) {
12454
12476
  decoratorOption: options,
12455
12477
  decoratorOptionsValidationSchema,
12456
12478
  lucidDecoratorFunction: column,
12457
- overrides: {
12458
- prepare: (value) => decorator_utils.prepareBoolean(propertyKey, value, options.nullable),
12459
- consume: (value) => decorator_utils.consumeBoolean(propertyKey, value, options.nullable)
12460
- }
12479
+ defaultPrepare: decorator_utils.prepareBoolean,
12480
+ defaultConsume: decorator_utils.consumeBoolean
12461
12481
  });
12462
12482
  };
12463
12483
  };
@@ -12472,10 +12492,8 @@ resourcefulColumn.object = function(options = {}) {
12472
12492
  decoratorOption: options,
12473
12493
  decoratorOptionsValidationSchema,
12474
12494
  lucidDecoratorFunction: column,
12475
- overrides: {
12476
- prepare: (value) => decorator_utils.prepareObject(propertyKey, value, options.nullable),
12477
- consume: (value) => decorator_utils.consumeObject(propertyKey, value, options.nullable)
12478
- }
12495
+ defaultPrepare: decorator_utils.prepareObject,
12496
+ defaultConsume: decorator_utils.consumeObject
12479
12497
  });
12480
12498
  };
12481
12499
  };
@@ -12490,10 +12508,8 @@ resourcefulColumn.array = function(options = {}) {
12490
12508
  decoratorOption: options,
12491
12509
  decoratorOptionsValidationSchema,
12492
12510
  lucidDecoratorFunction: column,
12493
- overrides: {
12494
- prepare: (value) => decorator_utils.prepareArray(propertyKey, value, options.nullable),
12495
- consume: (value) => decorator_utils.consumeArray(propertyKey, value, options.nullable)
12496
- }
12511
+ defaultPrepare: decorator_utils.prepareArray,
12512
+ defaultConsume: decorator_utils.consumeArray
12497
12513
  });
12498
12514
  };
12499
12515
  };
@@ -12721,7 +12737,7 @@ function resourcefulHasManyThrough(model, options = {}) {
12721
12737
  map.set(propertyKey, opts);
12722
12738
  };
12723
12739
  }
12724
- const version = "1.20250718.1";
12740
+ const version = "1.20250718.2";
12725
12741
  exports.errors = errors.errors;
12726
12742
  exports.definitions = definitions.definitions;
12727
12743
  exports.resourcefulBelongsTo = resourcefulBelongsTo;