Skip to content

Commit

Permalink
feat: coerce filter undefined value to null
Browse files Browse the repository at this point in the history
`undefined` value throws in Datastore, but `null` doesn't
  • Loading branch information
kirillgroshkov committed Aug 5, 2024
1 parent 3c48823 commit 21de9a9
Show file tree
Hide file tree
Showing 3 changed files with 237 additions and 231 deletions.
24 changes: 14 additions & 10 deletions src/query.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,27 @@ export function dbQueryToDatastoreQuery<ROW extends ObjectWithId>(
let q = emptyQuery

// filter
// eslint-disable-next-line unicorn/no-array-reduce
q = dbQuery._filters.reduce(
for (const f of dbQuery._filters) {
// keeping "previous syntax" commented out
// (q, f) => q.filter(f.name as string, OP_MAP[f.op] || (f.op as any), f.val),
(q, f) => q.filter(new PropertyFilter(f.name as string, OP_MAP[f.op] || (f.op as any), f.val)),
q,
)

// Datastore doesn't allow `undefined` as filter value.
// We don't want to throw on it, so instead we'll replace it with valid value of `null`.
// `a > null` will return anything that's indexed
// `a < null` should return nothing
// `a == null` will return just that - rows with null values
let { op, val } = f
if (val === undefined) val = null
q = q.filter(new PropertyFilter(f.name as string, OP_MAP[op] || (op as any), val))
}

// limit
q = q.limit(dbQuery._limitValue || 0)

// order
// eslint-disable-next-line unicorn/no-array-reduce
q = dbQuery._orders.reduce(
(q, ord) => q.order(ord.name as string, { descending: ord.descending }),
q,
)
for (const ord of dbQuery._orders) {
q = q.order(ord.name as string, { descending: ord.descending })
}

// select
if (dbQuery._selectedFieldNames) {
Expand Down
2 changes: 1 addition & 1 deletion src/test/datastore.manual.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ process.env['APP_ENV'] = 'master'

const credentials = JSON.parse(SECRET_GCP_SERVICE_ACCOUNT)

export const datastoreDB = new DatastoreDB({
const datastoreDB = new DatastoreDB({
credentials,
streamOptions: {
// experimentalCursorStream: true,
Expand Down
Loading

0 comments on commit 21de9a9

Please sign in to comment.