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/entity.d.cts CHANGED
@@ -1,10 +1,10 @@
1
- import { G as GetBuilder } from './batch-builder-CKYnMRyz.cjs';
2
- import { S as ScanBuilder, T as Table } from './table-CHitMHXE.cjs';
1
+ import { G as GetBuilder } from './batch-builder-CcxFDKhe.cjs';
2
+ import { S as ScanBuilder, T as Table } from './table-BpNOboD9.cjs';
3
3
  import { UpdateBuilder } from './builders/update-builder.cjs';
4
4
  import { StandardSchemaV1 } from './standard-schema.cjs';
5
5
  import { DynamoItem, TableConfig, Index } from './types.cjs';
6
- import { r as PrimaryKeyWithoutExpression, P as PrimaryKey } from './conditions-3ae5znV_.cjs';
7
- import { Q as QueryBuilder } from './query-builder-DFkxojBM.cjs';
6
+ import { r as PrimaryKeyWithoutExpression, P as PrimaryKey } from './conditions-CC3NDfUU.cjs';
7
+ import { Q as QueryBuilder } from './query-builder-DZ9JKgBN.cjs';
8
8
  import { PutBuilder } from './builders/put-builder.cjs';
9
9
  import { DeleteBuilder } from './builders/delete-builder.cjs';
10
10
  import './builder-types-BTVhQSHI.cjs';
package/dist/entity.d.ts CHANGED
@@ -1,10 +1,10 @@
1
- import { G as GetBuilder } from './batch-builder-BOBwOIUE.js';
2
- import { S as ScanBuilder, T as Table } from './table-m7DQk5dK.js';
1
+ import { G as GetBuilder } from './batch-builder-BytHNL_u.js';
2
+ import { S as ScanBuilder, T as Table } from './table-BhEeYauU.js';
3
3
  import { UpdateBuilder } from './builders/update-builder.js';
4
4
  import { StandardSchemaV1 } from './standard-schema.js';
5
5
  import { DynamoItem, TableConfig, Index } from './types.js';
6
- import { r as PrimaryKeyWithoutExpression, P as PrimaryKey } from './conditions-BtynAviC.js';
7
- import { Q as QueryBuilder } from './query-builder-CaHzZmDf.js';
6
+ import { r as PrimaryKeyWithoutExpression, P as PrimaryKey } from './conditions-DD0bvyHm.js';
7
+ import { Q as QueryBuilder } from './query-builder-BNWRCrJW.js';
8
8
  import { PutBuilder } from './builders/put-builder.js';
9
9
  import { DeleteBuilder } from './builders/delete-builder.js';
10
10
  import './builder-types-CzuLR4Th.js';
