-
Notifications
You must be signed in to change notification settings - Fork 181
/
index.js
89 lines (78 loc) · 3.26 KB
/
index.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import { get, keys, or, pick, resolvable, equals, not } from '@serverless/utils'
import { createTable, updateTable, deleteTable, ensureTable, updateTimeToLive } from './utils'
const AwsDynamoDb = (SuperClass) =>
class extends SuperClass {
async construct(inputs, context) {
await super.construct(inputs, context)
this.tableName = resolvable(() => or(inputs.tableName, `table-${this.instanceId}`))
this.provider = resolvable(() => or(inputs.provider, context.get('provider')))
}
shouldDeploy(prevInstance) {
const inputs = {
attributeDefinitions: this.attributeDefinitions,
provisionedThroughput: this.provisionedThroughput,
globalSecondaryIndexes: this.globalSecondaryIndexes,
sseSpecification: this.sseSpecification,
streamSpecification: this.streamSpecification,
timeToLiveSpecification: this.timeToLiveSpecification
}
const prevInputs = prevInstance ? pick(keys(inputs), prevInstance) : {}
const configChanged = not(equals(inputs, prevInputs))
if (!prevInstance || configChanged) {
return 'deploy'
} else if (
prevInstance.tableName !== this.tableName ||
not(equals(prevInstance.keySchema, this.keySchema))
) {
return 'replace'
}
}
async deploy(prevInstance, context) {
const tableName = get('tableName', this)
if (
prevInstance &&
(not(equals(prevInstance.attributeDefinitions, this.attributeDefinitions)) ||
not(equals(prevInstance.provisionedThroughput, this.provisionedThroughput)) ||
not(equals(prevInstance.globalSecondaryIndexes, this.globalSecondaryIndexes)) ||
not(equals(prevInstance.sseSpecification, this.sseSpecification)) ||
not(equals(prevInstance.streamSpecification, this.streamSpecification)))
) {
context.log(`Updating table: '${tableName}'`)
if (not(equals(prevInstance.globalSecondaryIndexes, this.globalSecondaryIndexes))) {
context.log(
`Skipping GlobalSecondaryIndex updates for table '${tableName}' (currently not supported)`
)
}
await ensureTable(updateTable, this)
context.log(`Table updated: '${tableName}'`)
} else if (
prevInstance &&
not(equals(prevInstance.timeToLiveSpecification, this.timeToLiveSpecification))
) {
context.log(`Updating time to live of the table: '${tableName}'`)
await updateTimeToLive(this)
context.log(`Time to live of the table updated: '${tableName}'`)
} else {
context.log(`Creating table: '${tableName}'`)
await ensureTable(createTable, this)
context.log(`Table created: '${tableName}'`)
if (this.timeToLiveSpecification) {
context.log(`Updating time to live of the table: '${tableName}'`)
await updateTimeToLive(this)
context.log(`Time to live of the table updated: '${tableName}'`)
}
}
}
async remove(context) {
context.log(`Removing table: '${this.tableName}'`)
await deleteTable(this)
}
async info() {
return {
title: this.name,
type: this.name,
data: pick(['name', 'license', 'version', 'tableName'], this)
}
}
}
export default AwsDynamoDb