This repository has been archived by the owner on Jun 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
95 lines (77 loc) · 2.96 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
const Dynalite = require('dynalite');
const AWS = require('aws-sdk');
const path = require('path')
const DEFAULT_PORT = 8000;
const DEFAULT_REGION = 'localhost';
const DEFAULT_DIR = undefined;
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.hooks = {
"before:offline:start:init": this.startHandler.bind(this),
"before:offline:start:end": this.endHandler.bind(this)
};
}
get port() {
return this.config.port || DEFAULT_PORT;
}
get dir() {
return this.config.dir || DEFAULT_DIR
}
get region() {
return this.config.region || DEFAULT_REGION
}
get 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 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 }`);
if(this.service.resources && this.service.resources.Resources) {
let resources = Object.values(this.service.resources.Resources);
let tables = resources.filter(el => el.Type === 'AWS::DynamoDB::Table').map(el => ({
TableName: el.Properties.TableName,
AttributeDefinitions: el.Properties.AttributeDefinitions,
KeySchema: el.Properties.KeySchema,
BillingMode: el.Properties.BillingMode,
GlobalSecondaryIndexes: el.Properties.GlobalSecondaryIndexes
}));
for(let table of tables) {
try {
this.log(`Dynalite creating table ${table.TableName}`)
await this.dynamodb.raw.createTable(table).promise()
} catch(e) {
this.log(e.message)
}
}
if(this.config.seed) {
for(let seed of this.config.seed) {
if(!seed.source) {
throw Error(`Source is 'undefined' for ${seed.table}`)
}
let items = require(path.join(process.cwd(), seed.source))
await Promise.all(items.map(Item => this.dynamodb.doc.put({ TableName: seed.table, Item}).promise()))
}
}
}
}
endHandler() {
this.dynalite.close();
}
}
module.exports = ServerlessDynalite;