Skip to content

Commit

Permalink
Merge pull request #543 from BloomBooks/QueryEfficiencyBooleanDefaults
Browse files Browse the repository at this point in the history
Make queries more efficient by assuming certain booleans have values
  • Loading branch information
hatton authored Mar 5, 2024
2 parents cf0a08d + 1d54222 commit 87a1ea6
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/connection/LibraryQueryHooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1452,10 +1452,12 @@ export function constructParseBookQuery(
}
}
delete params.where.inCirculation;
// As of March 2024, we have a defaultValue of true on the _Schema table for inCirculation, so we can assume a value is set.
// This helps the queries run more efficiently because we can use equality instead of inequality ($ne) or range ($nin).
switch (f.inCirculation) {
case undefined:
case BooleanOptions.Yes:
params.where.inCirculation = { $in: [true, null] };
params.where.inCirculation = true;
break;
case BooleanOptions.No:
params.where.inCirculation = false;
Expand All @@ -1466,13 +1468,15 @@ export function constructParseBookQuery(
}
// Unless the filter explicitly allows draft books, don't include them.
delete params.where.draft;
// As of March 2024, we have a defaultValue of false on the _Schema table for draft, so we can assume a value is set.
// This helps the queries run more efficiently because we can use equality instead of inequality ($ne) or range ($nin).
switch (f.draft) {
case BooleanOptions.Yes:
params.where.draft = true;
break;
case undefined:
case BooleanOptions.No:
params.where.draft = { $in: [false, null] };
params.where.draft = false;
break;
case BooleanOptions.All:
// just don't include it in the query
Expand Down Expand Up @@ -1525,13 +1529,14 @@ export function constructParseBookQuery(
}

delete params.where.rebrand;
// As of March 2024, we have a defaultValue of false on the _Schema table for rebrand, so we can assume a value is set.
// This helps the queries run more efficiently because we can use equality instead of inequality ($ne) or range ($nin).
switch (f.rebrand) {
case BooleanOptions.Yes:
params.where.rebrand = true;
break;
case BooleanOptions.No:
// can't use false because old records have undefined. Undefined and false both mean NOT branded. So we query for "not true".
params.where.rebrand = { $ne: true };
params.where.rebrand = false;
break;
case BooleanOptions.All:
// don't mention it
Expand Down

0 comments on commit 87a1ea6

Please sign in to comment.