Skip to content

Commit 8094669

Browse files
committed
Update serverless version, adding remote script execution support
1 parent c6e4ae9 commit 8094669

File tree

3 files changed

+39
-21
lines changed

3 files changed

+39
-21
lines changed

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ All the above options can be added to s-project.json to set default configuratio
6767
To remove the installed dynamodb local, run:
6868
`sls dynamodb remove`
6969

70-
## Manage migrations
70+
## Manage Migrations
7171

7272
Start dynamodb local instance in another window before running the following commands. To store your dynamodb migration templates do the following configuration (If not specified default directory <project-root>/dynamodb is used)
7373

@@ -84,14 +84,17 @@ Start dynamodb local instance in another window before running the following com
8484
```
8585

8686
To create new migration template with the given name, run:
87-
`sls dynamodb create -n <your-table-name>`
87+
`sls dynamodb create -n <migration-name>`
8888

8989
To execute a migration template with the given name, run:
90-
`sls dynamodb execute -n <your-table-name>`
90+
`sls dynamodb execute -n <migration-file-name>`
9191

9292
To execute all migration templates, run:
9393
`sls dynamodb executeAll`
9494

95+
Note: Optionally to execute/executeAll migrations on remote dynamodb, use -r <region> and -s <stage> parameters
96+
'sls dynamodb executeAll -r us-west-1 -s dev'
97+
9598
## Migration Template
9699

97100
```json
@@ -163,9 +166,6 @@ References
163166
* Defining table schema (Dynamodb Client SDK): http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB.html#createTable-property
164167
* Defining seeds (Dynamodb Document Client SDK): http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html#put-property
165168

166-
To create table & run the seeds in your project root, run:
167-
`sls dynamodb table -c`
168-
169169
If you need to prefix_<your-table-name>_suffix, you can configure the values accordingly. This is usefull when you have multiple stages which needs multiple database tables
170170

171171
Optionally if you want to execute all migrations on dynamodb starts, use the argument -m or add the "migration": true inside s-project.json as shown below
@@ -180,7 +180,7 @@ Optionally if you want to execute all migrations on dynamodb starts, use the arg
180180
}
181181
```
182182

183-
## Accessing dynamodb local from your code
183+
## Using in code
184184

185185
You need to add the following parameters to the AWS SDK dynamodb constructor
186186

index.js

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,13 @@ module.exports = function (S) {
4545
shortcut: 'n',
4646
description: 'Execute a migration template with the given name'
4747
}, {
48-
option: 'remote',
48+
option: 'region',
4949
shortcut: 'r',
50-
description: 'Execute migration template in remote dynamodb given in s-project.json.'
50+
description: 'Region that dynamodb should be remotely executed'
51+
}, {
52+
option: 'stage',
53+
shortcut: 's',
54+
description: 'Stage that dynamodb should be remotely executed'
5155
}]
5256
});
5357
S.addAction(this.executeAll.bind(this), {
@@ -56,9 +60,13 @@ module.exports = function (S) {
5660
context: 'dynamodb',
5761
contextAction: 'executeAll',
5862
options: [{
59-
option: 'remote',
63+
option: 'region',
6064
shortcut: 'r',
61-
description: 'Execute all migration templates in remote dynamodb given in s-project.json.'
65+
description: 'Region that dynamodb should be remotely executed'
66+
}, {
67+
option: 'stage',
68+
shortcut: 's',
69+
description: 'Stage that dynamodb should be remotely executed'
6270
}]
6371
});
6472
S.addAction(this.remove.bind(this), {
@@ -116,15 +124,25 @@ module.exports = function (S) {
116124
return BbPromise.resolve();
117125
}
118126

119-
dynamodbOptions(remote) {
120-
let config = S.getProject().custom.dynamodb || {},
121-
region = config.migration.region || 'localhost',
127+
dynamodbOptions(stage, region) {
128+
let credentials, config = S.getProject().custom.dynamodb || {},
122129
port = config.start && config.start.port || 8000,
123-
endpoint = remote ? config.migration.endpoint : 'http://localhost:' + port,
124-
dynamoOptions = {
130+
dynamoOptions;
131+
if (stage && region) {
132+
credentials = S.getProvider('aws').getCredentials(stage, region);
133+
AWS.config.update({
125134
region: region,
126-
endpoint: endpoint
135+
accessKeyId: credentials.accessKeyId,
136+
secretAccessKey: credentials.secretAccessKey,
137+
sessionToken: credentials.sessionToken
138+
});
139+
} else {
140+
dynamoOptions = {
141+
endpoint: 'http://localhost:' + port,
142+
region: 'localhost'
127143
};
144+
}
145+
128146
return {
129147
raw: new AWS.DynamoDB(dynamoOptions),
130148
doc: new AWS.DynamoDB.DocumentClient(dynamoOptions)
@@ -170,7 +188,7 @@ module.exports = function (S) {
170188
let self = this,
171189
options = evt.options;
172190
return new BbPromise(function (resolve, reject) {
173-
let dynamodb = self.dynamodbOptions(options.remote),
191+
let dynamodb = self.dynamodbOptions(options.stage, options.region),
174192
tableOptions = self.tableOptions();
175193
dynamodbMigrations.init(dynamodb, tableOptions.path);
176194
dynamodbMigrations.execute(options.name, {
@@ -184,7 +202,7 @@ module.exports = function (S) {
184202
let self = this,
185203
options = evt.options;
186204
return new BbPromise(function (resolve, reject) {
187-
let dynamodb = self.dynamodbOptions(options.remote),
205+
let dynamodb = self.dynamodbOptions(options.stage, options.region),
188206
tableOptions = self.tableOptions();
189207
dynamodbMigrations.init(dynamodb, tableOptions.path);
190208
dynamodbMigrations.executeAll({

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "serverless-dynamodb-local",
3-
"version": "0.2.1",
3+
"version": "0.2.2",
44
"engines": {
55
"node": ">=4.0"
66
},
@@ -39,7 +39,7 @@
3939
"aws-sdk": "^2.3.19",
4040
"bluebird": "^3.0.6",
4141
"dynamodb-localhost": "0.0.2",
42-
"dynamodb-migrations": "0.0.6",
42+
"dynamodb-migrations": "0.0.7",
4343
"lodash": "^4.13.1",
4444
"mkdirp": "^0.5.0",
4545
"tar": "^2.0.0"

0 commit comments

Comments
 (0)