Skip to content

Commit

Permalink
feat: Allow to output which indecies are used
Browse files Browse the repository at this point in the history
By setting _shouldLogQuery = true on the indexed-db service, it will output, for each query, which index is used. This can be useful to debug your schema.
  • Loading branch information
mydea committed Jun 23, 2020
1 parent 78d11a4 commit c583e60
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions addon/services/indexed-db.js
Expand Up @@ -43,6 +43,17 @@ export default class IndexedDbService extends Service {
*/
databaseName = 'ember-indexeddb';

/**
* If set to true, it will output which indecies are used for queries.
* This can be used to debug your indecies.
*
* @property _shouldLogQuery
* @type {Boolean}
* @default false
* @private
*/
_shouldLogQuery = false;

/**
* This is an object with an array per model type.
* It holds all the objects per model type that should be bulk saved.
Expand Down Expand Up @@ -544,6 +555,13 @@ export default class IndexedDbService extends Service {
return Promise.all(promises, 'indexedDb/_bulkSave');
}

_logQuery(str, query) {
if (this._shouldLogQuery) {
// eslint-disable-next-line
console.log(`[QUERY]: ${str}`, query);
}
}

/**
* Build a query for Dexie.
*
Expand Down Expand Up @@ -580,6 +598,7 @@ export default class IndexedDbService extends Service {
});

if (index) {
this._logQuery(`Using index "${key}"`, query);
let value = normalizeValue(query[key]);
return db[type].where(key).equals(value);
}
Expand Down Expand Up @@ -614,6 +633,7 @@ export default class IndexedDbService extends Service {
compareValues.push(value);
});

this._logQuery(`Using compound index "${keyPath}"`, query);
return db[type].where(keyName).equals(compareValues);
}
}
Expand All @@ -634,6 +654,17 @@ export default class IndexedDbService extends Service {
? db[type].where(whereKey).equals(whereKeyValue)
: db[type];

if (whereKey) {
this._logQuery(
`Using index "${whereKey}" and vanilla filtering for ${vanillaFilterKeys.join(
', '
)}`,
query
);
} else {
this._logQuery(`Using vanilla filtering`, query);
}

return collection.filter((item) => {
return vanillaFilterKeys.every((key) => {
return (
Expand Down

0 comments on commit c583e60

Please sign in to comment.