-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdynamo-db.ts
More file actions
201 lines (183 loc) · 6.12 KB
/
dynamo-db.ts
File metadata and controls
201 lines (183 loc) · 6.12 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// oxlint-disable id-length
import * as Dynamo from "@aws-sdk/client-dynamodb";
import {
DatabaseAdapterErrors,
type DatabaseAdapter,
type DatabaseAdapterOptions,
type DatabaseDocumentListOptions,
type StoryBookerDatabaseDocument,
} from "@storybooker/core/adapter";
export class AwsDynamoDatabaseService implements DatabaseAdapter {
#client: Dynamo.DynamoDBClient;
constructor(client: Dynamo.DynamoDBClient) {
this.#client = client;
}
metadata: DatabaseAdapter["metadata"] = { name: "AWS DynamoDB" };
listCollections: DatabaseAdapter["listCollections"] = async (options) => {
const response = await this.#client.send(new Dynamo.ListTablesCommand({}), {
abortSignal: options.abortSignal,
});
return response.TableNames ?? [];
};
createCollection: DatabaseAdapter["createCollection"] = async (collectionId, options) => {
try {
await this.#client.send(
new Dynamo.CreateTableCommand({
AttributeDefinitions: [{ AttributeName: "id", AttributeType: "S" }],
BillingMode: "PAY_PER_REQUEST",
KeySchema: [{ AttributeName: "id", KeyType: "HASH" }],
TableName: collectionId,
}),
{ abortSignal: options.abortSignal },
);
} catch (error) {
throw new DatabaseAdapterErrors.CollectionAlreadyExistsError(collectionId, error);
}
};
hasCollection: DatabaseAdapter["hasCollection"] = async (collectionId, options) => {
try {
const response = await this.#client.send(
new Dynamo.DescribeTableCommand({ TableName: collectionId }),
{ abortSignal: options.abortSignal },
);
return !!response.Table;
} catch {
return false;
}
};
deleteCollection: DatabaseAdapter["deleteCollection"] = async (collectionId, options) => {
try {
await this.#client.send(new Dynamo.DeleteTableCommand({ TableName: collectionId }), {
abortSignal: options.abortSignal,
});
} catch (error) {
throw new DatabaseAdapterErrors.CollectionDoesNotExistError(collectionId, error);
}
};
listDocuments: DatabaseAdapter["listDocuments"] = async <
Document extends StoryBookerDatabaseDocument,
>(
collectionId: string,
_listOptions: DatabaseDocumentListOptions<Document>,
options: DatabaseAdapterOptions,
) => {
const response = await this.#client.send(new Dynamo.ScanCommand({ TableName: collectionId }), {
abortSignal: options.abortSignal,
});
return (response.Items ?? []).map((item) => {
const doc: Record<string, unknown> = {};
for (const [key, value] of Object.entries(item)) {
doc[key] = value.S ?? value.N ?? value.BOOL ?? value.NULL ?? value;
}
return doc as Document;
});
};
getDocument: DatabaseAdapter["getDocument"] = async <
Document extends StoryBookerDatabaseDocument,
>(
collectionId: string,
documentId: string,
options: DatabaseAdapterOptions,
): Promise<Document> => {
try {
const response = await this.#client.send(
new Dynamo.GetItemCommand({
Key: { id: { S: documentId } },
TableName: collectionId,
}),
{ abortSignal: options.abortSignal },
);
const document = response.Item
? (Object.fromEntries(
Object.entries(response.Item).map(([key, value]) => [
key,
value.S ?? value.N ?? value.BOOL ?? value.NULL ?? value,
]),
) as Record<string, unknown>)
: undefined;
if (!document) {
throw new Error("Document not found");
}
document["id"] = documentId;
return document as Document;
} catch (error) {
throw new DatabaseAdapterErrors.DocumentDoesNotExistError(collectionId, documentId, error);
}
};
createDocument: DatabaseAdapter["createDocument"] = async (
collectionId,
documentData,
options,
) => {
try {
await this.#client.send(
new Dynamo.PutItemCommand({
Item: Object.fromEntries(
Object.entries(documentData).map(([key, value]) => [key, { S: String(value) }]),
),
TableName: collectionId,
}),
{ abortSignal: options.abortSignal },
);
} catch (error) {
throw new DatabaseAdapterErrors.DocumentAlreadyExistsError(
collectionId,
documentData.id,
error,
);
}
};
hasDocument: DatabaseAdapter["hasDocument"] = async (collectionId, documentId, options) => {
const response = await this.#client.send(
new Dynamo.GetItemCommand({
Key: { id: { S: documentId } },
TableName: collectionId,
}),
{ abortSignal: options.abortSignal },
);
return !!response.Item;
};
deleteDocument: DatabaseAdapter["deleteDocument"] = async (collectionId, documentId, options) => {
try {
await this.#client.send(
new Dynamo.DeleteItemCommand({
Key: { id: { S: documentId } },
TableName: collectionId,
}),
{ abortSignal: options.abortSignal },
);
} catch (error) {
throw new DatabaseAdapterErrors.DocumentDoesNotExistError(collectionId, documentId, error);
}
};
// oxlint-disable-next-line max-params
updateDocument: DatabaseAdapter["updateDocument"] = async (
collectionId,
documentId,
documentData,
options,
) => {
const updateExpr: string[] = [];
const exprAttrValues: Record<string, Dynamo.AttributeValue> = {};
for (const [key, value] of Object.entries(documentData)) {
updateExpr.push(`#${key} = :${key}`);
exprAttrValues[`:${key}`] = { S: String(value) };
}
try {
await this.#client.send(
new Dynamo.UpdateItemCommand({
ExpressionAttributeNames: Object.fromEntries(
Object.keys(documentData).map((k) => [`#${k}`, k]),
),
ExpressionAttributeValues: exprAttrValues,
Key: { id: { S: documentId } },
TableName: collectionId,
UpdateExpression: `SET ${updateExpr.join(", ")}`,
}),
{ abortSignal: options.abortSignal },
);
} catch (error) {
throw new DatabaseAdapterErrors.DocumentDoesNotExistError(collectionId, documentId, error);
}
};
}