Reusable EdgeDB filters with e.shape()
Riley Tomasek
The EdgeDB TypeScript client recently added e.shape()
for portable shapes, but it also works well for DRYing out your filters.
When working with backlinks, you often end up repeating filters at differnt depths like this:
e.select(e.Profile, (profile) => ({
// ...
filter: e.op(
e.op(profile.username, '=', username),
'and',
e.op(profile.platform, '=', platform)
),
}));
e.select(e.Person, () => ({
// ...
profile: (profile) => ({
// ...
filter: e.op(
e.op(profile.username, '=', username),
'and',
e.op(profile.platform, '=', platform)
),
}),
}));
With e.shape()
you can reuse the filter code:
const profileFilter = (username: string, platform: string) => {
return e.shape(e.Profile, (profile) => ({
filter: e.op(
e.op(profile.username, '=', username),
'and',
e.op(profile.platform, '=', platform)
),
});
}
e.select(e.Profile, (profile) => ({
// ...
...profileFilter(username, platform),
}));
e.select(e.Person, () => ({
// ...
igProfile: (profile) => ({
// ...
...profileFilter(username, platform),
}),
}));
These examples use a simple filter for clarity, but in practice this can save a ton of repeated code if you have complex filters.