Vincit/objection.js

New QueryBuilder method: none()

Open

#2,184 创建于 2022年1月7日

在 GitHub 查看
 (2 评论) (0 反应) (1 负责人)JavaScript (7,146 star) (663 fork)batch import
enhancementhelp wanted

描述

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

贡献者指南