Vincit/objection.js

New QueryBuilder method: none()

Open

#2184 aperta il 7 gen 2022

Vedi su GitHub
 (2 commenti) (0 reazioni) (1 assegnatario)JavaScript (663 fork)batch import
enhancementhelp wanted

Metriche repository

Star
 (7146 star)
Metriche merge PR
 (Nessuna PR mergiata in 30 g)

Descrizione

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

Guida contributor