|
|
@ -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 = [] |
|
|
|