mirror of
https://github.com/ToolJet/ToolJet
synced 2026-04-27 16:37:42 +00:00
33 lines
941 B
TypeScript
33 lines
941 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'];
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|