@nhtio/lucid-resourceful 1.20250718.0 → 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,36 +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
592
  const ResourcefulErrorHandlerMethod = ["bubble", "pass", "fail"];
620
593
  const getFieldKey = (key, definition) => {
621
- if (isString$1(definition.serializeAs)) {
594
+ if (decorator_utils.isString(definition.serializeAs)) {
622
595
  return definition.serializeAs;
623
596
  }
624
597
  return key;
@@ -1322,11 +1295,11 @@ function withResourceful(options = {}) {
1322
1295
  };
1323
1296
  }
1324
1297
  }
1325
- if (operation === CRUDOperationsEnum.DELETE) {
1298
+ if (operation === decorator_utils.CRUDOperationsEnum.DELETE) {
1326
1299
  return { isForbidden: false, allowedFieldsMap, allowedColumnsMap };
1327
1300
  }
1328
1301
  const columnsOptions = this.$resourcefulColumns.values();
1329
- const aclOperation = operationCRUDToACL(operation);
1302
+ const aclOperation = decorator_utils.operationCRUDToACL(operation);
1330
1303
  const addColumnOptionToAllowedFieldsMap = (propertyKey) => {
1331
1304
  const serializedName = this.$keys.attributesToSerialized.resolve(propertyKey);
1332
1305
  const columnName = this.$keys.serializedToColumns.resolve(serializedName);
@@ -1338,7 +1311,7 @@ function withResourceful(options = {}) {
1338
1311
  }
1339
1312
  };
1340
1313
  for (const columnOptions of columnsOptions) {
1341
- const propertyACLFilters = aclOperation === ACLOperationsEnum.READ ? columnOptions.readAccessControlFilters : columnOptions.writeAccessControlFilters;
1314
+ const propertyACLFilters = aclOperation === decorator_utils.ACLOperationsEnum.READ ? columnOptions.readAccessControlFilters : columnOptions.writeAccessControlFilters;
1342
1315
  if (!propertyACLFilters) {
1343
1316
  addColumnOptionToAllowedFieldsMap(columnOptions.propertyKey);
1344
1317
  continue;
@@ -1441,7 +1414,7 @@ function withResourceful(options = {}) {
1441
1414
  if (!opts) {
1442
1415
  continue;
1443
1416
  }
1444
- 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;
1445
1418
  result[serializable] = preparedValue;
1446
1419
  }
1447
1420
  return result;
@@ -1476,16 +1449,14 @@ function withResourceful(options = {}) {
1476
1449
  if (!primaryKey) {
1477
1450
  throw new errors.E_MISSING_PRIMARY_KEY_EXCEPTION(this.$resourcefulName);
1478
1451
  }
1479
- if (!isString$1(filter)) {
1452
+ if (!decorator_utils.isString(filter)) {
1480
1453
  filter = "";
1481
1454
  }
1482
- if (!Array.isArray(fields) || fields.length === 0) {
1483
- fields = [primaryKey];
1484
- }
1455
+ fields = decorator_utils.prepareFields(fields, primaryKey);
1485
1456
  const { isForbidden, allowedFieldsMap, message } = await this.$resourcefulCheckAccess({
1486
1457
  ctx,
1487
1458
  app,
1488
- operation: CRUDOperationsEnum.LIST
1459
+ operation: decorator_utils.CRUDOperationsEnum.LIST
1489
1460
  });
1490
1461
  if (isForbidden) {
1491
1462
  throw new errors.E_FORBIDDEN(message);
@@ -1593,7 +1564,7 @@ function withResourceful(options = {}) {
1593
1564
  ctx,
1594
1565
  app,
1595
1566
  instance: record,
1596
- operation: CRUDOperationsEnum.READ
1567
+ operation: decorator_utils.CRUDOperationsEnum.READ
1597
1568
  });
1598
1569
  if (isForbidden) {
1599
1570
  throw new errors.E_FORBIDDEN(message);
@@ -1649,7 +1620,7 @@ function withResourceful(options = {}) {
1649
1620
  const { isForbidden, allowedFieldsMap, message } = await this.$resourcefulCheckAccess({
1650
1621
  ctx,
1651
1622
  app,
1652
- operation: CRUDOperationsEnum.CREATE
1623
+ operation: decorator_utils.CRUDOperationsEnum.CREATE
1653
1624
  });
1654
1625
  if (isForbidden) {
1655
1626
  throw new errors.E_FORBIDDEN(message);
@@ -1726,7 +1697,7 @@ function withResourceful(options = {}) {
1726
1697
  ctx,
1727
1698
  app,
1728
1699
  instance: record,
1729
- operation: CRUDOperationsEnum.UPDATE
1700
+ operation: decorator_utils.CRUDOperationsEnum.UPDATE
1730
1701
  });
1731
1702
  if (isForbidden) {
1732
1703
  throw new errors.E_FORBIDDEN(message);
@@ -1789,7 +1760,7 @@ function withResourceful(options = {}) {
1789
1760
  ctx,
1790
1761
  app,
1791
1762
  instance: record,
1792
- operation: CRUDOperationsEnum.DELETE
1763
+ operation: decorator_utils.CRUDOperationsEnum.DELETE
1793
1764
  });
1794
1765
  if (isForbidden) {
1795
1766
  throw new errors.E_FORBIDDEN(message);
@@ -1851,6 +1822,56 @@ function withResourceful(options = {}) {
1851
1822
  return ResourcefulModel;
1852
1823
  };
1853
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
+ }
1854
1875
  const E_INVALID_DATE_COLUMN_VALUE = errors.createError('Invalid value for "%s". %s', "E_INVALID_DATE_COLUMN_VALUE", 500);
1855
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);
1856
1877
  const E_MODEL_DELETED = errors.createError("Cannot mutate delete model instance", "E_MODEL_DELETED", 500);
@@ -12181,7 +12202,17 @@ const extractLucidOptionsForLucidDecorator = (options, keys, overrides) => {
12181
12202
  const extractLucidOptionsForLucidColumnDecorator = (options, overrides) => {
12182
12203
  return extractLucidOptionsForLucidDecorator(
12183
12204
  options,
12184
- ["columnName", "serializeAs", "isPrimary", "meta", "serialize", "prepare", "consume"],
12205
+ [
12206
+ "columnName",
12207
+ "serializeAs",
12208
+ "isPrimary",
12209
+ "meta",
12210
+ "serialize",
12211
+ "prepare",
12212
+ "consume",
12213
+ "autoCreate",
12214
+ "autoUpdate"
12215
+ ],
12185
12216
  overrides
12186
12217
  );
12187
12218
  };
@@ -12243,17 +12274,39 @@ const handleResourcefulColumn = (config) => {
12243
12274
  propertyKey,
12244
12275
  decoratorOption,
12245
12276
  decoratorOptionsValidationSchema,
12246
- overrides
12277
+ overrides = {},
12278
+ lucidDecoratorFunction,
12279
+ defaultPrepare,
12280
+ defaultConsume
12247
12281
  } = config;
12248
12282
  const opts = validateNormalizeAndCastDecoratorOptions(propertyKey, decoratorName, decoratorOptionsValidationSchema, {
12249
12283
  ...decoratorOption,
12250
12284
  propertyKey
12251
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
+ );
12252
12301
  const lucidOptions = extractLucidOptionsForLucidColumnDecorator(
12253
12302
  opts,
12254
- overrides
12303
+ {
12304
+ ...overrides,
12305
+ ...chainedPrepare && { prepare: chainedPrepare },
12306
+ ...chainedConsume && { consume: chainedConsume }
12307
+ }
12255
12308
  );
12256
- column(lucidOptions)(target, propertyKey);
12309
+ lucidDecoratorFunction(lucidOptions)(target, propertyKey);
12257
12310
  const map = ensureOwnResourcefulMap(target, "$resourcefulColumns");
12258
12311
  map.set(propertyKey, opts);
12259
12312
  };
@@ -12298,10 +12351,9 @@ resourcefulColumn.string = function(options = {}) {
12298
12351
  propertyKey,
12299
12352
  decoratorOption: options,
12300
12353
  decoratorOptionsValidationSchema,
12301
- overrides: {
12302
- prepare: (value) => decorator_utils.prepareString(propertyKey, value, options.nullable),
12303
- consume: (value) => decorator_utils.consumeString(propertyKey, value, options.nullable)
12304
- }
12354
+ lucidDecoratorFunction: column,
12355
+ defaultPrepare: decorator_utils.prepareString,
12356
+ defaultConsume: decorator_utils.consumeString
12305
12357
  });
12306
12358
  };
12307
12359
  };
@@ -12314,7 +12366,8 @@ resourcefulColumn.date = function(options = {}) {
12314
12366
  target,
12315
12367
  propertyKey,
12316
12368
  decoratorOption: options,
12317
- decoratorOptionsValidationSchema
12369
+ decoratorOptionsValidationSchema,
12370
+ lucidDecoratorFunction: column.date
12318
12371
  });
12319
12372
  };
12320
12373
  };
@@ -12327,7 +12380,8 @@ resourcefulColumn.dateTime = function(options = {}) {
12327
12380
  target,
12328
12381
  propertyKey,
12329
12382
  decoratorOption: options,
12330
- decoratorOptionsValidationSchema
12383
+ decoratorOptionsValidationSchema,
12384
+ lucidDecoratorFunction: column.dateTime
12331
12385
  });
