Skip to content

Commit

Permalink
Fixing migrations and updating readme
Browse files Browse the repository at this point in the history
  • Loading branch information
mjzone committed Oct 2, 2016
1 parent 421157a commit eb54221
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 40 deletions.
61 changes: 26 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ serverless-dynamodb-local
[![license](https://img.shields.io/npm/l/serverless-dynamodb-local.svg)](https://www.npmjs.com/package/serverless-dynamodb-local)

## This Plugin Requires
* Serverless V0.5 or newer
* serverless@v1-rc.1
* Java Runtime Engine (JRE) version 6.x or newer

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

## Install Plugin
`npm install --save serverless-dynamodb-local`
`npm install --save serverless-dynamodb-local@v1`

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

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

* Execute migration(s) in remote DynamoDB use additional parameters(region and stage) after execute/executeAll. e.g.
`sls dynamodb executeAll -r us-west-1 -s dev`

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.

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

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

```json
"custom": {
"dynamodb": {
"start": {
"port": "8000",
"inMemory": true,
"migration": true
}
}
}
```yml
custom:
dynamodb:
start:
port: 8000
inMemory: true
migration: true
migration:
dir: ./offline/migrations
```
## Migrations: sls dynamodb create/execute/executeAll
### Configurations
In `s-project.json` add following to customize DynamoDB Migrations file directory and table prefixes/suffixes
```json
"custom": {
"dynamodb": {
"migration": {
"dir": "dynamodbMigrations",
"table_prefix": "",
"table_suffix": ""
}
}
}
```yml
custom:
dynamodb:
migration:
dir: dynamodbMigrations
table_prefix: prefix
table_suffix": suffix
```

In `s-project.json` add following to execute all the migration upon DynamoDB Local Start
```json
"custom": {
"dynamodb": {
"start": {
"migration": true
}
}
}
```yml
custom:
dynamodb:
start:
migration: true
```
### Migration Template
```json
Expand Down
39 changes: 34 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,10 @@ class ServerlessDynamodbLocal {
usage: 'Execute a migration template with the given name'
},
region: {
required: true,
shortcut: 'r',
usage: 'Region that dynamodb should be remotely executed'
},
stage: {
required: true,
shortcut: 's',
usage: 'Stage that dynamodb should be remotely executed'
}
Expand All @@ -49,12 +47,10 @@ class ServerlessDynamodbLocal {
lifecycleEvents: ['executeAllHandler'],
options: {
region: {
required: true,
shortcut: 'r',
usage: 'Region that dynamodb should be remotely executed'
},
stage: {
required: true,
shortcut: 's',
usage: 'Stage that dynamodb should be remotely executed'
}
Expand Down Expand Up @@ -127,6 +123,39 @@ class ServerlessDynamodbLocal {
});
}

dynamodbOptions(stage, region) {
let self = this;
let credentials, config = self.service.custom.dynamodb || {},
port = config.start && config.start.port || 8000,
dynamoOptions;

dynamoOptions = {
endpoint: 'http://localhost:' + port,
region: 'localhost'
};

return {
raw: new AWS.DynamoDB(dynamoOptions),
doc: new AWS.DynamoDB.DocumentClient(dynamoOptions)
};
}

tableOptions(table_prefix, table_suffix) {
let self = this;
let config = self.service.custom.dynamodb,
migration = config && config.migration || {},
rootPath = self.serverless.config.servicePath,
path = rootPath + '/' + (migration.dir || 'dynamodb'),
suffix = table_suffix || migration.table_suffix || '',
prefix = table_prefix || migration.table_prefix || '';

return {
suffix: suffix,
prefix: prefix,
path: path
};
}

executeHandler() {
let self = this,
options = this.options;
Expand Down Expand Up @@ -175,7 +204,7 @@ class ServerlessDynamodbLocal {
if (options.migration) {
dynamodbLocal.start(options);
console.log(""); // seperator
self.executeAll();
self.executeAllHandler();
resolve();
} else {
dynamodbLocal.start(options);
Expand Down

0 comments on commit eb54221

Please sign in to comment.