Skip to content

Commit c6e4ae9

Browse files
committed
Adding execute and executeAll methods, update the documentation with migration terminology, modifying the API and option parameters
2 parents 8826cd6 + 08a8444 commit c6e4ae9

File tree

5 files changed

+202
-210
lines changed

5 files changed

+202
-210
lines changed

README.md

Lines changed: 90 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ You can use this with ['serverless-offline'](https://github.com/dherault/serverl
1717

1818
* Automatically downloads dynamodb local
1919
* Allow to specify all the supported parameters in dynamodb local (e.g port, inMemory, sharedDb)
20-
* Provide you with a set of serverless commands for dynamodb local (e.g seeds, tables)
20+
* Provide you with a set of serverless commands for dynamodb migrations (e.g seeds, tables)
2121

2222
## Installation
2323

@@ -29,7 +29,10 @@ Like this: `"plugins": ["serverless-dynamodb-local"]`
2929

3030
## Starting Dynamodb Local
3131

32-
In your project root run (Note: Run this command first before any other command, since it will download the dynamodb local during the first run):
32+
In your project root run (Note: Run this command first before any other command, since it will download the dynamodb local):
33+
`sls dynamodb install`
34+
35+
In your project root run to start dynamodb instance:
3336
`sls dynamodb start`
3437

3538
DynamoDB will process incoming requests until you stop it. To stop DynamoDB, type Ctrl+C in the command prompt window
@@ -38,13 +41,13 @@ All CLI options are optional:
3841

3942
```
4043
--port -p Port to listen on. Default: 8000
41-
--cors -r 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.
42-
--inMemory -m 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.
44+
--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.
45+
--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.
4346
--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.
4447
--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.
4548
--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.)
4649
--optimizeDbBeforeStartup -o Optimizes the underlying database tables before starting up DynamoDB on your computer. You must also specify -dbPath when you use this parameter.
47-
--create -c After starting dynamodb local, create dynamodb tables and run seeds. Check the "Manage tables and seeds" section for more information.
50+
--migration -m After starting dynamodb local, run dynamodb migrations.
4851
```
4952

5053
All the above options can be added to s-project.json to set default configuration: e.g
@@ -55,7 +58,7 @@ All the above options can be added to s-project.json to set default configuratio
5558
"start": {
5659
"port": "8000",
5760
"inMemory": true,
58-
"create": true
61+
"migration": true
5962
}
6063
}
6164
}
@@ -64,43 +67,114 @@ All the above options can be added to s-project.json to set default configuratio
6467
To remove the installed dynamodb local, run:
6568
`sls dynamodb remove`
6669

67-
## Manage tables and seeds
70+
## Manage migrations
6871

69-
Start dynamodb local instance in another window before running the following commands. To store your dynamodb table creation and seed configurations do the following configuration (If not specified default directory <project-root>/dynamodb)
72+
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)
7073

7174
```json
7275
"custom": {
7376
"dynamodb": {
74-
"table": {
77+
"migration": {
7578
"dir": "dynamodbTables",
76-
"prefix": "",
77-
"suffix": ""
79+
"table_prefix": "",
80+
"table_suffix": ""
7881
}
7982
}
8083
}
8184
```
8285

83-
To create table & seed template in your project root, run:
84-
`sls dynamodb table -n <your-table-name>`
86+
To create new migration template with the given name, run:
87+
`sls dynamodb create -n <your-table-name>`
88+
89+
To execute a migration template with the given name, run:
90+
`sls dynamodb execute -n <your-table-name>`
91+
92+
To execute all migration templates, run:
93+
`sls dynamodb executeAll`
94+
95+
## Migration Template
96+
97+
```json
98+
{
99+
"Table": {
100+
"TableName": "TableName",
101+
"KeySchema": [{
102+
"AttributeName": "attr_1",
103+
"KeyType": "HASH"
104+
}, {
105+
"AttributeName": "attr_2",
106+
"KeyType": "RANGE"
107+
}],
108+
"AttributeDefinitions": [{
109+
"AttributeName": "attr_1",
110+
"AttributeType": "S"
111+
}, {
112+
"AttributeName": "attr_2",
113+
"AttributeType": "S"
114+
}],
115+
"LocalSecondaryIndexes": [{
116+
"IndexName": "local_index_1",
117+
"KeySchema": [{
118+
"AttributeName": "attr_1",
119+
"KeyType": "HASH"
120+
}, {
121+
"AttributeName": "attr_2",
122+
"KeyType": "RANGE"
123+
}],
124+
"Projection": {
125+
"NonKeyAttributes": ["attr_1", "attr_2"],
126+
"ProjectionType": "INCLUDE"
127+
}
128+
}],
129+
"GlobalSecondaryIndexes": [{
130+
"IndexName": "global_index_1",
131+
"KeySchema": [{
132+
"AttributeName": "attr_1",
133+
"KeyType": "HASH"
134+
}, {
135+
"AttributeName": "attr_2",
136+
"KeyType": "RANGE"
137+
}],
138+
"Projection": {
139+
"NonKeyAttributes": ["attr_1", "attr_2"],
140+
"ProjectionType": "INCLUDE"
141+
},
142+
"ProvisionedThroughput": {
143+
"ReadCapacityUnits": 1,
144+
"WriteCapacityUnits": 1
145+
}
146+
}],
147+
"ProvisionedThroughput": {
148+
"ReadCapacityUnits": 1,
149+
"WriteCapacityUnits": 1
150+
}
151+
},
152+
"Seeds": [{
153+
"attr_1": "attr_1_value",
154+
"attr_2": "attr_2_value"
155+
}]
156+
}
157+
```
158+
Before modifying the migration template, refer the Dynamodb Client SDK and Dynamodb Document Client SDK links.
85159

86160
This will create a template json inside configured directory. Open the file and edit the table schema and data.
87161

88162
References
89-
* Defining table schema (Dynamodb SDK): http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB.html#createTable-property
163+
* Defining table schema (Dynamodb Client SDK): http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB.html#createTable-property
90164
* Defining seeds (Dynamodb Document Client SDK): http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html#put-property
91165

92166
To create table & run the seeds in your project root, run:
93167
`sls dynamodb table -c`
94168

95169
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
96170

97-
Optionally if you want to create the tables and run the seeds on dynamodb starts, use the argument -c or add the "create": true inside s-project.json as shown below
171+
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
98172

99173
```json
100174
"custom": {
101175
"dynamodb": {
102176
"start": {
103-
"create": true
177+
"migration": true
104178
}
105179
}
106180
}
@@ -160,10 +234,6 @@ sls dynamodb start
160234
```
161235
* Go to dynamodb local [shell](http://localhost:8000/shell) in your browser and you should be able to see use the web shell
162236

163-
## Credits
164-
165-
Bunch of thanks to doapp-ryanp who started [dynamodb-local](https://github.com/doapp-ryanp/dynamodb-local) project
166-
167237
## License
168238

169239
[MIT](LICENSE)

0 commit comments

Comments
 (0)