Skip to content

Commit 093b4f2

Browse files
committed
2 parents f350dd2 + f04c2b7 commit 093b4f2

File tree

3 files changed

+19
-14
lines changed

3 files changed

+19
-14
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ In `serverless.yml` seeding categories are defined under `dynamodb.seed`.
107107

108108
If `dynamodb.start.seed` is true, then seeding is performed after table migrations.
109109

110+
If 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:`.
111+
110112
```yml
111113
dynamodb:
112114
start:
@@ -122,7 +124,7 @@ dynamodb:
122124
test:
123125
sources:
124126
- table: users
125-
sources: [./fake-test-users.json]
127+
rawsources: [./fake-test-users.json]
126128
- table: subscriptions
127129
sources: [./fake-test-subscriptions.json]
128130
```

index.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -153,14 +153,17 @@ class ServerlessDynamodbLocal {
153153

154154
seedHandler() {
155155
const options = this.options;
156-
const documentClient = this.dynamodbOptions(options).doc;
157-
const seedSources = this.seedSources;
158-
return BbPromise.each(seedSources, (source) => {
156+
const dynamodb = this.dynamodbOptions(options);
157+
158+
return BbPromise.each(this.seedSources, (source) => {
159159
if (!source.table) {
160160
throw new Error("seeding source \"table\" property not defined");
161161
}
162-
return seeder.locateSeeds(source.sources || [])
163-
.then((seeds) => seeder.writeSeeds(documentClient, source.table, seeds));
162+
const seedPromise = seeder.locateSeeds(source.sources || [])
163+
.then((seeds) => seeder.writeSeeds(dynamodb.doc.batchWrite.bind(dynamodb.doc), source.table, seeds));
164+
const rawSeedPromise = seeder.locateSeeds(source.rawsources || [])
165+
.then((seeds) => seeder.writeSeeds(dynamodb.raw.batchWriteItem.bind(dynamodb.raw), source.table, seeds));
166+
return BbPromise.all([seedPromise, rawSeedPromise]);
164167
});
165168
}
166169

src/seeder.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ const MIGRATION_SEED_CONCURRENCY = 5;
1515
/**
1616
* Writes a batch chunk of migration seeds to DynamoDB. DynamoDB has a limit on the number of
1717
* items that may be written in a batch operation.
18-
* @param {DynamoDocumentClient} dynamodb The DynamoDB Document client
18+
* @param {function} dynamodbWriteFunction The DynamoDB DocumentClient.batchWrite or DynamoDB.batchWriteItem function
1919
* @param {string} tableName The table name being written to
2020
* @param {any[]} seeds The migration seeds being written to the table
2121
*/
22-
function writeSeedBatch(dynamodb, tableName, seeds) {
22+
function writeSeedBatch(dynamodbWriteFunction, tableName, seeds) {
2323
const params = {
2424
RequestItems: {
2525
[tableName]: seeds.map((seed) => ({
@@ -34,7 +34,7 @@ function writeSeedBatch(dynamodb, tableName, seeds) {
3434
// again a few times in case the Database resources are in the middle of provisioning.
3535
let interval = 0;
3636
function execute(interval) {
37-
setTimeout(() => dynamodb.batchWrite(params, (err) => {
37+
setTimeout(() => dynamodbWriteFunction(params, (err) => {
3838
if (err) {
3939
if (err.code === "ResourceNotFoundException" && interval <= 5000) {
4040
execute(interval + 1000);
@@ -52,13 +52,13 @@ function writeSeedBatch(dynamodb, tableName, seeds) {
5252

5353
/**
5454
* Writes a seed corpus to the given database table
55-
* @param {DocumentClient} dynamodb The DynamoDB document instance
55+
* @param {function} dynamodbWriteFunction The DynamoDB DocumentClient.batchWrite or DynamoDB.batchWriteItem function
5656
* @param {string} tableName The table name
5757
* @param {any[]} seeds The seed values
5858
*/
59-
function writeSeeds(dynamodb, tableName, seeds) {
60-
if (!dynamodb) {
61-
throw new Error("dynamodb argument must be provided");
59+
function writeSeeds(dynamodbWriteFunction, tableName, seeds) {
60+
if (!dynamodbWriteFunction) {
61+
throw new Error("dynamodbWriteFunction argument must be provided");
6262
}
6363
if (!tableName) {
6464
throw new Error("table name argument must be provided");
@@ -71,7 +71,7 @@ function writeSeeds(dynamodb, tableName, seeds) {
7171
const seedChunks = _.chunk(seeds, MAX_MIGRATION_CHUNK);
7272
return BbPromise.map(
7373
seedChunks,
74-
(chunk) => writeSeedBatch(dynamodb, tableName, chunk),
74+
(chunk) => writeSeedBatch(dynamodbWriteFunction, tableName, chunk),
7575
{ concurrency: MIGRATION_SEED_CONCURRENCY }
7676
)
7777
.then(() => console.log("Seed running complete for table: " + tableName));

0 commit comments

Comments
 (0)