From 4df151b2035d0624092e92235f2b93d03cc6e265 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Rebours?= Date: Fri, 22 Oct 2021 08:58:09 +0200 Subject: [PATCH] WIP: simplify getCandidatesAsync and move async to devDependencies --- lib/datastore.js | 34 +--------------------------------- package-lock.json | 2 +- package.json | 2 +- 3 files changed, 3 insertions(+), 35 deletions(-) diff --git a/lib/datastore.js b/lib/datastore.js index 74411f1..1080956 100755 --- a/lib/datastore.js +++ b/lib/datastore.js @@ -288,42 +288,10 @@ class Datastore extends EventEmitter { } async getCandidatesAsync (query, dontExpireStaleDocs = false) { - const indexNames = Object.keys(this.indexes) const validDocs = [] - const _getCandidates = query => { - // STEP 1: get candidates list by checking indexes from most to least frequent usecase - // For a basic match - let usableQuery - usableQuery = Object.entries(query) - .filter(([k, v]) => - !!(typeof v === 'string' || typeof v === 'number' || typeof v === 'boolean' || isDate(v) || v === null) && - indexNames.includes(k) - ) - .pop() - if (usableQuery) return this.indexes[usableQuery[0]].getMatching(usableQuery[1]) - // For a $in match - usableQuery = Object.entries(query) - .filter(([k, v]) => - !!(query[k] && Object.prototype.hasOwnProperty.call(query[k], '$in')) && - indexNames.includes(k) - ) - .pop() - if (usableQuery) return this.indexes[usableQuery[0]].getMatching(usableQuery[1].$in) - // For a comparison match - usableQuery = Object.entries(query) - .filter(([k, v]) => - !!(query[k] && (Object.prototype.hasOwnProperty.call(query[k], '$lt') || Object.prototype.hasOwnProperty.call(query[k], '$lte') || Object.prototype.hasOwnProperty.call(query[k], '$gt') || Object.prototype.hasOwnProperty.call(query[k], '$gte'))) && - indexNames.includes(k) - ) - .pop() - if (usableQuery) return this.indexes[usableQuery[0]].getBetweenBounds(usableQuery[1]) - // By default, return all the DB data - return this.getAllData() - } - // STEP 1: get candidates list by checking indexes from most to least frequent usecase - const docs = _getCandidates(query) + const docs = this._getCandidates(query) // STEP 2: remove all expired documents if (!dontExpireStaleDocs) { const expiredDocsIds = [] diff --git a/package-lock.json b/package-lock.json index 62c8812..fe525d6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,12 +10,12 @@ "license": "MIT", "dependencies": { "@seald-io/binary-search-tree": "^1.0.2", - "async": "0.2.10", "localforage": "^1.9.0" }, "devDependencies": { "@react-native-async-storage/async-storage": "^1.15.9", "@types/jest": "^27.0.2", + "async": "0.2.10", "browser-resolve": "^2.0.0", "chai": "^4.3.4", "commander": "^7.2.0", diff --git a/package.json b/package.json index 34f11a4..7820f23 100755 --- a/package.json +++ b/package.json @@ -42,13 +42,13 @@ }, "dependencies": { "@seald-io/binary-search-tree": "^1.0.2", - "async": "0.2.10", "localforage": "^1.9.0" }, "devDependencies": { "@react-native-async-storage/async-storage": "^1.15.9", "@types/jest": "^27.0.2", "browser-resolve": "^2.0.0", + "async": "0.2.10", "chai": "^4.3.4", "commander": "^7.2.0", "events": "^3.3.0",