dyno-table 2.0.0 → 2.0.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.
Files changed (65) hide show
  1. package/dist/{batch-builder-BOBwOIUE.d.ts → batch-builder-BytHNL_u.d.ts} +2 -2
  2. package/dist/{batch-builder-CKYnMRyz.d.cts → batch-builder-CcxFDKhe.d.cts} +2 -2
  3. package/dist/builders/condition-check-builder.cjs +19 -0
  4. package/dist/builders/condition-check-builder.cjs.map +1 -1
  5. package/dist/builders/condition-check-builder.d.cts +12 -3
  6. package/dist/builders/condition-check-builder.d.ts +12 -3
  7. package/dist/builders/condition-check-builder.js +19 -0
  8. package/dist/builders/condition-check-builder.js.map +1 -1
  9. package/dist/builders/delete-builder.cjs +19 -0
  10. package/dist/builders/delete-builder.cjs.map +1 -1
  11. package/dist/builders/delete-builder.d.cts +12 -3
  12. package/dist/builders/delete-builder.d.ts +12 -3
  13. package/dist/builders/delete-builder.js +19 -0
  14. package/dist/builders/delete-builder.js.map +1 -1
  15. package/dist/builders/put-builder.cjs +19 -0
  16. package/dist/builders/put-builder.cjs.map +1 -1
  17. package/dist/builders/put-builder.d.cts +12 -3
  18. package/dist/builders/put-builder.d.ts +12 -3
  19. package/dist/builders/put-builder.js +19 -0
  20. package/dist/builders/put-builder.js.map +1 -1
  21. package/dist/builders/query-builder.cjs.map +1 -1
  22. package/dist/builders/query-builder.d.cts +2 -2
  23. package/dist/builders/query-builder.d.ts +2 -2
  24. package/dist/builders/query-builder.js.map +1 -1
  25. package/dist/builders/transaction-builder.cjs +19 -0
  26. package/dist/builders/transaction-builder.cjs.map +1 -1
  27. package/dist/builders/transaction-builder.d.cts +2 -2
  28. package/dist/builders/transaction-builder.d.ts +2 -2
  29. package/dist/builders/transaction-builder.js +19 -0
  30. package/dist/builders/transaction-builder.js.map +1 -1
  31. package/dist/builders/update-builder.cjs +19 -0
  32. package/dist/builders/update-builder.cjs.map +1 -1
  33. package/dist/builders/update-builder.d.cts +11 -2
  34. package/dist/builders/update-builder.d.ts +11 -2
  35. package/dist/builders/update-builder.js +19 -0
  36. package/dist/builders/update-builder.js.map +1 -1
  37. package/dist/{conditions-3ae5znV_.d.cts → conditions-CC3NDfUU.d.cts} +15 -13
  38. package/dist/{conditions-BtynAviC.d.ts → conditions-DD0bvyHm.d.ts} +15 -13
  39. package/dist/conditions.cjs.map +1 -1
  40. package/dist/conditions.d.cts +1 -1
  41. package/dist/conditions.d.ts +1 -1
  42. package/dist/conditions.js.map +1 -1
  43. package/dist/entity.cjs +61 -95
  44. package/dist/entity.cjs.map +1 -1
  45. package/dist/entity.d.cts +4 -4
  46. package/dist/entity.d.ts +4 -4
  47. package/dist/entity.js +61 -95
  48. package/dist/entity.js.map +1 -1
  49. package/dist/index.cjs +80 -95
  50. package/dist/index.cjs.map +1 -1
  51. package/dist/index.d.cts +4 -4
  52. package/dist/index.d.ts +4 -4
  53. package/dist/index.js +80 -95
  54. package/dist/index.js.map +1 -1
  55. package/dist/{query-builder-CaHzZmDf.d.ts → query-builder-BNWRCrJW.d.ts} +2 -2
  56. package/dist/{query-builder-DFkxojBM.d.cts → query-builder-DZ9JKgBN.d.cts} +2 -2
  57. package/dist/{table-m7DQk5dK.d.ts → table-BhEeYauU.d.ts} +3 -3
  58. package/dist/{table-CHitMHXE.d.cts → table-BpNOboD9.d.cts} +3 -3
  59. package/dist/table.cjs +19 -0
  60. package/dist/table.cjs.map +1 -1
  61. package/dist/table.d.cts +4 -4
  62. package/dist/table.d.ts +4 -4
  63. package/dist/table.js +19 -0
  64. package/dist/table.js.map +1 -1
  65. package/package.json +1 -1