package/dist/entity.js CHANGED
@@ -37,6 +37,25 @@ function createEntityAwareDeleteBuilder(builder, entityName) {
37
37
  // src/entity.ts
38
38
  function defineEntity(config) {
39
39
  const entityTypeAttributeName = config.settings?.entityTypeAttributeName ?? "entityType";
40
+ const buildIndexes = (dataForKeyGeneration, table) => {
41
+ return Object.entries(config.indexes ?? {}).reduce(
42
+ (acc, [indexName, index]) => {
43
+ const key = index.generateKey(dataForKeyGeneration);
44
+ const gsiConfig = table.gsis[indexName];
45
+ if (!gsiConfig) {
46
+ throw new Error(`GSI configuration not found for index: ${indexName}`);
47
+ }
48
+ if (key.pk) {
49
+ acc[gsiConfig.partitionKey] = key.pk;
50
+ }
51
+ if (key.sk && gsiConfig.sortKey) {
52
+ acc[gsiConfig.sortKey] = key.sk;
53
+ }
54
+ return acc;
55
+ },
56
+ {}
57
+ );
58
+ };
40
59
  const wrapMethodWithPreparation = (originalMethod, prepareFn, context) => {
41
60
  const wrappedMethod = (...args) => {
42
61
  prepareFn();
@@ -55,18 +74,17 @@ function defineEntity(config) {
55
74
  }
56
75
  return wrappedMethod;
57
76
  };
58
- const generateTimestamps = (timestampTypes) => {
77
+ const generateTimestamps = (timestampsToGenerate, data) => {
59
78
  if (!config.settings?.timestamps) return {};
60
79
  const timestamps = {};
61
80
  const now = /* @__PURE__ */ new Date();
62
81
  const unixTime = Math.floor(Date.now() / 1e3);
63
82
  const { createdAt, updatedAt } = config.settings.timestamps;
64
- const typesToGenerate = timestampTypes || ["createdAt", "updatedAt"];
65
- if (createdAt && typesToGenerate.includes("createdAt")) {
83
+ if (createdAt && timestampsToGenerate.includes("createdAt") && !data.createdAt) {
66
84
  const name = createdAt.attributeName ?? "createdAt";
67
85
  timestamps[name] = createdAt.format === "UNIX" ? unixTime : now.toISOString();
68
86
  }
69
- if (updatedAt && typesToGenerate.includes("updatedAt")) {
87
+ if (updatedAt && timestampsToGenerate.includes("updatedAt") && !data.updatedAt) {
70
88
  const name = updatedAt.attributeName ?? "updatedAt";
71
89
  timestamps[name] = updatedAt.format === "UNIX" ? unixTime : now.toISOString();
72
90
  }
@@ -79,36 +97,22 @@ function defineEntity(config) {
79
97
  create: (data) => {
80
98
  const builder = table.create({});
81
99
  const prepareValidatedItemAsync = async () => {
82
- const validationResult = config.schema["~standard"].validate(data);
83
- const validatedData = validationResult instanceof Promise ? await validationResult : validationResult;
100
+ const validatedData = await config.schema["~standard"].validate(data);
84
101
  if ("issues" in validatedData && validatedData.issues) {
85
102
  throw new Error(`Validation failed: ${validatedData.issues.map((i) => i.message).join(", ")}`);
86
103
  }
87
- const primaryKey = config.primaryKey.generateKey(validatedData.value);
88
- const indexes = Object.entries(config.indexes ?? {}).reduce(
89
- (acc, [indexName, index]) => {
90
- const key = index.generateKey(validatedData.value);
91
- const gsiConfig = table.gsis[indexName];
92
- if (!gsiConfig) {
93
- throw new Error(`GSI configuration not found for index: ${indexName}`);
94
- }
95
- if (key.pk) {
96
- acc[gsiConfig.partitionKey] = key.pk;
97
- }
98
- if (key.sk && gsiConfig.sortKey) {
99
- acc[gsiConfig.sortKey] = key.sk;
100
- }
101
- return acc;
102
- },
103
- {}
104
- );
105
- const validatedItem = {
104
+ const dataForKeyGeneration = {
106
105
  ...validatedData.value,
106
+ ...generateTimestamps(["createdAt", "updatedAt"], validatedData.value)
107
+ };
108
+ const primaryKey = config.primaryKey.generateKey(dataForKeyGeneration);
109
+ const indexes = buildIndexes(dataForKeyGeneration, table);
110
+ const validatedItem = {
111
+ ...dataForKeyGeneration,
107
112
  [entityTypeAttributeName]: config.name,
108
113
  [table.partitionKey]: primaryKey.pk,
109
114
  ...table.sortKey ? { [table.sortKey]: primaryKey.sk } : {},
110
- ...indexes,
111
- ...generateTimestamps(["createdAt", "updatedAt"])
115
+ ...indexes
112
116
  };
113
117
  Object.assign(builder, { item: validatedItem });
114
118
  return validatedItem;
@@ -117,37 +121,24 @@ function defineEntity(config) {
117
121
  const validationResult = config.schema["~standard"].validate(data);
118
122
  if (validationResult instanceof Promise) {
119
123
  throw new Error(
120
- "Async validation is not supported in create method. The schema must support synchronous validation for transaction compatibility."
124
+ "Async validation is not supported in withBatch or withTransaction. The schema must support synchronous validation for compatibility."
121
125
  );
122
126
  }
123
127
  if ("issues" in validationResult && validationResult.issues) {
124
128
  throw new Error(`Validation failed: ${validationResult.issues.map((i) => i.message).join(", ")}`);
125
129
  }
126
- const primaryKey = config.primaryKey.generateKey(validationResult.value);
127
- const indexes = Object.entries(config.indexes ?? {}).reduce(
128
- (acc, [indexName, index]) => {
129
- const key = index.generateKey(validationResult.value);
130
- const gsiConfig = table.gsis[indexName];
131
- if (!gsiConfig) {
132
- throw new Error(`GSI configuration not found for index: ${indexName}`);
133
- }
134
- if (key.pk) {
135
- acc[gsiConfig.partitionKey] = key.pk;
136
- }
137
- if (key.sk && gsiConfig.sortKey) {
138
- acc[gsiConfig.sortKey] = key.sk;
139
- }
140
- return acc;
141
- },
142
- {}
143
- );
144
- const validatedItem = {
130
+ const dataForKeyGeneration = {
145
131
  ...validationResult.value,
132
+ ...generateTimestamps(["createdAt", "updatedAt"], validationResult.value)
133
+ };
134
+ const primaryKey = config.primaryKey.generateKey(dataForKeyGeneration);
135
+ const indexes = buildIndexes(dataForKeyGeneration, table);
136
+ const validatedItem = {
137
+ ...dataForKeyGeneration,
146
138
  [entityTypeAttributeName]: config.name,
147
139
  [table.partitionKey]: primaryKey.pk,
148
140
  ...table.sortKey ? { [table.sortKey]: primaryKey.sk } : {},
149
- ...indexes,
150
- ...generateTimestamps(["createdAt", "updatedAt"])
141
+ ...indexes
151
142
  };
152
143
  Object.assign(builder, { item: validatedItem });
153
144
  return validatedItem;
@@ -174,36 +165,22 @@ function defineEntity(config) {
174
165
  upsert: (data) => {
175
166
  const builder = table.put({});
176
167
  const prepareValidatedItemAsync = async () => {
177
- const validationResult = config.schema["~standard"].validate(data);
178
- const validatedData = validationResult instanceof Promise ? await validationResult : validationResult;
168
+ const validatedData = await config.schema["~standard"].validate(data);
179
169
  if ("issues" in validatedData && validatedData.issues) {
180
170
  throw new Error(`Validation failed: ${validatedData.issues.map((i) => i.message).join(", ")}`);
181
171
  }
182
- const primaryKey = config.primaryKey.generateKey(validatedData.value);
183
- const indexes = Object.entries(config.indexes ?? {}).reduce(
184
- (acc, [indexName, index]) => {
185
- const key = index.generateKey(validatedData.value);
186
- const gsiConfig = table.gsis[indexName];
187
- if (!gsiConfig) {
188
- throw new Error(`GSI configuration not found for index: ${indexName}`);
189
- }
190
- if (key.pk) {
191
- acc[gsiConfig.partitionKey] = key.pk;
192
- }
193
- if (key.sk && gsiConfig.sortKey) {
194
- acc[gsiConfig.sortKey] = key.sk;
195
- }
196
- return acc;
197
- },
198
- {}
199
- );
172
+ const dataForKeyGeneration = {
173
+ ...validatedData.value,
174
+ ...generateTimestamps(["createdAt", "updatedAt"], validatedData.value)
175
+ };
176
+ const primaryKey = config.primaryKey.generateKey(dataForKeyGeneration);
177
+ const indexes = buildIndexes(dataForKeyGeneration, table);
200
178
  const validatedItem = {
201
179
  [table.partitionKey]: primaryKey.pk,
202
180
  ...table.sortKey ? { [table.sortKey]: primaryKey.sk } : {},
203
- ...validatedData.value,
181
+ ...dataForKeyGeneration,
204
182
  [entityTypeAttributeName]: config.name,
205
- ...indexes,
206
- ...generateTimestamps(["createdAt", "updatedAt"])
183
+ ...indexes
207
184
  };
208
185
  Object.assign(builder, { item: validatedItem });
209
186
  return validatedItem;
@@ -211,36 +188,25 @@ function defineEntity(config) {
211
188
  const prepareValidatedItemSync = () => {
212
189
  const validationResult = config.schema["~standard"].validate(data);
213
190
  if (validationResult instanceof Promise) {
214
- throw new Error("Async validation is not supported in withTransaction. Use execute() instead.");
191
+ throw new Error(
192
+ "Async validation is not supported in withTransaction or withBatch. Use execute() instead."
193
+ );
215
194
  }
216
195
  if ("issues" in validationResult && validationResult.issues) {
217
196
  throw new Error(`Validation failed: ${validationResult.issues.map((i) => i.message).join(", ")}`);
218
197
  }
219
- const primaryKey = config.primaryKey.generateKey(validationResult.value);
220
- const indexes = Object.entries(config.indexes ?? {}).reduce(
221
- (acc, [indexName, index]) => {
222
- const key = index.generateKey(validationResult.value);
223
- const gsiConfig = table.gsis[indexName];
224
- if (!gsiConfig) {
225
- throw new Error(`GSI configuration not found for index: ${indexName}`);
226
- }
227
- if (key.pk) {
228
- acc[gsiConfig.partitionKey] = key.pk;
229
- }
230
- if (key.sk && gsiConfig.sortKey) {
231
- acc[gsiConfig.sortKey] = key.sk;
232
- }
233
- return acc;
234
- },
235
- {}
236
- );
198
+ const dataForKeyGeneration = {
199
+ ...validationResult.value,
200
+ ...generateTimestamps(["createdAt", "updatedAt"], validationResult.value)
201
+ };
202
+ const primaryKey = config.primaryKey.generateKey(dataForKeyGeneration);
203
+ const indexes = buildIndexes(dataForKeyGeneration, table);
237
204
  const validatedItem = {
238
205
  [table.partitionKey]: primaryKey.pk,
239
206
  ...table.sortKey ? { [table.sortKey]: primaryKey.sk } : {},
240
- ...validationResult.value,
207
+ ...dataForKeyGeneration,
241
208
  [entityTypeAttributeName]: config.name,
242
- ...indexes,
243
- ...generateTimestamps(["createdAt", "updatedAt"])
209
+ ...indexes
244
210
  };
245
211
  Object.assign(builder, { item: validatedItem });
246
212
  return validatedItem;
@@ -273,7 +239,7 @@ function defineEntity(config) {
273
239
  const primaryKeyObj = config.primaryKey.generateKey(key);
274
240
  const builder = table.update(primaryKeyObj);
275
241
  builder.condition(eq(entityTypeAttributeName, config.name));
276
- const timestamps = generateTimestamps(["updatedAt"]);
242
+ const timestamps = generateTimestamps(["updatedAt"], data);
277
243
  builder.set({ ...data, ...timestamps });
278
244
  return builder;
279
245
  },