betterddb 0.4.7 → 0.4.8
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.
|
@@ -2,17 +2,11 @@ import { BetterDDB } from '../betterddb';
|
|
|
2
2
|
export declare class BatchGetBuilder<T> {
|
|
3
3
|
private parent;
|
|
4
4
|
private keys;
|
|
5
|
-
private projectionExpression?;
|
|
6
|
-
private expressionAttributeNames;
|
|
7
5
|
/**
|
|
8
6
|
* @param parent - The BetterDDB instance for the table.
|
|
9
7
|
* @param keys - An array of partial keys for the items you wish to retrieve.
|
|
10
8
|
*/
|
|
11
9
|
constructor(parent: BetterDDB<T>, keys: Partial<T>[]);
|
|
12
|
-
/**
|
|
13
|
-
* Specify a projection by providing an array of attribute names.
|
|
14
|
-
*/
|
|
15
|
-
withProjection(attributes: (keyof T)[]): this;
|
|
16
10
|
/**
|
|
17
11
|
* Executes the batch get operation.
|
|
18
12
|
* Returns an array of parsed items of type T.
|
|
@@ -10,17 +10,6 @@ class BatchGetBuilder {
|
|
|
10
10
|
constructor(parent, keys) {
|
|
11
11
|
this.parent = parent;
|
|
12
12
|
this.keys = keys;
|
|
13
|
-
this.expressionAttributeNames = {};
|
|
14
|
-
}
|
|
15
|
-
/**
|
|
16
|
-
* Specify a projection by providing an array of attribute names.
|
|
17
|
-
*/
|
|
18
|
-
withProjection(attributes) {
|
|
19
|
-
this.projectionExpression = attributes.map(attr => `#${String(attr)}`).join(', ');
|
|
20
|
-
for (const attr of attributes) {
|
|
21
|
-
this.expressionAttributeNames[`#${String(attr)}`] = String(attr);
|
|
22
|
-
}
|
|
23
|
-
return this;
|
|
24
13
|
}
|
|
25
14
|
/**
|
|
26
15
|
* Executes the batch get operation.
|
|
@@ -35,10 +24,6 @@ class BatchGetBuilder {
|
|
|
35
24
|
RequestItems: {
|
|
36
25
|
[tableName]: {
|
|
37
26
|
Keys: keysArray,
|
|
38
|
-
...(this.projectionExpression && {
|
|
39
|
-
ProjectionExpression: this.projectionExpression,
|
|
40
|
-
ExpressionAttributeNames: this.expressionAttributeNames,
|
|
41
|
-
}),
|
|
42
27
|
},
|
|
43
28
|
},
|
|
44
29
|
};
|
package/package.json
CHANGED
|
@@ -3,26 +3,12 @@ import { BatchGetCommand } from '@aws-sdk/lib-dynamodb';
|
|
|
3
3
|
import { BatchGetItemInput } from '@aws-sdk/client-dynamodb';
|
|
4
4
|
|
|
5
5
|
export class BatchGetBuilder<T> {
|
|
6
|
-
private projectionExpression?: string;
|
|
7
|
-
private expressionAttributeNames: Record<string, string> = {};
|
|
8
|
-
|
|
9
6
|
/**
|
|
10
7
|
* @param parent - The BetterDDB instance for the table.
|
|
11
8
|
* @param keys - An array of partial keys for the items you wish to retrieve.
|
|
12
9
|
*/
|
|
13
10
|
constructor(private parent: BetterDDB<T>, private keys: Partial<T>[]) {}
|
|
14
11
|
|
|
15
|
-
/**
|
|
16
|
-
* Specify a projection by providing an array of attribute names.
|
|
17
|
-
*/
|
|
18
|
-
public withProjection(attributes: (keyof T)[]): this {
|
|
19
|
-
this.projectionExpression = attributes.map(attr => `#${String(attr)}`).join(', ');
|
|
20
|
-
for (const attr of attributes) {
|
|
21
|
-
this.expressionAttributeNames[`#${String(attr)}`] = String(attr);
|
|
22
|
-
}
|
|
23
|
-
return this;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
12
|
/**
|
|
27
13
|
* Executes the batch get operation.
|
|
28
14
|
* Returns an array of parsed items of type T.
|
|
@@ -37,10 +23,6 @@ export class BatchGetBuilder<T> {
|
|
|
37
23
|
RequestItems: {
|
|
38
24
|
[tableName]: {
|
|
39
25
|
Keys: keysArray,
|
|
40
|
-
...(this.projectionExpression && {
|
|
41
|
-
ProjectionExpression: this.projectionExpression,
|
|
42
|
-
ExpressionAttributeNames: this.expressionAttributeNames,
|
|
43
|
-
}),
|
|
44
26
|
},
|
|
45
27
|
},
|
|
46
28
|
};
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { BetterDDB } from '../src/betterddb';
|
|
3
|
+
import { createTestTable, deleteTestTable } from './utils/table-setup';
|
|
4
|
+
import { DynamoDBDocumentClient } from '@aws-sdk/lib-dynamodb';
|
|
5
|
+
import { DynamoDB } from '@aws-sdk/client-dynamodb';
|
|
6
|
+
import { KeySchemaElement, AttributeDefinition } from '@aws-sdk/client-dynamodb';
|
|
7
|
+
const TEST_TABLE = "batch-get-test-table";
|
|
8
|
+
const ENDPOINT = 'http://localhost:4566';
|
|
9
|
+
const REGION = 'us-east-1';
|
|
10
|
+
const ENTITY_NAME = 'USER';
|
|
11
|
+
const PRIMARY_KEY = 'id';
|
|
12
|
+
const PRIMARY_KEY_TYPE = 'S';
|
|
13
|
+
const SORT_KEY = 'email';
|
|
14
|
+
const SORT_KEY_TYPE = 'S';
|
|
15
|
+
const KEY_SCHEMA = [{ AttributeName: PRIMARY_KEY, KeyType: 'HASH' }, { AttributeName: SORT_KEY, KeyType: 'RANGE' }] as KeySchemaElement[];
|
|
16
|
+
const ATTRIBUTE_DEFINITIONS = [{ AttributeName: PRIMARY_KEY, AttributeType: PRIMARY_KEY_TYPE }, { AttributeName: SORT_KEY, AttributeType: SORT_KEY_TYPE }] as AttributeDefinition[];
|
|
17
|
+
const client = DynamoDBDocumentClient.from(new DynamoDB({
|
|
18
|
+
region: REGION,
|
|
19
|
+
endpoint: ENDPOINT,
|
|
20
|
+
}));
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
const UserSchema = z.object({
|
|
24
|
+
id: z.string(),
|
|
25
|
+
name: z.string(),
|
|
26
|
+
email: z.string().email(),
|
|
27
|
+
createdAt: z.string(),
|
|
28
|
+
updatedAt: z.string(),
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
type User = z.infer<typeof UserSchema>;
|
|
32
|
+
|
|
33
|
+
const userDdb = new BetterDDB({
|
|
34
|
+
schema: UserSchema,
|
|
35
|
+
tableName: TEST_TABLE,
|
|
36
|
+
entityName: ENTITY_NAME,
|
|
37
|
+
keys: {
|
|
38
|
+
primary: { name: PRIMARY_KEY, definition: { build: (raw) => raw.id! } },
|
|
39
|
+
sort: { name: SORT_KEY, definition: { build: (raw) => raw.email! } },
|
|
40
|
+
},
|
|
41
|
+
client,
|
|
42
|
+
autoTimestamps: true,
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
beforeAll(async () => {
|
|
46
|
+
await createTestTable(TEST_TABLE, KEY_SCHEMA, ATTRIBUTE_DEFINITIONS);
|
|
47
|
+
await userDdb.create({ id: 'user-123', name: 'John Doe', email: 'john@example.com' } as any).execute();
|
|
48
|
+
await userDdb.create({ id: 'user-124', name: 'John Doe', email: 'john@example.com' } as any).execute();
|
|
49
|
+
await userDdb.create({ id: 'user-125', name: 'Bob Doe', email: 'bob@example.com' } as any).execute();
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
afterAll(async () => {
|
|
53
|
+
await deleteTestTable(TEST_TABLE);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
describe('BetterDDB - Get Operation', () => {
|
|
57
|
+
it('should retrieve an item using GetBuilder', async () => {
|
|
58
|
+
const users = await userDdb.batchGet([{ id: 'user-123', email: 'john@example.com' }, { id: 'user-124', email: 'john@example.com' }]).execute();
|
|
59
|
+
expect(users.length).toEqual(2);
|
|
60
|
+
expect(users[0].id).toBe('user-123');
|
|
61
|
+
expect(users[1].id).toBe('user-124');
|
|
62
|
+
});
|
|
63
|
+
});
|