ToolJet/plugins/packages/common/lib/utils.helper.ts
Maurits Lourens 60f515d19c
Feature/2395 - add eslint to plugins (#2402)
* merge develop

* Add eslint dependencies, configs and scripts to plugins project

* run lint with Github action

* ignore tests and dist folders

* fun eslint with --fix and manual fixes, renamed __tests_ to __tests__

* add plugins packages folder to lint-staged config

* fix lint issue
2022-03-10 12:29:48 +05:30

33 lines
942 B
TypeScript

import { QueryError } from './query.error';
const CACHED_CONNECTIONS: any = {};
export function parseJson(jsonString: string, errorMessage?: string): object {
try {
return JSON.parse(jsonString);
} catch (err) {
throw new QueryError(errorMessage, err.message, {});
}
}
export function cacheConnection(dataSourceId: string, connection: any): any {
const updatedAt = new Date();
CACHED_CONNECTIONS[dataSourceId] = { connection, updatedAt };
}
export function getCachedConnection(dataSourceId: string | number, dataSourceUpdatedAt: any): any {
const cachedData = CACHED_CONNECTIONS[dataSourceId];
if (cachedData) {
const updatedAt = new Date(dataSourceUpdatedAt || null);
const cachedAt = new Date(cachedData.updatedAt || null);
const diffTime = (cachedAt.getTime() - updatedAt.getTime()) / 1000;
if (diffTime < 0) {
return null;
} else {
return cachedData['connection'];
}
}
}