Extracted Bookshelf filtered collection code into plugin

no issue

- this commit extracts all code relating to filtering collections to a
  separate plugin to break down the Base model
This commit is contained in:
Daniel Lockyer 2021-06-16 13:54:52 +01:00
parent 930df4b7fb
commit 89ba4081b4
3 changed files with 38 additions and 32 deletions

View File

@ -54,7 +54,9 @@ ghostBookshelf.plugin(require('./sanitize'));
ghostBookshelf.plugin(require('./generate-slug'));
ghostBookshelf.plugin(require('./plugins/bulk-operations'));
ghostBookshelf.plugin(require('./bulk-operations'));
ghostBookshelf.plugin(require('./filtered-collection'));
// Manages nested updates (relationships)
ghostBookshelf.plugin('bookshelf-relations', {

View File

@ -0,0 +1,35 @@
/**
* @param {import('bookshelf')} Bookshelf
*/
module.exports = function (Bookshelf) {
Bookshelf.Model = Bookshelf.Model.extend({}, {
getFilteredCollection: function getFilteredCollection(options) {
const filteredCollection = this.forge();
// Apply model-specific query behaviour
filteredCollection.applyCustomQuery(options);
// Add Filter behaviour
filteredCollection.applyDefaultAndCustomFilters(options);
// Apply model-specific search behaviour
filteredCollection.applySearchQuery(options);
return filteredCollection;
},
getFilteredCollectionQuery: function getFilteredCollectionQuery(options) {
const filteredCollection = this.getFilteredCollection(options);
const filteredCollectionQuery = filteredCollection.query();
if (options.transacting) {
filteredCollectionQuery.transacting(options.transacting);
if (options.forUpdate) {
filteredCollectionQuery.forUpdate();
}
}
return filteredCollectionQuery;
}
});
};

View File

@ -327,37 +327,6 @@ ghostBookshelf.Model = ghostBookshelf.Model.extend({
isExternalUser: function isExternalUser(id) {
return id === ghostBookshelf.Model.externalUser || id === ghostBookshelf.Model.externalUser.toString();
},
// ## Model Data Functions
getFilteredCollection: function getFilteredCollection(options) {
const filteredCollection = this.forge();
// Apply model-specific query behaviour
filteredCollection.applyCustomQuery(options);
// Add Filter behaviour
filteredCollection.applyDefaultAndCustomFilters(options);
// Apply model-specific search behaviour
filteredCollection.applySearchQuery(options);
return filteredCollection;
},
getFilteredCollectionQuery: function getFilteredCollectionQuery(options) {
const filteredCollection = this.getFilteredCollection(options);
const filteredCollectionQuery = filteredCollection.query();
if (options.transacting) {
filteredCollectionQuery.transacting(options.transacting);
if (options.forUpdate) {
filteredCollectionQuery.forUpdate();
}
}
return filteredCollectionQuery;
}
});