dynoquery 0.1.18 → 0.1.19
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 +31 -0
- package/dist/index-query.d.ts +0 -1
- package/dist/index-query.js +1 -35
- package/dist/index.d.ts +14 -0
- package/dist/index.js +132 -0
- package/dist/partition.d.ts +3 -0
- package/dist/partition.js +14 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -182,6 +182,35 @@ mouse.price = 50;
|
|
|
182
182
|
await mouse.save();
|
|
183
183
|
```
|
|
184
184
|
|
|
185
|
+
### 4. Batch Operations
|
|
186
|
+
|
|
187
|
+
DynoQuery provides `batchWrite` and `batchRead` for processing multiple items across partitions.
|
|
188
|
+
|
|
189
|
+
```typescript
|
|
190
|
+
// 1. Batch Write (Create/Replace multiple items)
|
|
191
|
+
const user1 = db.User('john', 'METADATA');
|
|
192
|
+
user1.name = 'John Doe';
|
|
193
|
+
|
|
194
|
+
const user2 = db.User('jane', 'METADATA');
|
|
195
|
+
user2.name = 'Jane Doe';
|
|
196
|
+
|
|
197
|
+
await db.batchWrite([user1, user2]);
|
|
198
|
+
|
|
199
|
+
// 2. Batch Read (Fetch multiple items or index queries)
|
|
200
|
+
const userDraft = db.User('john', 'METADATA');
|
|
201
|
+
const categoryQuery = db.findByCategory('ELECTRONICS', 'p123');
|
|
202
|
+
|
|
203
|
+
const results = await db.batchRead([userDraft, categoryQuery]);
|
|
204
|
+
|
|
205
|
+
results.forEach(item => {
|
|
206
|
+
console.log(item.__model); // Automatically mapped to models
|
|
207
|
+
if (item.__model === 'User') {
|
|
208
|
+
item.status = 'active';
|
|
209
|
+
item.save(); // Second-level methods are available
|
|
210
|
+
}
|
|
211
|
+
});
|
|
212
|
+
```
|
|
213
|
+
|
|
185
214
|
## Optional Configuration Parameters
|
|
186
215
|
|
|
187
216
|
| Parameter | Type | Default | Description |
|
|
@@ -235,6 +264,8 @@ if (token) {
|
|
|
235
264
|
- `delete(params)`: Low-level DeleteCommand wrapper.
|
|
236
265
|
- `query(params)`: Low-level QueryCommand wrapper.
|
|
237
266
|
- `scan(params)`: Low-level ScanCommand wrapper.
|
|
267
|
+
- `batchWrite(items)`: Batch persists multiple `Item` objects.
|
|
268
|
+
- `batchRead(items)`: Batch fetches multiple `Item` or `IndexQuery` objects.
|
|
238
269
|
- `[ModelName](id, skValue?)`: Returns a `Partition` instance for the given ID. If `skValue` is provided, returns an `Item` object directly.
|
|
239
270
|
- `findBy[IndexName](id, skValue?)`: Returns an `IndexQuery` instance.
|
|
240
271
|
|
package/dist/index-query.d.ts
CHANGED
package/dist/index-query.js
CHANGED
|
@@ -10,8 +10,6 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
exports.IndexQuery = void 0;
|
|
13
|
-
const partition_1 = require("./partition");
|
|
14
|
-
const partition_2 = require("./partition");
|
|
15
13
|
class IndexQuery {
|
|
16
14
|
constructor(db, config) {
|
|
17
15
|
this.lastEvaluatedKey = null;
|
|
@@ -55,7 +53,7 @@ class IndexQuery {
|
|
|
55
53
|
ExclusiveStartKey: options === null || options === void 0 ? void 0 : options.exclusiveStartKey,
|
|
56
54
|
});
|
|
57
55
|
const items = (response.Items || []);
|
|
58
|
-
const mappedItems = items.map(item => this.mapItemToModelItem(item));
|
|
56
|
+
const mappedItems = items.map(item => this.db.mapItemToModelItem(item));
|
|
59
57
|
this.lastEvaluatedKey = response.LastEvaluatedKey || null;
|
|
60
58
|
return mappedItems;
|
|
61
59
|
});
|
|
@@ -81,37 +79,5 @@ class IndexQuery {
|
|
|
81
79
|
getLastEvaluatedKey() {
|
|
82
80
|
return this.lastEvaluatedKey;
|
|
83
81
|
}
|
|
84
|
-
mapItemToModelItem(item) {
|
|
85
|
-
const pkName = this.db.getPkName();
|
|
86
|
-
const pkValue = item[pkName];
|
|
87
|
-
if (!pkValue)
|
|
88
|
-
return item;
|
|
89
|
-
const registeredModels = this.db.getRegisteredModels();
|
|
90
|
-
const globalPrefix = this.db.getPkPrefix();
|
|
91
|
-
for (const [name, def] of Object.entries(registeredModels)) {
|
|
92
|
-
const fullPrefix = globalPrefix + def.pkPrefix;
|
|
93
|
-
if (pkValue.startsWith(fullPrefix)) {
|
|
94
|
-
// Find the ID by removing the prefix
|
|
95
|
-
const id = pkValue.substring(fullPrefix.length);
|
|
96
|
-
// Return a Partition instance and attach the data to its cache.
|
|
97
|
-
const partition = new partition_1.Partition(this.db, { pkPrefix: fullPrefix }, id);
|
|
98
|
-
// Pre-fill the cache if we have the SK
|
|
99
|
-
const skName = this.db.getSkName();
|
|
100
|
-
const skValue = item[skName];
|
|
101
|
-
if (skValue) {
|
|
102
|
-
partition["cache"][skValue] = item;
|
|
103
|
-
}
|
|
104
|
-
// Add a property __model to the item if it matches.
|
|
105
|
-
item.__model = name;
|
|
106
|
-
// Also provide a way to get the partition instance from the item
|
|
107
|
-
item.getPartition = () => partition;
|
|
108
|
-
if (skValue) {
|
|
109
|
-
return new partition_2.Item(partition, skValue, item);
|
|
110
|
-
}
|
|
111
|
-
return item;
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
return item;
|
|
115
|
-
}
|
|
116
82
|
}
|
|
117
83
|
exports.IndexQuery = IndexQuery;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import { PutCommandInput, GetCommandInput, UpdateCommandInput, DeleteCommandInput, QueryCommandInput, ScanCommandInput } from "@aws-sdk/lib-dynamodb";
|
|
2
|
+
import { Item } from "./partition";
|
|
3
|
+
import { IndexQuery } from "./index-query";
|
|
2
4
|
export interface DynoQueryConfig {
|
|
3
5
|
tableName?: string;
|
|
4
6
|
pkName?: string;
|
|
@@ -55,6 +57,18 @@ export declare class DynoQuery {
|
|
|
55
57
|
* Scan the table or index for items.
|
|
56
58
|
*/
|
|
57
59
|
scan(params: ScanCommandInput): Promise<import("@aws-sdk/lib-dynamodb").ScanCommandOutput>;
|
|
60
|
+
/**
|
|
61
|
+
* Batch write items to the table.
|
|
62
|
+
*/
|
|
63
|
+
batchWrite(items: Item[]): Promise<Item[]>;
|
|
64
|
+
/**
|
|
65
|
+
* Batch get items from the table.
|
|
66
|
+
*/
|
|
67
|
+
batchRead(items: (Item | IndexQuery)[]): Promise<any[]>;
|
|
68
|
+
/**
|
|
69
|
+
* Maps a raw DynamoDB item to a Model Item if it matches a registered model.
|
|
70
|
+
*/
|
|
71
|
+
mapItemToModelItem(item: any): any;
|
|
58
72
|
getTableName(): string | undefined;
|
|
59
73
|
getPkPrefix(): string;
|
|
60
74
|
getPkName(): string;
|
package/dist/index.js
CHANGED
|
@@ -38,6 +38,7 @@ exports.Item = exports.DynoQuery = void 0;
|
|
|
38
38
|
const client_dynamodb_1 = require("@aws-sdk/client-dynamodb");
|
|
39
39
|
const lib_dynamodb_1 = require("@aws-sdk/lib-dynamodb");
|
|
40
40
|
const partition_1 = require("./partition");
|
|
41
|
+
const index_query_1 = require("./index-query");
|
|
41
42
|
class DynoQuery {
|
|
42
43
|
constructor(config = {}) {
|
|
43
44
|
this.registeredModels = {};
|
|
@@ -153,6 +154,137 @@ class DynoQuery {
|
|
|
153
154
|
return yield this.docClient.send(command);
|
|
154
155
|
});
|
|
155
156
|
}
|
|
157
|
+
/**
|
|
158
|
+
* Batch write items to the table.
|
|
159
|
+
*/
|
|
160
|
+
batchWrite(items) {
|
|
161
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
162
|
+
const tableGroups = {};
|
|
163
|
+
items.forEach((item) => {
|
|
164
|
+
const partition = item.getPartition();
|
|
165
|
+
const tableName = partition.getTableName();
|
|
166
|
+
const skValue = item.getSkValue();
|
|
167
|
+
if (!tableGroups[tableName]) {
|
|
168
|
+
tableGroups[tableName] = [];
|
|
169
|
+
}
|
|
170
|
+
const dataToSave = {};
|
|
171
|
+
for (const key in item) {
|
|
172
|
+
if (Object.prototype.hasOwnProperty.call(item, key) &&
|
|
173
|
+
!["_indices", "_partition", "_skValue"].includes(key) &&
|
|
174
|
+
typeof item[key] !== "function") {
|
|
175
|
+
dataToSave[key] = item[key];
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
// Ensure PK and SK are in the data
|
|
179
|
+
dataToSave[this.pkName] = partition.getPkValue();
|
|
180
|
+
dataToSave[this.skName] = skValue;
|
|
181
|
+
tableGroups[tableName].push({
|
|
182
|
+
PutRequest: {
|
|
183
|
+
Item: dataToSave,
|
|
184
|
+
},
|
|
185
|
+
});
|
|
186
|
+
});
|
|
187
|
+
const results = [];
|
|
188
|
+
const tableNames = Object.keys(tableGroups);
|
|
189
|
+
for (const tableName of tableNames) {
|
|
190
|
+
const requests = tableGroups[tableName];
|
|
191
|
+
// Chunk requests into 25
|
|
192
|
+
for (let i = 0; i < requests.length; i += 25) {
|
|
193
|
+
const chunk = requests.slice(i, i + 25);
|
|
194
|
+
yield this.docClient.send(new lib_dynamodb_1.BatchWriteCommand({
|
|
195
|
+
RequestItems: {
|
|
196
|
+
[tableName]: chunk,
|
|
197
|
+
},
|
|
198
|
+
}));
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
return items;
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* Batch get items from the table.
|
|
206
|
+
*/
|
|
207
|
+
batchRead(items) {
|
|
208
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
209
|
+
const tableGroups = {};
|
|
210
|
+
const requestMap = {};
|
|
211
|
+
items.forEach((item) => {
|
|
212
|
+
let tableName;
|
|
213
|
+
let pkValue;
|
|
214
|
+
let skValue;
|
|
215
|
+
if (item instanceof index_query_1.IndexQuery) {
|
|
216
|
+
tableName = item.tableName;
|
|
217
|
+
pkValue = item.getPkValue();
|
|
218
|
+
skValue = item.getSkValue() || "";
|
|
219
|
+
}
|
|
220
|
+
else {
|
|
221
|
+
const partition = item.getPartition();
|
|
222
|
+
tableName = partition.getTableName();
|
|
223
|
+
pkValue = partition.getPkValue();
|
|
224
|
+
skValue = item.getSkValue();
|
|
225
|
+
}
|
|
226
|
+
if (!tableGroups[tableName]) {
|
|
227
|
+
tableGroups[tableName] = [];
|
|
228
|
+
}
|
|
229
|
+
const key = {
|
|
230
|
+
[this.pkName]: pkValue,
|
|
231
|
+
[this.skName]: skValue,
|
|
232
|
+
};
|
|
233
|
+
tableGroups[tableName].push(key);
|
|
234
|
+
const requestKey = `${tableName}|${pkValue}|${skValue}`;
|
|
235
|
+
requestMap[requestKey] = item;
|
|
236
|
+
});
|
|
237
|
+
const allItems = [];
|
|
238
|
+
const tableNames = Object.keys(tableGroups);
|
|
239
|
+
for (const tableName of tableNames) {
|
|
240
|
+
const keys = tableGroups[tableName];
|
|
241
|
+
for (let i = 0; i < keys.length; i += 100) {
|
|
242
|
+
const chunk = keys.slice(i, i + 100);
|
|
243
|
+
const response = yield this.docClient.send(new lib_dynamodb_1.BatchGetCommand({
|
|
244
|
+
RequestItems: {
|
|
245
|
+
[tableName]: {
|
|
246
|
+
Keys: chunk,
|
|
247
|
+
},
|
|
248
|
+
},
|
|
249
|
+
}));
|
|
250
|
+
if (response.Responses && response.Responses[tableName]) {
|
|
251
|
+
const items = response.Responses[tableName];
|
|
252
|
+
items.forEach((item) => {
|
|
253
|
+
const mappedItem = this.mapItemToModelItem(item);
|
|
254
|
+
allItems.push(mappedItem);
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
return allItems;
|
|
260
|
+
});
|
|
261
|
+
}
|
|
262
|
+
/**
|
|
263
|
+
* Maps a raw DynamoDB item to a Model Item if it matches a registered model.
|
|
264
|
+
*/
|
|
265
|
+
mapItemToModelItem(item) {
|
|
266
|
+
const pkValue = item[this.pkName];
|
|
267
|
+
if (!pkValue)
|
|
268
|
+
return item;
|
|
269
|
+
for (const [name, def] of Object.entries(this.registeredModels)) {
|
|
270
|
+
const fullPrefix = this.globalPkPrefix + def.pkPrefix;
|
|
271
|
+
if (pkValue.startsWith(fullPrefix)) {
|
|
272
|
+
const id = pkValue.substring(fullPrefix.length);
|
|
273
|
+
const partition = new partition_1.Partition(this, { pkPrefix: fullPrefix }, id);
|
|
274
|
+
const skValue = item[this.skName];
|
|
275
|
+
if (skValue) {
|
|
276
|
+
partition["cache"][skValue] = item;
|
|
277
|
+
}
|
|
278
|
+
// Add metadata to the item
|
|
279
|
+
item.__model = name;
|
|
280
|
+
item.getPartition = () => partition;
|
|
281
|
+
if (skValue) {
|
|
282
|
+
return new partition_1.Item(partition, skValue, item);
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
return item;
|
|
287
|
+
}
|
|
156
288
|
getTableName() {
|
|
157
289
|
return this.defaultTableName;
|
|
158
290
|
}
|
package/dist/partition.d.ts
CHANGED
|
@@ -8,6 +8,8 @@ export interface PartitionConfig {
|
|
|
8
8
|
export declare class Item {
|
|
9
9
|
[key: string]: any;
|
|
10
10
|
private _indices;
|
|
11
|
+
private _partition;
|
|
12
|
+
private _skValue;
|
|
11
13
|
constructor(partition: Partition, skValue: string, data: any);
|
|
12
14
|
}
|
|
13
15
|
export declare class Partition {
|
|
@@ -57,6 +59,7 @@ export declare class Partition {
|
|
|
57
59
|
* @param data Initial data for the row
|
|
58
60
|
*/
|
|
59
61
|
draft<T = any>(skValue: string, data?: any): T;
|
|
62
|
+
getTableName(): string;
|
|
60
63
|
getPkValue(): string;
|
|
61
64
|
getLastEvaluatedKey(): any;
|
|
62
65
|
/**
|
package/dist/partition.js
CHANGED
|
@@ -13,6 +13,8 @@ exports.Partition = exports.Item = void 0;
|
|
|
13
13
|
class Item {
|
|
14
14
|
constructor(partition, skValue, data) {
|
|
15
15
|
this._indices = [];
|
|
16
|
+
this._partition = partition;
|
|
17
|
+
this._skValue = skValue;
|
|
16
18
|
Object.assign(this, data);
|
|
17
19
|
const self = this;
|
|
18
20
|
return new Proxy(this, {
|
|
@@ -21,7 +23,9 @@ class Item {
|
|
|
21
23
|
return () => {
|
|
22
24
|
const dataToSave = {};
|
|
23
25
|
for (const key in target) {
|
|
24
|
-
if (Object.prototype.hasOwnProperty.call(target, key) &&
|
|
26
|
+
if (Object.prototype.hasOwnProperty.call(target, key) &&
|
|
27
|
+
!["_indices", "_partition", "_skValue"].includes(key) &&
|
|
28
|
+
typeof target[key] !== "function") {
|
|
25
29
|
dataToSave[key] = target[key];
|
|
26
30
|
}
|
|
27
31
|
}
|
|
@@ -51,6 +55,12 @@ class Item {
|
|
|
51
55
|
return receiver;
|
|
52
56
|
};
|
|
53
57
|
}
|
|
58
|
+
if (prop === "getPartition") {
|
|
59
|
+
return () => partition;
|
|
60
|
+
}
|
|
61
|
+
if (prop === "getSkValue") {
|
|
62
|
+
return () => skValue;
|
|
63
|
+
}
|
|
54
64
|
return Reflect.get(target, prop, receiver);
|
|
55
65
|
},
|
|
56
66
|
});
|
|
@@ -215,6 +225,9 @@ class Partition {
|
|
|
215
225
|
draft(skValue, data = {}) {
|
|
216
226
|
return new Item(this, skValue, data);
|
|
217
227
|
}
|
|
228
|
+
getTableName() {
|
|
229
|
+
return this.tableName || "";
|
|
230
|
+
}
|
|
218
231
|
getPkValue() {
|
|
219
232
|
return this.pkValue;
|
|
220
233
|
}
|