Ghost/core/server/models/plugins/eager-load.js
naz bbcc83dadb
Added support for ordering Post API resources by fields coming form posts_meta table (#12226)
refs #11729

- When ordering is done by fields from a relation (like post's `meta_title` that comes form `posts_meta` table), Bookshelf does not include those relations in the original query which caused errors. To support this usecase added a mechanism to detect fields from a relation and load those relations into query. 
- Extended ordering to include table name in ordered field name.  The information about the table name is needed to avoid using `tableName` within pagination plugin and gives path to having other than original table ordering fields (e.g. order by posts_meta table fields)
- Added test case to check ordering on posts_meta fields
- Added support for "eager loading" relations. Allows to extend query builder object with joins to related tables,
which could be used in ordering (possibly in filtering later). Bookshelf does not support ordering/filtering by proprieties coming from relations, that's why this kind of plugin and query expansion is needed
- Added note about lack of support for child relations with same property names.
2020-09-24 13:32:40 +12:00

80 lines
2.2 KiB
JavaScript

const _ = require('lodash');
const _debug = require('ghost-ignition').debug._base;
const debug = _debug('ghost-query');
/**
* Enchances knex query builder with a join to relation configured in
*
* @param {Bookshelf.Model} model instance of Bookshelf model
* @param {String[]} relationsToLoad relations to be included in joins
*/
function withEager(model, relationsToLoad) {
const tableName = _.result(model.constructor.prototype, 'tableName');
return function (qb) {
if (!model.relationsMeta) {
return qb;
}
for (const [key, config] of Object.entries(model.relationsMeta)) {
if (relationsToLoad.includes(key)) {
const innerQb = qb
.leftJoin(config.targetTableName, `${tableName}.id`, `${config.targetTableName}.${config.foreignKey}`);
debug(`QUERY has posts: ${innerQb.toSQL().sql}`);
}
}
return qb;
};
}
function load(options) {
if (!options) {
return;
}
if (this.eagerLoad) {
if (!options.columns && options.withRelated && _.intersection(this.eagerLoad, options.withRelated).length) {
this.query(withEager(this, this.eagerLoad));
}
}
}
/**
* ## Pagination
* Extends `bookshelf.Model` native `fetch` and `fetchAll` methods with
* a join to "eager loaded" relation. An exaple of such loading is when
* there is a need to order by fields in the related table.
*
*/
module.exports = function eagerLoadPlugin(Bookshelf) {
const modelPrototype = Bookshelf.Model.prototype;
Bookshelf.Model = Bookshelf.Model.extend({
initialize: function () {
return modelPrototype.initialize.apply(this, arguments);
},
fetch: function () {
load.apply(this, arguments);
if (_debug.enabled('ghost-query')) {
debug('QUERY', this.query().toQuery());
}
return modelPrototype.fetch.apply(this, arguments);
},
fetchAll: function () {
load.apply(this, arguments);
if (_debug.enabled('ghost-query')) {
debug('QUERY', this.query().toQuery());
}
return modelPrototype.fetchAll.apply(this, arguments);
}
});
};