Skip to content

Commit 0588459

Browse files
committed
Update seeding configuration. Add seed categories
1 parent b31a6e2 commit 0588459

File tree

2 files changed

+48
-6
lines changed

2 files changed

+48
-6
lines changed

README.md

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ All CLI options are optional:
5252
--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.
5353
--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.)
5454
--optimizeDbBeforeStartup -o Optimizes the underlying database tables before starting up DynamoDB on your computer. You must also specify -dbPath when you use this parameter.
55-
--migrate -m After starting DynamoDB local, create DynamoDB tables from the Serverless configuration..
55+
--migrate -m After starting DynamoDB local, create DynamoDB tables from the Serverless configuration.
56+
--seed -s After starting and migrating dynamodb local, injects seed data into your tables. The --seed option determines which data categories to onload.
5657
```
5758

5859
All the above options can be added to serverless.yml to set default configuration: e.g.
@@ -63,11 +64,12 @@ custom:
6364
start:
6465
port: 8000
6566
inMemory: true
66-
migration: true
67+
mirate: true
68+
seed: true
6769
```
6870

6971
## Migrations: sls dynamodb migrate
70-
### Configurations
72+
### Configuration
7173
In `serverless.yml` add following to execute all the migration upon DynamoDB Local Start
7274
```yml
7375
custom:
@@ -94,6 +96,38 @@ resources:
9496
WriteCapacityUnits: 1
9597
```
9698

99+
## Seeding: sls dynamodb seed
100+
### Configuration
101+
102+
In `serverless.yml` seeding categories are defined under `dynamodb.seed`.
103+
104+
If `dynamodb.start.seed` is true, then seeding is performed after table migrations.
105+
106+
```yml
107+
dynamodb:
108+
start:
109+
seed: true
110+
111+
seed:
112+
domain:
113+
sources:
114+
- table: domain-widgets
115+
sources: [./domainWidgets.json]
116+
- table: domain-fidgets
117+
sources: [./domainFidgets.json]
118+
test:
119+
sources:
120+
- table: users
121+
sources: [./fake-test-users.json]
122+
- table: subscriptions
123+
sources: [./fake-test-subscriptions.json]
124+
```
125+
126+
```bash
127+
> sls dynamodb seed --seed=domain,test
128+
> sls dynamodb start --seed=domain,test
129+
```
130+
97131
## Using DynamoDB Local in your code
98132
You need to add the following parameters to the AWS NODE SDK dynamodb constructor
99133

index.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,11 @@ class ServerlessDynamodbLocal {
5656
},
5757
migrate: {
5858
shortcut: "m",
59-
usage: "After starting dynamodb local, create DynamoDB tables from the current serverless configuration"
59+
usage: "After starting dynamodb local, create DynamoDB tables from the current serverless configuration."
6060
},
6161
seed: {
6262
shortcut: "s",
63-
usage: "After starting and migrating dynamodb local, injects seed data into your tables",
63+
usage: "After starting and migrating dynamodb local, injects seed data into your tables. The --seed option determines which data categories to onload.",
6464
}
6565
}
6666
},
@@ -170,7 +170,15 @@ class ServerlessDynamodbLocal {
170170
*/
171171
get seedSources() {
172172
const config = this.service.custom.dynamodb;
173-
return _.get(config, "start.seeds", []);
173+
const seedConfig = _.get(config, "seed", {});
174+
const { seed } = this.options;
175+
if (!seed) {
176+
console.log("No seed option defined. Cannot seed data.")
177+
return [];
178+
}
179+
const categories = seed.split(",");
180+
const sourcesByCategory = categories.map(category => seedConfig[category].sources);
181+
return [].concat.apply([], sourcesByCategory);
174182
}
175183

176184
createTable(dynamodb, migration) {

0 commit comments

Comments
 (0)