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.d.cts CHANGED
@@ -1,12 +1,12 @@
1
- export { T as Table } from './table-CHitMHXE.cjs';
1
+ export { T as Table } from './table-BpNOboD9.cjs';
2
2
  export { EntityConfig, EntityRepository, IndexDefinition, QueryEntity, QueryRecord, createIndex, createQueries, defineEntity } from './entity.cjs';
3
- export { p as ComparisonOperator, C as Condition, q as ConditionOperator, E as ExpressionParams, K as KeyConditionOperator, L as LogicalOperator, P as PrimaryKey, r as PrimaryKeyWithoutExpression, k as and, h as attributeExists, j as attributeNotExists, d as beginsWith, c as between, f as contains, e as eq, g as gt, b as gte, i as inArray, l as lt, a as lte, n as ne, m as not, o as or } from './conditions-3ae5znV_.cjs';
4
- export { Q as QueryBuilder, a as QueryOptions } from './query-builder-DFkxojBM.cjs';
3
+ export { p as ComparisonOperator, C as Condition, q as ConditionOperator, E as ExpressionParams, K as KeyConditionOperator, L as LogicalOperator, P as PrimaryKey, r as PrimaryKeyWithoutExpression, k as and, h as attributeExists, j as attributeNotExists, d as beginsWith, c as between, f as contains, e as eq, g as gt, b as gte, i as inArray, l as lt, a as lte, n as ne, m as not, o as or } from './conditions-CC3NDfUU.cjs';
4
+ export { Q as QueryBuilder, a as QueryOptions } from './query-builder-DZ9JKgBN.cjs';
5
5
  export { PutBuilder, PutOptions } from './builders/put-builder.cjs';
6
6
  export { UpdateBuilder, UpdateOptions } from './builders/update-builder.cjs';
7
7
  export { DeleteBuilder, DeleteOptions } from './builders/delete-builder.cjs';
8
8
  export { TransactionBuilder, TransactionOptions } from './builders/transaction-builder.cjs';
9
- export { B as BatchBuilder, a as BatchError, b as BatchResult } from './batch-builder-CKYnMRyz.cjs';
9
+ export { B as BatchBuilder, a as BatchError, b as BatchResult } from './batch-builder-CcxFDKhe.cjs';
10
10
  export { partitionKey, sortKey } from './utils.cjs';
11
11
  import './types.cjs';
12
12
  import '@aws-sdk/lib-dynamodb';
package/dist/index.d.ts CHANGED
@@ -1,12 +1,12 @@
1
- export { T as Table } from './table-m7DQk5dK.js';
1
+ export { T as Table } from './table-BhEeYauU.js';
2
2
  export { EntityConfig, EntityRepository, IndexDefinition, QueryEntity, QueryRecord, createIndex, createQueries, defineEntity } from './entity.js';
3
- export { p as ComparisonOperator, C as Condition, q as ConditionOperator, E as ExpressionParams, K as KeyConditionOperator, L as LogicalOperator, P as PrimaryKey, r as PrimaryKeyWithoutExpression, k as and, h as attributeExists, j as attributeNotExists, d as beginsWith, c as between, f as contains, e as eq, g as gt, b as gte, i as inArray, l as lt, a as lte, n as ne, m as not, o as or } from './conditions-BtynAviC.js';
4
- export { Q as QueryBuilder, a as QueryOptions } from './query-builder-CaHzZmDf.js';
3
+ export { p as ComparisonOperator, C as Condition, q as ConditionOperator, E as ExpressionParams, K as KeyConditionOperator, L as LogicalOperator, P as PrimaryKey, r as PrimaryKeyWithoutExpression, k as and, h as attributeExists, j as attributeNotExists, d as beginsWith, c as between, f as contains, e as eq, g as gt, b as gte, i as inArray, l as lt, a as lte, n as ne, m as not, o as or } from './conditions-DD0bvyHm.js';
4
+ export { Q as QueryBuilder, a as QueryOptions } from './query-builder-BNWRCrJW.js';
5
5
  export { PutBuilder, PutOptions } from './builders/put-builder.js';
6
6
  export { UpdateBuilder, UpdateOptions } from './builders/update-builder.js';
7
7
  export { DeleteBuilder, DeleteOptions } from './builders/delete-builder.js';
8
8
  export { TransactionBuilder, TransactionOptions } from './builders/transaction-builder.js';
9
- export { B as BatchBuilder, a as BatchError, b as BatchResult } from './batch-builder-BOBwOIUE.js';
9
+ export { B as BatchBuilder, a as BatchError, b as BatchResult } from './batch-builder-BytHNL_u.js';
10
10
  export { partitionKey, sortKey } from './utils.js';
11
11
  import './types.js';
12
12
  import '@aws-sdk/lib-dynamodb';
