fix: accounts types caching.

This commit is contained in:
Ahmed Bouhuolia
2020-09-30 11:45:25 +02:00
parent 4e8c670968
commit 7d8b05ff66
6 changed files with 99 additions and 23 deletions

View File

@@ -1,4 +1,5 @@
import TenantRepository from 'repositories/TenantRepository';
import { IAccount } from 'interfaces';
export default class AccountRepository extends TenantRepository {
models: any;
@@ -33,9 +34,9 @@ export default class AccountRepository extends TenantRepository {
/**
* Retrieve all accounts on the storage.
* @return {}
* @return {IAccount[]}
*/
allAccounts() {
allAccounts(): IAccount[] {
const { Account } = this.models;
return this.cache.get('accounts', async () => {
return Account.query();
@@ -45,8 +46,9 @@ export default class AccountRepository extends TenantRepository {
/**
* Retrieve account of the given account slug.
* @param {string} slug
* @return {IAccount}
*/
getBySlug(slug: string) {
getBySlug(slug: string): IAccount {
const { Account } = this.models;
return this.cache.get(`accounts.slug.${slug}`, () => {
return Account.query().findOne('slug', slug);
@@ -56,8 +58,9 @@ export default class AccountRepository extends TenantRepository {
/**
* Retrieve the account by the given id.
* @param {number} id - Account id.
* @return {IAccount}
*/
getById(id: number) {
getById(id: number): IAccount {
const { Account } = this.models;
return this.cache.get(`accounts.id.${id}`, () => {
return Account.query().findById(id);

View File

@@ -1,4 +1,5 @@
import TenantRepository from 'repositories/TenantRepository';
import { IAccountType } from 'interfaces';
export default class AccountTypeRepository extends TenantRepository {
cache: any;
@@ -17,29 +18,62 @@ export default class AccountTypeRepository extends TenantRepository {
this.cache = this.tenancy.cache(tenantId);
}
/**
* Retrieve all accounts types.
* @return {IAccountType[]}
*/
all() {
const { AccountType } = this.models;
return this.cache.get('accountType.all', () => {
return AccountType.query();
});
}
/**
* Retrieve account type meta.
* @param {number} accountTypeId
* @return {IAccountType}
*/
getTypeMeta(accountTypeId: number) {
getTypeMeta(accountTypeId: number): IAccountType {
const { AccountType } = this.models;
return this.cache.get(`accountType.${accountTypeId}`, () => {
return this.cache.get(`accountType.id.${accountTypeId}`, () => {
return AccountType.query().findById(accountTypeId);
});
}
getByKeys(keys: string[]) {
/**
* Retrieve accounts types of the given keys.
* @param {string[]} keys
* @return {IAccountType[]}
*/
getByKeys(keys: string[]): IAccountType[] {
const { AccountType } = this.models;
return AccountType.query().whereIn('key', keys);
return this.cache.get(`accountType.keys.${keys.join(',')}`, () => {
return AccountType.query().whereIn('key', keys);
});
}
getByKey(key: string) {
/**
* Retrieve account tpy eof the given key.
* @param {string} key
* @return {IAccountType}
*/
getByKey(key: string): IAccountType {
const { AccountType } = this.models;
return AccountType.query().findOne('key', key);
return this.cache.get(`accountType.key.${key}`, () => {
return AccountType.query().findOne('key', key);
});
}
getByRootType(rootType: string) {
/**
* Retrieve accounts types of the given root type.
* @param {string} rootType
* @return {IAccountType[]}
*/
getByRootType(rootType: string): IAccountType[] {
const { AccountType } = this.models;
return AccountType.query().where('root_type', rootType);
return this.cache.get(`accountType.rootType.${rootType}`, () => {
return AccountType.query().where('root_type', rootType);
});
}
}