dyno-table 1.5.0 → 1.6.0

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/dist/index.cjs CHANGED
@@ -3343,39 +3343,33 @@ function defineEntity(config) {
3343
3343
  createRepository: (table) => {
3344
3344
  const repository = {
3345
3345
  create: (data) => {
3346
- const primaryKey = config.primaryKey.generateKey(data);
3347
- const indexes = Object.entries(config.indexes ?? {}).reduce(
3348
- (acc, [indexName, index]) => {
3349
- const key = index.generateKey(data);
3350
- const gsiConfig = table.gsis[indexName];
3351
- if (!gsiConfig) {
3352
- throw new Error(`GSI configuration not found for index: ${indexName}`);
3353
- }
3354
- if (key.pk) {
3355
- acc[gsiConfig.partitionKey] = key.pk;
3356
- }
3357
- if (key.sk && gsiConfig.sortKey) {
3358
- acc[gsiConfig.sortKey] = key.sk;
3359
- }
3360
- return acc;
3361
- },
3362
- {}
3363
- );
3364
- const builder = table.create({
3365
- ...data,
3366
- [entityTypeAttributeName]: config.name,
3367
- [table.partitionKey]: primaryKey.pk,
3368
- ...table.sortKey ? { [table.sortKey]: primaryKey.sk } : {},
3369
- ...indexes,
3370
- ...generateTimestamps(["createdAt", "updatedAt"])
3371
- });
3346
+ const builder = table.create({});
3372
3347
  const prepareValidatedItemAsync = async () => {
3373
- const validationResult = await config.schema["~standard"].validate(data);
3374
- if ("issues" in validationResult && validationResult.issues) {
3375
- throw new Error(`Validation failed: ${validationResult.issues.map((i) => i.message).join(", ")}`);
3348
+ const validationResult = config.schema["~standard"].validate(data);
3349
+ const validatedData = validationResult instanceof Promise ? await validationResult : validationResult;
3350
+ if ("issues" in validatedData && validatedData.issues) {
3351
+ throw new Error(`Validation failed: ${validatedData.issues.map((i) => i.message).join(", ")}`);
3376
3352
  }
3353
+ const primaryKey = config.primaryKey.generateKey(validatedData.value);
3354
+ const indexes = Object.entries(config.indexes ?? {}).reduce(
3355
+ (acc, [indexName, index]) => {
3356
+ const key = index.generateKey(validatedData.value);
3357
+ const gsiConfig = table.gsis[indexName];
3358
+ if (!gsiConfig) {
3359
+ throw new Error(`GSI configuration not found for index: ${indexName}`);
3360
+ }
3361
+ if (key.pk) {
3362
+ acc[gsiConfig.partitionKey] = key.pk;
3363
+ }
3364
+ if (key.sk && gsiConfig.sortKey) {
3365
+ acc[gsiConfig.sortKey] = key.sk;
3366
+ }
3367
+ return acc;
3368
+ },
3369
+ {}
3370
+ );
3377
3371
  const validatedItem = {
3378
- ...validationResult.value,
3372
+ ...validatedData.value,
3379
3373
  [entityTypeAttributeName]: config.name,
3380
3374
  [table.partitionKey]: primaryKey.pk,
3381
3375
  ...table.sortKey ? { [table.sortKey]: primaryKey.sk } : {},
@@ -3388,11 +3382,31 @@ function defineEntity(config) {
3388
3382
  const prepareValidatedItemSync = () => {
3389
3383
  const validationResult = config.schema["~standard"].validate(data);
3390
3384
  if (validationResult instanceof Promise) {
3391
- throw new Error("Async validation is not supported in withTransaction. Use execute() instead.");
3385
+ throw new Error(
3386
+ "Async validation is not supported in create method. The schema must support synchronous validation for transaction compatibility."
3387
+ );
3392
3388
  }
3393
3389
  if ("issues" in validationResult && validationResult.issues) {
3394
3390
  throw new Error(`Validation failed: ${validationResult.issues.map((i) => i.message).join(", ")}`);
3395
3391
  }
3392
+ const primaryKey = config.primaryKey.generateKey(validationResult.value);
3393
+ const indexes = Object.entries(config.indexes ?? {}).reduce(
3394
+ (acc, [indexName, index]) => {
3395
+ const key = index.generateKey(validationResult.value);
3396
+ const gsiConfig = table.gsis[indexName];
3397
+ if (!gsiConfig) {
3398
+ throw new Error(`GSI configuration not found for index: ${indexName}`);
3399
+ }
3400
+ if (key.pk) {
3401
+ acc[gsiConfig.partitionKey] = key.pk;
3402
+ }
3403
+ if (key.sk && gsiConfig.sortKey) {
3404
+ acc[gsiConfig.sortKey] = key.sk;
3405
+ }
3406
+ return acc;
3407
+ },
3408
+ {}
3409
+ );
3396
3410
  const validatedItem = {
3397
3411
  ...validationResult.value,
3398
3412
  [entityTypeAttributeName]: config.name,
@@ -3431,25 +3445,37 @@ function defineEntity(config) {
3431
3445
  return builder;
3432
3446
  },
3433
3447
  upsert: (data) => {
3434
- const primaryKey = config.primaryKey.generateKey(data);
3435
- const builder = table.put({
3436
- [table.partitionKey]: primaryKey.pk,
3437
- ...table.sortKey ? { [table.sortKey]: primaryKey.sk } : {},
3438
- ...data,
3439
- [entityTypeAttributeName]: config.name,
3440
- ...generateTimestamps(["createdAt", "updatedAt"])
3441
- });
3448
+ const builder = table.put({});
3442
3449
  const prepareValidatedItemAsync = async () => {
3443
- const validationResult = await config.schema["~standard"].validate(data);
3444
- if ("issues" in validationResult && validationResult.issues) {
3445
- throw new Error(`Validation failed: ${validationResult.issues.map((i) => i.message).join(", ")}`);
3450
+ const validationResult = config.schema["~standard"].validate(data);
3451
+ const validatedData = validationResult instanceof Promise ? await validationResult : validationResult;
3452
+ if ("issues" in validatedData && validatedData.issues) {
3453
+ throw new Error(`Validation failed: ${validatedData.issues.map((i) => i.message).join(", ")}`);
3446
3454
  }
3447
- const primaryKey2 = config.primaryKey.generateKey(validationResult.value);
3455
+ const primaryKey = config.primaryKey.generateKey(validatedData.value);
3456
+ const indexes = Object.entries(config.indexes ?? {}).reduce(
3457
+ (acc, [indexName, index]) => {
3458
+ const key = index.generateKey(validatedData.value);
3459
+ const gsiConfig = table.gsis[indexName];
3460
+ if (!gsiConfig) {
3461
+ throw new Error(`GSI configuration not found for index: ${indexName}`);
3462
+ }
3463
+ if (key.pk) {
3464
+ acc[gsiConfig.partitionKey] = key.pk;
3465
+ }
3466
+ if (key.sk && gsiConfig.sortKey) {
3467
+ acc[gsiConfig.sortKey] = key.sk;
3468
+ }
3469
+ return acc;
3470
+ },
3471
+ {}
3472
+ );
3448
3473
  const validatedItem = {
3449
- [table.partitionKey]: primaryKey2.pk,
3450
- ...table.sortKey ? { [table.sortKey]: primaryKey2.sk } : {},
3451
- ...validationResult.value,
3474
+ [table.partitionKey]: primaryKey.pk,
3475
+ ...table.sortKey ? { [table.sortKey]: primaryKey.sk } : {},
3476
+ ...validatedData.value,
3452
3477
  [entityTypeAttributeName]: config.name,
3478
+ ...indexes,
3453
3479
  ...generateTimestamps(["createdAt", "updatedAt"])
3454
3480
  };
3455
3481
  Object.assign(builder, { item: validatedItem });
@@ -3463,12 +3489,30 @@ function defineEntity(config) {
3463
3489
  if ("issues" in validationResult && validationResult.issues) {
3464
3490
  throw new Error(`Validation failed: ${validationResult.issues.map((i) => i.message).join(", ")}`);
3465
3491
  }
3466
- const primaryKey2 = config.primaryKey.generateKey(validationResult.value);
3492
+ const primaryKey = config.primaryKey.generateKey(validationResult.value);
3493
+ const indexes = Object.entries(config.indexes ?? {}).reduce(
3494
+ (acc, [indexName, index]) => {
3495
+ const key = index.generateKey(validationResult.value);
3496
+ const gsiConfig = table.gsis[indexName];
3497
+ if (!gsiConfig) {
3498
+ throw new Error(`GSI configuration not found for index: ${indexName}`);
3499
+ }
3500
+ if (key.pk) {
3501
+ acc[gsiConfig.partitionKey] = key.pk;
3502
+ }
3503
+ if (key.sk && gsiConfig.sortKey) {
3504
+ acc[gsiConfig.sortKey] = key.sk;
3505
+ }
3506
+ return acc;
3507
+ },
3508
+ {}
3509
+ );
3467
3510
  const validatedItem = {
3468
- [table.partitionKey]: primaryKey2.pk,
3469
- ...table.sortKey ? { [table.sortKey]: primaryKey2.sk } : {},
3511
+ [table.partitionKey]: primaryKey.pk,
3512
+ ...table.sortKey ? { [table.sortKey]: primaryKey.sk } : {},
3470
3513
  ...validationResult.value,
3471
3514
  [entityTypeAttributeName]: config.name,
3515
+ ...indexes,
3472
3516
  ...generateTimestamps(["createdAt", "updatedAt"])
3473
3517
  };
3474
3518
  Object.assign(builder, { item: validatedItem });