Skip to content

Commit b3c2acb

Browse files
committed
Tables are created from the current serverless.yml. Seeding is no longer available.
1 parent ed14b98 commit b3c2acb

File tree

3 files changed

+134
-222
lines changed

3 files changed

+134
-222
lines changed

README.md

Lines changed: 27 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ serverless-dynamodb-local
1313
## Features
1414
* Install DynamoDB Local
1515
* Start DynamoDB Local with all the parameters supported (e.g port, inMemory, sharedDb)
16-
* Create, Manage and Execute DynamoDB Migration Scripts(Table Creation/ Data Seeds) for DynamoDB Local and Online
16+
* Table Creation for DynamoDB Local
1717

1818
## Install Plugin
1919
`npm install --save serverless-dynamodb-local`
@@ -28,18 +28,11 @@ plugins:
2828
1) Install DynamoDB Local
2929
`sls dynamodb install`
3030

31-
2) Start DynamoDB Local (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.
32-
`sls dynamodb start`
31+
2) Add DynamoDB Resource definitions to your Serverless configuration, as defined here: https://serverless.com/framework/docs/providers/aws/guide/resources/#configuration
3332

34-
3) Create/Execute DynamoDB (Migrations)
35-
* Create a new migration file (Default directory path /dynamodb). Make sure DynamoDB Local is started in another shell.
36-
`sls dynamodb create -n <filename>`
33+
3) 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.
34+
`sls dynamodb start --migrate`
3735

38-
* Execute a single migration. Make sure DynamoDB Local is started in another shell.
39-
`sls dynamodb execute -n <filename>`
40-
41-
* Execute all migrations on the remote dynamodb.
42-
`sls dynamodb executeAll`
4336

4437
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.
4538

@@ -59,7 +52,7 @@ All CLI options are optional:
5952
--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.
6053
--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.)
6154
--optimizeDbBeforeStartup -o Optimizes the underlying database tables before starting up DynamoDB on your computer. You must also specify -dbPath when you use this parameter.
62-
--migration -m After starting dynamodb local, run dynamodb migrations.
55+
--migrate -m After starting DynamoDB local, create DynamoDB tables from the Serverless configuration..
6356
```
6457

6558
All the above options can be added to serverless.yml to set default configuration: e.g.
@@ -71,92 +64,44 @@ custom:
7164
port: 8000
7265
inMemory: true
7366
migration: true
74-
migration:
75-
dir: ./offline/migrations
7667
```
7768

78-
## Migrations: sls dynamodb create/execute/executeAll
69+
## Migrations: sls dynamodb migrate
7970
### Configurations
80-
In `serverless.yml` add following to customize DynamoDB Migrations file directory and table prefixes/suffixes
71+
In `serverless.yml` add following to customize DynamoDB Migrations table prefixes/suffixes
8172
```yml
8273
custom:
8374
dynamodb:
8475
migration:
85-
dir: dynamodbMigrations
86-
table_prefix: prefix
87-
table_suffix": suffix
76+
table_prefix: prefix
77+
table_suffix": suffix
8878
```
8979

9080
In `serverless.yml` add following to execute all the migration upon DynamoDB Local Start
9181
```yml
9282
custom:
9383
dynamodb:
9484
start:
95-
migration: true
85+
migrate: true
9686
```
97-
### Migration Template
98-
```json
99-
{
100-
"Table": {
101-
"TableName": "TableName",
102-
"KeySchema": [{
103-
"AttributeName": "attr_1",
104-
"KeyType": "HASH"
105-
}, {
106-
"AttributeName": "attr_2",
107-
"KeyType": "RANGE"
108-
}],
109-
"AttributeDefinitions": [{
110-
"AttributeName": "attr_1",
111-
"AttributeType": "S"
112-
}, {
113-
"AttributeName": "attr_2",
114-
"AttributeType": "S"
115-
}],
116-
"LocalSecondaryIndexes": [{
117-
"IndexName": "local_index_1",
118-
"KeySchema": [{
119-
"AttributeName": "attr_1",
120-
"KeyType": "HASH"
121-
}, {
122-
"AttributeName": "attr_2",
123-
"KeyType": "RANGE"
124-
}],
125-
"Projection": {
126-
"NonKeyAttributes": ["attr_1", "attr_2"],
127-
"ProjectionType": "INCLUDE"
128-
}
129-
}],
130-
"GlobalSecondaryIndexes": [{
131-
"IndexName": "global_index_1",
132-
"KeySchema": [{
133-
"AttributeName": "attr_1",
134-
"KeyType": "HASH"
135-
}, {
136-
"AttributeName": "attr_2",
137-
"KeyType": "RANGE"
138-
}],
139-
"Projection": {
140-
"NonKeyAttributes": ["attr_1", "attr_2"],
141-
"ProjectionType": "INCLUDE"
142-
},
143-
"ProvisionedThroughput": {
144-
"ReadCapacityUnits": 1,
145-
"WriteCapacityUnits": 1
146-
}
147-
}],
148-
"ProvisionedThroughput": {
149-
"ReadCapacityUnits": 1,
150-
"WriteCapacityUnits": 1
151-
}
152-
},
153-
"Seeds": [{
154-
"attr_1": "attr_1_value",
155-
"attr_2": "attr_2_value"
156-
}]
157-
}
87+
### AWS::DynamoDB::Table Resource Template for serverless.yml
88+
```yml
89+
resources:
90+
Resources:
91+
usersTable:
92+
Type: AWS::DynamoDB::Table
93+
Properties:
94+
TableName: usersTable
95+
AttributeDefinitions:
96+
- AttributeName: email
97+
AttributeType: S
98+
KeySchema:
99+
- AttributeName: email
100+
KeyType: HASH
101+
ProvisionedThroughput:
102+
ReadCapacityUnits: 1
103+
WriteCapacityUnits: 1
158104
```
159-
Before modifying the migration template, refer the (Dynamodb Client SDK): http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB.html#createTable-property and (Dynamodb Document Client SDK): http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html#put-property links.
160105

161106
## Using DynamoDB Local in your code
162107
You need to add the following parameters to the AWS NODE SDK dynamodb constructor

0 commit comments

Comments
 (0)