@@ -30,9 +30,10 @@ var reserved = [
3030 'where'
3131] ;
3232
33- function filterQuery ( resourceConfig , params ) {
33+ function filterQuery ( resourceConfig , params , options ) {
3434 var r = this . r ;
3535 params = params || { } ;
36+ options = options || { } ;
3637 params . where = params . where || { } ;
3738 params . orderBy = params . orderBy || params . sort ;
3839 params . skip = params . skip || params . offset ;
@@ -51,7 +52,7 @@ function filterQuery(resourceConfig, params) {
5152 }
5253 } ) ;
5354
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 ) ) ;
5556 var subQuery ;
5657
5758 forOwn ( params . where , function ( criteria , field ) {
@@ -131,12 +132,41 @@ function DSRethinkDBAdapter(options) {
131132 this . defaults = new Defaults ( ) ;
132133 deepMixIn ( this . defaults , options ) ;
133134 this . r = rethinkdbdash ( this . defaults ) ;
135+ this . databases = { } ;
136+ this . tables = { } ;
134137}
135138
136139var dsRethinkDBAdapterPrototype = DSRethinkDBAdapter . prototype ;
137140
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 ) {
140170 if ( ! item ) {
141171 throw new Error ( 'Not Found!' ) ;
142172 } else {
@@ -145,25 +175,41 @@ dsRethinkDBAdapterPrototype.find = function find(resourceConfig, id) {
145175 } ) ;
146176} ;
147177
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+ } ) ;
150184} ;
151185
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 ) {
154192 return cursor . changes [ 0 ] . new_val ;
155193 } ) ;
156194} ;
157195
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 ) {
160202 return cursor . changes [ 0 ] . new_val ;
161203 } ) ;
162204} ;
163205
164- dsRethinkDBAdapterPrototype . updateAll = function ( resourceConfig , attrs , params ) {
206+ dsRethinkDBAdapterPrototype . updateAll = function ( resourceConfig , attrs , params , options ) {
207+ var _this = this ;
208+ options = options || { } ;
165209 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 ) {
167213 var items = [ ] ;
168214 cursor . changes . forEach ( function ( change ) {
169215 items . push ( change . new_val ) ;
@@ -172,15 +218,23 @@ dsRethinkDBAdapterPrototype.updateAll = function (resourceConfig, attrs, params)
172218 } ) ;
173219} ;
174220
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 ( ) {
177227 return undefined ;
178228 } ) ;
179229} ;
180230
181- dsRethinkDBAdapterPrototype . destroyAll = function ( resourceConfig , params ) {
231+ dsRethinkDBAdapterPrototype . destroyAll = function ( resourceConfig , params , options ) {
232+ var _this = this ;
233+ options = options || { } ;
182234 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 ( ) {
184238 return undefined ;
185239 } ) ;
186240} ;
0 commit comments