dynoquery 0.1.17 → 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 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,16 +264,18 @@ 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
 
241
272
  ### Partition
242
- - `get(sk)`: Fetches data for a specific Sort Key value (returns a Promise).
273
+ - `get(skValue)`: Fetches data for a specific Sort Key value (returns a Promise).
243
274
  - `getAll(options?)`: Fetches items in the partition. Options: `{ limit, exclusiveStartKey }`.
244
- - `create(sk, data, indices?)`: Creates an item. `indices` is an array of `IndexQuery` for GSI population.
245
- - `update(sk, data)`: Partial update of an item.
246
- - `delete(sk)`: Deletes an item.
247
- - `draft(sk, data?)`: Returns an `Item` object initialized with `data` (optional).
275
+ - `create(skValue, data, indices?)`: Creates an item. `indices` is an array of `IndexQuery` for GSI population.
276
+ - `update(skValue, data)`: Partial update of an item.
277
+ - `delete(skValue)`: Deletes an item.
278
+ - `draft(skValue, data?)`: Returns an `Item` object initialized with `data` (optional).
248
279
  - `deleteAll()`: Deletes all items in the partition.
249
280
  - `getLastEvaluatedKey()`: Returns the pagination token from the last `getAll()`.
250
281
 
@@ -33,5 +33,4 @@ export declare class IndexQuery {
33
33
  getSkName(): string;
34
34
  getSkValue(): string | undefined;
35
35
  getLastEvaluatedKey(): any;
36
- private mapItemToModelItem;
37
36
  }
