Skip to content

Commit eb54221

Browse files
committed
Fixing migrations and updating readme
1 parent 421157a commit eb54221

File tree

2 files changed

+60
-40
lines changed

2 files changed

+60
-40
lines changed

README.md

Lines changed: 26 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ serverless-dynamodb-local
77
[![license](https://img.shields.io/npm/l/serverless-dynamodb-local.svg)](https://www.npmjs.com/package/serverless-dynamodb-local)
88

99
## This Plugin Requires
10-
* Serverless V0.5 or newer
10+
1111
* Java Runtime Engine (JRE) version 6.x or newer
1212

1313
## Features
@@ -16,10 +16,11 @@ serverless-dynamodb-local
1616
* Create, Manage and Execute DynamoDB Migration Scripts(Table Creation/ Data Seeds) for DynamoDB Local and Online
1717

1818
## Install Plugin
19-
`npm install --save serverless-dynamodb-local`
19+
`npm install --save serverless-dynamodb-local@v1`
2020

21-
Then in `s-project.json` add following entry to the plugins array: `serverless-dynamodb-local`
22-
e.g `"plugins": ["serverless-dynamodb-local"]`
21+
Then in `serverless.yml` add following entry to the plugins array: `serverless-dynamodb-local`
22+
e.g `plugins:
23+
- serverless-dynamodb-local`
2324

2425
## Using the Plugin
2526
1) Install DynamoDB Local
@@ -38,9 +39,6 @@ e.g `"plugins": ["serverless-dynamodb-local"]`
3839
* Execute all migrations for DynamoDB Local.
3940
`sls dynamodb executeAll`
4041

41-
* Execute migration(s) in remote DynamoDB use additional parameters(region and stage) after execute/executeAll. e.g.
42-
`sls dynamodb executeAll -r us-west-1 -s dev`
43-
4442
Note: Read the detailed section for more information on advanced options and configurations. Open a browser and go to the url http://localhost:8000/shell to access the web shell for dynamodb local.
4543

4644
## Install: sls dynamodb install
@@ -64,42 +62,35 @@ All CLI options are optional:
6462

6563
All the above options can be added to s-project.json to set default configuration: e.g
6664

67-
```json
68-
"custom": {
69-
"dynamodb": {
70-
"start": {
71-
"port": "8000",
72-
"inMemory": true,
73-
"migration": true
74-
}
75-
}
76-
}
65+
```yml
66+
custom:
67+
dynamodb:
68+
start:
69+
port: 8000
70+
inMemory: true
71+
migration: true
72+
migration:
73+
dir: ./offline/migrations
7774
```
7875
7976
## Migrations: sls dynamodb create/execute/executeAll
8077
### Configurations
8178
In `s-project.json` add following to customize DynamoDB Migrations file directory and table prefixes/suffixes
82-
```json
83-
"custom": {
84-
"dynamodb": {
85-
"migration": {
86-
"dir": "dynamodbMigrations",
87-
"table_prefix": "",
88-
"table_suffix": ""
89-
}
90-
}
91-
}
79+
```yml
80+
custom:
81+
dynamodb:
82+
migration:
83+
dir: dynamodbMigrations
84+
table_prefix: prefix
85+
table_suffix": suffix
9286
```
9387

9488
In `s-project.json` add following to execute all the migration upon DynamoDB Local Start
95-
```json
96-
"custom": {
97-
"dynamodb": {
98-
"start": {
99-
"migration": true
100-
}
101-
}
102-
}
89+
```yml
90+
custom:
91+
dynamodb:
92+
start:
93+
migration: true
10394
```
10495
### Migration Template
10596
```json

index.js

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,10 @@ class ServerlessDynamodbLocal {
3434
usage: 'Execute a migration template with the given name'
3535
},
3636
region: {
37-
required: true,
3837
shortcut: 'r',
3938
usage: 'Region that dynamodb should be remotely executed'
4039
},
4140
stage: {
42-
required: true,
4341
shortcut: 's',
4442
usage: 'Stage that dynamodb should be remotely executed'
4543
}
@@ -49,12 +47,10 @@ class ServerlessDynamodbLocal {
4947
lifecycleEvents: ['executeAllHandler'],
5048
options: {
5149
region: {
52-
required: true,
5350
shortcut: 'r',
5451
usage: 'Region that dynamodb should be remotely executed'
5552
},
5653
stage: {
57-
required: true,
5854
shortcut: 's',
5955
usage: 'Stage that dynamodb should be remotely executed'
6056
}
@@ -127,6 +123,39 @@ class ServerlessDynamodbLocal {
127123
});
128124
}
129125

126+
dynamodbOptions(stage, region) {
127+
let self = this;
128+
let credentials, config = self.service.custom.dynamodb || {},
129+
port = config.start && config.start.port || 8000,
130+
dynamoOptions;
131+
132+
dynamoOptions = {
133+
endpoint: 'http://localhost:' + port,
134+
region: 'localhost'
135+
};
136+
137+
return {
138+
raw: new AWS.DynamoDB(dynamoOptions),
139+
doc: new AWS.DynamoDB.DocumentClient(dynamoOptions)
140+
};
141+
}
142+
143+
tableOptions(table_prefix, table_suffix) {
144+
let self = this;
145+
let config = self.service.custom.dynamodb,
146+
migration = config && config.migration || {},
147+
rootPath = self.serverless.config.servicePath,
148+
path = rootPath + '/' + (migration.dir || 'dynamodb'),
149+
suffix = table_suffix || migration.table_suffix || '',
150+
prefix = table_prefix || migration.table_prefix || '';
151+
152+
return {
153+
suffix: suffix,
154+
prefix: prefix,
155+
path: path
156+
};
157+
}
158+
130159
executeHandler() {
131160
let self = this,
132161
options = this.options;
@@ -175,7 +204,7 @@ class ServerlessDynamodbLocal {
175204
if (options.migration) {
176205
dynamodbLocal.start(options);
177206
console.log(""); // seperator
178-
self.executeAll();
207+
self.executeAllHandler();
179208
resolve();
180209
} else {
181210
dynamodbLocal.start(options);

0 commit comments

Comments
 (0)