package/dist/index.cjs CHANGED
@@ -47,6 +47,25 @@ var not = (condition) => ({
47
47
 
48
48
  // src/expression.ts
49
49
  var generateAttributeName = (params, attr) => {
50
+ if (attr.includes(".")) {
51
+ const pathSegments = attr.split(".");
52
+ const segmentNames = [];
53
+ for (const segment of pathSegments) {
54
+ let segmentName;
55
+ for (const [existingName, existingAttr] of Object.entries(params.expressionAttributeNames)) {
56
+ if (existingAttr === segment) {
57
+ segmentName = existingName;
58
+ break;
59
+ }
60
+ }
61
+ if (!segmentName) {
62
+ segmentName = `#${Object.keys(params.expressionAttributeNames).length}`;
63
+ params.expressionAttributeNames[segmentName] = segment;
64
+ }
65
+ segmentNames.push(segmentName);
66
+ }
67
+ return segmentNames.join(".");
68
+ }
50
69
  for (const [existingName, existingAttr] of Object.entries(params.expressionAttributeNames)) {
51
70
  if (existingAttr === attr) {
52
71
  return existingName;
@@ -3758,6 +3777,25 @@ function createEntityAwareDeleteBuilder(builder, entityName) {
3758
3777
  // src/entity.ts
3759
3778
  function defineEntity(config) {
3760
3779
  const entityTypeAttributeName = config.settings?.entityTypeAttributeName ?? "entityType";
3780
+ const buildIndexes = (dataForKeyGeneration, table) => {
3781
+ return Object.entries(config.indexes ?? {}).reduce(
3782
+ (acc, [indexName, index]) => {
3783
+ const key = index.generateKey(dataForKeyGeneration);
3784
+ const gsiConfig = table.gsis[indexName];
3785
+ if (!gsiConfig) {
3786
+ throw new Error(`GSI configuration not found for index: ${indexName}`);
3787
+ }
3788
+ if (key.pk) {
3789
+ acc[gsiConfig.partitionKey] = key.pk;
3790
+ }
3791
+ if (key.sk && gsiConfig.sortKey) {
3792
+ acc[gsiConfig.sortKey] = key.sk;
3793
+ }
3794
+ return acc;
3795
+ },
3796
+ {}
3797
+ );
3798
+ };
3761
3799
  const wrapMethodWithPreparation = (originalMethod, prepareFn, context) => {
3762
3800
  const wrappedMethod = (...args) => {
3763
3801
  prepareFn();
@@ -3776,18 +3814,17 @@ function defineEntity(config) {
3776
3814
  }
3777
3815
  return wrappedMethod;
3778
3816
  };
3779
- const generateTimestamps = (timestampTypes) => {
3817
+ const generateTimestamps = (timestampsToGenerate, data) => {
3780
3818
  if (!config.settings?.timestamps) return {};
3781
3819
  const timestamps = {};
3782
3820
  const now = /* @__PURE__ */ new Date();
3783
3821
  const unixTime = Math.floor(Date.now() / 1e3);
3784
3822
  const { createdAt, updatedAt } = config.settings.timestamps;
3785
- const typesToGenerate = timestampTypes || ["createdAt", "updatedAt"];
3786
- if (createdAt && typesToGenerate.includes("createdAt")) {
3823
+ if (createdAt && timestampsToGenerate.includes("createdAt") && !data.createdAt) {
3787
3824
  const name = createdAt.attributeName ?? "createdAt";
3788
3825
  timestamps[name] = createdAt.format === "UNIX" ? unixTime : now.toISOString();
3789
3826
  }
3790
- if (updatedAt && typesToGenerate.includes("updatedAt")) {
3827
+ if (updatedAt && timestampsToGenerate.includes("updatedAt") && !data.updatedAt) {
3791
3828
  const name = updatedAt.attributeName ?? "updatedAt";
3792
3829
  timestamps[name] = updatedAt.format === "UNIX" ? unixTime : now.toISOString();
3793
3830
  }
@@ -3800,36 +3837,22 @@ function defineEntity(config) {
3800
3837
  create: (data) => {
3801
3838
  const builder = table.create({});
3802
3839
  const prepareValidatedItemAsync = async () => {
3803
- const validationResult = config.schema["~standard"].validate(data);
3804
- const validatedData = validationResult instanceof Promise ? await validationResult : validationResult;
3840
+ const validatedData = await config.schema["~standard"].validate(data);
3805
3841
  if ("issues" in validatedData && validatedData.issues) {
3806
3842
  throw new Error(`Validation failed: ${validatedData.issues.map((i) => i.message).join(", ")}`);
3807
3843
  }
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 = {
3844
+ const dataForKeyGeneration = {
3827
3845
  ...validatedData.value,
3846
+ ...generateTimestamps(["createdAt", "updatedAt"], validatedData.value)
3847
+ };
3848
+ const primaryKey = config.primaryKey.generateKey(dataForKeyGeneration);
3849
+ const indexes = buildIndexes(dataForKeyGeneration, table);
3850
+ const validatedItem = {
3851
+ ...dataForKeyGeneration,
3828
3852
  [entityTypeAttributeName]: config.name,
3829
3853
  [table.partitionKey]: primaryKey.pk,
3830
3854
  ...table.sortKey ? { [table.sortKey]: primaryKey.sk } : {},
3831
- ...indexes,
3832
- ...generateTimestamps(["createdAt", "updatedAt"])
3855
+ ...indexes
3833
3856
  };
3834
3857
  Object.assign(builder, { item: validatedItem });
3835
3858
  return validatedItem;
@@ -3838,37 +3861,24 @@ function defineEntity(config) {
3838
3861
  const validationResult = config.schema["~standard"].validate(data);
3839
3862
  if (validationResult instanceof Promise) {
3840
3863
  throw new Error(
3841
- "Async validation is not supported in create method. The schema must support synchronous validation for transaction compatibility."
3864
+ "Async validation is not supported in withBatch or withTransaction. The schema must support synchronous validation for compatibility."
3842
3865
  );
3843
3866
  }
3844
3867
  if ("issues" in validationResult && validationResult.issues) {
3845
3868
  throw new Error(`Validation failed: ${validationResult.issues.map((i) => i.message).join(", ")}`);
3846
3869
  }
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 = {
3870
+ const dataForKeyGeneration = {
3866
3871
  ...validationResult.value,
3872
+ ...generateTimestamps(["createdAt", "updatedAt"], validationResult.value)
3873
+ };
3874
+ const primaryKey = config.primaryKey.generateKey(dataForKeyGeneration);
3875
+ const indexes = buildIndexes(dataForKeyGeneration, table);
3876
+ const validatedItem = {
3877
+ ...dataForKeyGeneration,
3867
3878
  [entityTypeAttributeName]: config.name,
3868
3879
  [table.partitionKey]: primaryKey.pk,
3869
3880
  ...table.sortKey ? { [table.sortKey]: primaryKey.sk } : {},
3870
- ...indexes,
3871
- ...generateTimestamps(["createdAt", "updatedAt"])
3881
+ ...indexes
3872
3882
  };
3873
3883
  Object.assign(builder, { item: validatedItem });
3874
3884
  return validatedItem;
@@ -3895,36 +3905,22 @@ function defineEntity(config) {
3895
3905
  upsert: (data) => {
3896
3906
  const builder = table.put({});
3897
3907
  const prepareValidatedItemAsync = async () => {
3898
- const validationResult = config.schema["~standard"].validate(data);
3899
- const validatedData = validationResult instanceof Promise ? await validationResult : validationResult;
3908
+ const validatedData = await config.schema["~standard"].validate(data);
3900
3909
  if ("issues" in validatedData && validatedData.issues) {
3901
3910
  throw new Error(`Validation failed: ${validatedData.issues.map((i) => i.message).join(", ")}`);
3902
3911
  }
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
- );
3912
+ const dataForKeyGeneration = {
3913
+ ...validatedData.value,
3914
+ ...generateTimestamps(["createdAt", "updatedAt"], validatedData.value)
3915
+ };
3916
+ const primaryKey = config.primaryKey.generateKey(dataForKeyGeneration);
3917
+ const indexes = buildIndexes(dataForKeyGeneration, table);
3921
3918
  const validatedItem = {
3922
3919
  [table.partitionKey]: primaryKey.pk,
3923
3920
  ...table.sortKey ? { [table.sortKey]: primaryKey.sk } : {},
3924
- ...validatedData.value,
3921
+ ...dataForKeyGeneration,
3925
3922
  [entityTypeAttributeName]: config.name,
3926
- ...indexes,
3927
- ...generateTimestamps(["createdAt", "updatedAt"])
3923
+ ...indexes
3928
3924
  };
3929
3925
  Object.assign(builder, { item: validatedItem });
3930
3926
  return validatedItem;
@@ -3932,36 +3928,25 @@ function defineEntity(config) {
3932
3928
  const prepareValidatedItemSync = () => {
3933
3929
  const validationResult = config.schema["~standard"].validate(data);
3934
3930
  if (validationResult instanceof Promise) {
3935
- throw new Error("Async validation is not supported in withTransaction. Use execute() instead.");
3931
+ throw new Error(
3932
+ "Async validation is not supported in withTransaction or withBatch. Use execute() instead."
3933
+ );
3936
3934
  }
3937
3935
  if ("issues" in validationResult && validationResult.issues) {
3938
3936
  throw new Error(`Validation failed: ${validationResult.issues.map((i) => i.message).join(", ")}`);
3939
3937
  }
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
- );
3938
+ const dataForKeyGeneration = {
3939
+ ...validationResult.value,
3940
+ ...generateTimestamps(["createdAt", "updatedAt"], validationResult.value)
3941
+ };
3942
+ const primaryKey = config.primaryKey.generateKey(dataForKeyGeneration);
3943
+ const indexes = buildIndexes(dataForKeyGeneration, table);
3958
3944
  const validatedItem = {
3959
3945
  [table.partitionKey]: primaryKey.pk,
3960
3946
  ...table.sortKey ? { [table.sortKey]: primaryKey.sk } : {},
3961
- ...validationResult.value,
3947
+ ...dataForKeyGeneration,
3962
3948
  [entityTypeAttributeName]: config.name,
3963
- ...indexes,
3964
- ...generateTimestamps(["createdAt", "updatedAt"])
3949
+ ...indexes
3965
3950
  };
3966
3951
  Object.assign(builder, { item: validatedItem });
3967
3952
  return validatedItem;
@@ -3994,7 +3979,7 @@ function defineEntity(config) {
3994
3979
  const primaryKeyObj = config.primaryKey.generateKey(key);
3995
3980
  const builder = table.update(primaryKeyObj);
3996
3981
  builder.condition(eq(entityTypeAttributeName, config.name));
3997
- const timestamps = generateTimestamps(["updatedAt"]);
3982
+ const timestamps = generateTimestamps(["updatedAt"], data);
3998
3983
  builder.set({ ...data, ...timestamps });
3999
3984
  return builder;
4000
3985
  },