12332
12386
  };
12333
12387
  };
@@ -12341,10 +12395,9 @@ resourcefulColumn.binary = function(options = {}) {
12341
12395
  propertyKey,
12342
12396
  decoratorOption: options,
12343
12397
  decoratorOptionsValidationSchema,
12344
- overrides: {
12345
- prepare: (value) => decorator_utils.prepareBinary(propertyKey, value, options.nullable),
12346
- consume: (value) => decorator_utils.consumeBinary(propertyKey, value, options.nullable)
12347
- }
12398
+ lucidDecoratorFunction: column,
12399
+ defaultPrepare: decorator_utils.prepareBinary,
12400
+ defaultConsume: decorator_utils.consumeBinary
12348
12401
  });
12349
12402
  };
12350
12403
  };
@@ -12358,10 +12411,9 @@ resourcefulColumn.number = function(options = {}) {
12358
12411
  propertyKey,
12359
12412
  decoratorOption: options,
12360
12413
  decoratorOptionsValidationSchema,
12361
- overrides: {
12362
- prepare: (value) => decorator_utils.prepareNumber(propertyKey, value, options.nullable),
12363
- consume: (value) => decorator_utils.consumeNumber(propertyKey, value, options.nullable)
12364
- }
12414
+ lucidDecoratorFunction: column,
12415
+ defaultPrepare: decorator_utils.prepareNumber,
12416
+ defaultConsume: decorator_utils.consumeNumber
12365
12417
  });
