1
1
'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 ' ) ;
7
7
8
8
class ServerlessDynamodbLocal {
9
9
constructor ( serverless , options ) {
@@ -18,6 +18,10 @@ class ServerlessDynamodbLocal {
18
18
lifecycleEvents : [ 'migrateHandler' ] ,
19
19
usage : 'Creates local DynamoDB tables from the current Serverless configuration'
20
20
} ,
21
+ seed : {
22
+ lifecycleEvents : [ 'seedHandler' ] ,
23
+ usage : 'Seeds local DynamoDB tables with data'
24
+ } ,
21
25
start : {
22
26
lifecycleEvents : [ 'startHandler' ] ,
23
27
usage : 'Starts local DynamoDB' ,
@@ -53,6 +57,10 @@ class ServerlessDynamodbLocal {
53
57
migrate : {
54
58
shortcut : 'm' ,
55
59
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' ,
56
64
}
57
65
}
58
66
} ,
@@ -77,23 +85,23 @@ class ServerlessDynamodbLocal {
77
85
78
86
this . hooks = {
79
87
'dynamodb:migrate:migrateHandler' : this . migrateHandler . bind ( this ) ,
88
+ 'dynamodb:migrate:seedHandler' : this . seedHandler . bind ( this ) ,
80
89
'dynamodb:remove:removeHandler' : this . removeHandler . bind ( this ) ,
81
90
'dynamodb:install:installHandler' : this . installHandler . bind ( this ) ,
82
91
'dynamodb:start:startHandler' : this . startHandler . bind ( this ) ,
83
- 'before:offline:start' : this . startHandler . bind ( this ) ,
92
+ 'before:offline:start:init ' : this . startHandler . bind ( this ) ,
84
93
} ;
85
94
}
86
95
87
96
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
+ } ;
97
105
98
106
return {
99
107
raw : new AWS . DynamoDB ( dynamoOptions ) ,
@@ -102,75 +110,79 @@ class ServerlessDynamodbLocal {
102
110
}
103
111
104
112
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
+ }
111
117
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 ) ) ;
115
127
} ) ;
116
128
}
117
129
118
130
removeHandler ( ) {
119
- return new BbPromise ( function ( resolve ) {
120
- dynamodbLocal . remove ( resolve ) ;
121
- } ) ;
131
+ return new BbPromise ( resolve => dynamodbLocal . remove ( resolve ) ) ;
122
132
}
123
133
124
134
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 ) ) ;
129
137
}
130
138
131
139
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 ( ) ) ;
152
154
}
153
155
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 ) => {
157
162
if ( resources [ key ] . Type == 'AWS::DynamoDB::Table' ) {
158
163
return resources [ key ] . Properties ;
159
164
}
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' , [ ] ) ;
163
174
}
164
175
165
176
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 ) => {
168
179
if ( err ) {
169
180
console . log ( err ) ;
181
+ reject ( err ) ;
170
182
} else {
171
183
console . log ( "Table creation completed for table: " + migration . TableName ) ;
184
+ resolve ( migration ) ;
172
185
}
173
- resolve ( migration ) ;
174
186
} ) ;
175
187
} ) ;
176
188
}
0 commit comments