Skip to content

Commit

Permalink
fix: Fix querying for unindexed fields
Browse files Browse the repository at this point in the history
This seems to have changed in newer Dexie versions.
  • Loading branch information
mydea committed Jun 23, 2020
1 parent 09b795a commit a3cddac
Showing 1 changed file with 18 additions and 15 deletions.
33 changes: 18 additions & 15 deletions addon/services/indexed-db.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ export default class IndexedDbService extends Service {
*/
queryRecord(type, query) {
let queryPromise = this._buildQuery(type, query);

let promise = new Promise(
(resolve, reject) => queryPromise.first().then(resolve, reject),
'indexedDb/queryRecord'
Expand Down Expand Up @@ -564,7 +565,6 @@ export default class IndexedDbService extends Service {
_buildQuery(type, query) {
let { db, _supportsCompoundIndices: supportsCompoundIndices } = this;

let promise = null;
let keys = Object.keys(query);

// Convert boolean queries to 1/0
Expand All @@ -574,16 +574,23 @@ export default class IndexedDbService extends Service {
}
}

// Only one, then do a simple where
if (keys.length === 1) {
let key = keys[0];
return db[type].where(key).equals(query[key]);
}

// Order of query params is important!
let { schema } = db[type];
let { indexes } = schema;

// Only one, try to find a simple index
if (keys.length === 1) {
let key = keys[0];
let index = indexes.find((index) => {
let { keyPath } = index;
return keyPath === key;
});

if (index) {
return db[type].where(key).equals(query[key]);
}
}

// try to find a fitting multi index
// only if the client supports compound indices!
if (supportsCompoundIndices) {
Expand Down Expand Up @@ -617,15 +624,11 @@ export default class IndexedDbService extends Service {
}

// Else, filter manually
Object.keys(query).forEach((i) => {
if (!promise) {
promise = db[type].where(i).equals(query[i]);
} else {
promise = promise.and((item) => item[i] === query[i]);
}
return db[type].filter((item) => {
return keys.every((key) => {
return item.json.attributes[key] === query[key];
});
});

return promise;
}

_mapItem(type, item) {
Expand Down

0 comments on commit a3cddac

Please sign in to comment.