12366
12418
  };
12367
12419
  };
@@ -12375,10 +12427,9 @@ resourcefulColumn.integer = function(options = {}) {
12375
12427
  propertyKey,
12376
12428
  decoratorOption: options,
12377
12429
  decoratorOptionsValidationSchema,
12378
- overrides: {
12379
- prepare: (value) => decorator_utils.prepareInteger(propertyKey, value, options.nullable),
12380
- consume: (value) => decorator_utils.consumeInteger(propertyKey, value, options.nullable)
12381
- }
12430
+ lucidDecoratorFunction: column,
12431
+ defaultPrepare: decorator_utils.prepareInteger,
12432
+ defaultConsume: decorator_utils.consumeInteger
12382
12433
  });
12383
12434
  };
12384
12435
  };
@@ -12392,10 +12443,9 @@ resourcefulColumn.bigint = function(options = {}) {
12392
12443
  propertyKey,
12393
12444
  decoratorOption: options,
12394
12445
  decoratorOptionsValidationSchema,
12395
- overrides: {
12396
- prepare: (value) => decorator_utils.prepareBigint(propertyKey, value, options.nullable),
12397
- consume: (value) => decorator_utils.consumeBigint(propertyKey, value, options.nullable)
12398
- }
12446
+ lucidDecoratorFunction: column,
12447
+ defaultPrepare: decorator_utils.prepareBigint,
12448
+ defaultConsume: decorator_utils.consumeBigint
12399
12449
  });
