From 2459d70048d22e7e2523649c7f273765386c9889 Mon Sep 17 00:00:00 2001 From: Fabien 'egg' O'Carroll Date: Mon, 7 Aug 2023 09:17:36 +0100 Subject: [PATCH] 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 --- ghost/core/core/server/models/collection-post.js | 9 +++++++++ ghost/core/core/server/models/collection.js | 6 ++++++ .../collections/BookshelfCollectionsRepository.js | 8 ++++---- 3 files changed, 19 insertions(+), 4 deletions(-) create mode 100644 ghost/core/core/server/models/collection-post.js diff --git a/ghost/core/core/server/models/collection-post.js b/ghost/core/core/server/models/collection-post.js new file mode 100644 index 0000000000..150017855f --- /dev/null +++ b/ghost/core/core/server/models/collection-post.js @@ -0,0 +1,9 @@ +const ghostBookshelf = require('./base'); + +const CollectionPost = ghostBookshelf.Model.extend({ + tableName: 'collections_posts' +}); + +module.exports = { + CollectionPost: ghostBookshelf.model('CollectionPost', CollectionPost) +}; diff --git a/ghost/core/core/server/models/collection.js b/ghost/core/core/server/models/collection.js index e96aaeacba..a3fcf19cb9 100644 --- a/ghost/core/core/server/models/collection.js +++ b/ghost/core/core/server/models/collection.js @@ -70,6 +70,12 @@ const Collection = ghostBookshelf.Model.extend({ 'id', 'id' ); + }, + + collectionPosts() { + return this.hasMany( + 'CollectionPost' + ); } }); diff --git a/ghost/core/core/server/services/collections/BookshelfCollectionsRepository.js b/ghost/core/core/server/services/collections/BookshelfCollectionsRepository.js index 7b479d614e..b76c004b38 100644 --- a/ghost/core/core/server/services/collections/BookshelfCollectionsRepository.js +++ b/ghost/core/core/server/services/collections/BookshelfCollectionsRepository.js @@ -23,7 +23,7 @@ module.exports = class BookshelfCollectionsRepository { async getById(id, options = {}) { const model = await this.#model.findOne({id}, { require: false, - withRelated: ['posts'], + withRelated: ['collectionPosts'], transacting: options.transaction }); if (!model) { @@ -39,7 +39,7 @@ module.exports = class BookshelfCollectionsRepository { async getBySlug(slug, options = {}) { const model = await this.#model.findOne({slug}, { require: false, - withRelated: ['posts'], + withRelated: ['collectionPosts'], transacting: options.transaction }); if (!model) { @@ -58,7 +58,7 @@ module.exports = class BookshelfCollectionsRepository { const models = await this.#model.findAll({ ...options, transacting: options.transaction, - withRelated: ['posts'] + withRelated: ['collectionPosts'] }); return await Promise.all(models.map(model => this.#modelToCollection(model))); @@ -75,7 +75,7 @@ module.exports = class BookshelfCollectionsRepository { filter: json.filter, type: json.type, featureImage: json.feature_image, - posts: json.posts.map(post => post.id), + posts: json.collectionPosts.map(collectionPost => collectionPost.post_id), createdAt: json.created_at, updatedAt: json.updated_at });