-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasicExample.ts
More file actions
67 lines (59 loc) · 1.91 KB
/
Copy pathbasicExample.ts
File metadata and controls
67 lines (59 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import { DocumentClient } from "aws-sdk/clients/dynamodb";
import { TableFromValue, Table, TypesafeDocumentClientv2, TypesafeDocumentClientRawv2 } from "ts-dynamodb";
// Step 1: define your table types
type DBUser = {
userID: string;
username: string;
email: string;
name: string;
created: number;
updated: number;
};
// Step 2: create a readonly table object
// satisfies is not necessary, but may provide some type hints if adding indices
// Make sure to add `as const` to tables.
export const UserTable = {
name: `users`
} as const satisfies TableFromValue;
// Step 3: create a table type
// the Table type takes 3 (or 4) arguments:
// 1. a TableFromValue
// 2. a union of types in the table
// 3. the primary key
// 4. optionally, sort key
export type UserTableType = Table<typeof UserTable, DBUser, 'userID'>;
// Step 4: create the client
// the client takes one type argument:
// 1. a union of table types (one client can handle all of your tables!)
export const tsDdb = new TypesafeDocumentClientv2<UserTableType>(
new DocumentClient({ region: 'us-east-1' })
);
// Use it as you normally would (but without having to call `.promise()`)
const { Item: userDetails } = await tsDdb.get({
TableName: UserTable.name, // 'users'
Key: {
userID: '123' // string
},
ProjectionExpression: 'username, email, name'
});
/**
typeof userDetails = {
username: string;
email: string;
name: string;
} | undefined
*/
// To use the raw client instead, replace Step 4 with the following:
// assert the DocumentClient as TypesafeDocumentClientRawv2, passing a union of table types as the only argument
export const tsDdbRaw = new DocumentClient({
region: 'us-east-1'
}) as TypesafeDocumentClientRawv2<UserTableType>;
const { Item: user } = await tsDdbRaw.get({
TableName: UserTable.name, // 'users'
Key: {
userID: '123' // string
},
}).promise();
/**
typeof user = TSDdbSet<DBUser, false> | undefined
*/