Skip to content

Commit e67c274

Browse files
committed
Use serverless logger; use serverless-offline end handler to stop database
See dherault/serverless-offline#203
1 parent 202d17c commit e67c274

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

index.js

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ class ServerlessDynamodbLocal {
99
constructor(serverless, options) {
1010
this.serverless = serverless;
1111
this.service = serverless.service;
12+
this.serverlessLog = serverless.cli.log.bind(serverless.cli);
1213
this.config = this.service.custom && this.service.custom.dynamodb || {};
1314
this.options = options;
1415
this.provider = "aws";
@@ -91,14 +92,19 @@ class ServerlessDynamodbLocal {
9192
"dynamodb:install:installHandler": this.installHandler.bind(this),
9293
"dynamodb:start:startHandler": this.startHandler.bind(this),
9394
"before:offline:start:init": this.startHandler.bind(this),
95+
"before:offline:start:end": this.endHandler.bind(this),
9496
};
9597
}
9698

97-
dynamodbOptions() {
99+
get port() {
98100
const config = this.config;
99101
const port = _.get(config, "start.port", 8000);
102+
return port;
103+
}
104+
105+
dynamodbOptions() {
100106
const dynamoOptions = {
101-
endpoint: `http://localhost:${port}`,
107+
endpoint: `http://localhost:${this.port}`,
102108
region: "localhost",
103109
accessKeyId: "MOCK_ACCESS_KEY_ID",
104110
secretAccessKey: "MOCK_SECRET_ACCESS_KEY"
@@ -147,13 +153,16 @@ class ServerlessDynamodbLocal {
147153
);
148154

149155
dynamodbLocal.start(options);
150-
console.log(""); // separator
151-
152156
return BbPromise.resolve()
153157
.then(() => options.migrate && this.migrateHandler())
154158
.then(() => options.seed && this.seedHandler());
155159
}
156160

161+
endHandler() {
162+
this.serverlessLog('DynamoDB - stopping local database');
163+
dynamodbLocal.stop(this.port);
164+
}
165+
157166
/**
158167
* Gets the table definitions
159168
*/
@@ -174,7 +183,7 @@ class ServerlessDynamodbLocal {
174183
const seedConfig = _.get(config, "seed", {});
175184
const seed = this.options.seed;
176185
if (!seed) {
177-
console.log("No seed option defined. Cannot seed data.");
186+
this.serverlessLog("DynamoDB - No seed categories defined. Skipping data seeding.");
178187
return [];
179188
}
180189
const categories = seed.split(",");
@@ -186,10 +195,10 @@ class ServerlessDynamodbLocal {
186195
return new BbPromise((resolve, reject) => {
187196
dynamodb.raw.createTable(migration, (err) => {
188197
if (err) {
189-
console.log(err);
198+
this.serverlessLog("DynamoDB - Error - ", err);
190199
reject(err);
191200
} else {
192-
console.log("Table creation completed for table: " + migration.TableName);
201+
this.serverlessLog("DynamoDB - created table " + migration.TableName);
193202
resolve(migration);
194203
}
195204
});

0 commit comments

Comments
 (0)