12400
12450
  };
12401
12451
  };
@@ -12409,10 +12459,9 @@ resourcefulColumn.unsignedint = function(options = {}) {
12409
12459
  propertyKey,
12410
12460
  decoratorOption: options,
12411
12461
  decoratorOptionsValidationSchema,
12412
- overrides: {
12413
- prepare: (value) => decorator_utils.prepareUnsignedint(propertyKey, value, options.nullable),
12414
- consume: (value) => decorator_utils.consumeUnsignedint(propertyKey, value, options.nullable)
12415
- }
12462
+ lucidDecoratorFunction: column,
12463
+ defaultPrepare: decorator_utils.prepareUnsignedint,
12464
+ defaultConsume: decorator_utils.consumeUnsignedint
12416
12465
  });
12417
12466
  };
12418
12467
  };
@@ -12426,10 +12475,9 @@ resourcefulColumn.boolean = function(options = {}) {
12426
12475
  propertyKey,
12427
12476
  decoratorOption: options,
12428
12477
  decoratorOptionsValidationSchema,
12429
- overrides: {
12430
- prepare: (value) => decorator_utils.prepareBoolean(propertyKey, value, options.nullable),
12431
- consume: (value) => decorator_utils.consumeBoolean(propertyKey, value, options.nullable)
12432
- }
12478
+ lucidDecoratorFunction: column,
12479
+ defaultPrepare: decorator_utils.prepareBoolean,
12480
+ defaultConsume: decorator_utils.consumeBoolean
12433
12481
  });
12434
12482
  };
12435
12483
  };
@@ -12443,10 +12491,9 @@ resourcefulColumn.object = function(options = {}) {
12443
12491
  propertyKey,
12444
12492
  decoratorOption: options,
12445
12493
  decoratorOptionsValidationSchema,
12446
- overrides: {
12447
- prepare: (value) => decorator_utils.prepareObject(propertyKey, value, options.nullable),
12448
- consume: (value) => decorator_utils.consumeObject(propertyKey, value, options.nullable)
12449
- }
12494
+ lucidDecoratorFunction: column,
12495
+ defaultPrepare: decorator_utils.prepareObject,
12496
+ defaultConsume: decorator_utils.consumeObject
12450
12497
  });
12451
12498
  };
12452
12499
  };
@@ -12460,10 +12507,9 @@ resourcefulColumn.array = function(options = {}) {
12460
12507
  propertyKey,
12461
12508
  decoratorOption: options,
12462
12509
  decoratorOptionsValidationSchema,
12463
- overrides: {
12464
- prepare: (value) => decorator_utils.prepareArray(propertyKey, value, options.nullable),
12465
- consume: (value) => decorator_utils.consumeArray(propertyKey, value, options.nullable)
12466
- }
12510
+ lucidDecoratorFunction: column,
12511
+ defaultPrepare: decorator_utils.prepareArray,
12512
+ defaultConsume: decorator_utils.consumeArray
12467
12513
  });
12468
12514
  };
12469
12515
  };
@@ -12691,7 +12737,7 @@ function resourcefulHasManyThrough(model, options = {}) {
12691
12737
  map.set(propertyKey, opts);
12692
12738
  };
12693
12739
  }
12694
- const version = "1.20250718.0";
12740
+ const version = "1.20250718.2";
12695
12741
  exports.errors = errors.errors;
12696
12742
  exports.definitions = definitions.definitions;
12697
12743
  exports.resourcefulBelongsTo = resourcefulBelongsTo;