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()
}
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()
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: