You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+25-89Lines changed: 25 additions & 89 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -13,7 +13,7 @@ serverless-dynamodb-local
13
13
## Features
14
14
* Install DynamoDB Local
15
15
* 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 Creationfor DynamoDB Local
17
17
18
18
## Install Plugin
19
19
`npm install --save serverless-dynamodb-local`
@@ -28,18 +28,11 @@ plugins:
28
28
1) Install DynamoDB Local
29
29
`sls dynamodb install`
30
30
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
33
32
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`
37
35
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`
43
36
44
37
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.
45
38
@@ -59,7 +52,7 @@ All CLI options are optional:
59
52
--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.
60
53
--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.)
61
54
--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..
63
56
```
64
57
65
58
All the above options can be added to serverless.yml to set default configuration: e.g.
In `serverless.yml` add following to customize DynamoDB Migrations file directory and table prefixes/suffixes
81
-
```yml
82
-
custom:
83
-
dynamodb:
84
-
migration:
85
-
dir: dynamodbMigrations
86
-
table_prefix: prefix
87
-
table_suffix": suffix
88
-
```
89
-
90
71
In `serverless.yml` add following to execute all the migration upon DynamoDB Local Start
91
72
```yml
92
73
custom:
93
74
dynamodb:
94
75
start:
95
-
migration: true
76
+
migrate: true
96
77
```
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
-
}
78
+
### AWS::DynamoDB::Table Resource Template for serverless.yml
79
+
```yml
80
+
resources:
81
+
Resources:
82
+
usersTable:
83
+
Type: AWS::DynamoDB::Table
84
+
Properties:
85
+
TableName: usersTable
86
+
AttributeDefinitions:
87
+
- AttributeName: email
88
+
AttributeType: S
89
+
KeySchema:
90
+
- AttributeName: email
91
+
KeyType: HASH
92
+
ProvisionedThroughput:
93
+
ReadCapacityUnits: 1
94
+
WriteCapacityUnits: 1
158
95
```
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.
160
96
161
97
## Using DynamoDB Local in your code
162
98
You need to add the following parameters to the AWS NODE SDK dynamodb constructor
@@ -205,4 +141,4 @@ Now your local DynamoDB database will be automatically started before running `s
0 commit comments