-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
158 lines (124 loc) · 5.46 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
'use strict';
module.exports = function(S) {
const path = require('path'),
SError = require(S.getServerlessPath('Error')),
SCli = require(S.getServerlessPath('utils/cli')),
Promise = require('bluebird'),
fs = require('fs'),
async = require('async'),
AWS = require('aws-sdk'),
fixturesPath = path.join(S.config.projectPath, 'fixtures');
class DynamoDbFixtures extends S.classes.Plugin {
constructor() {
super();
this.name = 'serverless-dynamodb-fixtures-plugin';
}
registerActions() {
S.addAction(this.loadFixture.bind(this), {
handler: 'loadFixture',
description: 'Load your dynamodb fixtures',
context: 'dynamodb',
contextAction: 'load',
options: [
{
option: 'stage',
shortcut: 's',
description: 'stage to populate any variables'
}, {
option: 'region',
shortcut: 'r',
description: 'region to populate any variables'
}
]
});
return Promise.resolve();
}
loadFixture(evt) {
let _this = this;
_this.evt = evt;
// Flow
return _this._prompt()
.bind(_this)
.then(_this._validateParams)
.then(_this._loadFixtures)
.then(function() {
SCli.log('Done loading.');
return _this.evt;
});
}
_prompt() {
let _this = this;
return _this.cliPromptSelectStage('Choose Stage: ', _this.evt.options.stage, true)
.then(stage => {
_this.evt.options.stage = stage;
Promise.resolve();
})
.then(function(){
return _this.cliPromptSelectRegion('Choose Region: ', false, true, _this.evt.options.region, _this.evt.options.stage)
.then(region => {
_this.evt.options.region = region;
Promise.resolve();
});
});
}
_validateParams() {
let _this = this;
if (!S.utils.dirExistsSync(fixturesPath)) {
return Promise.reject(new SError('Could not find "fixtures" folder in your project root.'));
}
// validate stage: make sure stage exists
if (!S.getProject().validateStageExists(_this.evt.options.stage)) {
return Promise.reject(new SError('Stage ' + _this.evt.options.stage + ' does not exist in your project', SError.errorCodes.UNKNOWN));
}
// make sure region exists in stage
if (!S.getProject().validateRegionExists(_this.evt.options.stage, _this.evt.options.region)) {
return Promise.reject(new SError('Region "' + _this.evt.options.region + '" does not exist in stage "' + _this.evt.options.stage + '"'));
}
_this.project = S.getProject();
_this.aws = S.getProvider('aws');
_this.dynamodb = new AWS.DynamoDB.DocumentClient({ region: _this.evt.options.region });
return Promise.resolve();
}
_loadFixture(filename) {
let _this = this;
SCli.log(`Loading fixtures/${filename}`);
var inputData = fs.readFileSync('fixtures/' + filename, 'utf8');
// TODO: I'm pretty sure there's a better way to render templates that actually use the full set of variables
inputData = inputData.replace(/\$\{SERVERLESS_STAGE\}/g, this.evt.options.stage);
inputData = inputData.replace(/\$\{SERVERLESS_PROJECT\}/g, this.project.name);
inputData = inputData.replace(/\$\{SERVERLESS_REGION\}/g, this.evt.options.region);
var importData = JSON.parse(inputData);
const batchWrite = Promise.promisify(_this.dynamodb.batchWrite.bind(_this.dynamodb))
let items = importData.entries.map(function(entry){
return {
PutRequest: {
Item: entry
}
}
});
var request = {
RequestItems: {
}
};
// You can only batch-write 25 entries at a time.
var batches = [];
for(var i = 0 ; i < items.length ; i += 25) {
batches.push(items.slice(i, i+25));
}
return Promise.each(batches,(batch) => {
SCli.log(`Writing ${batch.length} entries`);
_this._spinner.start();
request.RequestItems[importData.tableName] = batch;
return batchWrite(request).then(() => _this._spinner.stop(true))
});
}
_loadFixtures() {
let _this = this;
SCli.log('Loading fixtures to stage "' + _this.evt.options.stage + '" in region "' + _this.evt.options.region + '"...');
_this._spinner = SCli.spinner();
let files = fs.readdirSync(fixturesPath);
return Promise.each(files, this._loadFixture.bind(this));
}
}
return DynamoDbFixtures;
};