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