Ghost/core/server/services/members/limit.js
Kevin Ansfield f30d3cd2c2 Switched to bookshelf count in getTotalMembers()
no issue

- tested performance between knex raw, knex `count()` and bookshelf `count()` and found no difference over 1000 iterations of each (each ~19,500ms +- 500ms for 104k members locally)
- switched to using bookshelf as the code is the simplest
2020-08-27 01:52:36 +01:00

30 lines
1.0 KiB
JavaScript

const config = require('../../../shared/config');
const models = require('../../models');
const errors = require('@tryghost/errors');
// Get total members direct from DB
async function getTotalMembers() {
return models.Member.count('id');
}
module.exports = async () => {
const membersHostLimit = config.get('host_settings:limits:members');
if (membersHostLimit) {
const allowedMembersLimit = membersHostLimit.max;
const hostUpgradeLink = config.get('host_settings:limits').upgrade_url;
const totalMembers = await getTotalMembers();
if (totalMembers > allowedMembersLimit) {
throw new errors.HostLimitError({
message: `Your current plan allows you to have up to ${allowedMembersLimit} members, but you currently have ${totalMembers} members`,
help: hostUpgradeLink,
errorDetails: {
limit: allowedMembersLimit,
total: totalMembers
}
});
}
}
};