@@ -19,145 +19,6 @@ function getTable (resourceConfig) {
1919 return resourceConfig . table || underscore ( resourceConfig . name )
2020}
2121
22- function filterQuery ( resourceConfig , params , options ) {
23- let table = getTable ( resourceConfig )
24- let query = options && options . transaction || this . query
25- query = query . select ( `${ table } .*` ) . from ( table )
26- params = params || { }
27- params . where = params . where || { }
28- params . orderBy = params . orderBy || params . sort
29- params . skip = params . skip || params . offset
30-
31- let joinedTables = [ ]
32-
33- DSUtils . forEach ( DSUtils . keys ( params ) , k => {
34- let v = params [ k ]
35- if ( ! DSUtils . contains ( reserved , k ) ) {
36- if ( DSUtils . isObject ( v ) ) {
37- params . where [ k ] = v
38- } else {
39- params . where [ k ] = {
40- '==' : v
41- }
42- }
43- delete params [ k ]
44- }
45- } )
46-
47- if ( ! DSUtils . isEmpty ( params . where ) ) {
48- DSUtils . forOwn ( params . where , ( criteria , field ) => {
49- if ( ! DSUtils . isObject ( criteria ) ) {
50- params . where [ field ] = {
51- '==' : criteria
52- }
53- }
54-
55- DSUtils . forOwn ( criteria , ( v , op ) => {
56- if ( DSUtils . contains ( field , '.' ) ) {
57- let parts = field . split ( '.' )
58- let localResourceConfig = resourceConfig
59-
60- let relationPath = [ ]
61- while ( parts . length >= 2 ) {
62- let relationName = parts . shift ( )
63- let relationResourceConfig = resourceConfig . getResource ( relationName )
64- relationPath . push ( relationName )
65-
66- if ( ! joinedTables . some ( t => t === relationPath . join ( '.' ) ) ) {
67- let [ relation ] = localResourceConfig . relationList . filter ( r => r . relation === relationName )
68- if ( relation ) {
69- let table = getTable ( localResourceConfig )
70- let localId = `${ table } .${ relation . localKey } `
71-
72- let relationTable = getTable ( relationResourceConfig )
73- let foreignId = `${ relationTable } .${ relationResourceConfig . idAttribute } `
74-
75- query = query . join ( relationTable , localId , foreignId )
76- joinedTables . push ( relationPath . join ( '.' ) )
77- } else {
78- // local column
79- }
80- }
81- localResourceConfig = relationResourceConfig
82- }
83-
84- field = `${ getTable ( localResourceConfig ) } .${ parts [ 0 ] } `
85- }
86-
87- if ( op === '==' || op === '===' ) {
88- query = query . where ( field , v )
89- } else if ( op === '!=' || op === '!==' ) {
90- query = query . where ( field , '!=' , v )
91- } else if ( op === '>' ) {
92- query = query . where ( field , '>' , v )
93- } else if ( op === '>=' ) {
94- query = query . where ( field , '>=' , v )
95- } else if ( op === '<' ) {
96- query = query . where ( field , '<' , v )
97- } else if ( op === '<=' ) {
98- query = query . where ( field , '<=' , v )
99- // } else if (op === 'isectEmpty') {
100- // subQuery = subQuery ? subQuery.and(row(field).default([]).setIntersection(r.expr(v).default([])).count().eq(0)) : row(field).default([]).setIntersection(r.expr(v).default([])).count().eq(0)
101- // } else if (op === 'isectNotEmpty') {
102- // subQuery = subQuery ? subQuery.and(row(field).default([]).setIntersection(r.expr(v).default([])).count().ne(0)) : row(field).default([]).setIntersection(r.expr(v).default([])).count().ne(0)
103- } else if ( op === 'in' ) {
104- query = query . where ( field , 'in' , v )
105- } else if ( op === 'notIn' ) {
106- query = query . whereNotIn ( field , v )
107- } else if ( op === 'like' ) {
108- query = query . where ( field , 'like' , v )
109- } else if ( op === '|==' || op === '|===' ) {
110- query = query . orWhere ( field , v )
111- } else if ( op === '|!=' || op === '|!==' ) {
112- query = query . orWhere ( field , '!=' , v )
113- } else if ( op === '|>' ) {
114- query = query . orWhere ( field , '>' , v )
115- } else if ( op === '|>=' ) {
116- query = query . orWhere ( field , '>=' , v )
117- } else if ( op === '|<' ) {
118- query = query . orWhere ( field , '<' , v )
119- } else if ( op === '|<=' ) {
120- query = query . orWhere ( field , '<=' , v )
121- // } else if (op === '|isectEmpty') {
122- // subQuery = subQuery ? subQuery.or(row(field).default([]).setIntersection(r.expr(v).default([])).count().eq(0)) : row(field).default([]).setIntersection(r.expr(v).default([])).count().eq(0)
123- // } else if (op === '|isectNotEmpty') {
124- // subQuery = subQuery ? subQuery.or(row(field).default([]).setIntersection(r.expr(v).default([])).count().ne(0)) : row(field).default([]).setIntersection(r.expr(v).default([])).count().ne(0)
125- } else if ( op === '|in' ) {
126- query = query . orWhere ( field , 'in' , v )
127- } else if ( op === '|notIn' ) {
128- query = query . orWhereNotIn ( field , v )
129- } else {
130- throw new Error ( 'Operator not found' )
131- }
132- } )
133- } )
134- }
135-
136- if ( params . orderBy ) {
137- if ( DSUtils . isString ( params . orderBy ) ) {
138- params . orderBy = [
139- [ params . orderBy , 'asc' ]
140- ]
141- }
142- for ( var i = 0 ; i < params . orderBy . length ; i ++ ) {
143- if ( DSUtils . isString ( params . orderBy [ i ] ) ) {
144- params . orderBy [ i ] = [ params . orderBy [ i ] , 'asc' ]
145- }
146- query = DSUtils . upperCase ( params . orderBy [ i ] [ 1 ] ) === 'DESC' ? query . orderBy ( params . orderBy [ i ] [ 0 ] , 'desc' ) : query . orderBy ( params . orderBy [ i ] [ 0 ] , 'asc' )
147- }
148- }
149-
150- if ( params . skip ) {
151- query = query . offset ( + params . offset )
152- }
153-
154- if ( params . limit ) {
155- query = query . limit ( + params . limit )
156- }
157-
158- return query
159- }
160-
16122function loadWithRelations ( items , resourceConfig , options ) {
16223 let tasks = [ ]
16324 let instance = Array . isArray ( items ) ? null : items
@@ -322,7 +183,7 @@ class DSSqlAdapter {
322183 let items = null
323184 options = options || { }
324185 options . with = options . with || [ ]
325- return filterQuery . call ( this , resourceConfig , params , options ) . then ( _items => {
186+ return this . filterQuery ( resourceConfig , params , options ) . then ( _items => {
326187 items = _items
327188 return loadWithRelations . call ( this , _items , resourceConfig , options )
328189 } ) . then ( ( ) => items )
@@ -355,15 +216,15 @@ class DSSqlAdapter {
355216
356217 updateAll ( resourceConfig , attrs , params , options ) {
357218 attrs = DSUtils . removeCircular ( DSUtils . omit ( attrs , resourceConfig . relationFields || [ ] ) )
358- return filterQuery . call ( this , resourceConfig , params , options ) . then ( items => {
219+ return this . filterQuery ( resourceConfig , params , options ) . then ( items => {
359220 return map ( items , item => item [ resourceConfig . idAttribute ] )
360221 } ) . then ( ids => {
361- return filterQuery . call ( this , resourceConfig , params , options ) . update ( attrs ) . then ( ( ) => {
222+ return this . filterQuery ( resourceConfig , params , options ) . update ( attrs ) . then ( ( ) => {
362223 let _params = { where : { } }
363224 _params . where [ resourceConfig . idAttribute ] = {
364225 'in' : ids
365226 }
366- return filterQuery . call ( this , resourceConfig , _params , options )
227+ return this . filterQuery ( resourceConfig , _params , options )
367228 } )
368229 } )
369230 }
@@ -376,7 +237,146 @@ class DSSqlAdapter {
376237 }
377238
378239 destroyAll ( resourceConfig , params , options ) {
379- return filterQuery . call ( this , resourceConfig , params , options ) . del ( ) . then ( ( ) => undefined )
240+ return this . filterQuery ( resourceConfig , params , options ) . del ( ) . then ( ( ) => undefined )
241+ }
242+
243+ filterQuery ( resourceConfig , params , options ) {
244+ let table = getTable ( resourceConfig )
245+ let query = options && options . transaction || this . query
246+ query = query . select ( `${ table } .*` ) . from ( table )
247+ params = params || { }
248+ params . where = params . where || { }
249+ params . orderBy = params . orderBy || params . sort
250+ params . skip = params . skip || params . offset
251+
252+ let joinedTables = [ ]
253+
254+ DSUtils . forEach ( DSUtils . keys ( params ) , k => {
255+ let v = params [ k ]
256+ if ( ! DSUtils . contains ( reserved , k ) ) {
257+ if ( DSUtils . isObject ( v ) ) {
258+ params . where [ k ] = v
259+ } else {
260+ params . where [ k ] = {
261+ '==' : v
262+ }
263+ }
264+ delete params [ k ]
265+ }
266+ } )
267+
268+ if ( ! DSUtils . isEmpty ( params . where ) ) {
269+ DSUtils . forOwn ( params . where , ( criteria , field ) => {
270+ if ( ! DSUtils . isObject ( criteria ) ) {
271+ params . where [ field ] = {
272+ '==' : criteria
273+ }
274+ }
275+
276+ DSUtils . forOwn ( criteria , ( v , op ) => {
277+ if ( DSUtils . contains ( field , '.' ) ) {
278+ let parts = field . split ( '.' )
279+ let localResourceConfig = resourceConfig
280+
281+ let relationPath = [ ]
282+ while ( parts . length >= 2 ) {
283+ let relationName = parts . shift ( )
284+ let relationResourceConfig = resourceConfig . getResource ( relationName )
285+ relationPath . push ( relationName )
286+
287+ if ( ! joinedTables . some ( t => t === relationPath . join ( '.' ) ) ) {
288+ let [ relation ] = localResourceConfig . relationList . filter ( r => r . relation === relationName )
289+ if ( relation ) {
290+ let table = getTable ( localResourceConfig )
291+ let localId = `${ table } .${ relation . localKey } `
292+
293+ let relationTable = getTable ( relationResourceConfig )
294+ let foreignId = `${ relationTable } .${ relationResourceConfig . idAttribute } `
295+
296+ query = query . join ( relationTable , localId , foreignId )
297+ joinedTables . push ( relationPath . join ( '.' ) )
298+ } else {
299+ // local column
300+ }
301+ }
302+ localResourceConfig = relationResourceConfig
303+ }
304+
305+ field = `${ getTable ( localResourceConfig ) } .${ parts [ 0 ] } `
306+ }
307+
308+ if ( op === '==' || op === '===' ) {
309+ query = query . where ( field , v )
310+ } else if ( op === '!=' || op === '!==' ) {
311+ query = query . where ( field , '!=' , v )
312+ } else if ( op === '>' ) {
313+ query = query . where ( field , '>' , v )
314+ } else if ( op === '>=' ) {
315+ query = query . where ( field , '>=' , v )
316+ } else if ( op === '<' ) {
317+ query = query . where ( field , '<' , v )
318+ } else if ( op === '<=' ) {
319+ query = query . where ( field , '<=' , v )
320+ // } else if (op === 'isectEmpty') {
321+ // subQuery = subQuery ? subQuery.and(row(field).default([]).setIntersection(r.expr(v).default([])).count().eq(0)) : row(field).default([]).setIntersection(r.expr(v).default([])).count().eq(0)
322+ // } else if (op === 'isectNotEmpty') {
323+ // subQuery = subQuery ? subQuery.and(row(field).default([]).setIntersection(r.expr(v).default([])).count().ne(0)) : row(field).default([]).setIntersection(r.expr(v).default([])).count().ne(0)
324+ } else if ( op === 'in' ) {
325+ query = query . where ( field , 'in' , v )
326+ } else if ( op === 'notIn' ) {
327+ query = query . whereNotIn ( field , v )
328+ } else if ( op === 'like' ) {
329+ query = query . where ( field , 'like' , v )
330+ } else if ( op === '|==' || op === '|===' ) {
331+ query = query . orWhere ( field , v )
332+ } else if ( op === '|!=' || op === '|!==' ) {
333+ query = query . orWhere ( field , '!=' , v )
334+ } else if ( op === '|>' ) {
335+ query = query . orWhere ( field , '>' , v )
336+ } else if ( op === '|>=' ) {
337+ query = query . orWhere ( field , '>=' , v )
338+ } else if ( op === '|<' ) {
339+ query = query . orWhere ( field , '<' , v )
340+ } else if ( op === '|<=' ) {
341+ query = query . orWhere ( field , '<=' , v )
342+ // } else if (op === '|isectEmpty') {
343+ // subQuery = subQuery ? subQuery.or(row(field).default([]).setIntersection(r.expr(v).default([])).count().eq(0)) : row(field).default([]).setIntersection(r.expr(v).default([])).count().eq(0)
344+ // } else if (op === '|isectNotEmpty') {
345+ // subQuery = subQuery ? subQuery.or(row(field).default([]).setIntersection(r.expr(v).default([])).count().ne(0)) : row(field).default([]).setIntersection(r.expr(v).default([])).count().ne(0)
346+ } else if ( op === '|in' ) {
347+ query = query . orWhere ( field , 'in' , v )
348+ } else if ( op === '|notIn' ) {
349+ query = query . orWhereNotIn ( field , v )
350+ } else {
351+ throw new Error ( 'Operator not found' )
352+ }
353+ } )
354+ } )
355+ }
356+
357+ if ( params . orderBy ) {
358+ if ( DSUtils . isString ( params . orderBy ) ) {
359+ params . orderBy = [
360+ [ params . orderBy , 'asc' ]
361+ ]
362+ }
363+ for ( var i = 0 ; i < params . orderBy . length ; i ++ ) {
364+ if ( DSUtils . isString ( params . orderBy [ i ] ) ) {
365+ params . orderBy [ i ] = [ params . orderBy [ i ] , 'asc' ]
366+ }
367+ query = DSUtils . upperCase ( params . orderBy [ i ] [ 1 ] ) === 'DESC' ? query . orderBy ( params . orderBy [ i ] [ 0 ] , 'desc' ) : query . orderBy ( params . orderBy [ i ] [ 0 ] , 'asc' )
368+ }
369+ }
370+
371+ if ( params . skip ) {
372+ query = query . offset ( + params . offset )
373+ }
374+
375+ if ( params . limit ) {
376+ query = query . limit ( + params . limit )
377+ }
378+
379+ return query
380380 }
381381}
382382
0 commit comments