package/dist/index.js CHANGED
@@ -45,6 +45,25 @@ var not = (condition) => ({
45
45
 
46
46
  // src/expression.ts
47
47
  var generateAttributeName = (params, attr) => {
48
+ if (attr.includes(".")) {
49
+ const pathSegments = attr.split(".");
50
+ const segmentNames = [];
51
+ for (const segment of pathSegments) {
52
+ let segmentName;
53
+ for (const [existingName, existingAttr] of Object.entries(params.expressionAttributeNames)) {
54
+ if (existingAttr === segment) {
55
+ segmentName = existingName;
56
+ break;
57
+ }
58
+ }
59
+ if (!segmentName) {
60
+ segmentName = `#${Object.keys(params.expressionAttributeNames).length}`;
61
+ params.expressionAttributeNames[segmentName] = segment;
62
+ }
63
+ segmentNames.push(segmentName);
64
+ }
65
+ return segmentNames.join(".");
66
+ }
48
67
  for (const [existingName, existingAttr] of Object.entries(params.expressionAttributeNames)) {
49
68
  if (existingAttr === attr) {
50
69
  return existingName;
@@ -3756,6 +3775,25 @@ function createEntityAwareDeleteBuilder(builder, entityName) {
3756
3775
  // src/entity.ts
3757
3776
  function defineEntity(config) {
3758
3777
  const entityTypeAttributeName = config.settings?.entityTypeAttributeName ?? "entityType";
3778
+ const buildIndexes = (dataForKeyGeneration, table) => {
3779
+ return Object.entries(config.indexes ?? {}).reduce(
3780
+ (acc, [indexName, index]) => {
3781
+ const key = index.generateKey(dataForKeyGeneration);
3782
+ const gsiConfig = table.gsis[indexName];
3783
+ if (!gsiConfig) {
3784
+ throw new Error(`GSI configuration not found for index: ${indexName}`);
3785
+ }
3786
+ if (key.pk) {
3787
+ acc[gsiConfig.partitionKey] = key.pk;
3788
+ }
3789
+ if (key.sk && gsiConfig.sortKey) {
3790
+ acc[gsiConfig.sortKey] = key.sk;
3791
+ }
3792
+ return acc;
3793
+ },
3794
+ {}
3795
+ );
3796
+ };
3759
3797
  const wrapMethodWithPreparation = (originalMethod, prepareFn, context) => {
3760
3798
  const wrappedMethod = (...args) => {
3761
3799
  prepareFn();
@@ -3774,18 +3812,17 @@ function defineEntity(config) {
3774
3812
  }
3775
3813
  return wrappedMethod;
3776
3814
  };
3777
- const generateTimestamps = (timestampTypes) => {
3815
+ const generateTimestamps = (timestampsToGenerate, data) => {
3778
3816
  if (!config.settings?.timestamps) return {};
3779
3817
  const timestamps = {};
3780
3818
  const now = /* @__PURE__ */ new Date();
3781
3819
  const unixTime = Math.floor(Date.now() / 1e3);
3782
3820
  const { createdAt, updatedAt } = config.settings.timestamps;
3783
- const typesToGenerate = timestampTypes || ["createdAt", "updatedAt"];
3784
- if (createdAt && typesToGenerate.includes("createdAt")) {
3821
+ if (createdAt && timestampsToGenerate.includes("createdAt") && !data.createdAt) {
3785
3822
  const name = createdAt.attributeName ?? "createdAt";
3786
3823
  timestamps[name] = createdAt.format === "UNIX" ? unixTime : now.toISOString();
3787
3824
  }
3788
- if (updatedAt && typesToGenerate.includes("updatedAt")) {
3825
+ if (updatedAt && timestampsToGenerate.includes("updatedAt") && !data.updatedAt) {
3789
3826
  const name = updatedAt.attributeName ?? "updatedAt";
3790
3827
  timestamps[name] = updatedAt.format === "UNIX" ? unixTime : now.toISOString();
3791
3828
  }
@@ -3798,36 +3835,22 @@ function defineEntity(config) {
3798
3835
  create: (data) => {
3799
3836
  const builder = table.create({});
3800
3837
  const prepareValidatedItemAsync = async () => {
3801
- const validationResult = config.schema["~standard"].validate(data);
3802
- const validatedData = validationResult instanceof Promise ? await validationResult : validationResult;
3838
+ const validatedData = await config.schema["~standard"].validate(data);
3803
3839
  if ("issues" in validatedData && validatedData.issues) {
3804
3840
  throw new Error(`Validation failed: ${validatedData.issues.map((i) => i.message).join(", ")}`);
3805
3841
  }
3806
- const primaryKey = config.primaryKey.generateKey(validatedData.value);
3807
- const indexes = Object.entries(config.indexes ?? {}).reduce(
3808
- (acc, [indexName, index]) => {
3809
- const key = index.generateKey(validatedData.value);
3810
- const gsiConfig = table.gsis[indexName];
3811
- if (!gsiConfig) {
3812
- throw new Error(`GSI configuration not found for index: ${indexName}`);
3813
- }
3814
- if (key.pk) {
3815
- acc[gsiConfig.partitionKey] = key.pk;
3816
- }
3817
- if (key.sk && gsiConfig.sortKey) {
3818
- acc[gsiConfig.sortKey] = key.sk;
3819
- }
3820
- return acc;
3821
- },
3822
- {}
3823
- );
3824
- const validatedItem = {
3842
+ const dataForKeyGeneration = {
3825
3843
  ...validatedData.value,
3844
+ ...generateTimestamps(["createdAt", "updatedAt"], validatedData.value)
3845
+ };
3846
+ const primaryKey = config.primaryKey.generateKey(dataForKeyGeneration);
3847
+ const indexes = buildIndexes(dataForKeyGeneration, table);
3848
+ const validatedItem = {
3849
+ ...dataForKeyGeneration,
3826
3850
  [entityTypeAttributeName]: config.name,
3827
3851
  [table.partitionKey]: primaryKey.pk,
3828
3852
  ...table.sortKey ? { [table.sortKey]: primaryKey.sk } : {},
3829
- ...indexes,
3830
- ...generateTimestamps(["createdAt", "updatedAt"])
3853
+ ...indexes
3831
3854
  };
3832
3855
  Object.assign(builder, { item: validatedItem });
3833
3856
  return validatedItem;
@@ -3836,37 +3859,24 @@ function defineEntity(config) {
3836
3859
  const validationResult = config.schema["~standard"].validate(data);
3837
3860
  if (validationResult instanceof Promise) {
3838
3861
  throw new Error(
3839
- "Async validation is not supported in create method. The schema must support synchronous validation for transaction compatibility."
3862
+ "Async validation is not supported in withBatch or withTransaction. The schema must support synchronous validation for compatibility."
3840
3863
  );
3841
3864
  }
3842
3865
  if ("issues" in validationResult && validationResult.issues) {
3843
3866
  throw new Error(`Validation failed: ${validationResult.issues.map((i) => i.message).join(", ")}`);
3844
3867
  }
3845
- const primaryKey = config.primaryKey.generateKey(validationResult.value);
3846
- const indexes = Object.entries(config.indexes ?? {}).reduce(
3847
- (acc, [indexName, index]) => {
3848
- const key = index.generateKey(validationResult.value);
3849
- const gsiConfig = table.gsis[indexName];
3850
- if (!gsiConfig) {
3851
- throw new Error(`GSI configuration not found for index: ${indexName}`);
3852
- }
3853
- if (key.pk) {
3854
- acc[gsiConfig.partitionKey] = key.pk;
3855
- }
3856
- if (key.sk && gsiConfig.sortKey) {
3857
- acc[gsiConfig.sortKey] = key.sk;
3858
- }
3859
- return acc;
3860
- },
3861
- {}
3862
- );
3863
- const validatedItem = {
3868
+ const dataForKeyGeneration = {
3864
3869
  ...validationResult.value,
3870
+ ...generateTimestamps(["createdAt", "updatedAt"], validationResult.value)
3871
+ };
3872
+ const primaryKey = config.primaryKey.generateKey(dataForKeyGeneration);
3873
+ const indexes = buildIndexes(dataForKeyGeneration, table);
3874
+ const validatedItem = {
3875
+ ...dataForKeyGeneration,
3865
3876
  [entityTypeAttributeName]: config.name,
3866
3877
  [table.partitionKey]: primaryKey.pk,
3867
3878
  ...table.sortKey ? { [table.sortKey]: primaryKey.sk } : {},
3868
- ...indexes,
3869
- ...generateTimestamps(["createdAt", "updatedAt"])
3879
+ ...indexes
3870
3880
  };
3871
3881
  Object.assign(builder, { item: validatedItem });
3872
3882
  return validatedItem;
@@ -3893,36 +3903,22 @@ function defineEntity(config) {
3893
3903
  upsert: (data) => {
3894
3904
  const builder = table.put({});
3895
3905
  const prepareValidatedItemAsync = async () => {
3896
- const validationResult = config.schema["~standard"].validate(data);
3897
- const validatedData = validationResult instanceof Promise ? await validationResult : validationResult;
3906
+ const validatedData = await config.schema["~standard"].validate(data);
3898
3907
  if ("issues" in validatedData && validatedData.issues) {
3899
3908
  throw new Error(`Validation failed: ${validatedData.issues.map((i) => i.message).join(", ")}`);
3900
3909
  }
3901
- const primaryKey = config.primaryKey.generateKey(validatedData.value);
3902
- const indexes = Object.entries(config.indexes ?? {}).reduce(
3903
- (acc, [indexName, index]) => {
3904
- const key = index.generateKey(validatedData.value);
3905
- const gsiConfig = table.gsis[indexName];
3906
- if (!gsiConfig) {
3907
- throw new Error(`GSI configuration not found for index: ${indexName}`);
3908
- }
3909
- if (key.pk) {
3910
- acc[gsiConfig.partitionKey] = key.pk;
3911
- }
3912
- if (key.sk && gsiConfig.sortKey) {
3913
- acc[gsiConfig.sortKey] = key.sk;
3914
- }
3915
- return acc;
3916
- },
3917
- {}
3918
- );
3910
+ const dataForKeyGeneration = {
3911
+ ...validatedData.value,
3912
+ ...generateTimestamps(["createdAt", "updatedAt"], validatedData.value)
3913
+ };
3914
+ const primaryKey = config.primaryKey.generateKey(dataForKeyGeneration);
3915
+ const indexes = buildIndexes(dataForKeyGeneration, table);
3919
3916
  const validatedItem = {
3920
3917
  [table.partitionKey]: primaryKey.pk,
3921
3918
  ...table.sortKey ? { [table.sortKey]: primaryKey.sk } : {},
3922
- ...validatedData.value,
3919
+ ...dataForKeyGeneration,
3923
3920
  [entityTypeAttributeName]: config.name,
3924
- ...indexes,
3925
- ...generateTimestamps(["createdAt", "updatedAt"])
3921
+ ...indexes
3926
3922
  };
3927
3923
  Object.assign(builder, { item: validatedItem });
3928
3924
  return validatedItem;
@@ -3930,36 +3926,25 @@ function defineEntity(config) {
3930
3926
  const prepareValidatedItemSync = () => {
3931
3927
  const validationResult = config.schema["~standard"].validate(data);
3932
3928
  if (validationResult instanceof Promise) {
3933
- throw new Error("Async validation is not supported in withTransaction. Use execute() instead.");
3929
+ throw new Error(
3930
+ "Async validation is not supported in withTransaction or withBatch. Use execute() instead."
3931
+ );
3934
3932
  }
3935
3933
  if ("issues" in validationResult && validationResult.issues) {
3936
3934
  throw new Error(`Validation failed: ${validationResult.issues.map((i) => i.message).join(", ")}`);
3937
3935
  }
3938
- const primaryKey = config.primaryKey.generateKey(validationResult.value);
3939
- const indexes = Object.entries(config.indexes ?? {}).reduce(
3940
- (acc, [indexName, index]) => {
3941
- const key = index.generateKey(validationResult.value);
3942
- const gsiConfig = table.gsis[indexName];
3943
- if (!gsiConfig) {
3944
- throw new Error(`GSI configuration not found for index: ${indexName}`);
3945
- }
3946
- if (key.pk) {
3947
- acc[gsiConfig.partitionKey] = key.pk;
3948
- }
3949
- if (key.sk && gsiConfig.sortKey) {
3950
- acc[gsiConfig.sortKey] = key.sk;
3951
- }
3952
- return acc;
3953
- },
3954
- {}
3955
- );
3936
+ const dataForKeyGeneration = {
3937
+ ...validationResult.value,
3938
+ ...generateTimestamps(["createdAt", "updatedAt"], validationResult.value)
3939
+ };
3940
+ const primaryKey = config.primaryKey.generateKey(dataForKeyGeneration);
3941
+ const indexes = buildIndexes(dataForKeyGeneration, table);
3956
3942
  const validatedItem = {
3957
3943
  [table.partitionKey]: primaryKey.pk,
3958
3944
  ...table.sortKey ? { [table.sortKey]: primaryKey.sk } : {},
3959
- ...validationResult.value,
3945
+ ...dataForKeyGeneration,
3960
3946
  [entityTypeAttributeName]: config.name,
3961
- ...indexes,
3962
- ...generateTimestamps(["createdAt", "updatedAt"])
3947
+ ...indexes
3963
3948
  };
3964
3949
  Object.assign(builder, { item: validatedItem });
3965
3950
  return validatedItem;
@@ -3992,7 +3977,7 @@ function defineEntity(config) {
3992
3977
  const primaryKeyObj = config.primaryKey.generateKey(key);
3993
3978
  const builder = table.update(primaryKeyObj);
3994
3979
  builder.condition(eq(entityTypeAttributeName, config.name));
3995
- const timestamps = generateTimestamps(["updatedAt"]);
3980
+ const timestamps = generateTimestamps(["updatedAt"], data);
3996
3981
  builder.set({ ...data, ...timestamps });
3997
3982
  return builder;
3998
3983
  },