UNPKG

9.1 kBMarkdownView Raw
1serverless-dynamodb-local
2=================================
3
4[![Join the chat at https://gitter.im/99xt/serverless-dynamodb-local](https://badges.gitter.im/99xt/serverless-dynamodb-local.svg)](https://gitter.im/99xt/serverless-dynamodb-local?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
5[![npm version](https://badge.fury.io/js/serverless-dynamodb-local.svg)](https://badge.fury.io/js/serverless-dynamodb-local)
6[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
7
8## This Plugin Requires
9* serverless@v1-rc.1
10* Java Runtime Engine (JRE) version 6.x or newer
11
12## Features
13* Install DynamoDB Local
14* Start DynamoDB Local with all the parameters supported (e.g port, inMemory, sharedDb)
15* Table Creation for DynamoDB Local
16
17## Install Plugin
18`npm install --save serverless-dynamodb-local`
19
20Then in `serverless.yml` add following entry to the plugins array: `serverless-dynamodb-local`
21```yml
22plugins:
23 - serverless-dynamodb-local
24```
25
26## Using the Plugin
271) Install DynamoDB Local
28`sls dynamodb install`
29
302) Add DynamoDB Resource definitions to your Serverless configuration, as defined here: https://serverless.com/framework/docs/providers/aws/guide/resources/#configuration
31
323) Start DynamoDB Local and migrate (DynamoDB will process incoming requests until you stop it. To stop DynamoDB, type Ctrl+C in the command prompt window). Make sure above command is executed before this.
33`sls dynamodb start --migrate`
34
35
36Note: 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.
37
38## Install: sls dynamodb install
39To remove the installed dynamodb local, run:
40`sls dynamodb remove`
41Note: This is useful if the sls dynamodb install failed in between to completely remove and install a new copy of DynamoDB local.
42
43## Start: sls dynamodb start
44All CLI options are optional:
45
46```
47--port -p Port to listen on. Default: 8000
48--cors -c Enable CORS support (cross-origin resource sharing) for JavaScript. You must provide a comma-separated "allow" list of specific domains. The default setting for -cors is an asterisk (*), which allows public access.
49--inMemory -i DynamoDB; will run in memory, instead of using a database file. When you stop DynamoDB;, none of the data will be saved. Note that you cannot specify both -dbPath and -inMemory at once.
50--dbPath -d The directory where DynamoDB will write its database file. If you do not specify this option, the file will be written to the current directory. Note that you cannot specify both -dbPath and -inMemory at once. For the path, current working directory is <projectroot>/node_modules/serverless-dynamodb-local/dynamob. For example to create <projectroot>/node_modules/serverless-dynamodb-local/dynamob/<mypath> you should specify -d <mypath>/ or --dbPath <mypath>/ with a forwardslash at the end.
51--sharedDb -h DynamoDB will use a single database file, instead of using separate files for each credential and region. If you specify -sharedDb, all DynamoDB clients will interact with the same set of tables regardless of their region and credential configuration.
52--delayTransientStatuses -t Causes DynamoDB to introduce delays for certain operations. DynamoDB can perform some tasks almost instantaneously, such as create/update/delete operations on tables and indexes; however, the actual DynamoDB service requires more time for these tasks. Setting this parameter helps DynamoDB simulate the behavior of the Amazon DynamoDB web service more closely. (Currently, this parameter introduces delays only for global secondary indexes that are in either CREATING or DELETING status.)
53--optimizeDbBeforeStartup -o Optimizes the underlying database tables before starting up DynamoDB on your computer. You must also specify -dbPath when you use this parameter.
54--migrate -m After starting DynamoDB local, create DynamoDB tables from the Serverless configuration.
55--seed -s After starting and migrating dynamodb local, injects seed data into your tables. The --seed option determines which data categories to onload.
56--convertEmptyValues -e Set to true if you would like the document client to convert empty values (0-length strings, binary buffers, and sets) to be converted to NULL types when persisting to DynamoDB.
57```
58
59All the above options can be added to serverless.yml to set default configuration: e.g.
60
61```yml
62custom:
63 dynamodb:
64 # If you only want to use DynamoDB Local in some stages, declare them here
65 stages:
66 - dev
67 start:
68 port: 8000
69 inMemory: true
70 migrate: true
71 seed: true
72 convertEmptyValues: true
73 # Uncomment only if you already have a DynamoDB running locally
74 # noStart: true
75```
76
77## Migrations: sls dynamodb migrate
78### Configuration
79In `serverless.yml` add following to execute all the migration upon DynamoDB Local Start
80```yml
81custom:
82 dynamodb:
83 start:
84 migrate: true
85```
86### AWS::DynamoDB::Table Resource Template for serverless.yml
87```yml
88resources:
89 Resources:
90 usersTable:
91 Type: AWS::DynamoDB::Table
92 Properties:
93 TableName: usersTable
94 AttributeDefinitions:
95 - AttributeName: email
96 AttributeType: S
97 KeySchema:
98 - AttributeName: email
99 KeyType: HASH
100 ProvisionedThroughput:
101 ReadCapacityUnits: 1
102 WriteCapacityUnits: 1
103```
104
105**Note:**
106DynamoDB local doesn't support TTL specification, therefore plugin will simply ignore ttl configuration from Cloudformation template.
107
108## Seeding: sls dynamodb seed
109### Configuration
110
111In `serverless.yml` seeding categories are defined under `dynamodb.seed`.
112
113If `dynamodb.start.seed` is true, then seeding is performed after table migrations.
114
115If you wish to use raw AWS AttributeValues to specify your seed data instead of Javascript types then simply change the variable of any such json files from `sources:` to `rawsources:`.
116
117```yml
118dynamodb:
119 start:
120 seed: true
121
122 seed:
123 domain:
124 sources:
125 - table: domain-widgets
126 sources: [./domainWidgets.json]
127 - table: domain-fidgets
128 sources: [./domainFidgets.json]
129 test:
130 sources:
131 - table: users
132 rawsources: [./fake-test-users.json]
133 - table: subscriptions
134 sources: [./fake-test-subscriptions.json]
135```
136
137```bash
138> sls dynamodb seed --seed=domain,test
139> sls dynamodb start --seed=domain,test
140```
141
142If seed config is set to true, your configuration will be seeded automatically on startup. You can also put the seed to false to prevent initial seeding to use manual seeding via cli.
143
144```fake-test-users.json example
145[
146 {
147 "id": "John",
148 "name": "Doe",
149 },
150]
151```
152
153## Using DynamoDB Local in your code
154You need to add the following parameters to the AWS NODE SDK dynamodb constructor
155
156e.g. for dynamodb document client sdk
157```
158var AWS = require('aws-sdk');
159```
160```
161new AWS.DynamoDB.DocumentClient({
162 region: 'localhost',
163 endpoint: 'http://localhost:8000'
164})
165```
166e.g. for dynamodb document client sdk
167```
168new AWS.DynamoDB({
169 region: 'localhost',
170 endpoint: 'http://localhost:8000'
171})
172```
173
174### Using with serverless-offline plugin
175When using this plugin with serverless-offline, it is difficult to use above syntax since the code should use DynamoDB Local for development, and use DynamoDB Online after provisioning in AWS. Therefore we suggest you to use [serverless-dynamodb-client](https://github.com/99xt/serverless-dynamodb-client) plugin in your code.
176
177The `serverless dynamodb start` command can be triggered automatically when using `serverless-offline` plugin.
178Please note that you still need to install DynamoDB Local first.
179
180Add both plugins to your `serverless.yml` file:
181```yaml
182plugins:
183 - serverless-dynamodb-local
184 - serverless-offline
185```
186
187Make sure that `serverless-dynamodb-local` is above `serverless-offline` so it will be loaded earlier.
188
189Now your local DynamoDB database will be automatically started before running `serverless offline`.
190
191### Using with serverless-offline and serverless-webpack plugin
192Run `serverless offline start`. In comparison with `serverless offline`, the `start` command will fire an `init` and a `end` lifecycle hook which is needed for serverless-offline and serverless-dynamodb-local to switch off both ressources.
193
194Add plugins to your `serverless.yml` file:
195```yaml
196plugins:
197 - serverless-webpack
198 - serverless-dynamodb-local
199 - serverless-offline #serverless-offline needs to be last in the list
200```
201
202## Reference Project
203* [serverless-react-boilerplate](https://github.com/99xt/serverless-react-boilerplate)
204
205## Links
206* [Dynamodb local documentation](http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBLocal.html)
207* [Contact Us](mailto:ashanf@99x.lk)
208* [NPM Registry](https://www.npmjs.com/package/serverless-dynamodb-local)
209
210## License
211 [MIT](LICENSE)