betterddb 0.5.3 → 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: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "betterddb",
3
- "version": "0.5.3",
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 = {
@@ -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
  });