Vincit/objection.js

New QueryBuilder method: none()

Open

#2 184 ouverte le 7 janv. 2022

Voir sur GitHub
 (2 commentaires) (0 réactions) (1 assigné)JavaScript (663 forks)batch import
enhancementhelp wanted

Métriques du dépôt

Stars
 (7 146 stars)
Métriques de merge PR
 (Aucune PR mergée en 30 j)

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).

Guide contributeur