Vincit/objection.js

New QueryBuilder method: none()

Open

#2,184 opened on Jan 7, 2022

View on GitHub
 (2 comments) (0 reactions) (1 assignee)JavaScript (663 forks)batch import
enhancementhelp wanted

Repository metrics

Stars
 (7,146 stars)
PR merge metrics
 (No merged PRs in 30d)

Description

I would like to propose a new method in QueryBuilder that returns a builder that is guaranteed to make no database query and return empty result set (or undefined for SingleQueryBuilder).

That will repeat what Django has been having for years: https://docs.djangoproject.com/en/4.0/ref/models/querysets/#none

The use case is creating chainable query builder functions, where one of the chains might decide that it should not return any data at all. Consider this:

function get_foos(ctx: Context) {
  return ctx.allowFoos ? FooModel.query().orderBy("create_time") : FooModel.query().none()
}

httpServer.get("/foos1", (ctx) => {
  return get_foos(ctx).select("data1")
})

httpServer.get("/foos2", (ctx) => {
  return get_foos(ctx).select("data2").withGraphFetched(...)
})

Of course it's possible to use hacks like .filter(raw("false")) but why having hacks if this can be natively supported (and work faster).

Contributor guide