dyno-table 1.4.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.
Files changed (52) hide show
  1. package/README.md +324 -55
  2. package/dist/builders/condition-check-builder.d.cts +1 -1
  3. package/dist/builders/condition-check-builder.d.ts +1 -1
  4. package/dist/builders/delete-builder.d.cts +1 -1
  5. package/dist/builders/delete-builder.d.ts +1 -1
  6. package/dist/builders/put-builder.d.cts +1 -1
  7. package/dist/builders/put-builder.d.ts +1 -1
  8. package/dist/builders/query-builder.d.cts +2 -2
  9. package/dist/builders/query-builder.d.ts +2 -2
  10. package/dist/builders/transaction-builder.d.cts +1 -1
  11. package/dist/builders/transaction-builder.d.ts +1 -1
  12. package/dist/builders/update-builder.d.cts +1 -1
  13. package/dist/builders/update-builder.d.ts +1 -1
  14. package/dist/{conditions-x6kGWMR7.d.cts → conditions-3ae5znV_.d.cts} +1 -1
  15. package/dist/{conditions-BIpBkh4m.d.ts → conditions-BtynAviC.d.ts} +1 -1
  16. package/dist/conditions.d.cts +1 -1
  17. package/dist/conditions.d.ts +1 -1
  18. package/dist/entity.cjs +93 -49
  19. package/dist/entity.cjs.map +1 -1
  20. package/dist/entity.d.cts +14 -10
  21. package/dist/entity.d.ts +14 -10
  22. package/dist/entity.js +93 -49
  23. package/dist/entity.js.map +1 -1
  24. package/dist/index.cjs +3701 -0
  25. package/dist/index.cjs.map +1 -0
  26. package/dist/index.d.cts +15 -0
  27. package/dist/index.d.ts +15 -0
  28. package/dist/index.js +3674 -0
  29. package/dist/index.js.map +1 -0
  30. package/dist/{query-builder-H9Dn0qaS.d.ts → query-builder-BhrR31oO.d.ts} +2 -2
  31. package/dist/{query-builder-Dg9Loeco.d.cts → query-builder-CbHvimBk.d.cts} +2 -2
  32. package/dist/{table-CmIQe-jD.d.cts → table-CY9byPEg.d.cts} +2 -2
  33. package/dist/{table-TI4ULfra.d.ts → table-Des8C2od.d.ts} +2 -2
  34. package/dist/table.d.cts +3 -3
  35. package/dist/table.d.ts +3 -3
  36. package/dist/utils.cjs +34 -0
  37. package/dist/utils.cjs.map +1 -0
  38. package/dist/{utils/sort-key-template.d.cts → utils.d.cts} +32 -1
  39. package/dist/{utils/sort-key-template.d.ts → utils.d.ts} +32 -1
  40. package/dist/utils.js +31 -0
  41. package/dist/utils.js.map +1 -0
  42. package/package.json +114 -49
  43. package/dist/utils/partition-key-template.cjs +0 -19
  44. package/dist/utils/partition-key-template.cjs.map +0 -1
  45. package/dist/utils/partition-key-template.d.cts +0 -32
  46. package/dist/utils/partition-key-template.d.ts +0 -32
  47. package/dist/utils/partition-key-template.js +0 -17
  48. package/dist/utils/partition-key-template.js.map +0 -1
  49. package/dist/utils/sort-key-template.cjs +0 -19
  50. package/dist/utils/sort-key-template.cjs.map +0 -1
  51. package/dist/utils/sort-key-template.js +0 -17
  52. package/dist/utils/sort-key-template.js.map +0 -1
package/dist/entity.d.cts CHANGED
@@ -1,11 +1,11 @@
1
1
  import { DeleteBuilder } from './builders/delete-builder.cjs';
2
- import { S as ScanBuilder, G as GetBuilder, T as Table } from './table-CmIQe-jD.cjs';
2
+ import { S as ScanBuilder, G as GetBuilder, T as Table } from './table-CY9byPEg.cjs';
3
3
  import { PutBuilder } from './builders/put-builder.cjs';
4
4
  import { UpdateBuilder } from './builders/update-builder.cjs';
5
5
  import { StandardSchemaV1 } from './standard-schema.cjs';
