|
1 | 1 | import { DynamoDB } from 'aws-sdk'
|
2 | 2 | import { LoggerFunction, DDBType } from '../types'
|
3 | 3 |
|
4 |
| -export interface DDBClient<T extends DDBType> { |
5 |
| - get: (id: string) => Promise<T|null> |
6 |
| - put: (Item: T) => Promise<T> |
7 |
| - update: (id: string, obj: Partial<T>) => Promise<T> |
8 |
| - delete: (id: string) => Promise<T> |
| 4 | +export interface DDBClient<T extends DDBType, TKey> { |
| 5 | + get: (Key: TKey) => Promise<T|null> |
| 6 | + put: (obj: T) => Promise<T> |
| 7 | + update: (Key: TKey, obj: Partial<T>) => Promise<T> |
| 8 | + delete: (Key: TKey) => Promise<T> |
9 | 9 | query: (options: Omit<DynamoDB.DocumentClient.QueryInput, 'TableName' | 'Select'>) => AsyncGenerator<T, void, undefined>
|
10 | 10 | }
|
11 | 11 |
|
12 |
| -export const DDB = <T extends DDBType>({ |
| 12 | +export const DDB = <T extends DDBType, TKey>({ |
13 | 13 | dynamodb,
|
14 | 14 | tableName,
|
15 | 15 | log,
|
16 | 16 | }: {
|
17 | 17 | dynamodb: DynamoDB
|
18 | 18 | tableName: string
|
19 | 19 | log: LoggerFunction
|
20 |
| -}): DDBClient<T> => { |
| 20 | +}): DDBClient<T, TKey> => { |
21 | 21 | const documentClient = new DynamoDB.DocumentClient({ service: dynamodb })
|
22 | 22 |
|
23 |
| - const get = async (id: string): Promise<null | T> => { |
24 |
| - log('get', { tableName: tableName, id }) |
25 |
| - const { Item } = await documentClient.get({ |
26 |
| - TableName: tableName, |
27 |
| - Key: { id }, |
28 |
| - }).promise() |
29 |
| - return (Item as T) ?? null |
| 23 | + const get = async (Key: TKey): Promise<null | T> => { |
| 24 | + log('get', { tableName: tableName, Key }) |
| 25 | + try { |
| 26 | + const { Item } = await documentClient.get({ |
| 27 | + TableName: tableName, |
| 28 | + Key, |
| 29 | + }).promise() |
| 30 | + log('get:result', { Item }) |
| 31 | + return (Item as T) ?? null |
| 32 | + } catch (e) { |
| 33 | + log('get:error', e) |
| 34 | + throw e |
| 35 | + } |
30 | 36 | }
|
31 | 37 |
|
32 | 38 | const put = async (Item: T): Promise<T> => {
|
33 | 39 | log('put', { tableName: tableName, Item })
|
34 |
| - const { Attributes } = await documentClient.put({ |
35 |
| - TableName: tableName, |
36 |
| - Item, |
37 |
| - ReturnValues: 'ALL_OLD', |
38 |
| - }).promise() |
39 |
| - return Attributes as T |
| 40 | + try { |
| 41 | + const { Attributes } = await documentClient.put({ |
| 42 | + TableName: tableName, |
| 43 | + Item, |
| 44 | + ReturnValues: 'ALL_OLD', |
| 45 | + }).promise() |
| 46 | + return Attributes as T |
| 47 | + } catch (e) { |
| 48 | + log('put:error', e) |
| 49 | + throw e |
| 50 | + } |
40 | 51 | }
|
41 | 52 |
|
42 |
| - const update = async (id: string, obj: Partial<T>) => { |
43 |
| - const AttributeUpdates = Object.entries(obj) |
44 |
| - .map(([key, Value]) => ({ [key]: { Value, Action: 'PUT' } })) |
45 |
| - .reduce((memo, val) => ({ ...memo, ...val })) |
| 53 | + const update = async (Key: TKey, obj: Partial<T>) => { |
| 54 | + log('update', { tableName: tableName, Key, obj }) |
| 55 | + try { |
| 56 | + const AttributeUpdates = Object.entries(obj) |
| 57 | + .map(([key, Value]) => ({ [key]: { Value, Action: 'PUT' } })) |
| 58 | + .reduce((memo, val) => ({ ...memo, ...val })) |
46 | 59 |
|
47 |
| - const { Attributes } = await documentClient.update({ |
48 |
| - TableName: tableName, |
49 |
| - Key: { id }, |
50 |
| - AttributeUpdates, |
51 |
| - ReturnValues: 'ALL_NEW', |
52 |
| - }).promise() |
53 |
| - return Attributes as T |
| 60 | + const { Attributes } = await documentClient.update({ |
| 61 | + TableName: tableName, |
| 62 | + Key, |
| 63 | + AttributeUpdates, |
| 64 | + ReturnValues: 'ALL_NEW', |
| 65 | + }).promise() |
| 66 | + return Attributes as T |
| 67 | + } catch (e) { |
| 68 | + log('update:error', e) |
| 69 | + throw e |
| 70 | + } |
54 | 71 | }
|
55 | 72 |
|
56 |
| - const deleteFunction = async (id: string): Promise<T> => { |
57 |
| - const { Attributes } = await documentClient.delete({ |
58 |
| - TableName: tableName, |
59 |
| - Key: { id }, |
60 |
| - ReturnValues: 'ALL_OLD', |
61 |
| - }).promise() |
62 |
| - return Attributes as T |
| 73 | + const deleteFunction = async (Key: TKey): Promise<T> => { |
| 74 | + log('delete', { tableName: tableName, Key }) |
| 75 | + try { |
| 76 | + const { Attributes } = await documentClient.delete({ |
| 77 | + TableName: tableName, |
| 78 | + Key, |
| 79 | + ReturnValues: 'ALL_OLD', |
| 80 | + }).promise() |
| 81 | + return Attributes as T |
| 82 | + } catch (e) { |
| 83 | + log('delete:error', e) |
| 84 | + throw e |
| 85 | + } |
63 | 86 | }
|
64 | 87 |
|
65 | 88 | const queryOnce = async (options: Omit<DynamoDB.DocumentClient.QueryInput, 'TableName' | 'Select'>) => {
|
66 |
| - log('queryOnce', options) |
67 |
| - |
68 |
| - const response = await documentClient.query({ |
69 |
| - TableName: tableName, |
70 |
| - Select: 'ALL_ATTRIBUTES', |
71 |
| - ...options, |
72 |
| - }).promise() |
| 89 | + log('queryOnce', { tableName: tableName, options }) |
| 90 | + try { |
| 91 | + const response = await documentClient.query({ |
| 92 | + TableName: tableName, |
| 93 | + Select: 'ALL_ATTRIBUTES', |
| 94 | + ...options, |
| 95 | + }).promise() |
73 | 96 |
|
74 |
| - const { Items, LastEvaluatedKey, Count } = response |
75 |
| - return { |
76 |
| - items: (Items ?? []) as T[], |
77 |
| - lastEvaluatedKey: LastEvaluatedKey, |
78 |
| - count: Count ?? 0, |
| 97 | + const { Items, LastEvaluatedKey, Count } = response |
| 98 | + return { |
| 99 | + items: (Items ?? []) as T[], |
| 100 | + lastEvaluatedKey: LastEvaluatedKey, |
| 101 | + count: Count ?? 0, |
| 102 | + } |
| 103 | + } catch (e) { |
| 104 | + log('queryOnce:error', e) |
| 105 | + throw e |
79 | 106 | }
|
80 | 107 | }
|
81 | 108 |
|
82 | 109 | async function* query(options: Omit<DynamoDB.DocumentClient.QueryInput, 'TableName' | 'Select'>) {
|
83 |
| - log('query', options) |
84 |
| - const results = await queryOnce(options) |
85 |
| - yield* results.items |
86 |
| - let lastEvaluatedKey = results.lastEvaluatedKey |
87 |
| - while (lastEvaluatedKey) { |
88 |
| - const results = await queryOnce({ ...options, ExclusiveStartKey: lastEvaluatedKey }) |
| 110 | + log('query', { tableName: tableName, options }) |
| 111 | + try { |
| 112 | + const results = await queryOnce(options) |
89 | 113 | yield* results.items
|
90 |
| - lastEvaluatedKey = results.lastEvaluatedKey |
| 114 | + let lastEvaluatedKey = results.lastEvaluatedKey |
| 115 | + while (lastEvaluatedKey) { |
| 116 | + const results = await queryOnce({ ...options, ExclusiveStartKey: lastEvaluatedKey }) |
| 117 | + yield* results.items |
| 118 | + lastEvaluatedKey = results.lastEvaluatedKey |
| 119 | + } |
| 120 | + } catch (e) { |
| 121 | + log('query:error', e) |
| 122 | + throw e |
91 | 123 | }
|
92 | 124 | }
|
93 | 125 |
|
|
0 commit comments