-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdestroy.js
More file actions
32 lines (31 loc) · 773 Bytes
/
destroy.js
File metadata and controls
32 lines (31 loc) · 773 Bytes
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
import {
createClient,
createKey,
getKey,
getTableName,
unfmt,
} from "./deps.ts";
/** destroy record(s) */
export async function destroy(params) {
// destroy batch
if (Array.isArray(params)) {
let TableName = await getTableName();
let req = (Key) => ({ DeleteRequest: { Key } });
let batch = params.map(getKey).map(req);
let query = { RequestItems: {} };
query.RequestItems[TableName] = batch;
await createClient().batchWriteItem(query);
return;
}
// destroy one
if (params.table && params.key) {
let [TableName, Key] = await Promise.all([
getTableName(),
getKey(params),
]);
await createClient().deleteItem({ TableName, Key });
return;
}
// destroy fail
throw Error("destroy_invalid");
}