@@ -30,9 +30,10 @@ var reserved = [
30
30
'where'
31
31
] ;
32
32
33
- function filterQuery ( resourceConfig , params ) {
33
+ function filterQuery ( resourceConfig , params , options ) {
34
34
var r = this . r ;
35
35
params = params || { } ;
36
+ options = options || { } ;
36
37
params . where = params . where || { } ;
37
38
params . orderBy = params . orderBy || params . sort ;
38
39
params . skip = params . skip || params . offset ;
@@ -51,7 +52,7 @@ function filterQuery(resourceConfig, params) {
51
52
}
52
53
} ) ;
53
54
54
- var query = r . db ( this . defaults . db ) . table ( resourceConfig . table || underscore ( resourceConfig . name ) ) ;
55
+ var query = r . db ( options . db || this . defaults . db ) . table ( resourceConfig . table || underscore ( resourceConfig . name ) ) ;
55
56
var subQuery ;
56
57
57
58
forOwn ( params . where , function ( criteria , field ) {
@@ -131,12 +132,41 @@ function DSRethinkDBAdapter(options) {
131
132
this . defaults = new Defaults ( ) ;
132
133
deepMixIn ( this . defaults , options ) ;
133
134
this . r = rethinkdbdash ( this . defaults ) ;
135
+ this . databases = { } ;
136
+ this . tables = { } ;
134
137
}
135
138
136
139
var dsRethinkDBAdapterPrototype = DSRethinkDBAdapter . prototype ;
137
140
138
- dsRethinkDBAdapterPrototype . find = function find ( resourceConfig , id ) {
139
- return this . r . db ( this . defaults . db ) . table ( resourceConfig . table || underscore ( resourceConfig . name ) ) . get ( id ) . run ( ) . then ( function ( item ) {
141
+ dsRethinkDBAdapterPrototype . waitForDb = function waitForDb ( options ) {
142
+ var _this = this ;
143
+ options = options || { } ;
144
+ var db = options . db || _this . defaults . db ;
145
+ if ( ! _this . databases [ db ] ) {
146
+ _this . databases [ db ] = _this . r . branch ( _this . r . dbList ( ) . contains ( db ) , true , _this . r . dbCreate ( db ) ) . run ( ) ;
147
+ }
148
+ return _this . databases [ db ] ;
149
+ } ;
150
+
151
+ dsRethinkDBAdapterPrototype . waitForTable = function waitForTable ( table , options ) {
152
+ var _this = this ;
153
+ options = options || { } ;
154
+ var db = options . db || _this . defaults . db ;
155
+ return _this . waitForDb ( options ) . then ( function ( ) {
156
+ _this . tables [ db ] = _this . tables [ db ] || { } ;
157
+ if ( ! _this . tables [ db ] [ table ] ) {
158
+ _this . tables [ db ] [ table ] = _this . r . branch ( _this . r . db ( db ) . tableList ( ) . contains ( table ) , true , _this . r . db ( db ) . tableCreate ( table ) ) . run ( ) ;
159
+ }
160
+ return _this . tables [ db ] [ table ] ;
161
+ } ) ;
162
+ } ;
163
+
164
+ dsRethinkDBAdapterPrototype . find = function find ( resourceConfig , id , options ) {
165
+ var _this = this ;
166
+ options = options || { } ;
167
+ return _this . waitForTable ( resourceConfig . table || underscore ( resourceConfig . name ) , options ) . then ( function ( ) {
168
+ return _this . r . db ( options . db || _this . defaults . db ) . table ( resourceConfig . table || underscore ( resourceConfig . name ) ) . get ( id ) . run ( ) ;
169
+ } ) . then ( function ( item ) {
140
170
if ( ! item ) {
141
171
throw new Error ( 'Not Found!' ) ;
142
172
} else {
@@ -145,25 +175,41 @@ dsRethinkDBAdapterPrototype.find = function find(resourceConfig, id) {
145
175
} ) ;
146
176
} ;
147
177
148
- dsRethinkDBAdapterPrototype . findAll = function ( resourceConfig , params ) {
149
- return filterQuery . call ( this , resourceConfig , params ) . run ( ) ;
178
+ dsRethinkDBAdapterPrototype . findAll = function ( resourceConfig , params , options ) {
179
+ var _this = this ;
180
+ options = options || { } ;
181
+ return _this . waitForTable ( resourceConfig . table || underscore ( resourceConfig . name ) , options ) . then ( function ( ) {
182
+ return filterQuery . call ( _this , resourceConfig , params , options ) . run ( ) ;
183
+ } ) ;
150
184
} ;
151
185
152
- dsRethinkDBAdapterPrototype . create = function ( resourceConfig , attrs ) {
153
- return this . r . db ( this . defaults . db ) . table ( resourceConfig . table || underscore ( resourceConfig . name ) ) . insert ( attrs , { returnChanges : true } ) . run ( ) . then ( function ( cursor ) {
186
+ dsRethinkDBAdapterPrototype . create = function ( resourceConfig , attrs , options ) {
187
+ var _this = this ;
188
+ options = options || { } ;
189
+ return _this . waitForTable ( resourceConfig . table || underscore ( resourceConfig . name ) , options ) . then ( function ( ) {
190
+ return _this . r . db ( options . db || _this . defaults . db ) . table ( resourceConfig . table || underscore ( resourceConfig . name ) ) . insert ( attrs , { returnChanges : true } ) . run ( ) ;
191
+ } ) . then ( function ( cursor ) {
154
192
return cursor . changes [ 0 ] . new_val ;
155
193
} ) ;
156
194
} ;
157
195
158
- dsRethinkDBAdapterPrototype . update = function ( resourceConfig , id , attrs ) {
159
- return this . r . db ( this . defaults . db ) . table ( resourceConfig . table || underscore ( resourceConfig . name ) ) . get ( id ) . update ( attrs , { returnChanges : true } ) . run ( ) . then ( function ( cursor ) {
196
+ dsRethinkDBAdapterPrototype . update = function ( resourceConfig , id , attrs , options ) {
197
+ var _this = this ;
198
+ options = options || { } ;
199
+ return _this . waitForTable ( resourceConfig . table || underscore ( resourceConfig . name ) , options ) . then ( function ( ) {
200
+ return _this . r . db ( options . db || _this . defaults . db ) . table ( resourceConfig . table || underscore ( resourceConfig . name ) ) . get ( id ) . update ( attrs , { returnChanges : true } ) . run ( ) ;
201
+ } ) . then ( function ( cursor ) {
160
202
return cursor . changes [ 0 ] . new_val ;
161
203
} ) ;
162
204
} ;
163
205
164
- dsRethinkDBAdapterPrototype . updateAll = function ( resourceConfig , attrs , params ) {
206
+ dsRethinkDBAdapterPrototype . updateAll = function ( resourceConfig , attrs , params , options ) {
207
+ var _this = this ;
208
+ options = options || { } ;
165
209
params = params || { } ;
166
- return filterQuery . call ( this , resourceConfig , params ) . update ( attrs , { returnChanges : true } ) . run ( ) . then ( function ( cursor ) {
210
+ return _this . waitForTable ( resourceConfig . table || underscore ( resourceConfig . name ) , options ) . then ( function ( ) {
211
+ return filterQuery . call ( _this , resourceConfig , params , options ) . update ( attrs , { returnChanges : true } ) . run ( ) ;
212
+ } ) . then ( function ( cursor ) {
167
213
var items = [ ] ;
168
214
cursor . changes . forEach ( function ( change ) {
169
215
items . push ( change . new_val ) ;
@@ -172,15 +218,23 @@ dsRethinkDBAdapterPrototype.updateAll = function (resourceConfig, attrs, params)
172
218
} ) ;
173
219
} ;
174
220
175
- dsRethinkDBAdapterPrototype . destroy = function ( resourceConfig , id ) {
176
- return this . r . db ( this . defaults . db ) . table ( resourceConfig . table || underscore ( resourceConfig . name ) ) . get ( id ) . delete ( ) . run ( ) . then ( function ( ) {
221
+ dsRethinkDBAdapterPrototype . destroy = function ( resourceConfig , id , options ) {
222
+ var _this = this ;
223
+ options = options || { } ;
224
+ return _this . waitForTable ( resourceConfig . table || underscore ( resourceConfig . name ) , options ) . then ( function ( ) {
225
+ return _this . r . db ( options . db || _this . defaults . db ) . table ( resourceConfig . table || underscore ( resourceConfig . name ) ) . get ( id ) . delete ( ) . run ( ) ;
226
+ } ) . then ( function ( ) {
177
227
return undefined ;
178
228
} ) ;
179
229
} ;
180
230
181
- dsRethinkDBAdapterPrototype . destroyAll = function ( resourceConfig , params ) {
231
+ dsRethinkDBAdapterPrototype . destroyAll = function ( resourceConfig , params , options ) {
232
+ var _this = this ;
233
+ options = options || { } ;
182
234
params = params || { } ;
183
- return filterQuery . call ( this , resourceConfig , params ) . delete ( ) . run ( ) . then ( function ( ) {
235
+ return _this . waitForTable ( resourceConfig . table || underscore ( resourceConfig . name ) , options ) . then ( function ( ) {
236
+ return filterQuery . call ( _this , resourceConfig , params , options ) . delete ( ) . run ( ) ;
237
+ } ) . then ( function ( ) {
184
238
return undefined ;
185
239
} ) ;
186
240
} ;
0 commit comments