11'use strict' ;
2-
3- const _ = require ( 'lodash' ) ,
4- BbPromise = require ( 'bluebird' ) ,
5- AWS = require ( 'aws-sdk' ) ,
6- dynamodbLocal = require ( 'dynamodb-localhost ' ) ;
2+ const _ = require ( 'lodash' ) ;
3+ const BbPromise = require ( 'bluebird' ) ;
4+ const AWS = require ( 'aws-sdk' ) ;
5+ const dynamodbLocal = require ( 'dynamodb-localhost' ) ;
6+ const { writeSeeds , locateSeeds } = require ( './src/seeder ' ) ;
77
88class ServerlessDynamodbLocal {
99 constructor ( serverless , options ) {
@@ -18,6 +18,10 @@ class ServerlessDynamodbLocal {
1818 lifecycleEvents : [ 'migrateHandler' ] ,
1919 usage : 'Creates local DynamoDB tables from the current Serverless configuration'
2020 } ,
21+ seed : {
22+ lifecycleEvents : [ 'seedHandler' ] ,
23+ usage : 'Seeds local DynamoDB tables with data'
24+ } ,
2125 start : {
2226 lifecycleEvents : [ 'startHandler' ] ,
2327 usage : 'Starts local DynamoDB' ,
@@ -53,6 +57,10 @@ class ServerlessDynamodbLocal {
5357 migrate : {
5458 shortcut : 'm' ,
5559 usage : 'After starting dynamodb local, create DynamoDB tables from the current serverless configuration'
60+ } ,
61+ seed : {
62+ shortcut : 's' ,
63+ usage : 'After starting and migrating dynamodb local, injects seed data into your tables' ,
5664 }
5765 }
5866 } ,
@@ -77,23 +85,23 @@ class ServerlessDynamodbLocal {
7785
7886 this . hooks = {
7987 'dynamodb:migrate:migrateHandler' : this . migrateHandler . bind ( this ) ,
88+ 'dynamodb:migrate:seedHandler' : this . seedHandler . bind ( this ) ,
8089 'dynamodb:remove:removeHandler' : this . removeHandler . bind ( this ) ,
8190 'dynamodb:install:installHandler' : this . installHandler . bind ( this ) ,
8291 'dynamodb:start:startHandler' : this . startHandler . bind ( this ) ,
83- 'before:offline:start' : this . startHandler . bind ( this ) ,
92+ 'before:offline:start:init ' : this . startHandler . bind ( this ) ,
8493 } ;
8594 }
8695
8796 dynamodbOptions ( ) {
88- let self = this ;
89- let config = self . service . custom . dynamodb || { } ,
90- port = config . start && config . start . port || 8000 ,
91- dynamoOptions = {
92- endpoint : 'http://localhost:' + port ,
93- region : 'localhost' ,
94- accessKeyId : 'MOCK_ACCESS_KEY_ID' ,
95- secretAccessKey : 'MOCK_SECRET_ACCESS_KEY'
96- } ;
97+ const config = this . service . custom . dynamodb || { } ;
98+ const port = config . start && config . start . port || 8000 ;
99+ const dynamoOptions = {
100+ endpoint : 'http://localhost:' + port ,
101+ region : 'localhost' ,
102+ accessKeyId : 'MOCK_ACCESS_KEY_ID' ,
103+ secretAccessKey : 'MOCK_SECRET_ACCESS_KEY'
104+ } ;
97105
98106 return {
99107 raw : new AWS . DynamoDB ( dynamoOptions ) ,
@@ -102,75 +110,79 @@ class ServerlessDynamodbLocal {
102110 }
103111
104112 migrateHandler ( ) {
105- let self = this ;
106-
107- return new BbPromise ( function ( resolve , reject ) {
108- let dynamodb = self . dynamodbOptions ( ) ;
109-
110- var tables = self . resourceTables ( ) ;
113+ const dynamodb = this . dynamodbOptions ( ) ;
114+ const { tables } = this ;
115+ return BbPromise . each ( tables , table => this . createTable ( dynamodb , table ) ) ;
116+ }
111117
112- return BbPromise . each ( tables , function ( table ) {
113- return self . createTable ( dynamodb , table ) ;
114- } ) . then ( resolve , reject ) ;
118+ seedHandler ( ) {
119+ const { doc : documentClient } = this . dynamodbOptions ( ) ;
120+ const { seedSources } = this ;
121+ return BbPromise . each ( seedSources , source => {
122+ if ( ! source . table ) {
123+ throw new Error ( 'seeding source "table" not defined' ) ;
124+ }
125+ return locateSeeds ( source . sources || [ ] )
126+ . then ( ( seeds ) => writeSeeds ( documentClient , source . table , seeds ) ) ;
115127 } ) ;
116128 }
117129
118130 removeHandler ( ) {
119- return new BbPromise ( function ( resolve ) {
120- dynamodbLocal . remove ( resolve ) ;
121- } ) ;
131+ return new BbPromise ( resolve => dynamodbLocal . remove ( resolve ) ) ;
122132 }
123133
124134 installHandler ( ) {
125- let options = this . options ;
126- return new BbPromise ( function ( resolve ) {
127- dynamodbLocal . install ( resolve , options . localPath ) ;
128- } ) ;
135+ const { options } = this ;
136+ return new BbPromise ( ( resolve ) => dynamodbLocal . install ( resolve , options . localPath ) ) ;
129137 }
130138
131139 startHandler ( ) {
132- let self = this ;
133- return new BbPromise ( function ( resolve ) {
134- let config = self . service . custom . dynamodb ,
135- options = _ . merge ( {
136- sharedDb : self . options . sharedDb || true
137- } ,
138- self . options ,
139- config && config . start
140- ) ;
141- if ( options . migrate ) {
142- dynamodbLocal . start ( options ) ;
143- console . log ( "" ) ; // seperator
144- self . migrateHandler ( true ) ;
145- resolve ( ) ;
146- } else {
147- dynamodbLocal . start ( options ) ;
148- console . log ( "" ) ;
149- resolve ( ) ;
150- }
151- } ) ;
140+ const config = this . service . custom . dynamodb ;
141+ const options = _ . merge ( {
142+ sharedDb : this . options . sharedDb || true
143+ } ,
144+ this . options ,
145+ config && config . start
146+ ) ;
147+
148+ dynamodbLocal . start ( options ) ;
149+ console . log ( "" ) ; // separator
150+
151+ return BbPromise . resolve ( )
152+ . then ( ( ) => options . migrate && this . migrateHandler ( ) )
153+ . then ( ( ) => options . seed && this . seedHandler ( ) ) ;
152154 }
153155
154- resourceTables ( ) {
155- var resources = this . service . resources . Resources ;
156- return Object . keys ( resources ) . map ( function ( key ) {
156+ /**
157+ * Gets the table definitions
158+ */
159+ get tables ( ) {
160+ const resources = this . service . resources . Resources ;
161+ return Object . keys ( resources ) . map ( ( key ) => {
157162 if ( resources [ key ] . Type == 'AWS::DynamoDB::Table' ) {
158163 return resources [ key ] . Properties ;
159164 }
160- } ) . filter ( n => {
161- return n ;
162- } ) ;
165+ } ) . filter ( n => n ) ;
166+ }
167+
168+ /**
169+ * Gets the seeding sources
170+ */
171+ get seedSources ( ) {
172+ const config = this . service . custom . dynamodb ;
173+ return _ . get ( config , 'start.seeds' , [ ] ) ;
163174 }
164175
165176 createTable ( dynamodb , migration ) {
166- return new BbPromise ( function ( resolve ) {
167- dynamodb . raw . createTable ( migration , function ( err ) {
177+ return new BbPromise ( ( resolve , reject ) => {
178+ dynamodb . raw . createTable ( migration , ( err ) => {
168179 if ( err ) {
169180 console . log ( err ) ;
181+ reject ( err ) ;
170182 } else {
171183 console . log ( "Table creation completed for table: " + migration . TableName ) ;
184+ resolve ( migration ) ;
172185 }
173- resolve ( migration ) ;
174186 } ) ;
175187 } ) ;
176188 }
0 commit comments