From 93a10d8f4f780e8a3942fe5b77c1c1206fe96be5 Mon Sep 17 00:00:00 2001 From: Naz Date: Wed, 8 Feb 2023 19:05:31 +0800 Subject: [PATCH] Optimized queries for tiers (aka products) refs https://github.com/TryGhost/Toolbox/issues/515 refs https://github.com/TryGhost/Ghost/commit/dd4d6aeae5e34a85ef053217faddbc1f9195095e - The `productRepository.list` call produced 5 db queries and a transaction wrapping this call. - Transaction is not needed in this situation as there are no possible writes in the meantime (transaction wrapping code was put in there through refed commit to guard against failing Stripe API calls, which are no longer involved when calling the list method) - The `limit: 'all'` makes sure all product entries are fetched AND removes an extra aggregation query called over stripe_prices join - The 'monthlyPrice' and 'yearlyPrice' relations are not needed because this data is not used in downstream code - only slug and type are used for visiblity/content gating (ref. 1 https://github.com/TryGhost/Ghost/blob/3b6759ca6d4d282aa4c7d0e91b961b6d5384d39a/ghost/core/core/server/services/members/content-gating.js#L44-L55, ref. 2 https://github.com/TryGhost/Ghost/blob/3b6759ca6d4d282aa4c7d0e91b961b6d5384d39a/ghost/core/core/server/api/endpoints/utils/serializers/output/mappers/posts.js#L39-L54) --- .../api/endpoints/utils/serializers/output/email-posts.js | 2 +- .../api/endpoints/utils/serializers/output/pages.js | 2 +- .../api/endpoints/utils/serializers/output/posts.js | 2 +- .../api/endpoints/utils/serializers/output/previews.js | 2 +- ghost/members-api/lib/repositories/product.js | 8 -------- 5 files changed, 4 insertions(+), 12 deletions(-) diff --git a/ghost/core/core/server/api/endpoints/utils/serializers/output/email-posts.js b/ghost/core/core/server/api/endpoints/utils/serializers/output/email-posts.js index 353fb499b1..dd25443db4 100644 --- a/ghost/core/core/server/api/endpoints/utils/serializers/output/email-posts.js +++ b/ghost/core/core/server/api/endpoints/utils/serializers/output/email-posts.js @@ -5,7 +5,7 @@ const membersService = require('../../../../../services/members'); module.exports = { async read(model, apiConfig, frame) { const tiersModels = await membersService.api.productRepository.list({ - withRelated: ['monthlyPrice', 'yearlyPrice'] + limit: 'all' }); const tiers = tiersModels.data && tiersModels.data.map(tierModel => tierModel.toJSON()); diff --git a/ghost/core/core/server/api/endpoints/utils/serializers/output/pages.js b/ghost/core/core/server/api/endpoints/utils/serializers/output/pages.js index 12718e3570..b70b9d8a33 100644 --- a/ghost/core/core/server/api/endpoints/utils/serializers/output/pages.js +++ b/ghost/core/core/server/api/endpoints/utils/serializers/output/pages.js @@ -13,7 +13,7 @@ module.exports = { let pages = []; const tiersModels = await membersService.api.productRepository.list({ - withRelated: ['monthlyPrice', 'yearlyPrice'] + limit: 'all' }); const tiers = tiersModels.data ? tiersModels.data.map(tierModel => tierModel.toJSON()) : []; diff --git a/ghost/core/core/server/api/endpoints/utils/serializers/output/posts.js b/ghost/core/core/server/api/endpoints/utils/serializers/output/posts.js index f9dd3052bf..1a290bba45 100644 --- a/ghost/core/core/server/api/endpoints/utils/serializers/output/posts.js +++ b/ghost/core/core/server/api/endpoints/utils/serializers/output/posts.js @@ -13,7 +13,7 @@ module.exports = { let posts = []; const tiersModels = await membersService.api.productRepository.list({ - withRelated: ['monthlyPrice', 'yearlyPrice'] + limit: 'all' }); const tiers = tiersModels.data ? tiersModels.data.map(tierModel => tierModel.toJSON()) : []; if (models.meta) { diff --git a/ghost/core/core/server/api/endpoints/utils/serializers/output/previews.js b/ghost/core/core/server/api/endpoints/utils/serializers/output/previews.js index 27c6e1eed5..76c36e2093 100644 --- a/ghost/core/core/server/api/endpoints/utils/serializers/output/previews.js +++ b/ghost/core/core/server/api/endpoints/utils/serializers/output/previews.js @@ -4,7 +4,7 @@ const membersService = require('../../../../../services/members'); module.exports = { async all(model, apiConfig, frame) { const tiersModels = await membersService.api.productRepository.list({ - withRelated: ['monthlyPrice', 'yearlyPrice'] + limit: 'all' }); const tiers = tiersModels.data ? tiersModels.data.map(tierModel => tierModel.toJSON()) : []; diff --git a/ghost/members-api/lib/repositories/product.js b/ghost/members-api/lib/repositories/product.js index 51ee6c80c7..c1612b4452 100644 --- a/ghost/members-api/lib/repositories/product.js +++ b/ghost/members-api/lib/repositories/product.js @@ -650,14 +650,6 @@ class ProductRepository { * @returns {Promise<{data: ProductModel[], meta: object}>} **/ async list(options = {}) { - if (!options.transacting) { - return this._Product.transaction((transacting) => { - return this.list({ - ...options, - transacting - }); - }); - } return this._Product.findPage(options); }