UNPKG

@accounter/server

Version:

88 lines 3.54 kB
import { __decorate, __metadata } from "tslib"; import DataLoader from 'dataloader'; import { Injectable, Scope } from 'graphql-modules'; import { DBProvider } from '../../app-providers/db.provider.js'; import { sql } from '@pgtyped/runtime'; import { getCacheInstance } from '../../../shared/helpers/index.js'; const getFinancialAccountsByOwnerIds = sql ` SELECT * FROM accounter_schema.financial_accounts WHERE owner IN $$ownerIds;`; const getFinancialAccountsByAccountNumbers = sql ` SELECT * FROM accounter_schema.financial_accounts WHERE account_number IN $$accountNumbers;`; const getFinancialAccountsByAccountIDs = sql ` SELECT * FROM accounter_schema.financial_accounts WHERE id IN $$accountIDs;`; const getAllFinancialAccounts = sql ` SELECT * FROM accounter_schema.financial_accounts;`; let FinancialAccountsProvider = class FinancialAccountsProvider { dbProvider; cache = getCacheInstance({ stdTTL: 60 * 5, }); constructor(dbProvider) { this.dbProvider = dbProvider; } async batchFinancialAccountsByOwnerIds(ownerIds) { const accounts = await getFinancialAccountsByOwnerIds.run({ ownerIds, }, this.dbProvider); return ownerIds.map(ownerId => accounts.filter(charge => charge.owner === ownerId)); } getFinancialAccountsByOwnerIdLoader = new DataLoader((keys) => this.batchFinancialAccountsByOwnerIds(keys), { cacheKeyFn: key => `account-by-owner-id-${key}`, cacheMap: this.cache, }); async batchFinancialAccountsByAccountNumbers(accountNumbers) { const accounts = await getFinancialAccountsByAccountNumbers.run({ accountNumbers, }, this.dbProvider); return accountNumbers.map(accountNumber => accounts.find(charge => charge.account_number === accountNumber)); } getFinancialAccountByAccountNumberLoader = new DataLoader((keys) => this.batchFinancialAccountsByAccountNumbers(keys), { cacheKeyFn: key => `account-number-${key}`, cacheMap: this.cache, }); async batchFinancialAccountsByAccountIDs(accountIDs) { const accounts = await getFinancialAccountsByAccountIDs.run({ accountIDs, }, this.dbProvider); return accountIDs.map(id => accounts.find(account => account.id === id)); } getFinancialAccountByAccountIDLoader = new DataLoader((keys) => this.batchFinancialAccountsByAccountIDs(keys), { cacheKeyFn: key => `account-id-${key}`, cacheMap: this.cache, }); getAllFinancialAccounts() { const cached = this.cache.get('all-accounts'); if (cached) { return Promise.resolve(cached); } return getAllFinancialAccounts.run(undefined, this.dbProvider).then(res => { this.cache.set('all-accounts', res); res.map(account => { this.cache.set(`account-number-${account.account_number}`, account); this.cache.set(`account-id-${account.id}`, account); if (account.owner) this.cache.set(`account-by-owner-id-${account.owner}`, account); }); return res; }); } clearCache() { this.cache.clear(); } }; FinancialAccountsProvider = __decorate([ Injectable({ scope: Scope.Singleton, global: true, }), __metadata("design:paramtypes", [DBProvider]) ], FinancialAccountsProvider); export { FinancialAccountsProvider }; //# sourceMappingURL=financial-accounts.provider.js.map