Is there a simpler way to filter for a single document, using more than one parameter? #527
-
const doesDocumentExist = e.select(e.Registrant, registrant => ({
...e.Registrant["*"],
registrar: registrant.registrar["*"],
// TODO
// : https://github.com/edgedb/edgedb-js/issues/347
filter_single: e.op(
e.op(registrant.email, "=", query.email),
"and",
e.op(
e.op(registrant.registrar.id, "=", e.uuid(query.registrar)),
"and",
e.op(registrant.name, "=", query.name)
)
)
})); This is what I have but I don't like it. What this code is doing: given an object with keys email, registrar, and/or name, bring back the document that matches those defined params. Not sure where the linked issue is on the internal list of importance but I'd love to have a better solution in the meantime. #347 |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 8 replies
-
This is the error I get with my function. |
Beta Was this translation helpful? Give feedback.
-
A dirty workaround but will help: export function combineOp(
operator: string,
...inputs: $expr_Operator<any, any>[]
): $expr_Operator<any, any> | undefined {
if (inputs.length === 0) {
return undefined
}
if (inputs.length === 1) {
return inputs[0]
}
return e.op(
inputs[0] as $expr_Operator<any, any>,
operator as never,
combineOp(operator, ...inputs.slice(1)) as $expr_Operator<any, any>,
)
}
export function andOp(
...inputs: $expr_Operator<any, any>[]
): $expr_Operator<any, any> | undefined {
return combineOp('and', ...inputs)
}
export function orOp(
...inputs: $expr_Operator<any, any>[]
): $expr_Operator<any, any> | undefined {
return combineOp('or', ...inputs)
} then use it like this: e.select(e.Foo, () => ({
// ...
filter: andOp(e.op(foo, '=', bar), e.op(bar, '!=', baz)),
}) or providing an array, or a single element, etc. |
Beta Was this translation helpful? Give feedback.
A dirty workaround but will help: