forked from serverless-components/aws-dynamodb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
75 lines (67 loc) · 1.71 KB
/
utils.js
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
const { not, equals, pick } = require('ramda')
async function createTable({ dynamodb, name, attributeDefinitions, keySchema }) {
const res = await dynamodb
.createTable({
TableName: name,
AttributeDefinitions: attributeDefinitions,
KeySchema: keySchema,
BillingMode: 'PAY_PER_REQUEST'
})
.promise()
return res.TableDescription.TableArn
}
async function describeTable({ dynamodb, name }) {
let res
try {
const data = await dynamodb.describeTable({ TableName: name }).promise()
res = {
arn: data.Table.TableArn,
name: data.Table.TableName,
attributeDefinitions: data.Table.AttributeDefinitions,
keySchema: data.Table.KeySchema
}
} catch (error) {
if (error.code === 'ResourceNotFoundException') {
res = null
}
} finally {
return res
}
}
async function updateTable({ dynamodb, name, attributeDefinitions }) {
const res = await dynamodb
.updateTable({
TableName: name,
AttributeDefinitions: attributeDefinitions,
BillingMode: 'PAY_PER_REQUEST'
})
.promise()
return res.TableDescription.TableArn
}
async function deleteTable({ dynamodb, name }) {
let res = false
try {
res = await dynamodb
.deleteTable({
TableName: name
})
.promise()
} catch (error) {
if (error.code !== 'ResourceNotFoundException') {
throw error
}
}
return !!res
}
function configChanged(prevTable, table) {
const prevInputs = pick(['name', 'attributeDefinitions'], prevTable)
const inputs = pick(['name', 'attributeDefinitions'], table)
return not(equals(inputs, prevInputs))
}
module.exports = {
createTable,
describeTable,
updateTable,
deleteTable,
configChanged
}