@@ -24,7 +24,7 @@ function indexToSignature(index) {
2424 return index . ddoc . substring ( 8 ) + '/' + index . name ;
2525}
2626
27- function doAllDocs ( db , originalOpts ) {
27+ async function doAllDocs ( db , originalOpts ) {
2828 var opts = clone ( originalOpts ) ;
2929
3030 // CouchDB responds in weird ways when you provide a non-string to _id;
@@ -56,23 +56,28 @@ function doAllDocs(db, originalOpts) {
5656 opts . limit += opts . indexes_count ;
5757 }
5858
59- return db . allDocs ( opts )
60- . then ( function ( res ) {
61- // filter out any design docs that _all_docs might return
62- res . rows = res . rows . filter ( function ( row ) {
63- return ! / ^ _ d e s i g n \/ / . test ( row . id ) ;
64- } ) ;
65- // put back original limit
66- if ( opts . original_limit ) {
67- opts . limit = opts . original_limit ;
68- }
69- // enforce the rows to respect the given limit
70- res . rows = res . rows . slice ( 0 , opts . limit ) ;
71- return res ;
72- } ) ;
59+ const res = await db . allDocs ( opts ) ;
60+ // filter out any design docs that _all_docs might return
61+ res . rows = res . rows . filter ( function ( row ) {
62+ return ! / ^ _ d e s i g n \/ / . test ( row . id ) ;
63+ } ) ;
64+ // put back original limit
65+ if ( opts . original_limit ) {
66+ opts . limit = opts . original_limit ;
67+ }
68+ // enforce the rows to respect the given limit
69+ res . rows = res . rows . slice ( 0 , opts . limit ) ;
70+ return res ;
7371}
7472
75- function find ( db , requestDef , explain ) {
73+ async function queryAllOrIndex ( db , opts , indexToUse ) {
74+ if ( indexToUse . name === '_all_docs' ) {
75+ return doAllDocs ( db , opts ) ;
76+ }
77+ return abstractMapper ( db ) . query . call ( db , indexToSignature ( indexToUse ) , opts ) ;
78+ }
79+
80+ async function find ( db , requestDef , explain ) {
7681 if ( requestDef . selector ) {
7782 // must be validated before massaging
7883 validateSelector ( requestDef . selector , false ) ;
@@ -89,119 +94,111 @@ function find(db, requestDef, explain) {
8994
9095 validateFindRequest ( requestDef ) ;
9196
92- return getIndexes ( db ) . then ( function ( getIndexesRes ) {
97+ const getIndexesRes = await getIndexes ( db ) ;
9398
94- db . constructor . emit ( 'debug' , [ 'find' , 'planning query' , requestDef ] ) ;
95- var queryPlan = planQuery ( requestDef , getIndexesRes . indexes ) ;
96- db . constructor . emit ( 'debug' , [ 'find' , 'query plan' , queryPlan ] ) ;
99+ db . constructor . emit ( 'debug' , [ 'find' , 'planning query' , requestDef ] ) ;
100+ var queryPlan = planQuery ( requestDef , getIndexesRes . indexes ) ;
101+ db . constructor . emit ( 'debug' , [ 'find' , 'query plan' , queryPlan ] ) ;
97102
98- var indexToUse = queryPlan . index ;
103+ var indexToUse = queryPlan . index ;
99104
100- validateSort ( requestDef , indexToUse ) ;
105+ validateSort ( requestDef , indexToUse ) ;
101106
102- var opts = Object . assign ( {
103- include_docs : true ,
104- reduce : false ,
105- // Add amount of index for doAllDocs to use (related to issue #7810)
106- indexes_count : getIndexesRes . total_rows ,
107- } , queryPlan . queryOpts ) ;
107+ var opts = Object . assign ( {
108+ include_docs : true ,
109+ reduce : false ,
110+ // Add amount of index for doAllDocs to use (related to issue #7810)
111+ indexes_count : getIndexesRes . total_rows ,
112+ } , queryPlan . queryOpts ) ;
108113
109- if ( 'startkey' in opts && 'endkey' in opts &&
110- collate ( opts . startkey , opts . endkey ) > 0 ) {
111- // can't possibly return any results, startkey > endkey
112- /* istanbul ignore next */
113- return { docs : [ ] } ;
114- }
114+ if ( 'startkey' in opts && 'endkey' in opts &&
115+ collate ( opts . startkey , opts . endkey ) > 0 ) {
116+ // can't possibly return any results, startkey > endkey
117+ /* istanbul ignore next */
118+ return { docs : [ ] } ;
119+ }
115120
116- var isDescending = requestDef . sort &&
117- typeof requestDef . sort [ 0 ] !== 'string' &&
118- getValue ( requestDef . sort [ 0 ] ) === 'desc' ;
121+ var isDescending = requestDef . sort &&
122+ typeof requestDef . sort [ 0 ] !== 'string' &&
123+ getValue ( requestDef . sort [ 0 ] ) === 'desc' ;
119124
120- if ( isDescending ) {
121- // either all descending or all ascending
122- opts . descending = true ;
123- opts = reverseOptions ( opts ) ;
124- }
125+ if ( isDescending ) {
126+ // either all descending or all ascending
127+ opts . descending = true ;
128+ opts = reverseOptions ( opts ) ;
129+ }
125130
126- if ( ! queryPlan . inMemoryFields . length ) {
127- // no in-memory filtering necessary, so we can let the
128- // database do the limit/skip for us
129- if ( 'limit' in requestDef ) {
130- opts . limit = requestDef . limit ;
131- }
132- if ( 'skip' in requestDef ) {
133- opts . skip = requestDef . skip ;
134- }
131+ if ( ! queryPlan . inMemoryFields . length ) {
132+ // no in-memory filtering necessary, so we can let the
133+ // database do the limit/skip for us
134+ if ( 'limit' in requestDef ) {
135+ opts . limit = requestDef . limit ;
135136 }
136-
137- if ( explain ) {
138- return Promise . resolve ( queryPlan , opts ) ;
137+ if ( 'skip' in requestDef ) {
138+ opts . skip = requestDef . skip ;
139139 }
140+ }
140141
141- return Promise . resolve ( ) . then ( function ( ) {
142- if ( indexToUse . name === '_all_docs' ) {
143- return doAllDocs ( db , opts ) ;
144- } else {
145- var signature = indexToSignature ( indexToUse ) ;
146- return abstractMapper ( db ) . query . call ( db , signature , opts ) ;
147- }
148- } ) . then ( function ( res ) {
149- if ( opts . inclusive_start === false ) {
150- // may have to manually filter the first one,
151- // since couchdb has no true inclusive_start option
152- res . rows = filterInclusiveStart ( res . rows , opts . startkey , indexToUse ) ;
153- }
142+ if ( explain ) {
143+ return Promise . resolve ( queryPlan , opts ) ;
144+ }
154145
155- if ( queryPlan . inMemoryFields . length ) {
156- // need to filter some stuff in-memory
157- res . rows = filterInMemoryFields ( res . rows , requestDef , queryPlan . inMemoryFields ) ;
158- }
146+ const res = await queryAllOrIndex ( db , opts , indexToUse ) ;
147+
148+
149+ if ( opts . inclusive_start === false ) {
150+ // may have to manually filter the first one,
151+ // since couchdb has no true inclusive_start option
152+ res . rows = filterInclusiveStart ( res . rows , opts . startkey , indexToUse ) ;
153+ }
159154
160- var resp = {
161- docs : res . rows . map ( function ( row ) {
162- var doc = row . doc ;
163- if ( requestDef . fields ) {
164- return pick ( doc , requestDef . fields ) ;
165- }
166- return doc ;
167- } )
168- } ;
169-
170- if ( indexToUse . defaultUsed ) {
171- resp . warning = 'No matching index found, create an index to optimize query time.' ;
155+ if ( queryPlan . inMemoryFields . length ) {
156+ // need to filter some stuff in-memory
157+ res . rows = filterInMemoryFields ( res . rows , requestDef , queryPlan . inMemoryFields ) ;
158+ }
159+
160+ var resp = {
161+ docs : res . rows . map ( function ( row ) {
162+ var doc = row . doc ;
163+ if ( requestDef . fields ) {
164+ return pick ( doc , requestDef . fields ) ;
172165 }
166+ return doc ;
167+ } )
168+ } ;
173169
174- return resp ;
175- } ) ;
176- } ) ;
170+ if ( indexToUse . defaultUsed ) {
171+ resp . warning = 'No matching index found, create an index to optimize query time.' ;
172+ }
173+
174+ return resp ;
177175}
178176
179- function explain ( db , requestDef ) {
180- return find ( db , requestDef , true )
181- . then ( function ( queryPlan ) {
182- return {
183- dbname : db . name ,
184- index : queryPlan . index ,
185- selector : requestDef . selector ,
186- range : {
187- start_key : queryPlan . queryOpts . startkey ,
188- end_key : queryPlan . queryOpts . endkey ,
189- } ,
190- opts : {
191- use_index : requestDef . use_index || [ ] ,
192- bookmark : "nil" , //hardcoded to match CouchDB since its not supported,
193- limit : requestDef . limit ,
194- skip : requestDef . skip ,
195- sort : requestDef . sort || { } ,
196- fields : requestDef . fields ,
197- conflicts : false , //hardcoded to match CouchDB since its not supported,
198- r : [ 49 ] , // hardcoded to match CouchDB since its not support
199- } ,
177+ async function explain ( db , requestDef ) {
178+ const queryPlan = await find ( db , requestDef , true ) ;
179+
180+ return {
181+ dbname : db . name ,
182+ index : queryPlan . index ,
183+ selector : requestDef . selector ,
184+ range : {
185+ start_key : queryPlan . queryOpts . startkey ,
186+ end_key : queryPlan . queryOpts . endkey ,
187+ } ,
188+ opts : {
189+ use_index : requestDef . use_index || [ ] ,
190+ bookmark : "nil" , //hardcoded to match CouchDB since its not supported,
200191 limit : requestDef . limit ,
201- skip : requestDef . skip || 0 ,
192+ skip : requestDef . skip ,
193+ sort : requestDef . sort || { } ,
202194 fields : requestDef . fields ,
203- } ;
204- } ) ;
195+ conflicts : false , //hardcoded to match CouchDB since its not supported,
196+ r : [ 49 ] , // hardcoded to match CouchDB since its not support
197+ } ,
198+ limit : requestDef . limit ,
199+ skip : requestDef . skip || 0 ,
200+ fields : requestDef . fields ,
201+ } ;
205202}
206203
207204export { find , explain } ;
0 commit comments