@@ -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 = {};
@@ -55,10 +56,10 @@ class DynoQuery {
55
56
  if (models) {
56
57
  this.registeredModels = models;
57
58
  Object.entries(models).forEach(([name, def]) => {
58
- this[name] = (id, sk) => {
59
+ this[name] = (id, skValue) => {
59
60
  const partition = new partition_1.Partition(this, { pkPrefix: this.globalPkPrefix + def.pkPrefix }, id);
60
- if (sk) {
61
- return partition.draft(sk);
61
+ if (skValue) {
62
+ return partition.draft(skValue);
62
63
  }
63
64
  return partition;
64
65
  };
@@ -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
  }
@@ -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 {
@@ -34,7 +36,7 @@ export declare class Partition {
34
36
  /**
35
37
  * Create an item in this partition.
36
38
  */
37
- create<T = any>(sk: string, data: T, indices?: IndexQuery[]): Promise<T>;
39
+ create<T = any>(skValue: string, data: T, indices?: IndexQuery[]): Promise<T>;
38
40
  /**
39
41
  * Internal method to get raw data for a specific SK.
40
42
  */
@@ -42,21 +44,22 @@ export declare class Partition {
42
44
  /**
43
45
  * Update an existing item in this partition.
44
46
  */
45
- update<T = any>(sk: string, data: Partial<T>, indices?: IndexQuery[]): Promise<T>;
47
+ update<T = any>(skValue: string, data: Partial<T>, indices?: IndexQuery[]): Promise<T>;
46
48
  /**
47
49
  * Delete an item by its SK within this partition.
48
50
  */
49
- delete(sk: string): Promise<void>;
51
+ delete(skValue: string): Promise<void>;
50
52
  /**
51
53
  * Get data for a specific SK and return it wrapped in a Item object.
52
54
  */
53
- get<T = any>(sk: string): Promise<T | null>;
55
+ get<T = any>(skValue: string): Promise<T | null>;
54
56
  /**
55
57
  * Pre-draft an item for creation. Returns an Item object.
56
58
  * @param sk The sort key value
57
59
  * @param data Initial data for the row
58
60
  */
59
- draft<T = any>(sk: string, data?: any): T;
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) && key !== "_indices" && typeof target[key] !== 'function') {
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
  });
@@ -129,9 +139,9 @@ class Partition {
129
139
  /**
130
140
  * Create an item in this partition.
131
141
  */
132
- create(sk, data, indices) {
142
+ create(skValue, data, indices) {
133
143
  return __awaiter(this, void 0, void 0, function* () {
134
- const item = Object.assign({ [this.pkName]: this.pkValue, [this.skName]: sk }, data);
144
+ const item = Object.assign({ [this.pkName]: this.pkValue, [this.skName]: skValue }, data);
135
145
  if (indices) {
136
146
  indices.forEach((index) => {
137
147
  item[index.getPkName()] = index.getPkValue();
@@ -144,17 +154,17 @@ class Partition {
144
154
  TableName: this.tableName,
145
155
  Item: item,
146
156
  });
147
- this.cache[sk] = item;
148
- return new Item(this, sk, item);
157
+ this.cache[skValue] = item;
158
+ return new Item(this, skValue, item);
149
159
  });
150
160
  }
151
161
  /**
152
162
  * Internal method to get raw data for a specific SK.
153
163
  */
154
- _getRaw(sk) {
164
+ _getRaw(skValue) {
155
165
  return __awaiter(this, void 0, void 0, function* () {
156
- if (this.cache[sk] !== undefined) {
157
- return this.cache[sk] || null;
166
+ if (this.cache[skValue] !== undefined) {
167
+ return this.cache[skValue] || null;
158
168
  }
159
169
  if (this.isLoaded) {
160
170
  return null;
@@ -163,12 +173,12 @@ class Partition {
163
173
  TableName: this.tableName,
164
174
  Key: {
165
175
  [this.pkName]: this.pkValue,
166
- [this.skName]: sk,
176
+ [this.skName]: skValue,
167
177
  },
168
178
  });
169
179
  const data = response.Item || null;
170
180
  if (data) {
171
- this.cache[sk] = data;
181
+ this.cache[skValue] = data;
172
182
  }
173
183
  return data;
174
184
  });
@@ -176,35 +186,35 @@ class Partition {
176
186
  /**
177
187
  * Update an existing item in this partition.
178
188
  */
179
- update(sk, data, indices) {
189
+ update(skValue, data, indices) {
180
190
  return __awaiter(this, void 0, void 0, function* () {
181
- const current = (yield this._getRaw(sk)) || {};
191
+ const current = (yield this._getRaw(skValue)) || {};
182
192
  const updated = Object.assign(Object.assign({}, current), data);
183
- return yield this.create(sk, updated, indices);
193
+ return yield this.create(skValue, updated, indices);
184
194
  });
185
195
  }
186
196
  /**
187
197
  * Delete an item by its SK within this partition.
188
198
  */
189
- delete(sk) {
199
+ delete(skValue) {
190
200
  return __awaiter(this, void 0, void 0, function* () {
191
201
  yield this.db.delete({
192
202
  TableName: this.tableName,
193
203
  Key: {
194
204
  [this.pkName]: this.pkValue,
195
- [this.skName]: sk,
205
+ [this.skName]: skValue,
196
206
  },
197
207
  });
198
- delete this.cache[sk];
208
+ delete this.cache[skValue];
199
209
  });
200
210
  }
201
211
  /**
202
212
  * Get data for a specific SK and return it wrapped in a Item object.
203
213
  */
204
- get(sk) {
214
+ get(skValue) {
205
215
  return __awaiter(this, void 0, void 0, function* () {
206
- const data = yield this._getRaw(sk);
207
- return data ? new Item(this, sk, data) : null;
216
+ const data = yield this._getRaw(skValue);
217
+ return data ? new Item(this, skValue, data) : null;
208
218
  });
209
219
  }
210
220
  /**
@@ -212,8 +222,11 @@ class Partition {
212
222
  * @param sk The sort key value
213
223
  * @param data Initial data for the row
214
224
  */
215
- draft(sk, data = {}) {
216
- return new Item(this, sk, data);
225
+ draft(skValue, data = {}) {
226
+ return new Item(this, skValue, data);
227
+ }
228
+ getTableName() {
229
+ return this.tableName || "";
217
230
  }
218
231
  getPkValue() {
219
232
  return this.pkValue;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dynoquery",
3
- "version": "0.1.17",
3
+ "version": "0.1.19",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/devspikejs/dynoquery.git"