Added CollectionPost model & relation to Collection (#17588)

By using the `collections_posts` table as a pivot table, I couldn't see
a way to get bookshelf to *not* load the Post models for the relation.
We don't actually need those Post models for our usescases, the the
queries were causing issues with our database servers! Here we've added
a new CollectionPost model which allows us to treat the
collections_posts table as a resource ratherthan pivot, and means we can
use the hasMany relation rather than belongsToMany relation. This
removes all queries to the posts table when fetching collections with
relations
This commit is contained in:
Fabien 'egg' O'Carroll 2023-08-07 09:17:36 +01:00 committed by GitHub
parent a9d980f9c1
commit 2459d70048
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 4 deletions

View File

@ -0,0 +1,9 @@
const ghostBookshelf = require('./base');
const CollectionPost = ghostBookshelf.Model.extend({
tableName: 'collections_posts'
});
module.exports = {
CollectionPost: ghostBookshelf.model('CollectionPost', CollectionPost)
};

View File

@ -70,6 +70,12 @@ const Collection = ghostBookshelf.Model.extend({
'id', 'id',
'id' 'id'
); );
},
collectionPosts() {
return this.hasMany(
'CollectionPost'
);
} }
}); });

View File

@ -23,7 +23,7 @@ module.exports = class BookshelfCollectionsRepository {
async getById(id, options = {}) { async getById(id, options = {}) {
const model = await this.#model.findOne({id}, { const model = await this.#model.findOne({id}, {
require: false, require: false,
withRelated: ['posts'], withRelated: ['collectionPosts'],
transacting: options.transaction transacting: options.transaction
}); });
if (!model) { if (!model) {
@ -39,7 +39,7 @@ module.exports = class BookshelfCollectionsRepository {
async getBySlug(slug, options = {}) { async getBySlug(slug, options = {}) {
const model = await this.#model.findOne({slug}, { const model = await this.#model.findOne({slug}, {
require: false, require: false,
withRelated: ['posts'], withRelated: ['collectionPosts'],
transacting: options.transaction transacting: options.transaction
}); });
if (!model) { if (!model) {
@ -58,7 +58,7 @@ module.exports = class BookshelfCollectionsRepository {
const models = await this.#model.findAll({ const models = await this.#model.findAll({
...options, ...options,
transacting: options.transaction, transacting: options.transaction,
withRelated: ['posts'] withRelated: ['collectionPosts']
}); });
return await Promise.all(models.map(model => this.#modelToCollection(model))); return await Promise.all(models.map(model => this.#modelToCollection(model)));
@ -75,7 +75,7 @@ module.exports = class BookshelfCollectionsRepository {
filter: json.filter, filter: json.filter,
type: json.type, type: json.type,
featureImage: json.feature_image, featureImage: json.feature_image,
posts: json.posts.map(post => post.id), posts: json.collectionPosts.map(collectionPost => collectionPost.post_id),
createdAt: json.created_at, createdAt: json.created_at,
updatedAt: json.updated_at updatedAt: json.updated_at
}); });