-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.ts
340 lines (331 loc) · 10.2 KB
/
functions.ts
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
import * as log from "./dependencies/logger.ts";
import {
DatastoreItem,
DatastoreQueryArgs,
DatastoreSchema,
} from "./dependencies/deno_slack_api_typed_method_types.ts";
import { DatastoreError } from "./errors.ts";
import {
BulkDeleteResponse,
BulkGetResponse,
CountResponse,
Definition,
DeleteResponse,
FindFirstRawExpressionQueryArgs,
GetResponse,
IdQueryArgs,
IdsQueryArgs,
PutResponse,
QueryResponse,
RawExpressionQueryArgs,
SaveArgs,
} from "./types.ts";
import { RawExpressionCountArgs } from "./types.ts";
import { DatastoreCountArgs } from "./dependencies/deno_slack_api_typed_method_types.ts";
export const defaultLogger = log.getLogger();
const logName = "[deno_slack_data_mapper]";
export async function save<Def extends Definition>({
client,
datastore,
primaryKey,
attributes,
logger,
}: SaveArgs<Def>): Promise<PutResponse<Def>> {
const _logger = logger ?? defaultLogger;
if (_logger.level === log.LogLevels.DEBUG) {
const data = JSON.stringify(attributes);
_logger.debug(
`${logName} Saving a record: (datastore: ${datastore}, attributes: ${data})`,
);
}
const item: DatastoreItem<DatastoreSchema> = {
...attributes,
};
const pkName = primaryKey ?? "id";
item[pkName] = attributes[pkName] ?? crypto.randomUUID();
const result = await client.apps.datastore.put({ datastore, item });
if (_logger.level === log.LogLevels.DEBUG) {
const response = JSON.stringify(result);
_logger.debug(
`${logName} Saved result: (datastore: ${datastore}, response: ${response})`,
);
}
if (result.error) {
const error = `Failed to save a row due to ${result.error}`;
throw new DatastoreError(error, result);
}
return result as PutResponse<Def>;
}
export async function findById<Def extends Definition>({
client,
datastore,
id,
logger,
}: IdQueryArgs): Promise<GetResponse<Def>> {
const _logger = logger ?? defaultLogger;
if (_logger.level === log.LogLevels.DEBUG) {
_logger.debug(
`${logName} Finding a record (datastore: ${datastore}, PK: ${id})`,
);
}
const result = await client.apps.datastore.get({ datastore, id });
if (_logger.level === log.LogLevels.DEBUG) {
const response = JSON.stringify(result);
_logger.debug(
`${logName} Found: (datastore: ${datastore}, response: ${response})`,
);
}
if (result.error) {
const error =
`Failed to fetch a row (datastore: ${datastore}, error ${result.error})`;
throw new DatastoreError(error, result);
}
return result as GetResponse<Def>;
}
export async function findAllByIds<Def extends Definition>({
client,
datastore,
ids,
logger,
}: IdsQueryArgs): Promise<BulkGetResponse<Def>> {
const _logger = logger ?? defaultLogger;
if (_logger.level === log.LogLevels.DEBUG) {
_logger.debug(
`${logName} Finding records (datastore: ${datastore}, IDs: ${ids})`,
);
}
const result = await client.apps.datastore.bulkGet({ datastore, ids });
if (_logger.level === log.LogLevels.DEBUG) {
const response = JSON.stringify(result);
_logger.debug(
`${logName} Found: (datastore: ${datastore}, response: ${response})`,
);
}
if (result.error) {
const error =
`Failed to fetch rows (datastore: ${datastore}, error ${result.error})`;
throw new DatastoreError(error, result);
}
return result as BulkGetResponse<Def>;
}
export async function findFirstBy<Def extends Definition>({
client,
datastore,
expression,
logger,
}: FindFirstRawExpressionQueryArgs): Promise<QueryResponse<Def>> {
const _logger = logger ?? defaultLogger;
if (_logger.level === log.LogLevels.DEBUG) {
const data = JSON.stringify(expression);
_logger.debug(
`${logName} Finding records: (datastore: ${datastore}, expression: ${data})`,
);
}
const results = await client.apps.datastore.query({
datastore,
expression: expression.expression,
expression_attributes: expression.attributes,
expression_values: expression.values,
limit: 1000,
});
if (_logger.level === log.LogLevels.DEBUG) {
const response = JSON.stringify(results);
_logger.debug(
`${logName} Found: (datastore: ${datastore}, response: ${response})`,
);
}
if (results.error) {
const error =
`Failed to fetch rows: (datastore: ${datastore}, error: ${results.error})`;
throw new DatastoreError(error, results);
}
if (results.items.length > 0) {
results.items = results.items.slice(0, 1);
return results as QueryResponse<Def>;
}
let nextCursor = results.response_metadata?.next_cursor;
while (nextCursor !== undefined && nextCursor !== "") {
const pageResults = await client.apps.datastore.query({
datastore,
expression: expression.expression,
expression_attributes: expression.attributes,
expression_values: expression.values,
cursor: nextCursor,
limit: 1000,
});
if (pageResults.items.length > 0) {
results.items = pageResults.items.slice(0, 1);
return results as QueryResponse<Def>;
}
nextCursor = pageResults.response_metadata?.next_cursor;
}
return results as QueryResponse<Def>;
}
export async function findAllBy<Def extends Definition>({
client,
datastore,
expression,
cursor,
limit,
logger,
autoPagination,
}: RawExpressionQueryArgs): Promise<QueryResponse<Def>> {
const _autoPagination = autoPagination === undefined || autoPagination;
const _logger = logger ?? defaultLogger;
const _limit = limit ?? 1000;
if (_logger.level === log.LogLevels.DEBUG) {
if (expression.expression) {
const data = JSON.stringify(expression);
_logger.debug(
`${logName} Finding records: (datastore: ${datastore}, expression: ${data})`,
);
} else {
_logger.debug(`${logName} Finding all records (datastore: ${datastore})`);
}
}
let queryArgs: DatastoreQueryArgs<Def> = {
datastore,
cursor,
limit: _autoPagination ? 1000 : _limit,
};
if (expression.expression && expression.expression !== "") {
queryArgs = {
datastore,
expression: expression.expression,
expression_attributes: expression.attributes,
expression_values: expression.values,
cursor,
limit: _autoPagination ? 1000 : _limit,
};
}
const results = await client.apps.datastore.query(queryArgs);
if (_logger.level === log.LogLevels.DEBUG) {
const response = JSON.stringify(results);
_logger.debug(
`${logName} Found: (datastore: ${datastore}, response: ${response})`,
);
}
if (results.error) {
const error = `Failed to fetch rows due to ${results.error}`;
throw new DatastoreError(error, results);
}
if (results.items.length > _limit) {
results.items = results.items.slice(0, _limit);
}
if (_autoPagination) {
let nextCursor = results.response_metadata?.next_cursor;
while (nextCursor !== undefined && nextCursor !== "") {
const pageResults = await client.apps.datastore.query({
datastore,
expression: expression.expression,
expression_attributes: expression.attributes,
expression_values: expression.values,
cursor: nextCursor,
limit: 1000,
});
for (const item of pageResults.items) {
if (results.items.length >= _limit) {
break;
}
results.items.push(item);
}
nextCursor = pageResults.response_metadata?.next_cursor;
}
}
return results as QueryResponse<Def>;
}
export async function countBy<Def extends Definition>({
client,
datastore,
expression,
logger,
}: RawExpressionCountArgs): Promise<CountResponse> {
const _logger = logger ?? defaultLogger;
if (_logger.level === log.LogLevels.DEBUG) {
if (expression.expression) {
const data = JSON.stringify(expression);
_logger.debug(
`${logName} Counting records: (datastore: ${datastore}, expression: ${data})`,
);
} else {
_logger.debug(
`${logName} Counting all records (datastore: ${datastore})`,
);
}
}
let queryArgs: DatastoreCountArgs<Def> = {
datastore,
};
if (expression.expression && expression.expression !== "") {
queryArgs = {
datastore,
expression: expression.expression,
expression_attributes: expression.attributes,
expression_values: expression.values,
};
}
const result = await client.apps.datastore.count(queryArgs);
if (_logger.level === log.LogLevels.DEBUG) {
const response = JSON.stringify(result);
_logger.debug(
`${logName} Found: (datastore: ${datastore}, response: ${response})`,
);
}
if (result.error) {
const error = `Failed to count rows due to ${result.error}`;
throw new DatastoreError(error, result);
}
return result;
}
export async function deleteById({
client,
datastore,
id,
logger,
}: IdQueryArgs): Promise<DeleteResponse> {
const _logger = logger ?? defaultLogger;
if (_logger.level === log.LogLevels.DEBUG) {
_logger.debug(
`${logName} Deleting a record (datastore: ${datastore}, PK: ${id})`,
);
}
const result = await client.apps.datastore.delete({ datastore, id });
if (_logger.level === log.LogLevels.DEBUG) {
const jsonData = JSON.stringify(result);
_logger.debug(
`${logName} Deletion result: (datastore: ${datastore}, response: ${jsonData})`,
);
}
if (result.error) {
const error =
`${logName} Failed to delete a row: (datastore: ${datastore}, error: ${result.error})`;
throw new DatastoreError(error, result);
}
return result;
}
export async function deleteAllByIds({
client,
datastore,
ids,
logger,
}: IdsQueryArgs): Promise<BulkDeleteResponse> {
const _logger = logger ?? defaultLogger;
if (_logger.level === log.LogLevels.DEBUG) {
_logger.debug(
`${logName} Deleting records (datastore: ${datastore}, IDs: ${ids})`,
);
}
const result = await client.apps.datastore.bulkDelete({ datastore, ids });
if (_logger.level === log.LogLevels.DEBUG) {
const jsonData = JSON.stringify(result);
_logger.debug(
`${logName} Deletion result: (datastore: ${datastore}, response: ${jsonData})`,
);
}
if (result.error) {
const error =
`${logName} Failed to delete rows: (datastore: ${datastore}, error: ${result.error})`;
throw new DatastoreError(error, result);
}
return result;
}