dyno-table 2.0.0 → 2.0.1

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
@@ -3758,6 +3758,25 @@ function createEntityAwareDeleteBuilder(builder, entityName) {
3758
3758
  // src/entity.ts
3759
3759
  function defineEntity(config) {
3760
3760
  const entityTypeAttributeName = config.settings?.entityTypeAttributeName ?? "entityType";
3761
+ const buildIndexes = (dataForKeyGeneration, table) => {
3762
+ return Object.entries(config.indexes ?? {}).reduce(
3763
+ (acc, [indexName, index]) => {
3764
+ const key = index.generateKey(dataForKeyGeneration);
3765
+ const gsiConfig = table.gsis[indexName];
3766
+ if (!gsiConfig) {
3767
+ throw new Error(`GSI configuration not found for index: ${indexName}`);
3768
+ }
3769
+ if (key.pk) {
3770
+ acc[gsiConfig.partitionKey] = key.pk;
3771
+ }
3772
+ if (key.sk && gsiConfig.sortKey) {
3773
+ acc[gsiConfig.sortKey] = key.sk;
3774
+ }
3775
+ return acc;
3776
+ },
3777
+ {}
3778
+ );
3779
+ };
3761
3780
  const wrapMethodWithPreparation = (originalMethod, prepareFn, context) => {
3762
3781
  const wrappedMethod = (...args) => {
3763
3782
  prepareFn();
@@ -3776,18 +3795,17 @@ function defineEntity(config) {
3776
3795
  }
3777
3796
  return wrappedMethod;
3778
3797
  };
3779
- const generateTimestamps = (timestampTypes) => {
3798
+ const generateTimestamps = (timestampsToGenerate, data) => {
3780
3799
  if (!config.settings?.timestamps) return {};
3781
3800
  const timestamps = {};
3782
3801
  const now = /* @__PURE__ */ new Date();
3783
3802
  const unixTime = Math.floor(Date.now() / 1e3);
3784
3803
  const { createdAt, updatedAt } = config.settings.timestamps;
3785
- const typesToGenerate = timestampTypes || ["createdAt", "updatedAt"];
3786
- if (createdAt && typesToGenerate.includes("createdAt")) {
3804
+ if (createdAt && timestampsToGenerate.includes("createdAt") && !data.createdAt) {
3787
3805
  const name = createdAt.attributeName ?? "createdAt";
3788
3806
  timestamps[name] = createdAt.format === "UNIX" ? unixTime : now.toISOString();
3789
3807
  }
3790
- if (updatedAt && typesToGenerate.includes("updatedAt")) {
3808
+ if (updatedAt && timestampsToGenerate.includes("updatedAt") && !data.updatedAt) {
3791
3809
  const name = updatedAt.attributeName ?? "updatedAt";
3792
3810
  timestamps[name] = updatedAt.format === "UNIX" ? unixTime : now.toISOString();
3793
3811
  }
@@ -3800,36 +3818,22 @@ function defineEntity(config) {
3800
3818
  create: (data) => {
3801
3819
  const builder = table.create({});
3802
3820
  const prepareValidatedItemAsync = async () => {
3803
- const validationResult = config.schema["~standard"].validate(data);
3804
- const validatedData = validationResult instanceof Promise ? await validationResult : validationResult;
3821
+ const validatedData = await config.schema["~standard"].validate(data);
3805
3822
  if ("issues" in validatedData && validatedData.issues) {
3806
3823
  throw new Error(`Validation failed: ${validatedData.issues.map((i) => i.message).join(", ")}`);
3807
3824
  }
3808
- const primaryKey = config.primaryKey.generateKey(validatedData.value);
3809
- const indexes = Object.entries(config.indexes ?? {}).reduce(
3810
- (acc, [indexName, index]) => {
3811
- const key = index.generateKey(validatedData.value);
3812
- const gsiConfig = table.gsis[indexName];
3813
- if (!gsiConfig) {
3814
- throw new Error(`GSI configuration not found for index: ${indexName}`);
3815
- }
3816
- if (key.pk) {
3817
- acc[gsiConfig.partitionKey] = key.pk;
3818
- }
3819
- if (key.sk && gsiConfig.sortKey) {
3820
- acc[gsiConfig.sortKey] = key.sk;
3821
- }
3822
- return acc;
3823
- },
3824
- {}
3825
- );
3826
- const validatedItem = {
3825
+ const dataForKeyGeneration = {
3827
3826
  ...validatedData.value,
3827
+ ...generateTimestamps(["createdAt", "updatedAt"], validatedData.value)
3828
+ };
3829
+ const primaryKey = config.primaryKey.generateKey(dataForKeyGeneration);
3830
+ const indexes = buildIndexes(dataForKeyGeneration, table);
3831
+ const validatedItem = {
3832
+ ...dataForKeyGeneration,
3828
3833
  [entityTypeAttributeName]: config.name,
3829
3834
  [table.partitionKey]: primaryKey.pk,
3830
3835
  ...table.sortKey ? { [table.sortKey]: primaryKey.sk } : {},
3831
- ...indexes,
3832
- ...generateTimestamps(["createdAt", "updatedAt"])
3836
+ ...indexes
3833
3837
  };
3834
3838
  Object.assign(builder, { item: validatedItem });
3835
3839
  return validatedItem;
@@ -3838,37 +3842,24 @@ function defineEntity(config) {
3838
3842
  const validationResult = config.schema["~standard"].validate(data);
3839
3843
  if (validationResult instanceof Promise) {
3840
3844
  throw new Error(
3841
- "Async validation is not supported in create method. The schema must support synchronous validation for transaction compatibility."
3845
+ "Async validation is not supported in withBatch or withTransaction. The schema must support synchronous validation for compatibility."
3842
3846
  );
3843
3847
  }
3844
3848
  if ("issues" in validationResult && validationResult.issues) {
3845
3849
  throw new Error(`Validation failed: ${validationResult.issues.map((i) => i.message).join(", ")}`);
3846
3850
  }
3847
- const primaryKey = config.primaryKey.generateKey(validationResult.value);
3848
- const indexes = Object.entries(config.indexes ?? {}).reduce(
3849
- (acc, [indexName, index]) => {
3850
- const key = index.generateKey(validationResult.value);
3851
- const gsiConfig = table.gsis[indexName];
3852
- if (!gsiConfig) {
3853
- throw new Error(`GSI configuration not found for index: ${indexName}`);
3854
- }
3855
- if (key.pk) {
3856
- acc[gsiConfig.partitionKey] = key.pk;
3857
- }
3858
- if (key.sk && gsiConfig.sortKey) {
3859
- acc[gsiConfig.sortKey] = key.sk;
3860
- }
3861
- return acc;
3862
- },
3863
- {}
3864
- );
3865
- const validatedItem = {
3851
+ const dataForKeyGeneration = {
3866
3852
  ...validationResult.value,
3853
+ ...generateTimestamps(["createdAt", "updatedAt"], validationResult.value)
3854
+ };
3855
+ const primaryKey = config.primaryKey.generateKey(dataForKeyGeneration);
3856
+ const indexes = buildIndexes(dataForKeyGeneration, table);
3857
+ const validatedItem = {
3858
+ ...dataForKeyGeneration,
3867
3859
  [entityTypeAttributeName]: config.name,
3868
3860
  [table.partitionKey]: primaryKey.pk,
3869
3861
  ...table.sortKey ? { [table.sortKey]: primaryKey.sk } : {},
3870
- ...indexes,
3871
- ...generateTimestamps(["createdAt", "updatedAt"])
3862
+ ...indexes
3872
3863
  };
3873
3864
  Object.assign(builder, { item: validatedItem });
3874
3865
  return validatedItem;
@@ -3895,36 +3886,22 @@ function defineEntity(config) {
3895
3886
  upsert: (data) => {
3896
3887
  const builder = table.put({});
3897
3888
  const prepareValidatedItemAsync = async () => {
3898
- const validationResult = config.schema["~standard"].validate(data);
3899
- const validatedData = validationResult instanceof Promise ? await validationResult : validationResult;
3889
+ const validatedData = await config.schema["~standard"].validate(data);
3900
3890
  if ("issues" in validatedData && validatedData.issues) {
3901
3891
  throw new Error(`Validation failed: ${validatedData.issues.map((i) => i.message).join(", ")}`);
3902
3892
  }
3903
- const primaryKey = config.primaryKey.generateKey(validatedData.value);
3904
- const indexes = Object.entries(config.indexes ?? {}).reduce(
3905
- (acc, [indexName, index]) => {
3906
- const key = index.generateKey(validatedData.value);
3907
- const gsiConfig = table.gsis[indexName];
3908
- if (!gsiConfig) {
3909
- throw new Error(`GSI configuration not found for index: ${indexName}`);
3910
- }
3911
- if (key.pk) {
3912
- acc[gsiConfig.partitionKey] = key.pk;
3913
- }
3914
- if (key.sk && gsiConfig.sortKey) {
3915
- acc[gsiConfig.sortKey] = key.sk;
3916
- }
3917
- return acc;
3918
- },
3919
- {}
3920
- );
3893
+ const dataForKeyGeneration = {
3894
+ ...validatedData.value,
3895
+ ...generateTimestamps(["createdAt", "updatedAt"], validatedData.value)
3896
+ };
3897
+ const primaryKey = config.primaryKey.generateKey(dataForKeyGeneration);
3898
+ const indexes = buildIndexes(dataForKeyGeneration, table);
3921
3899
  const validatedItem = {
3922
3900
  [table.partitionKey]: primaryKey.pk,
3923
3901
  ...table.sortKey ? { [table.sortKey]: primaryKey.sk } : {},
3924
- ...validatedData.value,
3902
+ ...dataForKeyGeneration,
3925
3903
  [entityTypeAttributeName]: config.name,
3926
- ...indexes,
3927
- ...generateTimestamps(["createdAt", "updatedAt"])
3904
+ ...indexes
3928
3905
  };
3929
3906
  Object.assign(builder, { item: validatedItem });
3930
3907
  return validatedItem;
@@ -3932,36 +3909,25 @@ function defineEntity(config) {
3932
3909
  const prepareValidatedItemSync = () => {
3933
3910
  const validationResult = config.schema["~standard"].validate(data);
3934
3911
  if (validationResult instanceof Promise) {
3935
- throw new Error("Async validation is not supported in withTransaction. Use execute() instead.");
3912
+ throw new Error(
3913
+ "Async validation is not supported in withTransaction or withBatch. Use execute() instead."
3914
+ );
3936
3915
  }
3937
3916
  if ("issues" in validationResult && validationResult.issues) {
3938
3917
  throw new Error(`Validation failed: ${validationResult.issues.map((i) => i.message).join(", ")}`);
3939
3918
  }
3940
- const primaryKey = config.primaryKey.generateKey(validationResult.value);
3941
- const indexes = Object.entries(config.indexes ?? {}).reduce(
3942
- (acc, [indexName, index]) => {
3943
- const key = index.generateKey(validationResult.value);
3944
- const gsiConfig = table.gsis[indexName];
3945
- if (!gsiConfig) {
3946
- throw new Error(`GSI configuration not found for index: ${indexName}`);
3947
- }
3948
- if (key.pk) {
3949
- acc[gsiConfig.partitionKey] = key.pk;
3950
- }
3951
- if (key.sk && gsiConfig.sortKey) {
3952
- acc[gsiConfig.sortKey] = key.sk;
3953
- }
3954
- return acc;
3955
- },
3956
- {}
3957
- );
3919
+ const dataForKeyGeneration = {
3920
+ ...validationResult.value,
3921
+ ...generateTimestamps(["createdAt", "updatedAt"], validationResult.value)
3922
+ };
3923
+ const primaryKey = config.primaryKey.generateKey(dataForKeyGeneration);
3924
+ const indexes = buildIndexes(dataForKeyGeneration, table);
3958
3925
  const validatedItem = {
3959
3926
  [table.partitionKey]: primaryKey.pk,
3960
3927
  ...table.sortKey ? { [table.sortKey]: primaryKey.sk } : {},
3961
- ...validationResult.value,
3928
+ ...dataForKeyGeneration,
3962
3929
  [entityTypeAttributeName]: config.name,
3963
- ...indexes,
3964
- ...generateTimestamps(["createdAt", "updatedAt"])
3930
+ ...indexes
3965
3931
  };
3966
3932
  Object.assign(builder, { item: validatedItem });
3967
3933
  return validatedItem;
@@ -3994,7 +3960,7 @@ function defineEntity(config) {
3994
3960
  const primaryKeyObj = config.primaryKey.generateKey(key);
3995
3961
  const builder = table.update(primaryKeyObj);
3996
3962
  builder.condition(eq(entityTypeAttributeName, config.name));
3997
- const timestamps = generateTimestamps(["updatedAt"]);
3963
+ const timestamps = generateTimestamps(["updatedAt"], data);
3998
3964
  builder.set({ ...data, ...timestamps });
3999
3965
  return builder;
4000
3966
  },