|
1 | | -'use strict' |
| 1 | +'use strict'; |
2 | 2 |
|
3 | | -const { mergeDeepRight, pick } = require('ramda') |
| 3 | +const { mergeDeepRight, pick } = require('ramda'); |
4 | 4 | // eslint-disable-next-line import/no-extraneous-dependencies |
5 | | -const AWS = require('aws-sdk') |
| 5 | +const AWS = require('aws-sdk'); |
6 | 6 | // eslint-disable-next-line import/no-unresolved |
7 | | -const { Component } = require('@serverless/core') |
8 | | -const { log, createTable, deleteTable, describeTable, updateTable } = require('./utils') |
| 7 | +const { Component } = require('@serverless/core'); |
| 8 | +const { log, createTable, deleteTable, describeTable, updateTable } = require('./utils'); |
9 | 9 |
|
10 | | -const outputsList = ['name', 'arn', 'region'] |
| 10 | +const outputsList = ['name', 'arn', 'region']; |
11 | 11 |
|
12 | 12 | const defaults = { |
13 | 13 | attributeDefinitions: [ |
14 | 14 | { |
15 | 15 | AttributeName: 'id', |
16 | | - AttributeType: 'S' |
17 | | - } |
| 16 | + AttributeType: 'S', |
| 17 | + }, |
18 | 18 | ], |
19 | 19 | keySchema: [ |
20 | 20 | { |
21 | 21 | AttributeName: 'id', |
22 | | - KeyType: 'HASH' |
23 | | - } |
| 22 | + KeyType: 'HASH', |
| 23 | + }, |
24 | 24 | ], |
25 | 25 | globalSecondaryIndexes: [], |
26 | 26 | localSecondaryIndexes: [], |
27 | 27 | name: null, |
28 | 28 | region: 'us-east-1', |
29 | | - deletionPolicy: 'delete' |
30 | | -} |
| 29 | + deletionPolicy: 'delete', |
| 30 | +}; |
31 | 31 |
|
32 | 32 | class AwsDynamoDb extends Component { |
33 | 33 | async deploy(inputs = {}) { |
34 | 34 | // this error message assumes that the user is running via the CLI though... |
35 | 35 | if (Object.keys(this.credentials.aws).length === 0) { |
36 | 36 | const msg = |
37 | | - 'Credentials not found. Make sure you have a .env file in the cwd. - Docs: https://git.io/JvArp' |
38 | | - throw new Error(msg) |
| 37 | + 'Credentials not found. Make sure you have a .env file in the cwd. - Docs: https://git.io/JvArp'; |
| 38 | + throw new Error(msg); |
39 | 39 | } |
40 | 40 |
|
41 | | - const config = mergeDeepRight(defaults, inputs) |
42 | | - config.name = config.name || this.name |
| 41 | + const config = mergeDeepRight(defaults, inputs); |
| 42 | + config.name = config.name || this.name; |
43 | 43 |
|
44 | 44 | // If first deploy and no name is found, set default name.. |
45 | 45 | if (!config.name && !this.state.name) { |
46 | | - config.name = `dynamodb-table-${Math.random() |
47 | | - .toString(36) |
48 | | - .substring(6)}` |
49 | | - this.state.name = config.name |
| 46 | + config.name = `dynamodb-table-${Math.random().toString(36).substring(6)}`; |
| 47 | + this.state.name = config.name; |
50 | 48 | } |
51 | 49 | // If first deploy, and a name is set... |
52 | 50 | else if (config.name && !this.state.name) { |
53 | | - this.state.name = config.name |
| 51 | + this.state.name = config.name; |
54 | 52 | } |
55 | 53 | // If subequent deploy, and name is different from a previously used name, throw error. |
56 | 54 | else if (config.name && this.state.name && config.name !== this.state.name) { |
57 | 55 | throw new Error( |
58 | 56 | 'You cannot change the name of your DynamoDB table once it has been deployed (or this will deploy a new table). Please remove this Component Instance first by running "serverless remove", then redeploy it with "serverless deploy".' |
59 | | - ) |
| 57 | + ); |
60 | 58 | } |
61 | 59 |
|
62 | | - console.log(`Starting deployment of table ${config.name} in the ${config.region} region.`) |
| 60 | + console.log(`Starting deployment of table ${config.name} in the ${config.region} region.`); |
63 | 61 |
|
64 | 62 | const dynamodb = new AWS.DynamoDB({ |
65 | 63 | region: config.region, |
66 | | - credentials: this.credentials.aws |
67 | | - }) |
| 64 | + credentials: this.credentials.aws, |
| 65 | + }); |
68 | 66 |
|
69 | | - console.log(`Checking if table ${config.name} already exists in the ${config.region} region.`) |
| 67 | + console.log(`Checking if table ${config.name} already exists in the ${config.region} region.`); |
70 | 68 |
|
71 | | - const prevTable = await describeTable({ dynamodb, name: config.name }) |
| 69 | + const prevTable = await describeTable({ dynamodb, name: config.name }); |
72 | 70 |
|
73 | 71 | if (!prevTable) { |
74 | | - log(`Table ${config.name} does not exist. Creating...`) |
| 72 | + log(`Table ${config.name} does not exist. Creating...`); |
75 | 73 |
|
76 | | - config.arn = await createTable({ dynamodb, ...config }) |
| 74 | + config.arn = await createTable({ dynamodb, ...config }); |
77 | 75 | } else { |
78 | | - console.log(`Table ${config.name} already exists. Comparing config changes...`) |
| 76 | + console.log(`Table ${config.name} already exists. Comparing config changes...`); |
79 | 77 |
|
80 | 78 | // Check region |
81 | 79 | if (config.region && this.state.region && config.region !== this.state.region) { |
82 | 80 | throw new Error( |
83 | 81 | 'You cannot change the region of a DynamoDB Table. Please remove it and redeploy in your desired region.' |
84 | | - ) |
| 82 | + ); |
85 | 83 | } |
86 | 84 |
|
87 | | - config.arn = prevTable.arn |
| 85 | + config.arn = prevTable.arn; |
88 | 86 |
|
89 | | - const prevGlobalSecondaryIndexes = prevTable.globalSecondaryIndexes || [] |
90 | | - await updateTable.call(this, { dynamodb, prevGlobalSecondaryIndexes, ...config }) |
| 87 | + const prevGlobalSecondaryIndexes = prevTable.globalSecondaryIndexes || []; |
| 88 | + await updateTable.call(this, { dynamodb, prevGlobalSecondaryIndexes, ...config }); |
91 | 89 | } |
92 | 90 |
|
93 | | - log(`Table ${config.name} was successfully deployed to the ${config.region} region.`) |
| 91 | + log(`Table ${config.name} was successfully deployed to the ${config.region} region.`); |
94 | 92 |
|
95 | | - this.state.arn = config.arn |
96 | | - this.state.name = config.name |
97 | | - this.state.region = config.region |
98 | | - this.state.deletionPolicy = config.deletionPolicy |
| 93 | + this.state.arn = config.arn; |
| 94 | + this.state.name = config.name; |
| 95 | + this.state.region = config.region; |
| 96 | + this.state.deletionPolicy = config.deletionPolicy; |
99 | 97 |
|
100 | | - const outputs = pick(outputsList, config) |
| 98 | + const outputs = pick(outputsList, config); |
101 | 99 |
|
102 | 100 | // Add indexes to outputs as objects, which are easier to reference as serverless variables |
103 | 101 | if (config.globalSecondaryIndexes) { |
104 | | - outputs.indexes = outputs.indexes || {} |
| 102 | + outputs.indexes = outputs.indexes || {}; |
105 | 103 | config.globalSecondaryIndexes.forEach((index) => { |
106 | 104 | outputs.indexes[index.IndexName] = { |
107 | 105 | name: index.IndexName, |
108 | | - arn: `${outputs.arn}/index/${index.IndexName}` |
109 | | - } |
110 | | - }) |
| 106 | + arn: `${outputs.arn}/index/${index.IndexName}`, |
| 107 | + }; |
| 108 | + }); |
111 | 109 | } |
112 | 110 |
|
113 | 111 | if (config.localSecondaryIndexes) { |
114 | | - outputs.indexes = outputs.indexes || {} |
| 112 | + outputs.indexes = outputs.indexes || {}; |
115 | 113 | config.localSecondaryIndexes.forEach((index) => { |
116 | 114 | outputs.indexes[index.IndexName] = { |
117 | 115 | name: index.IndexName, |
118 | | - arn: `${outputs.arn}/index/${index.IndexName}` |
119 | | - } |
120 | | - }) |
| 116 | + arn: `${outputs.arn}/index/${index.IndexName}`, |
| 117 | + }; |
| 118 | + }); |
121 | 119 | } |
122 | 120 |
|
123 | | - return outputs |
| 121 | + return outputs; |
124 | 122 | } |
125 | 123 |
|
126 | 124 | /** |
127 | 125 | * Remove |
128 | 126 | */ |
129 | 127 | async remove() { |
130 | | - console.log('Removing') |
| 128 | + console.log('Removing'); |
131 | 129 |
|
132 | 130 | // If "delete: false", don't delete the table, and warn instead |
133 | 131 | if (this.state.deletionPolicy && this.state.deletionPolicy === 'retain') { |
134 | | - console.log('Skipping table removal because "deletionPolicy" is set to "retain".') |
135 | | - this.state = {} |
136 | | - return {} |
| 132 | + console.log('Skipping table removal because "deletionPolicy" is set to "retain".'); |
| 133 | + this.state = {}; |
| 134 | + return {}; |
137 | 135 | } |
138 | 136 |
|
139 | | - const { name, region } = this.state |
| 137 | + const { name, region } = this.state; |
140 | 138 |
|
141 | 139 | if (!name) { |
142 | | - console.log('Aborting removal. Table name not found in state.') |
143 | | - return null |
| 140 | + console.log('Aborting removal. Table name not found in state.'); |
| 141 | + return null; |
144 | 142 | } |
145 | 143 |
|
146 | 144 | const dynamodb = new AWS.DynamoDB({ |
147 | 145 | region, |
148 | | - credentials: this.credentials.aws |
149 | | - }) |
| 146 | + credentials: this.credentials.aws, |
| 147 | + }); |
150 | 148 |
|
151 | | - console.log(`Removing table ${name} from the ${region} region.`) |
| 149 | + console.log(`Removing table ${name} from the ${region} region.`); |
152 | 150 |
|
153 | | - await deleteTable({ dynamodb, name }) |
| 151 | + await deleteTable({ dynamodb, name }); |
154 | 152 |
|
155 | | - const outputs = pick(outputsList, this.state) |
| 153 | + const outputs = pick(outputsList, this.state); |
156 | 154 |
|
157 | | - console.log(`Table ${name} was successfully removed from the ${region} region.`) |
| 155 | + console.log(`Table ${name} was successfully removed from the ${region} region.`); |
158 | 156 |
|
159 | | - this.state = {} |
160 | | - return outputs |
| 157 | + this.state = {}; |
| 158 | + return outputs; |
161 | 159 | } |
162 | 160 | } |
163 | 161 |
|
164 | | -module.exports = AwsDynamoDb |
| 162 | +module.exports = AwsDynamoDb; |
0 commit comments