6
6
  import { DynamoItem, TableConfig, Index } from './types.cjs';
7
- import { P as PrimaryKeyWithoutExpression, a as PrimaryKey } from './conditions-x6kGWMR7.cjs';
8
- import { Q as QueryBuilder } from './query-builder-Dg9Loeco.cjs';
7
+ import { r as PrimaryKeyWithoutExpression, P as PrimaryKey } from './conditions-3ae5znV_.cjs';
8
+ import { Q as QueryBuilder } from './query-builder-CbHvimBk.cjs';
9
9
  import './builders/transaction-builder.cjs';
10
10
  import '@aws-sdk/lib-dynamodb';
11
11
  import './builder-types-DlaUSc-b.cjs';
@@ -72,9 +72,9 @@ interface Settings {
72
72
  };
73
73
  };
74
74
  }
75
- interface EntityConfig<T extends DynamoItem, I extends DynamoItem = T, Q extends QueryRecord<T> = QueryRecord<T>> {
75
+ interface EntityConfig<T extends DynamoItem, TInput extends DynamoItem = T, I extends DynamoItem = T, Q extends QueryRecord<T> = QueryRecord<T>> {
76
76
  name: string;
77
- schema: StandardSchemaV1<T>;
77
+ schema: StandardSchemaV1<TInput, T>;
78
78
  primaryKey: IndexDefinition<I>;
79
79
  indexes?: Record<string, Index>;
80
80
  queries: Q;
@@ -82,9 +82,13 @@ interface EntityConfig<T extends DynamoItem, I extends DynamoItem = T, Q extends
82
82
  }
83
83
  interface EntityRepository<
84
84
  /**
85
- * The Entity Type
85
+ * The Entity Type (output type)
86
86
  */
87
87
  T extends DynamoItem,
88
+ /**
89
+ * The Input Type (for create operations)
90
+ */
91
+ TInput extends DynamoItem = T,
88
92
  /**
89
93
  * The Primary Index (Partition index) Type
90
94
  */
@@ -93,8 +97,8 @@ I extends DynamoItem = T,
93
97
  * The Queries object
94
98
  */
95
99
  Q extends QueryRecord<T> = QueryRecord<T>> {
96
- create: (data: T) => PutBuilder<T>;
97
- upsert: (data: T & I) => PutBuilder<T>;
100
+ create: (data: TInput) => PutBuilder<T>;
101
+ upsert: (data: TInput & I) => PutBuilder<T>;
98
102
  get: (key: I) => GetBuilder<T>;
99
103
  update: (key: I, data: Partial<T>) => UpdateBuilder<T>;
100
104
  delete: (key: I) => DeleteBuilder;
@@ -118,9 +122,9 @@ Q extends QueryRecord<T> = QueryRecord<T>> {
118
122
  * });
119
123
  * ```
120
124
  */
121
- declare function defineEntity<T extends DynamoItem, I extends DynamoItem = T, Q extends QueryRecord<T> = QueryRecord<T>>(config: EntityConfig<T, I, Q>): {
125
+ declare function defineEntity<T extends DynamoItem, TInput extends DynamoItem = T, I extends DynamoItem = T, Q extends QueryRecord<T> = QueryRecord<T>>(config: EntityConfig<T, TInput, I, Q>): {
122
126
  name: string;
123
- createRepository: (table: Table) => EntityRepository<T, I, Q>;
127
+ createRepository: (table: Table) => EntityRepository<T, TInput, I, Q>;
124
128
  };
125
129
  declare function createQueries<T extends DynamoItem>(): {
126
130
  input: <I>(schema: StandardSchemaV1<I>) => {
package/dist/entity.d.ts CHANGED
@@ -1,11 +1,11 @@
1
1
  import { DeleteBuilder } from './builders/delete-builder.js';
2
- import { S as ScanBuilder, G as GetBuilder, T as Table } from './table-TI4ULfra.js';
2
+ import { S as ScanBuilder, G as GetBuilder, T as Table } from './table-Des8C2od.js';
3
3
  import { PutBuilder } from './builders/put-builder.js';
4
4
  import { UpdateBuilder } from './builders/update-builder.js';
5
5
  import { StandardSchemaV1 } from './standard-schema.js';
6
6
  import { DynamoItem, TableConfig, Index } from './types.js';
7
- import { P as PrimaryKeyWithoutExpression, a as PrimaryKey } from './conditions-BIpBkh4m.js';
8
- import { Q as QueryBuilder } from './query-builder-H9Dn0qaS.js';
7
+ import { r as PrimaryKeyWithoutExpression, P as PrimaryKey } from './conditions-BtynAviC.js';
8
+ import { Q as QueryBuilder } from './query-builder-BhrR31oO.js';
9
9
  import './builders/transaction-builder.js';
10
10
  import '@aws-sdk/lib-dynamodb';
11
11
  import './builder-types-B_tCpn9F.js';
@@ -72,9 +72,9 @@ interface Settings {
72
72
  };
73
73
  };
74
74
  }
75
- interface EntityConfig<T extends DynamoItem, I extends DynamoItem = T, Q extends QueryRecord<T> = QueryRecord<T>> {
75
+ interface EntityConfig<T extends DynamoItem, TInput extends DynamoItem = T, I extends DynamoItem = T, Q extends QueryRecord<T> = QueryRecord<T>> {
76
76
  name: string;
77
- schema: StandardSchemaV1<T>;
77
+ schema: StandardSchemaV1<TInput, T>;
78
78
  primaryKey: IndexDefinition<I>;
79
79
  indexes?: Record<string, Index>;
80
80
  queries: Q;
@@ -82,9 +82,13 @@ interface EntityConfig<T extends DynamoItem, I extends DynamoItem = T, Q extends
82
82
  }
83
83
  interface EntityRepository<
84
84
  /**
85
- * The Entity Type
85
+ * The Entity Type (output type)
86
86
  */
87
87
  T extends DynamoItem,
88
+ /**
89
+ * The Input Type (for create operations)
90
+ */
91
+ TInput extends DynamoItem = T,
88
92
  /**
89
93
  * The Primary Index (Partition index) Type
90
94
  */
@@ -93,8 +97,8 @@ I extends DynamoItem = T,
93
97
  * The Queries object
94
98
  */
95
99
  Q extends QueryRecord<T> = QueryRecord<T>> {
96
- create: (data: T) => PutBuilder<T>;
97
- upsert: (data: T & I) => PutBuilder<T>;
100
+ create: (data: TInput) => PutBuilder<T>;
101
+ upsert: (data: TInput & I) => PutBuilder<T>;
98
102
  get: (key: I) => GetBuilder<T>;
99
103
  update: (key: I, data: Partial<T>) => UpdateBuilder<T>;
100
104
  delete: (key: I) => DeleteBuilder;
@@ -118,9 +122,9 @@ Q extends QueryRecord<T> = QueryRecord<T>> {
118
122
  * });
119
123
  * ```
120
124
  */
121
- declare function defineEntity<T extends DynamoItem, I extends DynamoItem = T, Q extends QueryRecord<T> = QueryRecord<T>>(config: EntityConfig<T, I, Q>): {
125
+ declare function defineEntity<T extends DynamoItem, TInput extends DynamoItem = T, I extends DynamoItem = T, Q extends QueryRecord<T> = QueryRecord<T>>(config: EntityConfig<T, TInput, I, Q>): {
122
126
  name: string;
123
- createRepository: (table: Table) => EntityRepository<T, I, Q>;
127
+ createRepository: (table: Table) => EntityRepository<T, TInput, I, Q>;
124
128
  };
125
129
  declare function createQueries<T extends DynamoItem>(): {
126
130
  input: <I>(schema: StandardSchemaV1<I>) => {
package/dist/entity.js CHANGED
@@ -31,39 +31,33 @@ function defineEntity(config) {
31
31
  createRepository: (table) => {
32
32
  const repository = {
33
33
  create: (data) => {
34
- const primaryKey = config.primaryKey.generateKey(data);
35
- const indexes = Object.entries(config.indexes ?? {}).reduce(
36
- (acc, [indexName, index]) => {
37
- const key = index.generateKey(data);
38
- const gsiConfig = table.gsis[indexName];
39
- if (!gsiConfig) {
40
- throw new Error(`GSI configuration not found for index: ${indexName}`);
41
- }
42
- if (key.pk) {
43
- acc[gsiConfig.partitionKey] = key.pk;
44
- }
45
- if (key.sk && gsiConfig.sortKey) {
46
- acc[gsiConfig.sortKey] = key.sk;
47
- }
48
- return acc;
49
- },
50
- {}
51
- );
52
- const builder = table.create({
53
- ...data,
54
- [entityTypeAttributeName]: config.name,
55
- [table.partitionKey]: primaryKey.pk,
56
- ...table.sortKey ? { [table.sortKey]: primaryKey.sk } : {},
57
- ...indexes,
58
- ...generateTimestamps(["createdAt", "updatedAt"])
59
- });
34
+ const builder = table.create({});
60
35
  const prepareValidatedItemAsync = async () => {
61
- const validationResult = await config.schema["~standard"].validate(data);
62
- if ("issues" in validationResult && validationResult.issues) {
63
- throw new Error(`Validation failed: ${validationResult.issues.map((i) => i.message).join(", ")}`);
36
+ const validationResult = config.schema["~standard"].validate(data);
37
+ const validatedData = validationResult instanceof Promise ? await validationResult : validationResult;
38
+ if ("issues" in validatedData && validatedData.issues) {
39
+ throw new Error(`Validation failed: ${validatedData.issues.map((i) => i.message).join(", ")}`);
64
40
  }
41
+ const primaryKey = config.primaryKey.generateKey(validatedData.value);
42
+ const indexes = Object.entries(config.indexes ?? {}).reduce(
43
+ (acc, [indexName, index]) => {
44
+ const key = index.generateKey(validatedData.value);
45
+ const gsiConfig = table.gsis[indexName];
46
+ if (!gsiConfig) {
47
+ throw new Error(`GSI configuration not found for index: ${indexName}`);
48
+ }
49
+ if (key.pk) {
50
+ acc[gsiConfig.partitionKey] = key.pk;
51
+ }
52
+ if (key.sk && gsiConfig.sortKey) {
53
+ acc[gsiConfig.sortKey] = key.sk;
54
+ }
55
+ return acc;
56
+ },
57
+ {}
58
+ );
65
59
  const validatedItem = {
66
- ...validationResult.value,
60
+ ...validatedData.value,
67
61
  [entityTypeAttributeName]: config.name,
68
62
  [table.partitionKey]: primaryKey.pk,
69
63
  ...table.sortKey ? { [table.sortKey]: primaryKey.sk } : {},
@@ -76,11 +70,31 @@ function defineEntity(config) {
76
70
  const prepareValidatedItemSync = () => {
77
71
  const validationResult = config.schema["~standard"].validate(data);
78
72
  if (validationResult instanceof Promise) {
79
- throw new Error("Async validation is not supported in withTransaction. Use execute() instead.");
73
+ throw new Error(
74
+ "Async validation is not supported in create method. The schema must support synchronous validation for transaction compatibility."
75
+ );
80
76
  }
81
77
  if ("issues" in validationResult && validationResult.issues) {
82
78
  throw new Error(`Validation failed: ${validationResult.issues.map((i) => i.message).join(", ")}`);
83
79
  }
80
+ const primaryKey = config.primaryKey.generateKey(validationResult.value);
81
+ const indexes = Object.entries(config.indexes ?? {}).reduce(
82
+ (acc, [indexName, index]) => {
83
+ const key = index.generateKey(validationResult.value);
84
+ const gsiConfig = table.gsis[indexName];
85
+ if (!gsiConfig) {
86
+ throw new Error(`GSI configuration not found for index: ${indexName}`);
87
+ }
88
+ if (key.pk) {
89
+ acc[gsiConfig.partitionKey] = key.pk;
90
+ }
91
+ if (key.sk && gsiConfig.sortKey) {
92
+ acc[gsiConfig.sortKey] = key.sk;
93
+ }
94
+ return acc;
95
+ },
96
+ {}
97
+ );
84
98
  const validatedItem = {
85
99
  ...validationResult.value,
86
100
  [entityTypeAttributeName]: config.name,
@@ -119,25 +133,37 @@ function defineEntity(config) {
119
133
  return builder;
120
134
  },
121
135
  upsert: (data) => {
122
- const primaryKey = config.primaryKey.generateKey(data);
123
- const builder = table.put({
124
- [table.partitionKey]: primaryKey.pk,
125
- ...table.sortKey ? { [table.sortKey]: primaryKey.sk } : {},
126
- ...data,
127
- [entityTypeAttributeName]: config.name,
128
- ...generateTimestamps(["createdAt", "updatedAt"])
129
- });
136
+ const builder = table.put({});
130
137
  const prepareValidatedItemAsync = async () => {
131
- const validationResult = await config.schema["~standard"].validate(data);
132
- if ("issues" in validationResult && validationResult.issues) {
133
- throw new Error(`Validation failed: ${validationResult.issues.map((i) => i.message).join(", ")}`);
138
+ const validationResult = config.schema["~standard"].validate(data);
139
+ const validatedData = validationResult instanceof Promise ? await validationResult : validationResult;
140
+ if ("issues" in validatedData && validatedData.issues) {
141
+ throw new Error(`Validation failed: ${validatedData.issues.map((i) => i.message).join(", ")}`);
134
142
  }
135
- const primaryKey2 = config.primaryKey.generateKey(validationResult.value);
143
+ const primaryKey = config.primaryKey.generateKey(validatedData.value);
144
+ const indexes = Object.entries(config.indexes ?? {}).reduce(
145
+ (acc, [indexName, index]) => {
146
+ const key = index.generateKey(validatedData.value);
147
+ const gsiConfig = table.gsis[indexName];
148
+ if (!gsiConfig) {
149
+ throw new Error(`GSI configuration not found for index: ${indexName}`);
150
+ }
151
+ if (key.pk) {
152
+ acc[gsiConfig.partitionKey] = key.pk;
153
+ }
154
+ if (key.sk && gsiConfig.sortKey) {
155
+ acc[gsiConfig.sortKey] = key.sk;
156
+ }
157
+ return acc;
158
+ },
159
+ {}
160
+ );
136
161
  const validatedItem = {
137
- [table.partitionKey]: primaryKey2.pk,
138
- ...table.sortKey ? { [table.sortKey]: primaryKey2.sk } : {},
139
- ...validationResult.value,
162
+ [table.partitionKey]: primaryKey.pk,
163
+ ...table.sortKey ? { [table.sortKey]: primaryKey.sk } : {},
164
+ ...validatedData.value,
140
165
  [entityTypeAttributeName]: config.name,
166
+ ...indexes,
141
167
  ...generateTimestamps(["createdAt", "updatedAt"])
142
168
  };
143
169
  Object.assign(builder, { item: validatedItem });
@@ -151,12 +177,30 @@ function defineEntity(config) {
151
177
  if ("issues" in validationResult && validationResult.issues) {
152
178
  throw new Error(`Validation failed: ${validationResult.issues.map((i) => i.message).join(", ")}`);
153
179
  }
154
- const primaryKey2 = config.primaryKey.generateKey(validationResult.value);
180
+ const primaryKey = config.primaryKey.generateKey(validationResult.value);
181
+ const indexes = Object.entries(config.indexes ?? {}).reduce(
182
+ (acc, [indexName, index]) => {
183
+ const key = index.generateKey(validationResult.value);
184
+ const gsiConfig = table.gsis[indexName];
185
+ if (!gsiConfig) {
186
+ throw new Error(`GSI configuration not found for index: ${indexName}`);
187
+ }
188
+ if (key.pk) {
189
+ acc[gsiConfig.partitionKey] = key.pk;
190
+ }
191
+ if (key.sk && gsiConfig.sortKey) {
192
+ acc[gsiConfig.sortKey] = key.sk;
193
+ }
194
+ return acc;
195
+ },
196
+ {}
197
+ );
155
198
  const validatedItem = {
156
- [table.partitionKey]: primaryKey2.pk,
157
- ...table.sortKey ? { [table.sortKey]: primaryKey2.sk } : {},
199
+ [table.partitionKey]: primaryKey.pk,
200
+ ...table.sortKey ? { [table.sortKey]: primaryKey.sk } : {},
158
201
  ...validationResult.value,
159
202
  [entityTypeAttributeName]: config.name,
203
+ ...indexes,
160
204
  ...generateTimestamps(["createdAt", "updatedAt"])
161
205
  };
162
206
  Object.assign(builder, { item: validatedItem });