fbc23a624e
refs https://github.com/TryGhost/Team/issues/2078 This pulls the current Tiers logic into its own package, the persistence part of the work has not been done yet, that will be handled in core, so all bookshelf model specific stuff is kept together.
74 lines
1.6 KiB
JavaScript
74 lines
1.6 KiB
JavaScript
const nql = require('@tryghost/nql');
|
|
|
|
/**
|
|
* @typedef {import('./Tier')} Tier
|
|
*/
|
|
|
|
class InMemoryTierRepository {
|
|
/** @type {Tier[]} */
|
|
#store = [];
|
|
/** @type {Object.<string, true>} */
|
|
#ids = {};
|
|
|
|
/**
|
|
* @param {Tier} tier
|
|
* @returns {any}
|
|
*/
|
|
toPrimitive(tier) {
|
|
return {
|
|
...tier,
|
|
id: tier.id.toHexString()
|
|
};
|
|
}
|
|
|
|
/**
|
|
* @param {Tier} linkClick
|
|
* @returns {Promise<void>}
|
|
*/
|
|
async save(tier) {
|
|
if (this.#ids[tier.id.toHexString()]) {
|
|
const existing = this.#store.findIndex((item) => {
|
|
return item.id.equals(tier.id);
|
|
});
|
|
this.#store.splice(existing, 1, tier);
|
|
} else {
|
|
this.#store.push(tier);
|
|
this.#ids[tier.id.toHexString()] = true;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param {import('bson-objectid').default} id
|
|
* @returns {Promise<Tier>}
|
|
*/
|
|
async getById(id) {
|
|
return this.#store.find((item) => {
|
|
return item.id.equals(id);
|
|
});
|
|
}
|
|
|
|
/**
|
|
* @param {string} slug
|
|
* @returns {Promise<Tier>}
|
|
*/
|
|
async getBySlug(slug) {
|
|
return this.#store.find((item) => {
|
|
return item.slug === slug;
|
|
});
|
|
}
|
|
|
|
/**
|
|
* @param {object} [options]
|
|
* @param {string} [options.filter]
|
|
* @returns {Promise<Tier[]>}
|
|
*/
|
|
async getAll(options = {}) {
|
|
const filter = nql(options.filter, {});
|
|
return this.#store.slice().filter((item) => {
|
|
return filter.queryJSON(this.toPrimitive(item));
|
|
});
|
|
}
|
|
}
|
|
|
|
module.exports = InMemoryTierRepository;
|