Optimized queries for tiers (aka products)

refs https://github.com/TryGhost/Toolbox/issues/515
refs dd4d6aeae5

- 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 3b6759ca6d/ghost/core/core/server/services/members/content-gating.js (L44-L55), ref. 2 3b6759ca6d/ghost/core/core/server/api/endpoints/utils/serializers/output/mappers/posts.js (L39-L54))
This commit is contained in:
Naz 2023-02-08 19:05:31 +08:00 committed by naz
parent ca568817dd
commit 93a10d8f4f
5 changed files with 4 additions and 12 deletions

View File

@ -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());

View File

@ -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()) : [];

View File

@ -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) {

View File

@ -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()) : [];

View File

@ -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);
}