0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-20 22:42:53 -05:00

Extracted Bookshelf setup code into separate file

no issue

- I'm working on pulling apart the base index.js and this code is
  specific to setting up Bookshelf + the plugins, which is pretty
  contained and can stay in one file
- it only has one local require so it might be a good candidate for
  extracting out of Ghost in the future
This commit is contained in:
Daniel Lockyer 2021-06-16 11:32:54 +01:00
parent 44053553f5
commit 14ffd0b9d9
2 changed files with 85 additions and 76 deletions

View file

@ -0,0 +1,83 @@
const _ = require('lodash');
const bookshelf = require('bookshelf');
const ObjectId = require('bson-objectid');
const plugins = require('@tryghost/bookshelf-plugins');
const Promise = require('bluebird');
const db = require('../../data/db');
// ### ghostBookshelf
// Initializes a new Bookshelf instance called ghostBookshelf, for reference elsewhere in Ghost.
const ghostBookshelf = bookshelf(db.knex);
// Load the Bookshelf registry plugin, which helps us avoid circular dependencies
ghostBookshelf.plugin('registry');
ghostBookshelf.plugin(plugins.eagerLoad);
// Add committed/rollback events.
ghostBookshelf.plugin(plugins.transactionEvents);
// Load the Ghost custom-query plugin, which applying a custom query to findPage requests
ghostBookshelf.plugin(plugins.customQuery);
// Load the Ghost filter plugin, which handles applying a 'filter' to findPage requests
ghostBookshelf.plugin(plugins.filter);
// Load the Ghost filter plugin, which handles applying a 'order' to findPage requests
ghostBookshelf.plugin(plugins.order);
// Load the Ghost search plugin, which handles applying a search query to findPage requests
ghostBookshelf.plugin(plugins.search);
// Load the Ghost include count plugin, which allows for the inclusion of cross-table counts
ghostBookshelf.plugin(plugins.includeCount);
// Load the Ghost pagination plugin, which gives us the `fetchPage` method on Models
ghostBookshelf.plugin(plugins.pagination);
// Update collision plugin
ghostBookshelf.plugin(plugins.collision);
// Load hasPosts plugin for authors models
ghostBookshelf.plugin(plugins.hasPosts);
ghostBookshelf.plugin(require('./crud'));
// Manages nested updates (relationships)
ghostBookshelf.plugin('bookshelf-relations', {
allowedOptions: ['context', 'importing', 'migrating'],
unsetRelations: true,
extendChanged: '_changed',
attachPreviousRelations: true,
hooks: {
belongsToMany: {
after: function (existing, targets, options) {
// reorder tags/authors
const queryOptions = {
query: {
where: {}
}
};
// CASE: disable after hook for specific relations
if (['permissions_roles'].indexOf(existing.relatedData.joinTableName) !== -1) {
return Promise.resolve();
}
return Promise.each(targets.models, function (target, index) {
queryOptions.query.where[existing.relatedData.otherKey] = target.id;
return existing.updatePivot({
sort_order: index
}, _.extend({}, options, queryOptions));
});
},
beforeRelationCreation: function onCreatingRelation(model, data) {
data.id = ObjectId().toHexString();
}
}
}
});
module.exports = ghostBookshelf;

View file

@ -8,7 +8,6 @@
// All other parts of Ghost, including the frontend & admin UI are only allowed to access data via the API.
const _ = require('lodash');
const bookshelf = require('bookshelf');
const moment = require('moment');
const Promise = require('bluebird');
const ObjectId = require('bson-objectid');
@ -24,88 +23,15 @@ const bulkOperations = require('./bulk-operations');
const plugins = require('@tryghost/bookshelf-plugins');
const tpl = require('@tryghost/tpl');
const ghostBookshelf = require('./bookshelf');
const messages = {
missingContext: 'missing context',
invalidDate: 'Date format for `{key}` is invalid.'
};
let ghostBookshelf;
let proto;
// ### ghostBookshelf
// Initializes a new Bookshelf instance called ghostBookshelf, for reference elsewhere in Ghost.
ghostBookshelf = bookshelf(db.knex);
// Load the Bookshelf registry plugin, which helps us avoid circular dependencies
ghostBookshelf.plugin('registry');
ghostBookshelf.plugin(plugins.eagerLoad);
// Add committed/rollback events.
ghostBookshelf.plugin(plugins.transactionEvents);
// Load the Ghost custom-query plugin, which applying a custom query to findPage requests
ghostBookshelf.plugin(plugins.customQuery);
// Load the Ghost filter plugin, which handles applying a 'filter' to findPage requests
ghostBookshelf.plugin(plugins.filter);
// Load the Ghost filter plugin, which handles applying a 'order' to findPage requests
ghostBookshelf.plugin(plugins.order);
// Load the Ghost search plugin, which handles applying a search query to findPage requests
ghostBookshelf.plugin(plugins.search);
// Load the Ghost include count plugin, which allows for the inclusion of cross-table counts
ghostBookshelf.plugin(plugins.includeCount);
// Load the Ghost pagination plugin, which gives us the `fetchPage` method on Models
ghostBookshelf.plugin(plugins.pagination);
// Update collision plugin
ghostBookshelf.plugin(plugins.collision);
// Load hasPosts plugin for authors models
ghostBookshelf.plugin(plugins.hasPosts);
ghostBookshelf.plugin(require('./crud'));
// Manages nested updates (relationships)
ghostBookshelf.plugin('bookshelf-relations', {
allowedOptions: ['context', 'importing', 'migrating'],
unsetRelations: true,
extendChanged: '_changed',
attachPreviousRelations: true,
hooks: {
belongsToMany: {
after: function (existing, targets, options) {
// reorder tags/authors
const queryOptions = {
query: {
where: {}
}
};
// CASE: disable after hook for specific relations
if (['permissions_roles'].indexOf(existing.relatedData.joinTableName) !== -1) {
return Promise.resolve();
}
return Promise.each(targets.models, function (target, index) {
queryOptions.query.where[existing.relatedData.otherKey] = target.id;
return existing.updatePivot({
sort_order: index
}, _.extend({}, options, queryOptions));
});
},
beforeRelationCreation: function onCreatingRelation(model, data) {
data.id = ObjectId().toHexString();
}
}
}
});
// Cache an instance of the base model prototype
proto = ghostBookshelf.Model.prototype;