@accounter/server
Version:
The test suite is split into three Vitest projects for efficiency and isolation:
104 lines • 4.04 kB
JavaScript
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 getTemplate = sql `
SELECT *
FROM accounter_schema.dynamic_report_templates
WHERE name = $name AND owner_id = $ownerId;`;
const getTemplatesByOwnerIds = sql `
SELECT *
FROM accounter_schema.dynamic_report_templates
WHERE owner_id IN $$ownerIds;`;
const updateTemplate = sql `
UPDATE accounter_schema.dynamic_report_templates
SET template = $template
WHERE name = $name AND owner_id = $ownerId
RETURNING *;`;
const updateTemplateName = sql `
UPDATE accounter_schema.dynamic_report_templates
SET name = $newName
WHERE name = $prevName AND owner_id = $ownerId
RETURNING *;`;
const insertTemplate = sql `
INSERT INTO accounter_schema.dynamic_report_templates (name, owner_id, template)
VALUES ($name, $ownerId, $template)
RETURNING *;`;
const deleteTemplate = sql `
DELETE FROM accounter_schema.dynamic_report_templates
WHERE name = $name AND owner_id = $ownerId
RETURNING name;`;
let DynamicReportProvider = class DynamicReportProvider {
dbProvider;
cache = getCacheInstance({
stdTTL: 60 * 60 * 5,
});
constructor(dbProvider) {
this.dbProvider = dbProvider;
}
async getTemplate(params) {
return getTemplate.run(params, this.dbProvider).then(res => {
const [template] = res;
if (template) {
this.cache.set(`template-owner-${template.owner_id}-${template.name}`, template);
}
return template;
});
}
async batchTemplatesByOwnerIdLoader(ownerIds) {
const templates = await getTemplatesByOwnerIds.run({ ownerIds }, this.dbProvider);
templates.map(template => this.cache.set(`template-owner-${template.owner_id}-${template.name}`, template));
return ownerIds.map(id => templates.filter(template => template.owner_id === id));
}
getTemplatesByOwnerIdLoader = new DataLoader((ownerIds) => this.batchTemplatesByOwnerIdLoader(ownerIds), {
cacheKeyFn: id => `templates-owner-${id}`,
cacheMap: this.cache,
});
async updateTemplate(params) {
if (params.name && params.ownerId) {
await this.invalidateByNameAndOwnerId(params.name, params.ownerId);
}
return updateTemplate.run(params, this.dbProvider);
}
async updateTemplateName(params) {
if (params.prevName && params.ownerId) {
await this.invalidateByNameAndOwnerId(params.prevName, params.ownerId);
}
return updateTemplateName.run(params, this.dbProvider);
}
insertTemplate(params) {
if (params.ownerId) {
this.invalidateByOwnerId(params.ownerId);
}
return insertTemplate.run(params, this.dbProvider);
}
async deleteTemplate(params) {
if (params.name && params.ownerId) {
await this.invalidateByNameAndOwnerId(params.name, params.ownerId);
}
return deleteTemplate.run(params, this.dbProvider);
}
async invalidateByOwnerId(ownerId) {
const templates = await this.getTemplatesByOwnerIdLoader.load(ownerId);
await Promise.all(templates.map(({ name }) => this.invalidateByNameAndOwnerId(name, ownerId)));
this.cache.delete(`templates-owner-${ownerId}`);
}
async invalidateByNameAndOwnerId(name, ownerId) {
this.cache.delete(`templates-owner-${ownerId}`);
this.cache.delete(`template-owner-${ownerId}-${name}`);
}
clearCache() {
this.cache.clear();
}
};
DynamicReportProvider = __decorate([
Injectable({
scope: Scope.Singleton,
global: true,
}),
__metadata("design:paramtypes", [DBProvider])
], DynamicReportProvider);
export { DynamicReportProvider };
//# sourceMappingURL=dynamic-report.provider.js.map