Objection.js: Support for extending QueryBuilder similar to Knex.QueryBuilder.extend

Created on 21 Sep 2019  路  1Comment  路  Source: Vincit/objection.js

Hi. We were hoping to utilize something similar to https://github.com/cvburgess/SQLDataSource where the Knex QueryBuilder is extended to support caching.

From our testing, extending the knex QueryBuilder works, but the extension is not recognized by the Objection QueryBuilder. See following examples.

Would it be possible to add support in Objection for extending the QueryBuilder in this fashion? Thanks,

    Knex.QueryBuilder.extend("cache", function cacheFn() {
     ...
      return this // `this` is the query builder
   })

Succeeds using the Knex QueryBuilder

  static selectById(id) {
    const knex = MyObjectionModel.knex()
    return knex('my_objection_model_table')
      .where('id', id)
      .cache()
  }

But it fails using Objection QueryBuilder
"TypeError: this.query(...).where(...).cache is not a function",

  static selectById(id) {
    return this.query()
      .where('id', id)
      .cache()
  }

Most helpful comment

https://vincit.github.io/objection.js/recipes/custom-query-builder.html#custom-query-builder
https://vincit.github.io/objection.js/api/query-builder/other-methods.html#onbuildknex

So something like this:

class CustomQueryBuilder extends Model.QueryBuilder {
  cache() {
    return this.onBuildKnex(knexQuery => knexQuery.cache())
  }
}
class BaseModel extends Model {
  static QueryBuilder = CustomQueryBuilder
}
class SomeModel extends BaseModel {
  ...
}
await SomeModel.query().where(...).cache()

>All comments

https://vincit.github.io/objection.js/recipes/custom-query-builder.html#custom-query-builder
https://vincit.github.io/objection.js/api/query-builder/other-methods.html#onbuildknex

So something like this:

class CustomQueryBuilder extends Model.QueryBuilder {
  cache() {
    return this.onBuildKnex(knexQuery => knexQuery.cache())
  }
}
class BaseModel extends Model {
  static QueryBuilder = CustomQueryBuilder
}
class SomeModel extends BaseModel {
  ...
}
await SomeModel.query().where(...).cache()
Was this page helpful?
0 / 5 - 0 ratings

Related issues

louis-etne picture louis-etne  路  4Comments

officer-rosmarino picture officer-rosmarino  路  4Comments

rickmed picture rickmed  路  4Comments

zuck picture zuck  路  4Comments

bsdo64 picture bsdo64  路  3Comments