-
Notifications
You must be signed in to change notification settings - Fork 16
/
index.js
173 lines (140 loc) · 5.2 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
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
'use strict';
require("babel-polyfill");
const _ = require('lodash');
const Dynalite = require('dynalite');
const chokidar = require('graceful-chokidar');
const AWS = require('aws-sdk');
const DEFAULT_PORT = 4567;
const DEFAULT_REGION = 'localhost';
const DEFAULT_DIR = undefined;
const PORT_OPTIONS = {
shortcut: 'p',
usage: `the port number that dynalite will listen on (default ${ DEFAULT_PORT })`,
required: false
};
const DIR_OPTIONS = {
shortcut: 'd',
usage: `the directory dynalite will store its db file (default In-Memory)`,
required: false
};
class ServerlessDynalite {
constructor(serverless, options) {
this.serverless = serverless;
this.service = serverless.service;
this.log = serverless.cli.log.bind(serverless.cli);
this.config = this.service.custom && this.service.custom.dynalite || {};
this.options = options;
this.commands = {
dynalite: {
commands: {
start: {
usage: 'start a persistent dynalite server',
lifecycleEvents: [ 'startHandler' ],
options: {
port: PORT_OPTIONS,
dir: DIR_OPTIONS
}
},
watch: {
usage: 'start persistent dynalite server and watch for table definition changes',
lifecycleEvents: [ 'watchHandler' ],
options: {
port: PORT_OPTIONS,
dir: DIR_OPTIONS
}
}
}
}
};
this.hooks = {
"dynalite:start:startHandler": this.startHandler.bind(this),
"dynalite:watch:watchHandler": this.watchHandler.bind(this),
"before:offline:start:init": this.watchHandler.bind(this),
"before:offline:start:end": this.endHandler.bind(this)
};
}
get port() {
return _.get(this, ['config', 'start', 'port'], DEFAULT_PORT);
}
get dir() {
return _.get(this, ['config', 'start', 'dir'], DEFAULT_DIR);
}
get region() {
return _.get(this, ['config', 'start', 'region'], DEFAULT_REGION);
}
get dynamodb() {
if (this._dynamodb) {
return this._dynamodb;
}
const dynamoOptions = {
endpoint: `http://localhost:${this.port}`,
region: this.region
};
this._dynamodb = {
raw: new AWS.DynamoDB(dynamoOptions),
doc: new AWS.DynamoDB.DocumentClient(dynamoOptions)
};
return this._dynamodb;
}
async watchHandler() {
await this.startHandler();
this.watcher = chokidar.watch('./serverless.yml', { persistent: true, interval: 1000 })
.on('change', async () => {
this.log('serverless.yml changed, updating...');
await this.reloadService();
this.updateTables();
});
this.log('Listening for table additions / deletions.');
}
async startHandler() {
this.dynalite = Dynalite({ createTableMs: 0, path: this.dir });
await new Promise(
(res, rej) => this.dynalite.listen(this.port, err => err ? rej(err) : res())
);
this.log(`Dynalite listening on http://localhost:${ this.port }`);
return this.updateTables();
}
endHandler() {
if (this.watcher) {
this.watcher.close();
}
if (this.dynalite) {
this.dynalite.close();
}
}
async reloadService() {
const options = this.serverless.processedInput.options;
await this.service.load(options);
await this.serverless.variables.populateService(options);
await this.service.setFunctionNames(options);
await this.service.mergeResourceArrays();
await this.service.validate();
}
async updateTables() {
const requiredTables = _.map(
_.filter(
_.values(
_.get(this.service, ['resources', 'Resources'], {})
),
{ 'Type': 'AWS::DynamoDB::Table' }
),
'Properties'
);
this.log(`Tables in config: ${ JSON.stringify(_.map(requiredTables, 'TableName')) }`);
const currentTables = await this.dynamodb.raw.listTables({}).promise();
this.log(`Current Tables: ${ JSON.stringify(currentTables.TableNames) }`);
const missingTables = _.reject(requiredTables,
({ TableName }) => _.includes(currentTables.TableNames, TableName)
);
this.log(`Missing Tables: ${ JSON.stringify(_.map(missingTables, 'TableName')) }`);
_.forEach(missingTables, async table => {
this.log(`Creating table ${ table.TableName }...`);
await this.dynamodb.raw.createTable(table).promise();
});
setTimeout(async () => {
const finalTables = await this.dynamodb.raw.listTables({}).promise();
this.log(`Current Tables: ${ JSON.stringify(finalTables.TableNames) }`);
}, 1000);
}
}
module.exports = ServerlessDynalite;