dynoquery 0.1.7 → 0.1.9

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
@@ -13,7 +13,6 @@ npm install dynoquery
13
13
  - Basic CRUD operations (create, get, update, delete)
14
14
  - Optimized for **Single-Table Design**
15
15
  - Query and Scan support
16
- - Batch operations (batchGet, batchWrite)
17
16
  - TypeScript support
18
17
 
19
18
  ## Usage
@@ -32,9 +31,9 @@ const db = new DynoQuery({
32
31
  models: {
33
32
  User: { pkPrefix: 'USER#' }, // TENANT#A#USER#
34
33
  },
35
- indexes: {
34
+ findBy: {
36
35
  // TENANT#A#CAT#
37
- ByCategory: { indexName: 'GSI1', pkPrefix: 'CAT#' } // pkName defaults to GSI1PK, skName defaults to GSI1SK
36
+ Category: { indexName: 'GSI1', pkPrefix: 'CAT#' } // pkName defaults to GSI1PK, skName defaults to GSI1SK
38
37
  }
39
38
  });
40
39
 
@@ -45,7 +44,7 @@ async function example() {
45
44
 
46
45
  // Use registered index
47
46
  // Resulting GSI1PK: TENANT#A#CAT#1
48
- const categories = db.ByCategory('1');
47
+ const categories = db.findByCategory('1');
49
48
  const items = await categories.get('100');
50
49
  const allItems = await categories.getAll();
51
50
 
@@ -68,9 +67,8 @@ async function example() {
68
67
  // Create an item through partition
69
68
  await john.create('PROFILE', { name: 'John Doe', email: 'john@example.com' });
70
69
 
71
- // You can also use getPkValue() to get the generated PK for the partition or index
72
- // This is useful when you need to store it in another attribute (e.g., GSI)
73
- const cat = db.ByCategory('USER');
70
+ // Resulting GSI1PK: TENANT#A#CAT#USER
71
+ const cat = db.findByCategory('USER');
74
72
  await john.create('PROFILE', {
75
73
  name: 'John Doe',
76
74
  email: 'john@example.com',
@@ -97,48 +95,6 @@ async function example() {
97
95
  }
98
96
  ```
99
97
 
100
- ### Batch Operations
101
-
102
- If you have a `tableName` configured in `DynoQuery`, you can use the `Items` property in `batchGet` and `batchWrite` to automatically target that table.
103
-
104
- You can also generate items for `batchGet` using the `batchGetInput` method from partitions and indexes:
105
-
106
- ```typescript
107
- const john = db.User('john@example.com');
108
- const jack = db.User('jack@example.com');
109
- const cat = db.ByCategory('1');
110
-
111
- const batchItem1 = john.batchGetInput('METADATA');
112
- const batchOthers = cat.batchGetInput('METADATA', 'DATA', 'PROFILE');
113
- const batchCat = cat.batchGetInput(); // No sk defined, so will get partition key only
114
-
115
- await db.batchGet(batchItem1, batchOthers, batchCat);
116
- ```
117
-
118
- ```typescript
119
- // batchGet with explicit Items
120
- await db.batchGet({
121
- Items: [
122
- { PK: 'USER#1', SK: 'METADATA' },
123
- { PK: 'USER#2', SK: 'METADATA' }
124
- ]
125
- });
126
-
127
- // batchWrite with simplified Items (automatically wrapped in PutRequest)
128
- await db.batchWrite({
129
- Items: [
130
- { PK: 'USER#3', SK: 'METADATA', name: 'Alice' },
131
- {
132
- DeleteRequest: {
133
- Key: { PK: 'USER#1', SK: 'SESSION#123' }
134
- }
135
- }
136
- ]
137
- });
138
- ```
139
-
140
- You can still use the standard AWS SDK `RequestItems` if you need to target multiple tables or if you prefer the original syntax.
141
-
142
98
  ## API Reference
143
99
 
144
100
  ### DynoQuery
@@ -149,8 +105,6 @@ The main client for interacting with DynamoDB.
149
105
  - `delete(params)`: Delete an item.
150
106
  - `query(params)`: Query items.
151
107
  - `scan(params)`: Scan items.
152
- - `batchGet(params)`: Batch get items.
153
- - `batchWrite(params)`: Batch write items.
154
108
 
155
109
  ### Partition
156
110
  A way to manage data within a specific partition.
@@ -160,9 +114,6 @@ A way to manage data within a specific partition.
160
114
  - `create(sk, data)`: Creates an item in the partition.
161
115
  - `update(sk, data)`: Updates an existing item (partial update).
162
116
  - `delete(sk)`: Deletes an item.
163
- - `batchGetInput(...sks)`: Generates items for batch query.
164
- - `batchWriteInput(...items)`: Generates items for batch write.
165
- - `batchDeleteInput(...sks)`: Generates items for batch delete.
166
117
  - `deleteAll()`: Deletes all items in the partition.
167
118
 
168
119
  ### IndexQuery
@@ -170,9 +121,6 @@ A way to query Global Secondary Indexes.
170
121
  - `getPkValue()`: Returns the generated partition key value for this index.
171
122
  - `get(skValue | options)`: Query items in the index. Supports `skValue` (string) for `begins_with` search, or an options object with `skValue`, `limit`, and `scanIndexForward`.
172
123
  - `getAll()`: Fetches all items in the index for the given partition key.
173
- - `batchGetInput(...sks)`: Generates items for batch query.
174
- - `batchWriteInput(...items)`: Generates items for batch write.
175
- - `batchDeleteInput(...sks)`: Generates items for batch delete.
176
124
  - Automatically identifies the model name in results using `__model` (based on registered models) and provides `getPartition()` helper.
177
125
 
178
126
  ## License
@@ -22,17 +22,5 @@ export declare class IndexQuery {
22
22
  }): Promise<T[]>;
23
23
  getAll<T = any>(): Promise<T[]>;
24
24
  getPkValue(): string;
25
- /**
26
- * Generates items for batch query.
27
- */
28
- batchGetInput(...sks: string[]): any[];
29
- /**
30
- * Generates items for batch write (put).
31
- */
32
- batchWriteInput(...items: any[]): any[];
33
- /**
34
- * Generates items for batch delete.
35
- */
36
- batchDeleteInput(...sks: string[]): any[];
37
25
  private mapItemToModel;
38
26
  }
@@ -71,49 +71,6 @@ class IndexQuery {
71
71
  getPkValue() {
72
72
  return this.pkValue;
73
73
  }
74
- /**
75
- * Generates items for batch query.
76
- */
77
- batchGetInput(...sks) {
78
- if (sks.length === 0) {
79
- return [{
80
- TableName: this.tableName,
81
- Key: { [this.pkName]: this.pkValue }
82
- }];
83
- }
84
- return sks.map(sk => ({
85
- TableName: this.tableName,
86
- Key: {
87
- [this.pkName]: this.pkValue,
88
- [this.skName]: sk
89
- }
90
- }));
91
- }
92
- /**
93
- * Generates items for batch write (put).
94
- */
95
- batchWriteInput(...items) {
96
- return items.map(item => ({
97
- TableName: this.tableName,
98
- PutRequest: {
99
- Item: Object.assign({ [this.pkName]: this.pkValue }, item)
100
- }
101
- }));
102
- }
103
- /**
104
- * Generates items for batch delete.
105
- */
106
- batchDeleteInput(...sks) {
107
- return sks.map(sk => ({
108
- TableName: this.tableName,
109
- DeleteRequest: {
110
- Key: {
111
- [this.pkName]: this.pkValue,
112
- [this.skName]: sk
113
- }
114
- }
115
- }));
116
- }
117
74
  mapItemToModel(item) {
118
75
  const pkName = this.db.getPkName();
119
76
  const pkValue = item[pkName];
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { PutCommandInput, GetCommandInput, UpdateCommandInput, DeleteCommandInput, QueryCommandInput, ScanCommandInput, BatchGetCommandInput, BatchWriteCommandInput } from "@aws-sdk/lib-dynamodb";
1
+ import { PutCommandInput, GetCommandInput, UpdateCommandInput, DeleteCommandInput, QueryCommandInput, ScanCommandInput } from "@aws-sdk/lib-dynamodb";
2
2
  export interface DynoQueryConfig {
3
3
  tableName?: string;
4
4
  pkName?: string;
@@ -14,19 +14,13 @@ export interface DynoQueryConfig {
14
14
  models?: Record<string, {
15
15
  pkPrefix: string;
16
16
  }>;
17
- indexes?: Record<string, {
17
+ findBy?: Record<string, {
18
18
  indexName: string;
19
19
  pkName?: string;
20
20
  skName?: string;
21
21
  pkPrefix?: string;
22
22
  }>;
23
23
  }
24
- export type BatchGetInput = BatchGetCommandInput & {
25
- Items?: any[];
26
- };
27
- export type BatchWriteInput = BatchWriteCommandInput & {
28
- Items?: any[];
29
- };
30
24
  export declare class DynoQuery {
31
25
  private client;
32
26
  private docClient;
@@ -61,14 +55,6 @@ export declare class DynoQuery {
61
55
  * Scan the table or index for items.
62
56
  */
63
57
  scan(params: ScanCommandInput): Promise<import("@aws-sdk/lib-dynamodb").ScanCommandOutput>;
64
- /**
65
- * Get multiple items by their primary keys.
66
- */
67
- batchGet(params: BatchGetInput | any, ...additionalItems: any[][]): Promise<import("@aws-sdk/lib-dynamodb").BatchGetCommandOutput>;
68
- /**
69
- * Put or delete multiple items in one or more tables.
70
- */
71
- batchWrite(params: BatchWriteInput): Promise<import("@aws-sdk/lib-dynamodb").BatchWriteCommandOutput>;
72
58
  getTableName(): string | undefined;
73
59
  getPkPrefix(): string;
74
60
  getPkName(): string;
package/dist/index.js CHANGED
@@ -41,7 +41,7 @@ const partition_1 = require("./partition");
41
41
  class DynoQuery {
42
42
  constructor(config = {}) {
43
43
  this.registeredModels = {};
44
- const { tableName, pkName, skName, pkPrefix, models, indexes } = config, clientConfig = __rest(config, ["tableName", "pkName", "skName", "pkPrefix", "models", "indexes"]);
44
+ const { tableName, pkName, skName, pkPrefix, models, findBy } = config, clientConfig = __rest(config, ["tableName", "pkName", "skName", "pkPrefix", "models", "findBy"]);
45
45
  this.client = new client_dynamodb_1.DynamoDBClient(clientConfig);
46
46
  this.docClient = lib_dynamodb_1.DynamoDBDocumentClient.from(this.client, {
47
47
  marshallOptions: {
@@ -60,10 +60,11 @@ class DynoQuery {
60
60
  };
61
61
  });
62
62
  }
63
- if (indexes) {
63
+ if (findBy) {
64
64
  const { IndexQuery } = require("./index-query");
65
- Object.entries(indexes).forEach(([name, def]) => {
66
- this[name] = (id) => {
65
+ Object.entries(findBy).forEach(([name, def]) => {
66
+ const methodName = `findBy${name}`;
67
+ this[methodName] = (id) => {
67
68
  return new IndexQuery(this, {
68
69
  indexName: def.indexName,
69
70
  pkName: def.pkName,
@@ -147,79 +148,6 @@ class DynoQuery {
147
148
  return yield this.docClient.send(command);
148
149
  });
149
150
  }
150
- /**
151
- * Get multiple items by their primary keys.
152
- */
153
- batchGet(params, ...additionalItems) {
154
- return __awaiter(this, void 0, void 0, function* () {
155
- let finalParams;
156
- if (params && !params.RequestItems && !params.Items && (Array.isArray(params) || additionalItems.length > 0)) {
157
- // Handle the case where arguments are multiple arrays of items
158
- const allItems = Array.isArray(params) ? [...params] : [];
159
- additionalItems.forEach(chunk => {
160
- if (Array.isArray(chunk)) {
161
- allItems.push(...chunk);
162
- }
163
- else {
164
- allItems.push(chunk);
165
- }
166
- });
167
- finalParams = {
168
- Items: allItems
169
- };
170
- }
171
- else {
172
- finalParams = params;
173
- }
174
- if (!finalParams.RequestItems && finalParams.Items) {
175
- finalParams.RequestItems = {};
176
- finalParams.Items.forEach((item) => {
177
- const tableName = item.TableName || this.defaultTableName;
178
- if (!tableName) {
179
- throw new Error("TableName must be provided for batch operations if no default tableName is set");
180
- }
181
- if (!finalParams.RequestItems[tableName]) {
182
- finalParams.RequestItems[tableName] = { Keys: [] };
183
- }
184
- const key = item.Key || item;
185
- finalParams.RequestItems[tableName].Keys.push(key);
186
- });
187
- delete finalParams.Items;
188
- }
189
- else if (!finalParams.RequestItems && this.defaultTableName) {
190
- finalParams.RequestItems = {};
191
- }
192
- const command = new lib_dynamodb_1.BatchGetCommand(finalParams);
193
- return yield this.docClient.send(command);
194
- });
195
- }
196
- /**
197
- * Put or delete multiple items in one or more tables.
198
- */
199
- batchWrite(params) {
200
- return __awaiter(this, void 0, void 0, function* () {
201
- if (!params.RequestItems && params.Items) {
202
- params.RequestItems = {};
203
- params.Items.forEach((item) => {
204
- const tableName = item.TableName || this.defaultTableName;
205
- if (!tableName) {
206
- throw new Error("TableName must be provided for batch operations if no default tableName is set");
207
- }
208
- if (!params.RequestItems[tableName]) {
209
- params.RequestItems[tableName] = [];
210
- }
211
- const request = item.PutRequest || item.DeleteRequest ? item : { PutRequest: { Item: item } };
212
- params.RequestItems[tableName].push(request);
213
- });
214
- delete params.Items;
215
- }
216
- else if (!params.RequestItems && this.defaultTableName) {
217
- params.RequestItems = {};
218
- }
219
- const command = new lib_dynamodb_1.BatchWriteCommand(params);
220
- return yield this.docClient.send(command);
221
- });
222
- }
223
151
  getTableName() {
224
152
  return this.defaultTableName;
225
153
  }
@@ -37,22 +37,6 @@ export declare class Partition {
37
37
  */
38
38
  get<T = any>(sk: string): Promise<T | null>;
39
39
  getPkValue(): string;
40
- /**
41
- * Generates items for batch query.
42
- * If no SKs are provided, it might not be very useful for batchGet (which requires full keys),
43
- * but the requirement says "will get all by pkValue" if no sk defined.
44
- * Actually, BatchGetItem requires both PK and SK if the table has both.
45
- * If it's for IndexQuery, it might be different.
46
- */
47
- batchGetInput(...sks: string[]): any[];
48
- /**
49
- * Generates items for batch write (put).
50
- */
51
- batchWriteInput(...items: any[]): any[];
52
- /**
53
- * Generates items for batch delete.
54
- */
55
- batchDeleteInput(...sks: string[]): any[];
56
40
  /**
57
41
  * Delete all data in this partition.
58
42
  */
package/dist/partition.js CHANGED
@@ -144,53 +144,6 @@ class Partition {
144
144
  getPkValue() {
145
145
  return this.pkValue;
146
146
  }
147
- /**
148
- * Generates items for batch query.
149
- * If no SKs are provided, it might not be very useful for batchGet (which requires full keys),
150
- * but the requirement says "will get all by pkValue" if no sk defined.
151
- * Actually, BatchGetItem requires both PK and SK if the table has both.
152
- * If it's for IndexQuery, it might be different.
153
- */
154
- batchGetInput(...sks) {
155
- if (sks.length === 0) {
156
- return [{
157
- TableName: this.tableName,
158
- Key: { [this.pkName]: this.pkValue }
159
- }];
160
- }
161
- return sks.map(sk => ({
162
- TableName: this.tableName,
163
- Key: {
164
- [this.pkName]: this.pkValue,
165
- [this.skName]: sk
166
- }
167
- }));
168
- }
169
- /**
170
- * Generates items for batch write (put).
171
- */
172
- batchWriteInput(...items) {
173
- return items.map(item => ({
174
- TableName: this.tableName,
175
- PutRequest: {
176
- Item: Object.assign({ [this.pkName]: this.pkValue }, item)
177
- }
178
- }));
179
- }
180
- /**
181
- * Generates items for batch delete.
182
- */
183
- batchDeleteInput(...sks) {
184
- return sks.map(sk => ({
185
- TableName: this.tableName,
186
- DeleteRequest: {
187
- Key: {
188
- [this.pkName]: this.pkValue,
189
- [this.skName]: sk
190
- }
191
- }
192
- }));
193
- }
194
147
  /**
195
148
  * Delete all data in this partition.
196
149
  */
@@ -207,24 +160,12 @@ class Partition {
207
160
  },
208
161
  });
209
162
  if (response.Items && response.Items.length > 0) {
210
- // DynamoDB BatchWriteItem supports up to 25 requests at once
211
- const items = response.Items;
212
- const chunks = [];
213
- for (let i = 0; i < items.length; i += 25) {
214
- chunks.push(items.slice(i, i + 25));
215
- }
216
- for (const chunk of chunks) {
217
- const deleteRequests = chunk.map((item) => ({
218
- DeleteRequest: {
219
- Key: {
220
- [this.pkName]: item[this.pkName],
221
- [this.skName]: item[this.skName],
222
- },
223
- },
224
- }));
225
- yield this.db.batchWrite({
226
- RequestItems: {
227
- [this.tableName]: deleteRequests,
163
+ for (const item of response.Items) {
164
+ yield this.db.delete({
165
+ TableName: this.tableName,
166
+ Key: {
167
+ [this.pkName]: item[this.pkName],
168
+ [this.skName]: item[this.skName],
228
169
  },
229
170
  });
230
171
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dynoquery",
3
- "version": "0.1.7",
3
+ "version": "0.1.9",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/devspikejs/dynoquery.git"