This repository was archived by the owner on May 17, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathDDB.ts
More file actions
136 lines (124 loc) · 3.6 KB
/
DDB.ts
File metadata and controls
136 lines (124 loc) · 3.6 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import { DynamoDBDocument, PutCommandInput, QueryCommandInput } from '@aws-sdk/lib-dynamodb'
import { DynamoDB } from '@aws-sdk/client-dynamodb'
import { LoggerFunction, DDBType } from '../types'
export interface DDBClient<T extends DDBType, TKey> {
get: (Key: TKey) => Promise<T | null>
put: (obj: T, putOptions?: Partial<PutCommandInput>) => Promise<T>
update: (Key: TKey, obj: Partial<T>) => Promise<T>
delete: (Key: TKey) => Promise<T>
query: (options: Omit<QueryCommandInput, 'TableName' | 'Select'>) => AsyncGenerator<T, void, undefined>
}
export const DDB = <T extends DDBType, TKey>({
dynamodb,
tableName,
log,
}: {
dynamodb: DynamoDB
tableName: string
log: LoggerFunction
}): DDBClient<T, TKey> => {
const documentClient = DynamoDBDocument.from(dynamodb)
const get = async (Key: TKey): Promise<null | T> => {
log('get', { tableName: tableName, Key })
try {
const { Item } = await documentClient.get({
TableName: tableName,
Key,
})
log('get:result', { Item })
return (Item as T) ?? null
} catch (e) {
log('get:error', e)
throw e
}
}
const put = async (Item: T, putOptions?: Partial<PutCommandInput>): Promise<T> => {
log('put', { tableName: tableName, Item })
try {
const { Attributes } = await documentClient.put({
TableName: tableName,
Item,
ReturnValues: 'ALL_OLD',
...putOptions,
})
return Attributes as T
} catch (e) {
log('put:error', e)
throw e
}
}
const update = async (Key: TKey, obj: Partial<T>) => {
log('update', { tableName: tableName, Key, obj })
try {
const AttributeUpdates = Object.entries(obj)
.map(([key, Value]) => ({ [key]: { Value } }))
.reduce((memo, val) => ({ ...memo, ...val }))
const { Attributes } = await documentClient.update({
TableName: tableName,
Key,
ReturnValues: 'ALL_NEW',
AttributeUpdates,
})
return Attributes as T
} catch (e) {
log('update:error', e)
throw e
}
}
const deleteFunction = async (Key: TKey): Promise<T> => {
log('delete', { tableName: tableName, Key })
try {
const { Attributes } = await documentClient.delete({
TableName: tableName,
Key,
ReturnValues: 'ALL_OLD',
})
return Attributes as T
} catch (e) {
log('delete:error', e)
throw e
}
}
const queryOnce = async (options: Omit<QueryCommandInput, 'TableName' | 'Select'>) => {
log('queryOnce', { tableName: tableName, options })
try {
const response = await documentClient.query({
TableName: tableName,
Select: 'ALL_ATTRIBUTES',
...options,
})
const { Items, LastEvaluatedKey, Count } = response
return {
items: (Items ?? []) as T[],
lastEvaluatedKey: LastEvaluatedKey,
count: Count ?? 0,
}
} catch (e) {
log('queryOnce:error', e)
throw e
}
}
async function* query(options: Omit<QueryCommandInput, 'TableName' | 'Select'>) {
log('query', { tableName: tableName, options })
try {
const results = await queryOnce(options)
yield* results.items
let lastEvaluatedKey = results.lastEvaluatedKey
while (lastEvaluatedKey) {
const results = await queryOnce({ ...options, ExclusiveStartKey: lastEvaluatedKey })
yield* results.items
lastEvaluatedKey = results.lastEvaluatedKey
}
} catch (e) {
log('query:error', e)
throw e
}
}
return {
get,
put,
update,
query,
delete: deleteFunction,
}
}