betterddb 0.5.2 → 0.5.4

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.
@@ -16,9 +16,21 @@ class BatchGetBuilder {
16
16
  * Returns an array of parsed items of type T.
17
17
  */
18
18
  async execute() {
19
+ if (this.keys.length === 0) {
20
+ return [];
21
+ }
22
+ const seen = new Set();
23
+ const deduplicatedKeys = this.keys.filter((key) => {
24
+ const keyString = JSON.stringify(key);
25
+ if (seen.has(keyString)) {
26
+ return false;
27
+ }
28
+ seen.add(keyString);
29
+ return true;
30
+ });
19
31
  const tableName = this.parent.getTableName();
20
32
  // Build an array of keys using the parent's key builder.
21
- const keysArray = this.keys.map(key => this.parent.buildKey(key));
33
+ const keysArray = deduplicatedKeys.map(key => this.parent.buildKey(key));
22
34
  // Construct the BatchGet parameters.
23
35
  const params = {
24
36
  RequestItems: {
@@ -144,7 +144,6 @@ class QueryBuilder {
144
144
  this.expressionAttributeNames['#entity'] = 'entityType';
145
145
  this.expressionAttributeValues[':entity_value'] = this.parent.getEntityType();
146
146
  params.FilterExpression = this.filterConditions.join(' AND ');
147
- console.log(params);
148
147
  const result = await this.parent.getClient().send(new lib_dynamodb_1.QueryCommand(params));
149
148
  return { items: this.parent.getSchema().array().parse(result.Items), lastKey: (_c = result.LastEvaluatedKey) !== null && _c !== void 0 ? _c : undefined };
150
149
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "betterddb",
3
- "version": "0.5.2",
3
+ "version": "0.5.4",
4
4
  "description": "A definition-based DynamoDB wrapper library that provides a schema-driven and fully typesafe DAL.",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
@@ -14,9 +14,22 @@ export class BatchGetBuilder<T> {
14
14
  * Returns an array of parsed items of type T.
15
15
  */
16
16
  public async execute(): Promise<T[]> {
17
+ if (this.keys.length === 0) {
18
+ return [];
19
+ }
20
+
21
+ const seen = new Set();
22
+ const deduplicatedKeys = this.keys.filter((key) => {
23
+ const keyString = JSON.stringify(key);
24
+ if (seen.has(keyString)) {
25
+ return false;
26
+ }
27
+ seen.add(keyString);
28
+ return true;
29
+ });
17
30
  const tableName = this.parent.getTableName();
18
31
  // Build an array of keys using the parent's key builder.
19
- const keysArray = this.keys.map(key => this.parent.buildKey(key));
32
+ const keysArray = deduplicatedKeys.map(key => this.parent.buildKey(key));
20
33
 
21
34
  // Construct the BatchGet parameters.
22
35
  const params: BatchGetItemInput = {
@@ -168,7 +168,6 @@ export class QueryBuilder<T> {
168
168
  this.expressionAttributeValues[':entity_value'] = this.parent.getEntityType();
169
169
  params.FilterExpression = this.filterConditions.join(' AND ');
170
170
 
171
- console.log(params);
172
171
  const result = await this.parent.getClient().send(new QueryCommand(params));
173
172
  return {items: this.parent.getSchema().array().parse(result.Items) as T[], lastKey: result.LastEvaluatedKey ?? undefined};
174
173
  }
@@ -78,4 +78,20 @@ describe('BetterDDB - Get Operation', () => {
78
78
  expect(users.some(user => user.id === 'user-123')).toBe(true);
79
79
  expect(users.some(user => user.id === 'user-124')).toBe(true);
80
80
  });
81
+
82
+ it('should retrieve an item using GetBuilder that does not exist', async () => {
83
+ const users = await userDdb.batchGet([{ id: 'user-123', email: 'jane@example.com' }]).execute();
84
+ expect(users.length).toEqual(0);
85
+ });
86
+
87
+ it('should return an empty array if no keys are provided', async () => {
88
+ const users = await userDdb.batchGet([]).execute();
89
+ expect(users.length).toEqual(0);
90
+ });
91
+
92
+ it('should deduplicate keys', async () => {
93
+ const users = await userDdb.batchGet([{ id: 'user-123', email: 'john@example.com' }, { id: 'user-123', email: 'john@example.com' }]).execute();
94
+ expect(users.length).toEqual(1);
95
+ expect(users[0].id).toEqual('user-123');
96
+ });
81
97
  });
package/test/get.test.ts CHANGED
@@ -77,4 +77,9 @@ describe('BetterDDB - Get Operation', () => {
77
77
  expect(user).not.toBeNull();
78
78
  expect(user?.id).toBe('user-123');
79
79
  });
80
+
81
+ it('should retrieve an item using GetBuilder that does not exist', async () => {
82
+ const user = await userDdb.get({ id: 'user-123', email: 'jane@example.com' }).execute();
83
+ expect(user).toBeNull();
84
+ });
80
85
  });
@@ -179,4 +179,14 @@ describe('BetterDDB - Query Operation', () => {
179
179
  expect(result.name).toContain('B');
180
180
  });
181
181
  });
182
+
183
+ it('should query items using QueryBuilder with where clause on an index', async () => {
184
+ // Query the GSI for email "alice@example.com". Two items match.
185
+ // Then apply two filter conditions: name begins_with "Alice" and name contains "B" should only match one.
186
+ const results = await userDdb.query({ email: 'alice@example.com' })
187
+ .usingIndex('EmailIndex')
188
+ .where('begins_with', { email: 'alice' })
189
+ .execute();
190
+ expect(results.items.length).toEqual(2);
191
+ });
182
192
  });