dyno-table 2.2.1 → 2.3.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/README.md +187 -1865
- package/dist/builders.cjs +55 -0
- package/dist/builders.d.cts +4 -0
- package/dist/builders.d.ts +4 -0
- package/dist/builders.js +2 -0
- package/dist/chunk-2EWNZOUK.js +618 -0
- package/dist/chunk-2WIBY7PZ.js +46 -0
- package/dist/chunk-7UJJ7JXM.cjs +63 -0
- package/dist/chunk-DTFJJASK.js +3200 -0
- package/dist/chunk-EODPMYPE.js +558 -0
- package/dist/chunk-KA3VPIPS.cjs +560 -0
- package/dist/chunk-NTA6GDPP.cjs +622 -0
- package/dist/chunk-PB7BBCZO.cjs +32 -0
- package/dist/chunk-QVRMYGC4.js +29 -0
- package/dist/chunk-XYL43FDX.cjs +3217 -0
- package/dist/conditions.cjs +67 -62
- package/dist/conditions.js +1 -48
- package/dist/entity.cjs +14 -625
- package/dist/entity.d.cts +2 -10
- package/dist/entity.d.ts +2 -10
- package/dist/entity.js +2 -626
- package/dist/index-2cbm07Bi.d.ts +2797 -0
- package/dist/index-DlN8G9hd.d.cts +2797 -0
- package/dist/index.cjs +111 -4460
- package/dist/index.d.cts +2 -10
- package/dist/index.d.ts +2 -10
- package/dist/index.js +5 -4442
- package/dist/standard-schema.cjs +0 -2
- package/dist/standard-schema.js +0 -2
- package/dist/table.cjs +7 -3796
- package/dist/table.d.cts +163 -12
- package/dist/table.d.ts +163 -12
- package/dist/table.js +3 -3799
- package/dist/types.cjs +0 -2
- package/dist/types.js +0 -2
- package/dist/utils.cjs +10 -30
- package/dist/utils.js +1 -31
- package/package.json +6 -66
- package/dist/batch-builder-BiQDIZ7p.d.cts +0 -398
- package/dist/batch-builder-CNsLS6sR.d.ts +0 -398
- package/dist/builder-types-BTVhQSHI.d.cts +0 -169
- package/dist/builder-types-CzuLR4Th.d.ts +0 -169
- package/dist/builders/condition-check-builder.cjs +0 -422
- package/dist/builders/condition-check-builder.cjs.map +0 -1
- package/dist/builders/condition-check-builder.d.cts +0 -153
- package/dist/builders/condition-check-builder.d.ts +0 -153
- package/dist/builders/condition-check-builder.js +0 -420
- package/dist/builders/condition-check-builder.js.map +0 -1
- package/dist/builders/delete-builder.cjs +0 -484
- package/dist/builders/delete-builder.cjs.map +0 -1
- package/dist/builders/delete-builder.d.cts +0 -211
- package/dist/builders/delete-builder.d.ts +0 -211
- package/dist/builders/delete-builder.js +0 -482
- package/dist/builders/delete-builder.js.map +0 -1
- package/dist/builders/paginator.cjs +0 -193
- package/dist/builders/paginator.cjs.map +0 -1
- package/dist/builders/paginator.d.cts +0 -155
- package/dist/builders/paginator.d.ts +0 -155
- package/dist/builders/paginator.js +0 -191
- package/dist/builders/paginator.js.map +0 -1
- package/dist/builders/put-builder.cjs +0 -554
- package/dist/builders/put-builder.cjs.map +0 -1
- package/dist/builders/put-builder.d.cts +0 -319
- package/dist/builders/put-builder.d.ts +0 -319
- package/dist/builders/put-builder.js +0 -552
- package/dist/builders/put-builder.js.map +0 -1
- package/dist/builders/query-builder.cjs +0 -757
- package/dist/builders/query-builder.cjs.map +0 -1
- package/dist/builders/query-builder.d.cts +0 -6
- package/dist/builders/query-builder.d.ts +0 -6
- package/dist/builders/query-builder.js +0 -755
- package/dist/builders/query-builder.js.map +0 -1
- package/dist/builders/transaction-builder.cjs +0 -906
- package/dist/builders/transaction-builder.cjs.map +0 -1
- package/dist/builders/transaction-builder.d.cts +0 -464
- package/dist/builders/transaction-builder.d.ts +0 -464
- package/dist/builders/transaction-builder.js +0 -904
- package/dist/builders/transaction-builder.js.map +0 -1
- package/dist/builders/update-builder.cjs +0 -668
- package/dist/builders/update-builder.cjs.map +0 -1
- package/dist/builders/update-builder.d.cts +0 -374
- package/dist/builders/update-builder.d.ts +0 -374
- package/dist/builders/update-builder.js +0 -666
- package/dist/builders/update-builder.js.map +0 -1
- package/dist/conditions.cjs.map +0 -1
- package/dist/conditions.js.map +0 -1
- package/dist/entity.cjs.map +0 -1
- package/dist/entity.js.map +0 -1
- package/dist/index.cjs.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/query-builder-D3URwK9k.d.cts +0 -477
- package/dist/query-builder-cfEkU0_w.d.ts +0 -477
- package/dist/standard-schema.cjs.map +0 -1
- package/dist/standard-schema.js.map +0 -1
- package/dist/table-ClST8nkR.d.cts +0 -276
- package/dist/table-vE3cGoDy.d.ts +0 -276
- package/dist/table.cjs.map +0 -1
- package/dist/table.js.map +0 -1
- package/dist/types.cjs.map +0 -1
- package/dist/types.js.map +0 -1
- package/dist/utils.cjs.map +0 -1
- package/dist/utils.js.map +0 -1
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var chunkXYL43FDX_cjs = require('./chunk-XYL43FDX.cjs');
|
|
4
|
+
require('./chunk-7UJJ7JXM.cjs');
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
Object.defineProperty(exports, "BatchBuilder", {
|
|
9
|
+
enumerable: true,
|
|
10
|
+
get: function () { return chunkXYL43FDX_cjs.BatchBuilder; }
|
|
11
|
+
});
|
|
12
|
+
Object.defineProperty(exports, "ConditionCheckBuilder", {
|
|
13
|
+
enumerable: true,
|
|
14
|
+
get: function () { return chunkXYL43FDX_cjs.ConditionCheckBuilder; }
|
|
15
|
+
});
|
|
16
|
+
Object.defineProperty(exports, "DeleteBuilder", {
|
|
17
|
+
enumerable: true,
|
|
18
|
+
get: function () { return chunkXYL43FDX_cjs.DeleteBuilder; }
|
|
19
|
+
});
|
|
20
|
+
Object.defineProperty(exports, "FilterBuilder", {
|
|
21
|
+
enumerable: true,
|
|
22
|
+
get: function () { return chunkXYL43FDX_cjs.FilterBuilder; }
|
|
23
|
+
});
|
|
24
|
+
Object.defineProperty(exports, "GetBuilder", {
|
|
25
|
+
enumerable: true,
|
|
26
|
+
get: function () { return chunkXYL43FDX_cjs.GetBuilder; }
|
|
27
|
+
});
|
|
28
|
+
Object.defineProperty(exports, "Paginator", {
|
|
29
|
+
enumerable: true,
|
|
30
|
+
get: function () { return chunkXYL43FDX_cjs.Paginator; }
|
|
31
|
+
});
|
|
32
|
+
Object.defineProperty(exports, "PutBuilder", {
|
|
33
|
+
enumerable: true,
|
|
34
|
+
get: function () { return chunkXYL43FDX_cjs.PutBuilder; }
|
|
35
|
+
});
|
|
36
|
+
Object.defineProperty(exports, "QueryBuilder", {
|
|
37
|
+
enumerable: true,
|
|
38
|
+
get: function () { return chunkXYL43FDX_cjs.QueryBuilder; }
|
|
39
|
+
});
|
|
40
|
+
Object.defineProperty(exports, "ResultIterator", {
|
|
41
|
+
enumerable: true,
|
|
42
|
+
get: function () { return chunkXYL43FDX_cjs.ResultIterator; }
|
|
43
|
+
});
|
|
44
|
+
Object.defineProperty(exports, "ScanBuilder", {
|
|
45
|
+
enumerable: true,
|
|
46
|
+
get: function () { return chunkXYL43FDX_cjs.ScanBuilder; }
|
|
47
|
+
});
|
|
48
|
+
Object.defineProperty(exports, "TransactionBuilder", {
|
|
49
|
+
enumerable: true,
|
|
50
|
+
get: function () { return chunkXYL43FDX_cjs.TransactionBuilder; }
|
|
51
|
+
});
|
|
52
|
+
Object.defineProperty(exports, "UpdateBuilder", {
|
|
53
|
+
enumerable: true,
|
|
54
|
+
get: function () { return chunkXYL43FDX_cjs.UpdateBuilder; }
|
|
55
|
+
});
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { n as BaseBuilderInterface, B as BatchBuilder, C as ConditionCheckBuilder, m as ConditionCheckCommandParams, D as DeleteBuilder, k as DeleteCommandParams, F as FilterBuilder, q as FilterBuilderInterface, G as GetBuilder, r as PaginationResult, j as Paginator, P as PutBuilder, l as PutCommandParams, Q as QueryBuilder, o as QueryBuilderInterface, R as ResultIterator, S as ScanBuilder, p as ScanBuilderInterface, T as TransactionBuilder, s as TransactionItem, U as UpdateBuilder, i as UpdateCommandParams } from './index-DlN8G9hd.cjs';
|
|
2
|
+
export { s as Path, t as PathType } from './conditions-CcZL0sR2.cjs';
|
|
3
|
+
import './types.cjs';
|
|
4
|
+
import '@aws-sdk/lib-dynamodb';
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { n as BaseBuilderInterface, B as BatchBuilder, C as ConditionCheckBuilder, m as ConditionCheckCommandParams, D as DeleteBuilder, k as DeleteCommandParams, F as FilterBuilder, q as FilterBuilderInterface, G as GetBuilder, r as PaginationResult, j as Paginator, P as PutBuilder, l as PutCommandParams, Q as QueryBuilder, o as QueryBuilderInterface, R as ResultIterator, S as ScanBuilder, p as ScanBuilderInterface, T as TransactionBuilder, s as TransactionItem, U as UpdateBuilder, i as UpdateCommandParams } from './index-2cbm07Bi.js';
|
|
2
|
+
export { s as Path, t as PathType } from './conditions-D_w7vVYG.js';
|
|
3
|
+
import './types.js';
|
|
4
|
+
import '@aws-sdk/lib-dynamodb';
|
package/dist/builders.js
ADDED
|
@@ -0,0 +1,618 @@
|
|
|
1
|
+
import { eq } from './chunk-2WIBY7PZ.js';
|
|
2
|
+
|
|
3
|
+
// src/builders/entity-aware-builders.ts
|
|
4
|
+
function createEntityAwareBuilder(builder, entityName) {
|
|
5
|
+
return new Proxy(builder, {
|
|
6
|
+
get(target, prop, receiver) {
|
|
7
|
+
if (prop === "entityName") {
|
|
8
|
+
return entityName;
|
|
9
|
+
}
|
|
10
|
+
if (prop === "withBatch" && typeof target[prop] === "function") {
|
|
11
|
+
return (batch, entityType) => {
|
|
12
|
+
const typeToUse = entityType ?? entityName;
|
|
13
|
+
const fn = target[prop];
|
|
14
|
+
return fn.call(target, batch, typeToUse);
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
return Reflect.get(target, prop, receiver);
|
|
18
|
+
}
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
function createEntityAwarePutBuilder(builder, entityName) {
|
|
22
|
+
return createEntityAwareBuilder(builder, entityName);
|
|
23
|
+
}
|
|
24
|
+
function createEntityAwareGetBuilder(builder, entityName) {
|
|
25
|
+
return createEntityAwareBuilder(builder, entityName);
|
|
26
|
+
}
|
|
27
|
+
function createEntityAwareDeleteBuilder(builder, entityName) {
|
|
28
|
+
return createEntityAwareBuilder(builder, entityName);
|
|
29
|
+
}
|
|
30
|
+
var EntityAwareUpdateBuilder = class {
|
|
31
|
+
forceRebuildIndexes = [];
|
|
32
|
+
entityName;
|
|
33
|
+
builder;
|
|
34
|
+
entityConfig;
|
|
35
|
+
updateDataApplied = false;
|
|
36
|
+
constructor(builder, entityName) {
|
|
37
|
+
this.builder = builder;
|
|
38
|
+
this.entityName = entityName;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Configure entity-specific logic for automatic timestamp generation and index updates
|
|
42
|
+
*/
|
|
43
|
+
configureEntityLogic(config) {
|
|
44
|
+
this.entityConfig = config;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Forces a rebuild of one or more readonly indexes during the update operation.
|
|
48
|
+
*
|
|
49
|
+
* By default, readonly indexes are not updated during entity updates to prevent
|
|
50
|
+
* errors when required index attributes are missing. This method allows you to
|
|
51
|
+
* override that behavior and force specific indexes to be rebuilt.
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
* ```typescript
|
|
55
|
+
* // Force rebuild a single readonly index
|
|
56
|
+
* const result = await repo.update({ id: 'TREX-001' }, { status: 'ACTIVE' })
|
|
57
|
+
* .forceIndexRebuild('gsi1')
|
|
58
|
+
* .execute();
|
|
59
|
+
*
|
|
60
|
+
* // Force rebuild multiple readonly indexes
|
|
61
|
+
* const result = await repo.update({ id: 'TREX-001' }, { status: 'ACTIVE' })
|
|
62
|
+
* .forceIndexRebuild(['gsi1', 'gsi2'])
|
|
63
|
+
* .execute();
|
|
64
|
+
*
|
|
65
|
+
* // Chain with other update operations
|
|
66
|
+
* const result = await repo.update({ id: 'TREX-001' }, { status: 'ACTIVE' })
|
|
67
|
+
* .set('lastUpdated', new Date().toISOString())
|
|
68
|
+
* .forceIndexRebuild('gsi1')
|
|
69
|
+
* .condition(op => op.eq('status', 'INACTIVE'))
|
|
70
|
+
* .execute();
|
|
71
|
+
* ```
|
|
72
|
+
*
|
|
73
|
+
* @param indexes - A single index name or array of index names to force rebuild
|
|
74
|
+
* @returns The builder instance for method chaining
|
|
75
|
+
*/
|
|
76
|
+
forceIndexRebuild(indexes) {
|
|
77
|
+
if (Array.isArray(indexes)) {
|
|
78
|
+
this.forceRebuildIndexes = [...this.forceRebuildIndexes, ...indexes];
|
|
79
|
+
} else {
|
|
80
|
+
this.forceRebuildIndexes.push(indexes);
|
|
81
|
+
}
|
|
82
|
+
return this;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Gets the list of indexes that should be force rebuilt.
|
|
86
|
+
* This is used internally by entity update logic.
|
|
87
|
+
*
|
|
88
|
+
* @returns Array of index names to force rebuild
|
|
89
|
+
*/
|
|
90
|
+
getForceRebuildIndexes() {
|
|
91
|
+
return [...this.forceRebuildIndexes];
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Apply entity-specific update data (timestamps and index updates)
|
|
95
|
+
* This is called automatically when needed
|
|
96
|
+
*/
|
|
97
|
+
applyEntityUpdates() {
|
|
98
|
+
if (!this.entityConfig || this.updateDataApplied) return;
|
|
99
|
+
const timestamps = this.entityConfig.generateTimestamps();
|
|
100
|
+
const updatedItem = { ...this.entityConfig.key, ...this.entityConfig.data, ...timestamps };
|
|
101
|
+
const indexUpdates = this.entityConfig.buildIndexUpdates(
|
|
102
|
+
this.entityConfig.key,
|
|
103
|
+
updatedItem,
|
|
104
|
+
this.entityConfig.table,
|
|
105
|
+
this.entityConfig.indexes,
|
|
106
|
+
this.forceRebuildIndexes
|
|
107
|
+
);
|
|
108
|
+
this.builder.set({ ...this.entityConfig.data, ...timestamps, ...indexUpdates });
|
|
109
|
+
this.updateDataApplied = true;
|
|
110
|
+
}
|
|
111
|
+
set(valuesOrPath, value) {
|
|
112
|
+
if (typeof valuesOrPath === "object") {
|
|
113
|
+
this.builder.set(valuesOrPath);
|
|
114
|
+
} else {
|
|
115
|
+
if (value === void 0) {
|
|
116
|
+
throw new Error("Value is required when setting a single path");
|
|
117
|
+
}
|
|
118
|
+
this.builder.set(valuesOrPath, value);
|
|
119
|
+
}
|
|
120
|
+
return this;
|
|
121
|
+
}
|
|
122
|
+
remove(path) {
|
|
123
|
+
this.builder.remove(path);
|
|
124
|
+
return this;
|
|
125
|
+
}
|
|
126
|
+
add(path, value) {
|
|
127
|
+
this.builder.add(path, value);
|
|
128
|
+
return this;
|
|
129
|
+
}
|
|
130
|
+
deleteElementsFromSet(path, value) {
|
|
131
|
+
this.builder.deleteElementsFromSet(path, value);
|
|
132
|
+
return this;
|
|
133
|
+
}
|
|
134
|
+
condition(condition) {
|
|
135
|
+
this.builder.condition(condition);
|
|
136
|
+
return this;
|
|
137
|
+
}
|
|
138
|
+
returnValues(returnValues) {
|
|
139
|
+
this.builder.returnValues(returnValues);
|
|
140
|
+
return this;
|
|
141
|
+
}
|
|
142
|
+
toDynamoCommand() {
|
|
143
|
+
return this.builder.toDynamoCommand();
|
|
144
|
+
}
|
|
145
|
+
withTransaction(transaction) {
|
|
146
|
+
this.applyEntityUpdates();
|
|
147
|
+
this.builder.withTransaction(transaction);
|
|
148
|
+
}
|
|
149
|
+
debug() {
|
|
150
|
+
return this.builder.debug();
|
|
151
|
+
}
|
|
152
|
+
async execute() {
|
|
153
|
+
this.updateDataApplied = false;
|
|
154
|
+
this.applyEntityUpdates();
|
|
155
|
+
return this.builder.execute();
|
|
156
|
+
}
|
|
157
|
+
};
|
|
158
|
+
function createEntityAwareUpdateBuilder(builder, entityName) {
|
|
159
|
+
return new EntityAwareUpdateBuilder(builder, entityName);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// src/entity/ddb-indexing.ts
|
|
163
|
+
var IndexBuilder = class {
|
|
164
|
+
/**
|
|
165
|
+
* Creates a new IndexBuilder instance
|
|
166
|
+
*
|
|
167
|
+
* @param table - The DynamoDB table instance
|
|
168
|
+
* @param indexes - The index definitions
|
|
169
|
+
*/
|
|
170
|
+
constructor(table, indexes = {}) {
|
|
171
|
+
this.table = table;
|
|
172
|
+
this.indexes = indexes;
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Build index attributes for item creation
|
|
176
|
+
*
|
|
177
|
+
* @param item - The item to generate indexes for
|
|
178
|
+
* @param options - Options for building indexes
|
|
179
|
+
* @returns Record of GSI attribute names to their values
|
|
180
|
+
*/
|
|
181
|
+
buildForCreate(item, options = {}) {
|
|
182
|
+
const attributes = {};
|
|
183
|
+
for (const [indexName, indexDef] of Object.entries(this.indexes)) {
|
|
184
|
+
if (options.excludeReadOnly && indexDef.isReadOnly) {
|
|
185
|
+
continue;
|
|
186
|
+
}
|
|
187
|
+
const key = indexDef.generateKey(item);
|
|
188
|
+
const gsiConfig = this.table.gsis[indexName];
|
|
189
|
+
if (!gsiConfig) {
|
|
190
|
+
throw new Error(`GSI configuration not found for index: ${indexName}`);
|
|
191
|
+
}
|
|
192
|
+
if (key.pk) {
|
|
193
|
+
attributes[gsiConfig.partitionKey] = key.pk;
|
|
194
|
+
}
|
|
195
|
+
if (key.sk && gsiConfig.sortKey) {
|
|
196
|
+
attributes[gsiConfig.sortKey] = key.sk;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
return attributes;
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* Build index attributes for item updates
|
|
203
|
+
*
|
|
204
|
+
* @param currentData - The current data before update
|
|
205
|
+
* @param updates - The update data
|
|
206
|
+
* @param options - Options for building indexes
|
|
207
|
+
* @returns Record of GSI attribute names to their updated values
|
|
208
|
+
*/
|
|
209
|
+
buildForUpdate(currentData, updates, options = {}) {
|
|
210
|
+
const attributes = {};
|
|
211
|
+
const updatedItem = { ...currentData, ...updates };
|
|
212
|
+
if (options.forceRebuildIndexes && options.forceRebuildIndexes.length > 0) {
|
|
213
|
+
const invalidIndexes = options.forceRebuildIndexes.filter((indexName) => !this.indexes[indexName]);
|
|
214
|
+
if (invalidIndexes.length > 0) {
|
|
215
|
+
throw new Error(
|
|
216
|
+
`Cannot force rebuild unknown indexes: ${invalidIndexes.join(", ")}. Available indexes: ${Object.keys(this.indexes).join(", ")}`
|
|
217
|
+
);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
for (const [indexName, indexDef] of Object.entries(this.indexes)) {
|
|
221
|
+
const isForced = options.forceRebuildIndexes?.includes(indexName);
|
|
222
|
+
if (indexDef.isReadOnly && !isForced) {
|
|
223
|
+
continue;
|
|
224
|
+
}
|
|
225
|
+
if (!isForced) {
|
|
226
|
+
let shouldUpdateIndex = false;
|
|
227
|
+
try {
|
|
228
|
+
const currentKey = indexDef.generateKey(currentData);
|
|
229
|
+
const updatedKey = indexDef.generateKey(updatedItem);
|
|
230
|
+
if (currentKey.pk !== updatedKey.pk || currentKey.sk !== updatedKey.sk) {
|
|
231
|
+
shouldUpdateIndex = true;
|
|
232
|
+
}
|
|
233
|
+
} catch {
|
|
234
|
+
shouldUpdateIndex = true;
|
|
235
|
+
}
|
|
236
|
+
if (!shouldUpdateIndex) {
|
|
237
|
+
continue;
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
let key;
|
|
241
|
+
try {
|
|
242
|
+
key = indexDef.generateKey(updatedItem);
|
|
243
|
+
} catch (error) {
|
|
244
|
+
if (error instanceof Error) {
|
|
245
|
+
throw new Error(`Missing attributes: ${error.message}`);
|
|
246
|
+
}
|
|
247
|
+
throw error;
|
|
248
|
+
}
|
|
249
|
+
if (this.hasUndefinedValues(key)) {
|
|
250
|
+
throw new Error(
|
|
251
|
+
`Missing attributes: Cannot update entity: insufficient data to regenerate index "${indexName}". All attributes required by the index must be provided in the update operation, or the index must be marked as readOnly.`
|
|
252
|
+
);
|
|
253
|
+
}
|
|
254
|
+
const gsiConfig = this.table.gsis[indexName];
|
|
255
|
+
if (!gsiConfig) {
|
|
256
|
+
throw new Error(`GSI configuration not found for index: ${indexName}`);
|
|
257
|
+
}
|
|
258
|
+
if (key.pk) {
|
|
259
|
+
attributes[gsiConfig.partitionKey] = key.pk;
|
|
260
|
+
}
|
|
261
|
+
if (key.sk && gsiConfig.sortKey) {
|
|
262
|
+
attributes[gsiConfig.sortKey] = key.sk;
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
return attributes;
|
|
266
|
+
}
|
|
267
|
+
/**
|
|
268
|
+
* Check if a key has undefined values
|
|
269
|
+
*
|
|
270
|
+
* @param key - The index key to check
|
|
271
|
+
* @returns True if the key contains undefined values, false otherwise
|
|
272
|
+
*/
|
|
273
|
+
hasUndefinedValues(key) {
|
|
274
|
+
return (key.pk?.includes("undefined") ?? false) || (key.sk?.includes("undefined") ?? false);
|
|
275
|
+
}
|
|
276
|
+
};
|
|
277
|
+
|
|
278
|
+
// src/entity/index-utils.ts
|
|
279
|
+
function buildIndexes(dataForKeyGeneration, table, indexes, excludeReadOnly = false) {
|
|
280
|
+
if (!indexes) {
|
|
281
|
+
return {};
|
|
282
|
+
}
|
|
283
|
+
const indexBuilder = new IndexBuilder(table, indexes);
|
|
284
|
+
return indexBuilder.buildForCreate(dataForKeyGeneration, { excludeReadOnly });
|
|
285
|
+
}
|
|
286
|
+
function buildIndexUpdates(currentData, updates, table, indexes, forceRebuildIndexes) {
|
|
287
|
+
if (!indexes) {
|
|
288
|
+
return {};
|
|
289
|
+
}
|
|
290
|
+
const indexBuilder = new IndexBuilder(table, indexes);
|
|
291
|
+
return indexBuilder.buildForUpdate(currentData, updates, { forceRebuildIndexes });
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
// src/entity/entity.ts
|
|
295
|
+
function defineEntity(config) {
|
|
296
|
+
const entityTypeAttributeName = config.settings?.entityTypeAttributeName ?? "entityType";
|
|
297
|
+
const buildIndexes2 = (dataForKeyGeneration, table, excludeReadOnly = false) => {
|
|
298
|
+
return buildIndexes(dataForKeyGeneration, table, config.indexes, excludeReadOnly);
|
|
299
|
+
};
|
|
300
|
+
const wrapMethodWithPreparation = (originalMethod, prepareFn, context) => {
|
|
301
|
+
const wrappedMethod = (...args) => {
|
|
302
|
+
prepareFn();
|
|
303
|
+
return originalMethod.call(context, ...args);
|
|
304
|
+
};
|
|
305
|
+
Object.setPrototypeOf(wrappedMethod, originalMethod);
|
|
306
|
+
const propertyNames = Object.getOwnPropertyNames(originalMethod);
|
|
307
|
+
for (let i = 0; i < propertyNames.length; i++) {
|
|
308
|
+
const prop = propertyNames[i];
|
|
309
|
+
if (prop !== "length" && prop !== "name" && prop !== "prototype") {
|
|
310
|
+
const descriptor = Object.getOwnPropertyDescriptor(originalMethod, prop);
|
|
311
|
+
if (descriptor && descriptor.writable !== false && !descriptor.get) {
|
|
312
|
+
wrappedMethod[prop] = originalMethod[prop];
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
return wrappedMethod;
|
|
317
|
+
};
|
|
318
|
+
const generateTimestamps = (timestampsToGenerate, data) => {
|
|
319
|
+
if (!config.settings?.timestamps) return {};
|
|
320
|
+
const timestamps = {};
|
|
321
|
+
const now = /* @__PURE__ */ new Date();
|
|
322
|
+
const unixTime = Math.floor(Date.now() / 1e3);
|
|
323
|
+
const { createdAt, updatedAt } = config.settings.timestamps;
|
|
324
|
+
if (createdAt && timestampsToGenerate.includes("createdAt") && !data.createdAt) {
|
|
325
|
+
const name = createdAt.attributeName ?? "createdAt";
|
|
326
|
+
timestamps[name] = createdAt.format === "UNIX" ? unixTime : now.toISOString();
|
|
327
|
+
}
|
|
328
|
+
if (updatedAt && timestampsToGenerate.includes("updatedAt") && !data.updatedAt) {
|
|
329
|
+
const name = updatedAt.attributeName ?? "updatedAt";
|
|
330
|
+
timestamps[name] = updatedAt.format === "UNIX" ? unixTime : now.toISOString();
|
|
331
|
+
}
|
|
332
|
+
return timestamps;
|
|
333
|
+
};
|
|
334
|
+
return {
|
|
335
|
+
name: config.name,
|
|
336
|
+
createRepository: (table) => {
|
|
337
|
+
const repository = {
|
|
338
|
+
create: (data) => {
|
|
339
|
+
const builder = table.create({});
|
|
340
|
+
const prepareValidatedItemAsync = async () => {
|
|
341
|
+
const validatedData = await config.schema["~standard"].validate(data);
|
|
342
|
+
if ("issues" in validatedData && validatedData.issues) {
|
|
343
|
+
throw new Error(`Validation failed: ${validatedData.issues.map((i) => i.message).join(", ")}`);
|
|
344
|
+
}
|
|
345
|
+
const dataForKeyGeneration = {
|
|
346
|
+
...validatedData.value,
|
|
347
|
+
...generateTimestamps(["createdAt", "updatedAt"], validatedData.value)
|
|
348
|
+
};
|
|
349
|
+
const primaryKey = config.primaryKey.generateKey(dataForKeyGeneration);
|
|
350
|
+
const indexes = buildIndexes(dataForKeyGeneration, table, config.indexes, false);
|
|
351
|
+
const validatedItem = {
|
|
352
|
+
...dataForKeyGeneration,
|
|
353
|
+
[entityTypeAttributeName]: config.name,
|
|
354
|
+
[table.partitionKey]: primaryKey.pk,
|
|
355
|
+
...table.sortKey ? { [table.sortKey]: primaryKey.sk } : {},
|
|
356
|
+
...indexes
|
|
357
|
+
};
|
|
358
|
+
Object.assign(builder, { item: validatedItem });
|
|
359
|
+
return validatedItem;
|
|
360
|
+
};
|
|
361
|
+
const prepareValidatedItemSync = () => {
|
|
362
|
+
const validationResult = config.schema["~standard"].validate(data);
|
|
363
|
+
if (validationResult instanceof Promise) {
|
|
364
|
+
throw new Error(
|
|
365
|
+
"Async validation is not supported in withBatch or withTransaction. The schema must support synchronous validation for compatibility."
|
|
366
|
+
);
|
|
367
|
+
}
|
|
368
|
+
if ("issues" in validationResult && validationResult.issues) {
|
|
369
|
+
throw new Error(`Validation failed: ${validationResult.issues.map((i) => i.message).join(", ")}`);
|
|
370
|
+
}
|
|
371
|
+
const dataForKeyGeneration = {
|
|
372
|
+
...validationResult.value,
|
|
373
|
+
...generateTimestamps(["createdAt", "updatedAt"], validationResult.value)
|
|
374
|
+
};
|
|
375
|
+
const primaryKey = config.primaryKey.generateKey(dataForKeyGeneration);
|
|
376
|
+
const indexes = buildIndexes(dataForKeyGeneration, table, config.indexes, false);
|
|
377
|
+
const validatedItem = {
|
|
378
|
+
...dataForKeyGeneration,
|
|
379
|
+
[entityTypeAttributeName]: config.name,
|
|
380
|
+
[table.partitionKey]: primaryKey.pk,
|
|
381
|
+
...table.sortKey ? { [table.sortKey]: primaryKey.sk } : {},
|
|
382
|
+
...indexes
|
|
383
|
+
};
|
|
384
|
+
Object.assign(builder, { item: validatedItem });
|
|
385
|
+
return validatedItem;
|
|
386
|
+
};
|
|
387
|
+
const originalExecute = builder.execute;
|
|
388
|
+
builder.execute = async () => {
|
|
389
|
+
await prepareValidatedItemAsync();
|
|
390
|
+
return await originalExecute.call(builder);
|
|
391
|
+
};
|
|
392
|
+
const originalWithTransaction = builder.withTransaction;
|
|
393
|
+
if (originalWithTransaction) {
|
|
394
|
+
builder.withTransaction = wrapMethodWithPreparation(
|
|
395
|
+
originalWithTransaction,
|
|
396
|
+
prepareValidatedItemSync,
|
|
397
|
+
builder
|
|
398
|
+
);
|
|
399
|
+
}
|
|
400
|
+
const originalWithBatch = builder.withBatch;
|
|
401
|
+
if (originalWithBatch) {
|
|
402
|
+
builder.withBatch = wrapMethodWithPreparation(originalWithBatch, prepareValidatedItemSync, builder);
|
|
403
|
+
}
|
|
404
|
+
return createEntityAwarePutBuilder(builder, config.name);
|
|
405
|
+
},
|
|
406
|
+
upsert: (data) => {
|
|
407
|
+
const builder = table.put({});
|
|
408
|
+
const prepareValidatedItemAsync = async () => {
|
|
409
|
+
const validatedData = await config.schema["~standard"].validate(data);
|
|
410
|
+
if ("issues" in validatedData && validatedData.issues) {
|
|
411
|
+
throw new Error(`Validation failed: ${validatedData.issues.map((i) => i.message).join(", ")}`);
|
|
412
|
+
}
|
|
413
|
+
const dataForKeyGeneration = {
|
|
414
|
+
...validatedData.value,
|
|
415
|
+
...generateTimestamps(["createdAt", "updatedAt"], validatedData.value)
|
|
416
|
+
};
|
|
417
|
+
const primaryKey = config.primaryKey.generateKey(dataForKeyGeneration);
|
|
418
|
+
const indexes = buildIndexes2(dataForKeyGeneration, table, false);
|
|
419
|
+
const validatedItem = {
|
|
420
|
+
[table.partitionKey]: primaryKey.pk,
|
|
421
|
+
...table.sortKey ? { [table.sortKey]: primaryKey.sk } : {},
|
|
422
|
+
...dataForKeyGeneration,
|
|
423
|
+
[entityTypeAttributeName]: config.name,
|
|
424
|
+
...indexes
|
|
425
|
+
};
|
|
426
|
+
Object.assign(builder, { item: validatedItem });
|
|
427
|
+
return validatedItem;
|
|
428
|
+
};
|
|
429
|
+
const prepareValidatedItemSync = () => {
|
|
430
|
+
const validationResult = config.schema["~standard"].validate(data);
|
|
431
|
+
if (validationResult instanceof Promise) {
|
|
432
|
+
throw new Error(
|
|
433
|
+
"Async validation is not supported in withTransaction or withBatch. Use execute() instead."
|
|
434
|
+
);
|
|
435
|
+
}
|
|
436
|
+
if ("issues" in validationResult && validationResult.issues) {
|
|
437
|
+
throw new Error(`Validation failed: ${validationResult.issues.map((i) => i.message).join(", ")}`);
|
|
438
|
+
}
|
|
439
|
+
const dataForKeyGeneration = {
|
|
440
|
+
...validationResult.value,
|
|
441
|
+
...generateTimestamps(["createdAt", "updatedAt"], validationResult.value)
|
|
442
|
+
};
|
|
443
|
+
const primaryKey = config.primaryKey.generateKey(dataForKeyGeneration);
|
|
444
|
+
const indexes = buildIndexes(dataForKeyGeneration, table, config.indexes, false);
|
|
445
|
+
const validatedItem = {
|
|
446
|
+
[table.partitionKey]: primaryKey.pk,
|
|
447
|
+
...table.sortKey ? { [table.sortKey]: primaryKey.sk } : {},
|
|
448
|
+
...dataForKeyGeneration,
|
|
449
|
+
[entityTypeAttributeName]: config.name,
|
|
450
|
+
...indexes
|
|
451
|
+
};
|
|
452
|
+
Object.assign(builder, { item: validatedItem });
|
|
453
|
+
return validatedItem;
|
|
454
|
+
};
|
|
455
|
+
const originalExecute = builder.execute;
|
|
456
|
+
builder.execute = async () => {
|
|
457
|
+
await prepareValidatedItemAsync();
|
|
458
|
+
const result = await originalExecute.call(builder);
|
|
459
|
+
if (!result) {
|
|
460
|
+
throw new Error("Failed to upsert item");
|
|
461
|
+
}
|
|
462
|
+
return result;
|
|
463
|
+
};
|
|
464
|
+
const originalWithTransaction = builder.withTransaction;
|
|
465
|
+
if (originalWithTransaction) {
|
|
466
|
+
builder.withTransaction = wrapMethodWithPreparation(
|
|
467
|
+
originalWithTransaction,
|
|
468
|
+
prepareValidatedItemSync,
|
|
469
|
+
builder
|
|
470
|
+
);
|
|
471
|
+
}
|
|
472
|
+
const originalWithBatch = builder.withBatch;
|
|
473
|
+
if (originalWithBatch) {
|
|
474
|
+
builder.withBatch = wrapMethodWithPreparation(originalWithBatch, prepareValidatedItemSync, builder);
|
|
475
|
+
}
|
|
476
|
+
return createEntityAwarePutBuilder(builder, config.name);
|
|
477
|
+
},
|
|
478
|
+
get: (key) => {
|
|
479
|
+
return createEntityAwareGetBuilder(table.get(config.primaryKey.generateKey(key)), config.name);
|
|
480
|
+
},
|
|
481
|
+
update: (key, data) => {
|
|
482
|
+
const primaryKeyObj = config.primaryKey.generateKey(key);
|
|
483
|
+
const builder = table.update(primaryKeyObj);
|
|
484
|
+
builder.condition(eq(entityTypeAttributeName, config.name));
|
|
485
|
+
const entityAwareBuilder = createEntityAwareUpdateBuilder(builder, config.name);
|
|
486
|
+
entityAwareBuilder.configureEntityLogic({
|
|
487
|
+
data,
|
|
488
|
+
key,
|
|
489
|
+
table,
|
|
490
|
+
indexes: config.indexes,
|
|
491
|
+
generateTimestamps: () => generateTimestamps(["updatedAt"], data),
|
|
492
|
+
buildIndexUpdates
|
|
493
|
+
});
|
|
494
|
+
return entityAwareBuilder;
|
|
495
|
+
},
|
|
496
|
+
delete: (key) => {
|
|
497
|
+
const builder = table.delete(config.primaryKey.generateKey(key));
|
|
498
|
+
builder.condition(eq(entityTypeAttributeName, config.name));
|
|
499
|
+
return createEntityAwareDeleteBuilder(builder, config.name);
|
|
500
|
+
},
|
|
501
|
+
query: Object.entries(config.queries || {}).reduce((acc, [key, inputCallback]) => {
|
|
502
|
+
acc[key] = (input) => {
|
|
503
|
+
const queryEntity = {
|
|
504
|
+
scan: repository.scan,
|
|
505
|
+
get: (key2) => createEntityAwareGetBuilder(table.get(key2), config.name),
|
|
506
|
+
query: (keyCondition) => {
|
|
507
|
+
return table.query(keyCondition);
|
|
508
|
+
}
|
|
509
|
+
};
|
|
510
|
+
const queryBuilderCallback = inputCallback(input);
|
|
511
|
+
const builder = queryBuilderCallback(queryEntity);
|
|
512
|
+
if (builder && typeof builder === "object" && "filter" in builder && typeof builder.filter === "function") {
|
|
513
|
+
builder.filter(eq(entityTypeAttributeName, config.name));
|
|
514
|
+
}
|
|
515
|
+
if (builder && typeof builder === "object" && "execute" in builder) {
|
|
516
|
+
const originalExecute = builder.execute;
|
|
517
|
+
builder.execute = async () => {
|
|
518
|
+
const queryFn = config.queries[key];
|
|
519
|
+
if (queryFn && typeof queryFn === "function") {
|
|
520
|
+
const schema = queryFn.schema;
|
|
521
|
+
if (schema?.["~standard"]?.validate && typeof schema["~standard"].validate === "function") {
|
|
522
|
+
const validationResult = schema["~standard"].validate(input);
|
|
523
|
+
if ("issues" in validationResult && validationResult.issues) {
|
|
524
|
+
throw new Error(
|
|
525
|
+
`Validation failed: ${validationResult.issues.map((issue) => issue.message).join(", ")}`
|
|
526
|
+
);
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
const result = await originalExecute.call(builder);
|
|
531
|
+
if (!result) {
|
|
532
|
+
throw new Error("Failed to execute query");
|
|
533
|
+
}
|
|
534
|
+
return result;
|
|
535
|
+
};
|
|
536
|
+
}
|
|
537
|
+
return builder;
|
|
538
|
+
};
|
|
539
|
+
return acc;
|
|
540
|
+
}, {}),
|
|
541
|
+
scan: () => {
|
|
542
|
+
const builder = table.scan();
|
|
543
|
+
builder.filter(eq(entityTypeAttributeName, config.name));
|
|
544
|
+
return builder;
|
|
545
|
+
}
|
|
546
|
+
};
|
|
547
|
+
return repository;
|
|
548
|
+
}
|
|
549
|
+
};
|
|
550
|
+
}
|
|
551
|
+
function createQueries() {
|
|
552
|
+
return {
|
|
553
|
+
input: (schema) => ({
|
|
554
|
+
query: (handler) => {
|
|
555
|
+
const queryFn = (input) => (entity) => handler({ input, entity });
|
|
556
|
+
queryFn.schema = schema;
|
|
557
|
+
return queryFn;
|
|
558
|
+
}
|
|
559
|
+
})
|
|
560
|
+
};
|
|
561
|
+
}
|
|
562
|
+
function createIndex() {
|
|
563
|
+
return {
|
|
564
|
+
input: (schema) => {
|
|
565
|
+
const createIndexBuilder = (isReadOnly = false) => ({
|
|
566
|
+
partitionKey: (pkFn) => ({
|
|
567
|
+
sortKey: (skFn) => {
|
|
568
|
+
const index = {
|
|
569
|
+
name: "custom",
|
|
570
|
+
partitionKey: "pk",
|
|
571
|
+
sortKey: "sk",
|
|
572
|
+
isReadOnly,
|
|
573
|
+
generateKey: (item) => {
|
|
574
|
+
const data = schema["~standard"].validate(item);
|
|
575
|
+
if ("issues" in data && data.issues) {
|
|
576
|
+
throw new Error(`Index validation failed: ${data.issues.map((i) => i.message).join(", ")}`);
|
|
577
|
+
}
|
|
578
|
+
const validData = "value" in data ? data.value : item;
|
|
579
|
+
return { pk: pkFn(validData), sk: skFn(validData) };
|
|
580
|
+
}
|
|
581
|
+
};
|
|
582
|
+
return Object.assign(index, {
|
|
583
|
+
readOnly: (value = false) => ({
|
|
584
|
+
...index,
|
|
585
|
+
isReadOnly: value
|
|
586
|
+
})
|
|
587
|
+
});
|
|
588
|
+
},
|
|
589
|
+
withoutSortKey: () => {
|
|
590
|
+
const index = {
|
|
591
|
+
name: "custom",
|
|
592
|
+
partitionKey: "pk",
|
|
593
|
+
isReadOnly,
|
|
594
|
+
generateKey: (item) => {
|
|
595
|
+
const data = schema["~standard"].validate(item);
|
|
596
|
+
if ("issues" in data && data.issues) {
|
|
597
|
+
throw new Error(`Index validation failed: ${data.issues.map((i) => i.message).join(", ")}`);
|
|
598
|
+
}
|
|
599
|
+
const validData = "value" in data ? data.value : item;
|
|
600
|
+
return { pk: pkFn(validData) };
|
|
601
|
+
}
|
|
602
|
+
};
|
|
603
|
+
return Object.assign(index, {
|
|
604
|
+
readOnly: (value = true) => ({
|
|
605
|
+
...index,
|
|
606
|
+
isReadOnly: value
|
|
607
|
+
})
|
|
608
|
+
});
|
|
609
|
+
}
|
|
610
|
+
}),
|
|
611
|
+
readOnly: (value = true) => createIndexBuilder(value)
|
|
612
|
+
});
|
|
613
|
+
return createIndexBuilder(false);
|
|
614
|
+
}
|
|
615
|
+
};
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
export { createIndex, createQueries, defineEntity };
|