Skip to content

Commit 3e3be0b

Browse files
authored
chore: PouchDB-find adapters/local refactor to async/await (#8914)
* chore: async/await adapters/local's createIndex, deleteIndex, find/explain, and getIndexes
1 parent 63c856f commit 3e3be0b

File tree

4 files changed

+162
-169
lines changed

4 files changed

+162
-169
lines changed

packages/node_modules/pouchdb-find/src/adapters/local/create-index/index.js

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { stringMd5 } from 'pouchdb-md5';
55
import massageCreateIndexRequest from '../../../massageCreateIndexRequest';
66
import { mergeObjects } from '../../../utils';
77

8-
function createIndex(db, requestDef) {
8+
async function createIndex(db, requestDef) {
99
requestDef = massageCreateIndexRequest(requestDef);
1010
var originalIndexDef = clone(requestDef.index);
1111
requestDef.index = massageIndexDef(requestDef.index);
@@ -56,28 +56,27 @@ function createIndex(db, requestDef) {
5656

5757
db.constructor.emit('debug', ['find', 'creating index', ddocId]);
5858

59-
return upsert(db, ddocId, updateDdoc).then(function () {
60-
if (hasInvalidLanguage) {
61-
throw new Error('invalid language for ddoc with id "' +
59+
await upsert(db, ddocId, updateDdoc);
60+
61+
if (hasInvalidLanguage) {
62+
throw new Error('invalid language for ddoc with id "' +
6263
ddocId +
6364
'" (should be "query")');
64-
}
65-
}).then(function () {
66-
// kick off a build
67-
// TODO: abstract-pouchdb-mapreduce should support auto-updating
68-
// TODO: should also use update_after, but pouchdb/pouchdb#3415 blocks me
69-
var signature = ddocName + '/' + viewName;
70-
return abstractMapper(db).query.call(db, signature, {
71-
limit: 0,
72-
reduce: false
73-
}).then(function () {
74-
return {
75-
id: ddocId,
76-
name: viewName,
77-
result: viewExists ? 'exists' : 'created'
78-
};
79-
});
65+
}
66+
67+
// kick off a build
68+
// TODO: abstract-pouchdb-mapreduce should support auto-updating
69+
// TODO: should also use update_after, but pouchdb/pouchdb#3415 blocks me
70+
const signature = ddocName + '/' + viewName;
71+
await abstractMapper(db).query.call(db, signature, {
72+
limit: 0,
73+
reduce: false
8074
});
75+
return {
76+
id: ddocId,
77+
name: viewName,
78+
result: viewExists ? 'exists' : 'created'
79+
};
8180
}
8281

8382
export default createIndex;

packages/node_modules/pouchdb-find/src/adapters/local/delete-index/index.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import abstractMapper from '../abstract-mapper';
22
import { upsert } from 'pouchdb-utils';
33

4-
function deleteIndex(db, index) {
4+
async function deleteIndex(db, index) {
55

66
if (!index.ddoc) {
77
throw new Error('you must supply an index.ddoc when deleting');
@@ -24,11 +24,9 @@ function deleteIndex(db, index) {
2424
return doc;
2525
}
2626

27-
return upsert(db, docId, deltaFun).then(function () {
28-
return abstractMapper(db).viewCleanup.apply(db);
29-
}).then(function () {
30-
return {ok: true};
31-
});
27+
await upsert(db, docId, deltaFun);
28+
await abstractMapper(db).viewCleanup.apply(db);
29+
return { ok: true };
3230
}
3331

3432
export default deleteIndex;

packages/node_modules/pouchdb-find/src/adapters/local/find/index.js

Lines changed: 107 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -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 !/^_design\//.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 !/^_design\//.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

207204
export { find, explain };

0 commit comments

Comments
 (0)