UNPKG

@accounter/server

Version:
53 lines 1.81 kB
import { __decorate, __metadata } from "tslib"; import DataLoader from 'dataloader'; import { Injectable, Scope } from 'graphql-modules'; import { sql } from '@pgtyped/runtime'; import { getCacheInstance } from '../../../shared/helpers/index.js'; import { DBProvider } from '../../app-providers/db.provider.js'; const getAllCountries = sql ` SELECT * FROM accounter_schema.countries;`; let CountriesProvider = class CountriesProvider { dbProvider; cache = getCacheInstance({ stdTTL: 60 * 60, // 1 hours }); constructor(dbProvider) { this.dbProvider = dbProvider; } getAllCountries() { const cached = this.cache.get('all-countries'); if (cached) { return Promise.resolve(cached); } return getAllCountries.run(undefined, this.dbProvider).then(countries => { if (countries) { this.cache.set('all-countries', countries); countries.map(country => { this.cache.set(`country-${country.code}`, country); }); } return countries; }); } async batchCountryByCode(codes) { const countries = await this.getAllCountries(); return codes.map(code => countries.find(country => country.code === code)); } getCountryByCodeLoader = new DataLoader((codes) => this.batchCountryByCode(codes), { cacheKeyFn: code => `country-${code}`, cacheMap: this.cache, }); clearCache() { this.cache.clear(); } }; CountriesProvider = __decorate([ Injectable({ scope: Scope.Singleton, global: true, }), __metadata("design:paramtypes", [DBProvider]) ], CountriesProvider); export { CountriesProvider }; //# sourceMappingURL=countries.provider.js.map