jsm-core
Version:
Core library for JSM project
119 lines (118 loc) • 5.53 kB
TypeScript
import { JsmLogger } from "jsm-logger";
import { RedisClientType } from "redis";
import ActionsCacheManager from "./managers/actions.cache-manager";
import FCMTokensCacheManager from "./managers/fcm-tokens.cache-manager";
import HeavyComputingCacheManager from "./managers/heavy-computing.cache-manager";
import TasksCacheManager from "./managers/tasks.cache-manager";
export type TSdkClient = Jsm.Sdk.IClient & {
getById: (id: string, query?: any, config?: Jsm.Sdk.TRequestConfig) => Promise<Jsm.Core.Utils.Api.Response.BuildSingleItemResponse<Jsm.Core.CacheManager.TBaseEntityObject>>;
list: (query: {
filters?: Record<string, any>;
}, config?: Jsm.Sdk.TRequestConfig) => Promise<Jsm.Core.Utils.Api.Response.BuildListResponse<Jsm.Core.CacheManager.TBaseEntityObject>>;
};
export declare class CacheManager {
protected logger: JsmLogger;
protected client: RedisClientType;
constructor();
getClient(): Promise<RedisClientType>;
getClientStatus(): Promise<{
status: string;
db: {
dbSize: number | null;
name: number | undefined;
url: string | undefined;
};
redis: {
config: {
url: string;
port: number;
user: string | null;
password: string | null;
} | undefined;
};
}>;
/**
* Check if the entity is related to a company
* @param {string} entity - The entity name
* @returns {boolean} - True if the entity is related to a company, false otherwise
*/
protected _entityRelatedToCompany(entity: string): boolean;
generateEntityKey(entity: string, company?: string | null): string;
generateForeignKey(id: string, company?: string | null): string;
getCollectionEntries<T = any>(key: string): Promise<{
[key: string]: T;
}>;
setForeign<T>(key: string, id: string, value: T, config: {
company?: string | null;
}): Promise<void>;
getForeign<T>(key: string, id: string, config?: {
expiration?: number;
company?: string | null;
}): Promise<T | null>;
unsetForeign(key: string, id: string, company?: string | null): Promise<void>;
/**
*
* @param {Date} date
* @param {number} expiration in seconds
*/
isExpired(last_updated: Date, expiration: number): boolean;
generateLogItemName(method: CallableFunction): string;
loadObjectByIdFormDB<TEntity extends Jsm.Core.CacheManager.TBaseEntityObject>(sdkClient: TSdkClient, id: string): Promise<TEntity | undefined>;
loadObjectListFromDB<TEntity extends Jsm.Core.CacheManager.TBaseEntityObject, ListQuery extends {
filters?: Record<string, any>;
}>(sdkClient: TSdkClient, query: ListQuery, company: string | null): Promise<TEntity[]>;
set<TEntity extends Jsm.Core.CacheManager.TBaseEntityObject>(entity: string, id: string, value: TEntity, company?: string | null, config?: {
customKey?: string | null;
}): Promise<void>;
/**
*
* @param entity string representing the entity name [camelCased] single
* @param id
* @param config
* @returns
*/
get<TEntity extends Jsm.Core.CacheManager.TBaseEntityObject>(entity: string, id: string, _config?: {
expiration?: number;
forceLoadFromDb?: boolean;
company?: string | null;
customKey?: string | null;
sdkClient?: TSdkClient;
}): Promise<TEntity | undefined>;
getMany<TEntity extends Jsm.Core.CacheManager.TBaseEntityObject>(entity: string, ids: string[], _config?: {
expiration?: number;
forceLoadFromDb?: boolean;
company?: string | null;
sdkClient?: TSdkClient;
}): Promise<(TEntity | undefined)[]>;
unset(entity: string, id: string, company?: string | null): Promise<void>;
unsetAll(entity: string, company?: string | null): Promise<void>;
list<TEntity extends Jsm.Core.CacheManager.TBaseEntityObject>(entity: string, config?: {
query?: Jsm.Core.Utils.Api.Request.BuildSearchablePagedFilterable<TEntity>;
forceLoadFromDb?: boolean;
filter?: (item: TEntity) => boolean;
company?: string | null;
customKey?: string | null;
sdkClient?: TSdkClient;
}): Promise<TEntity[]>;
/**
* @description Retrieves a configuration value from the cache, returning a default value if not found or expired.
* @param {string} id - The identifier for the configuration value.
* @param {T} defaultValue - The default value to return if the configuration value is not found.
* @returns {Promise<T | D>} - A promise that resolves to the configuration value or the default value.
* @template T - The type of the configuration value.
* @template D - The type of the default value, which can be the same as T or undefined.
*/
getConfigValue<T, D extends T | undefined = T>(id: string, defaultValue: D): Promise<T | D>;
/**
* Sets a configuration value in the cache.
* @param {string} id - The identifier for the configuration value.
* @param {T} value - The value to set for the configuration.
* @param {number} [ttl] - Optional time-to-live in milliseconds.
*/
setConfigValue<T = any>(id: string, value: T, ttl?: number): Promise<void>;
get actions(): ActionsCacheManager;
get fcmTokens(): FCMTokensCacheManager;
get heavyComputing(): HeavyComputingCacheManager;
get tasks(): TasksCacheManager;
flushAll(): Promise<void>;
}