WIP: simplify getCandidatesAsync and move async to devDependencies

pull/11/head
Timothée Rebours 3 years ago
parent 2c4589e108
commit 4df151b203
  1. 34
      lib/datastore.js
  2. 2
      package-lock.json
  3. 2
      package.json

@ -288,42 +288,10 @@ class Datastore extends EventEmitter {
} }
async getCandidatesAsync (query, dontExpireStaleDocs = false) { async getCandidatesAsync (query, dontExpireStaleDocs = false) {
const indexNames = Object.keys(this.indexes)
const validDocs = [] 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 // 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 // STEP 2: remove all expired documents
if (!dontExpireStaleDocs) { if (!dontExpireStaleDocs) {
const expiredDocsIds = [] const expiredDocsIds = []

2
package-lock.json generated

@ -10,12 +10,12 @@
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"@seald-io/binary-search-tree": "^1.0.2", "@seald-io/binary-search-tree": "^1.0.2",
"async": "0.2.10",
"localforage": "^1.9.0" "localforage": "^1.9.0"
}, },
"devDependencies": { "devDependencies": {
"@react-native-async-storage/async-storage": "^1.15.9", "@react-native-async-storage/async-storage": "^1.15.9",
"@types/jest": "^27.0.2", "@types/jest": "^27.0.2",
"async": "0.2.10",
"browser-resolve": "^2.0.0", "browser-resolve": "^2.0.0",
"chai": "^4.3.4", "chai": "^4.3.4",
"commander": "^7.2.0", "commander": "^7.2.0",

@ -42,13 +42,13 @@
}, },
"dependencies": { "dependencies": {
"@seald-io/binary-search-tree": "^1.0.2", "@seald-io/binary-search-tree": "^1.0.2",
"async": "0.2.10",
"localforage": "^1.9.0" "localforage": "^1.9.0"
}, },
"devDependencies": { "devDependencies": {
"@react-native-async-storage/async-storage": "^1.15.9", "@react-native-async-storage/async-storage": "^1.15.9",
"@types/jest": "^27.0.2", "@types/jest": "^27.0.2",
"browser-resolve": "^2.0.0", "browser-resolve": "^2.0.0",
"async": "0.2.10",
"chai": "^4.3.4", "chai": "^4.3.4",
"commander": "^7.2.0", "commander": "^7.2.0",
"events": "^3.3.0", "events": "^3.3.0",

Loading…
Cancel
Save