@okf/ootils 1.5.6 → 1.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/node.d.mts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { Client } from '@elastic/elasticsearch';
2
+ import Redis from 'ioredis';
2
3
  import mongoose, { Connection, Document, Types } from 'mongoose';
3
- import IORedis from 'ioredis';
4
4
  import * as bullmq from 'bullmq';
5
5
  import { Queue } from 'bullmq/dist/esm/classes/queue';
6
6
  import { Worker } from 'bullmq/dist/esm/classes/worker';
@@ -79,7 +79,7 @@ interface Section {
79
79
  [key: string]: any;
80
80
  blocks?: Block[];
81
81
  }
82
- interface Template$1 {
82
+ interface Template {
83
83
  category: string;
84
84
  kp_templates?: {
85
85
  [spaceId: string]: SpaceConfig;
@@ -128,7 +128,7 @@ type BuilderType = 'FormBuilder' | 'MetaBuilder' | 'kp_settings' | string;
128
128
  * ```
129
129
  */
130
130
  declare const extractAllBlocksFromTpl: ({ tpl, buildersWhitelist }: {
131
- tpl: Template$1;
131
+ tpl: Template;
132
132
  buildersWhitelist?: BuilderType[];
133
133
  }) => ExtractedBlock[];
134
134
  /**
@@ -208,6 +208,116 @@ declare namespace ElasticSearchConnector {
208
208
  let instance: any;
209
209
  }
210
210
 
211
+ declare class RedisCacheConnector {
212
+ static getEnv(): any;
213
+ static getInstance(): any;
214
+ static getClient(): any;
215
+ /**
216
+ * Find documents in Redis cache
217
+ * @param {Object} params - Search parameters
218
+ * @param {string} params.tenant - The tenant identifier
219
+ * @param {'platformConfigs'|'tpl'} params.modelName - The collection to search in
220
+ * @param {string} [params.type] - When modelName='platformConfigs': one of platformConfigTypes array values; when modelName='tpl': content type string. If omitted, returns all documents for the collection.
221
+ * @returns {Promise<Array>} Array of matching documents
222
+ * @see {platformConfigTypes} for valid platformConfigs type values
223
+ */
224
+ static findConfigsInCache({ tenant, modelName, type }: {
225
+ tenant: string;
226
+ modelName: "platformConfigs" | "tpl";
227
+ type?: string | undefined;
228
+ }): Promise<any[]>;
229
+ /**
230
+ * Find a single document in Redis cache (returns first match)
231
+ * @param {Object} params - Search parameters
232
+ * @param {string} params.tenant - The tenant identifier
233
+ * @param {'platformConfigs'|'tpl'} params.modelName - The collection to search in
234
+ * @param {string} [params.type] - When modelName='platformConfigs': one of platformConfigTypes array values; when modelName='tpl': content type string. If omitted, returns first document of any type.
235
+ * @returns {Promise<Object|undefined>} First matching document or undefined if none found
236
+ * @see {platformConfigTypes} for valid platformConfigs type values
237
+ */
238
+ static findOneConfigInCache({ tenant, modelName, type }: {
239
+ tenant: string;
240
+ modelName: "platformConfigs" | "tpl";
241
+ type?: string | undefined;
242
+ }): Promise<Object | undefined>;
243
+ /**
244
+ * Generate Redis key for config caching
245
+ * @private
246
+ * @param {Object} params - Parameters for key generation
247
+ * @param {string} params.tenant - The tenant identifier
248
+ * @param {'platformConfigs'|'tpl'} params.modelName - The model/collection name
249
+ * @param {string} params.type - The document type
250
+ * @param {string} [params.env] - Environment (defaults to current env)
251
+ * @returns {string} The Redis key
252
+ */
253
+ private static _getRedisKeyForConfig;
254
+ /**
255
+ * Set a single config document in Redis cache
256
+ * @param {Object} params - Cache parameters
257
+ * @param {string} params.tenant - The tenant identifier
258
+ * @param {'platformConfigs'|'tpl'} params.modelName - The model/collection name
259
+ * @param {string} params.type - When modelName='platformConfigs': one of platformConfigTypes array values; when modelName='tpl': content type string
260
+ * @param {Object} params.val - The value to cache
261
+ * @param {string} [params.env] - Environment (defaults to current env)
262
+ * @returns {Promise<void>}
263
+ * @see {platformConfigTypes} for valid platformConfigs type values
264
+ */
265
+ static setOneConfigInCache({ tenant, modelName, type, val, env }: {
266
+ tenant: string;
267
+ modelName: "platformConfigs" | "tpl";
268
+ type: string;
269
+ val: Object;
270
+ env?: string | undefined;
271
+ }): Promise<void>;
272
+ /**
273
+ * Invalidate (delete) a config from Redis cache and optionally set a new value
274
+ * @param {Object} params - Cache parameters
275
+ * @param {string} params.tenant - The tenant identifier
276
+ * @param {'platformConfigs'|'tpl'} params.modelName - The model/collection name
277
+ * @param {string} params.type - When modelName='platformConfigs': one of platformConfigTypes array values; when modelName='tpl': content type string
278
+ * @param {Object} [params.newVal] - New value to set after deletion
279
+ * @param {string} [params.env] - Environment (defaults to current env)
280
+ * @returns {Promise<void>}
281
+ * @see {platformConfigTypes} for valid platformConfigs type values
282
+ * @note According to the current usecases this function is called only after a database operation.
283
+ * We should include this function call within the MongoDB transaction.
284
+ * Otherwise, if the database operation succeeds but cache deletion fails, the cache will hold the outdated data.
285
+ * We can ignore setAsync failure as we are checking db also if cache is not present (inside findInCache function)
286
+ */
287
+ static invalidateOneConfigInCache({ tenant, modelName, type, newVal, env, }: {
288
+ tenant: string;
289
+ modelName: "platformConfigs" | "tpl";
290
+ type: string;
291
+ newVal?: Object | undefined;
292
+ env?: string | undefined;
293
+ }): Promise<void>;
294
+ /**
295
+ * Load all collections and templates into Redis cache for all tenants
296
+ * Clears existing cache and rebuilds it from database
297
+ * @returns {Promise<void>}
298
+ * @param {string} [params.tenant] - Tenant (either a single tenant or an array of tenants for whom the config cache should be refreshed)
299
+ * @static
300
+ */
301
+ static loadTplsAndPlatformConfigsToCache({ tenant }?: string): Promise<void>;
302
+ constructor(options?: {});
303
+ client: Redis | null;
304
+ host: any;
305
+ env: any;
306
+ port: any;
307
+ password: any;
308
+ /**
309
+ * Initialize Redis client
310
+ */
311
+ connect: () => Promise<Redis>;
312
+ /**
313
+ * Close the Redis client
314
+ */
315
+ close(): Promise<void>;
316
+ }
317
+ declare namespace RedisCacheConnector {
318
+ let instance: any;
319
+ }
320
+
211
321
  /**
212
322
  * Get database connection by tenant
213
323
  * @param tenant - Tenant identifier for the database
@@ -273,41 +383,6 @@ declare function getTplModelByTenant({ tenant, env, mongodb, dbConfigs }: {
273
383
  dbConfigs: any;
274
384
  }): any;
275
385
 
276
- interface GetTplParams {
277
- name: string;
278
- tenant: string;
279
- }
280
- interface GetAIConfigsParams {
281
- tenant: string;
282
- }
283
- interface Template {
284
- kp_content_type: string;
285
- category: Record<string, string>;
286
- kp_templates: Record<string, any>;
287
- listing: Record<string, any>;
288
- general: Record<string, any>;
289
- }
290
- interface Config {
291
- enable: boolean;
292
- contentTypes?: [{
293
- name: string;
294
- config?: Record<string, any>;
295
- }];
296
- defaultConfig: Record<string, any>;
297
- }
298
- interface AIconfig {
299
- contentChunking: Config;
300
- contentEnhance: Config;
301
- annoChunking: Config;
302
- embedding: Config;
303
- aiAnnotation: Config;
304
- }
305
- declare const getTpl: ({ name, tenant }: GetTplParams) => Promise<Template>;
306
- declare const getAIConfigs: ({ tenant }: GetAIConfigsParams) => Promise<AIconfig>;
307
-
308
- declare const connectToRedis: () => Promise<void>;
309
- declare const getRedisClient: () => IORedis;
310
-
311
386
  interface ITagData {
312
387
  _id?: Types.ObjectId;
313
388
  display: string;
@@ -401,6 +476,7 @@ declare const AIChatSchema: mongoose.Schema<IAIChat, mongoose.Model<IAIChat, any
401
476
  }>;
402
477
 
403
478
  declare const platformConfigTypes: readonly ["roles", "nav", "deployment", "userAgreement", "localeData", "theme", "ai"];
479
+
404
480
  type PlatformConfigType = typeof platformConfigTypes[number];
405
481
  interface IPlatformConfig extends Document {
406
482
  type: PlatformConfigType;
@@ -629,4 +705,4 @@ declare function GET_GLOBAL_BULLMQ_CONFIG({ env, redisCredentials }: {
629
705
  };
630
706
  }): Object;
631
707
 
632
- export { AIChatSchema, AnnotationSchema, BaseProducer, BaseWorker, ElasticSearchConnector, GET_GLOBAL_BULLMQ_CONFIG, MongoConnector, PlatformConfigsSchema, ProducerManager, TplSchema, WorkerManager, connectToRedis, deleteVal, extractAllBlocksFromTpl, genTagId, getAIChatModelByTenant, getAIConfigs, getAnnotationsModelByTenant, getDbByTenant, getModelByTenant, getPlatformConfigsModelByTenant, getRedisClient, getTpl, getTplModelByTenant, getVal, initializeGlobalConfig, _recursExtractBlocks as recursivelyExtractBlocks, setVal, toArray, updateGlobalConfig };
708
+ export { AIChatSchema, AnnotationSchema, BaseProducer, BaseWorker, ElasticSearchConnector, GET_GLOBAL_BULLMQ_CONFIG, MongoConnector, PlatformConfigsSchema, ProducerManager, RedisCacheConnector, TplSchema, WorkerManager, deleteVal, extractAllBlocksFromTpl, genTagId, getAIChatModelByTenant, getAnnotationsModelByTenant, getDbByTenant, getModelByTenant, getPlatformConfigsModelByTenant, getTplModelByTenant, getVal, initializeGlobalConfig, _recursExtractBlocks as recursivelyExtractBlocks, setVal, toArray, updateGlobalConfig };
package/dist/node.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { Client } from '@elastic/elasticsearch';
2
+ import Redis from 'ioredis';
2
3
  import mongoose, { Connection, Document, Types } from 'mongoose';
3
- import IORedis from 'ioredis';
4
4
  import * as bullmq from 'bullmq';
5
5
  import { Queue } from 'bullmq/dist/esm/classes/queue';
6
6
  import { Worker } from 'bullmq/dist/esm/classes/worker';
@@ -79,7 +79,7 @@ interface Section {
79
79
  [key: string]: any;
80
80
  blocks?: Block[];
81
81
  }
82
- interface Template$1 {
82
+ interface Template {
83
83
  category: string;
84
84
  kp_templates?: {
85
85
  [spaceId: string]: SpaceConfig;
@@ -128,7 +128,7 @@ type BuilderType = 'FormBuilder' | 'MetaBuilder' | 'kp_settings' | string;
128
128
  * ```
129
129
  */
130
130
  declare const extractAllBlocksFromTpl: ({ tpl, buildersWhitelist }: {
131
- tpl: Template$1;
131
+ tpl: Template;
132
132
  buildersWhitelist?: BuilderType[];
133
133
  }) => ExtractedBlock[];
134
134
  /**
@@ -208,6 +208,116 @@ declare namespace ElasticSearchConnector {
208
208
  let instance: any;
209
209
  }
210
210
 
211
+ declare class RedisCacheConnector {
212
+ static getEnv(): any;
213
+ static getInstance(): any;
214
+ static getClient(): any;
215
+ /**
216
+ * Find documents in Redis cache
217
+ * @param {Object} params - Search parameters
218
+ * @param {string} params.tenant - The tenant identifier
219
+ * @param {'platformConfigs'|'tpl'} params.modelName - The collection to search in
220
+ * @param {string} [params.type] - When modelName='platformConfigs': one of platformConfigTypes array values; when modelName='tpl': content type string. If omitted, returns all documents for the collection.
221
+ * @returns {Promise<Array>} Array of matching documents
222
+ * @see {platformConfigTypes} for valid platformConfigs type values
223
+ */
224
+ static findConfigsInCache({ tenant, modelName, type }: {
225
+ tenant: string;
226
+ modelName: "platformConfigs" | "tpl";
227
+ type?: string | undefined;
228
+ }): Promise<any[]>;
229
+ /**
230
+ * Find a single document in Redis cache (returns first match)
231
+ * @param {Object} params - Search parameters
232
+ * @param {string} params.tenant - The tenant identifier
233
+ * @param {'platformConfigs'|'tpl'} params.modelName - The collection to search in
234
+ * @param {string} [params.type] - When modelName='platformConfigs': one of platformConfigTypes array values; when modelName='tpl': content type string. If omitted, returns first document of any type.
235
+ * @returns {Promise<Object|undefined>} First matching document or undefined if none found
236
+ * @see {platformConfigTypes} for valid platformConfigs type values
237
+ */
238
+ static findOneConfigInCache({ tenant, modelName, type }: {
239
+ tenant: string;
240
+ modelName: "platformConfigs" | "tpl";
241
+ type?: string | undefined;
242
+ }): Promise<Object | undefined>;
243
+ /**
244
+ * Generate Redis key for config caching
245
+ * @private
246
+ * @param {Object} params - Parameters for key generation
247
+ * @param {string} params.tenant - The tenant identifier
248
+ * @param {'platformConfigs'|'tpl'} params.modelName - The model/collection name
249
+ * @param {string} params.type - The document type
250
+ * @param {string} [params.env] - Environment (defaults to current env)
251
+ * @returns {string} The Redis key
252
+ */
253
+ private static _getRedisKeyForConfig;
254
+ /**
255
+ * Set a single config document in Redis cache
256
+ * @param {Object} params - Cache parameters
257
+ * @param {string} params.tenant - The tenant identifier
258
+ * @param {'platformConfigs'|'tpl'} params.modelName - The model/collection name
259
+ * @param {string} params.type - When modelName='platformConfigs': one of platformConfigTypes array values; when modelName='tpl': content type string
260
+ * @param {Object} params.val - The value to cache
261
+ * @param {string} [params.env] - Environment (defaults to current env)
262
+ * @returns {Promise<void>}
263
+ * @see {platformConfigTypes} for valid platformConfigs type values
264
+ */
265
+ static setOneConfigInCache({ tenant, modelName, type, val, env }: {
266
+ tenant: string;
267
+ modelName: "platformConfigs" | "tpl";
268
+ type: string;
269
+ val: Object;
270
+ env?: string | undefined;
271
+ }): Promise<void>;
272
+ /**
273
+ * Invalidate (delete) a config from Redis cache and optionally set a new value
274
+ * @param {Object} params - Cache parameters
275
+ * @param {string} params.tenant - The tenant identifier
276
+ * @param {'platformConfigs'|'tpl'} params.modelName - The model/collection name
277
+ * @param {string} params.type - When modelName='platformConfigs': one of platformConfigTypes array values; when modelName='tpl': content type string
278
+ * @param {Object} [params.newVal] - New value to set after deletion
279
+ * @param {string} [params.env] - Environment (defaults to current env)
280
+ * @returns {Promise<void>}
281
+ * @see {platformConfigTypes} for valid platformConfigs type values
282
+ * @note According to the current usecases this function is called only after a database operation.
283
+ * We should include this function call within the MongoDB transaction.
284
+ * Otherwise, if the database operation succeeds but cache deletion fails, the cache will hold the outdated data.
285
+ * We can ignore setAsync failure as we are checking db also if cache is not present (inside findInCache function)
286
+ */
287
+ static invalidateOneConfigInCache({ tenant, modelName, type, newVal, env, }: {
288
+ tenant: string;
289
+ modelName: "platformConfigs" | "tpl";
290
+ type: string;
291
+ newVal?: Object | undefined;
292
+ env?: string | undefined;
293
+ }): Promise<void>;
294
+ /**
295
+ * Load all collections and templates into Redis cache for all tenants
296
+ * Clears existing cache and rebuilds it from database
297
+ * @returns {Promise<void>}
298
+ * @param {string} [params.tenant] - Tenant (either a single tenant or an array of tenants for whom the config cache should be refreshed)
299
+ * @static
300
+ */
301
+ static loadTplsAndPlatformConfigsToCache({ tenant }?: string): Promise<void>;
302
+ constructor(options?: {});
303
+ client: Redis | null;
304
+ host: any;
305
+ env: any;
306
+ port: any;
307
+ password: any;
308
+ /**
309
+ * Initialize Redis client
310
+ */
311
+ connect: () => Promise<Redis>;
312
+ /**
313
+ * Close the Redis client
314
+ */
315
+ close(): Promise<void>;
316
+ }
317
+ declare namespace RedisCacheConnector {
318
+ let instance: any;
319
+ }
320
+
211
321
  /**
212
322
  * Get database connection by tenant
213
323
  * @param tenant - Tenant identifier for the database
@@ -273,41 +383,6 @@ declare function getTplModelByTenant({ tenant, env, mongodb, dbConfigs }: {
273
383
  dbConfigs: any;
274
384
  }): any;
275
385
 
276
- interface GetTplParams {
277
- name: string;
278
- tenant: string;
279
- }
280
- interface GetAIConfigsParams {
281
- tenant: string;
282
- }
283
- interface Template {
284
- kp_content_type: string;
285
- category: Record<string, string>;
286
- kp_templates: Record<string, any>;
287
- listing: Record<string, any>;
288
- general: Record<string, any>;
289
- }
290
- interface Config {
291
- enable: boolean;
292
- contentTypes?: [{
293
- name: string;
294
- config?: Record<string, any>;
295
- }];
296
- defaultConfig: Record<string, any>;
297
- }
298
- interface AIconfig {
299
- contentChunking: Config;
300
- contentEnhance: Config;
301
- annoChunking: Config;
302
- embedding: Config;
303
- aiAnnotation: Config;
304
- }
305
- declare const getTpl: ({ name, tenant }: GetTplParams) => Promise<Template>;
306
- declare const getAIConfigs: ({ tenant }: GetAIConfigsParams) => Promise<AIconfig>;
307
-
308
- declare const connectToRedis: () => Promise<void>;
309
- declare const getRedisClient: () => IORedis;
310
-
311
386
  interface ITagData {
312
387
  _id?: Types.ObjectId;
313
388
  display: string;
@@ -401,6 +476,7 @@ declare const AIChatSchema: mongoose.Schema<IAIChat, mongoose.Model<IAIChat, any
401
476
  }>;
402
477
 
403
478
  declare const platformConfigTypes: readonly ["roles", "nav", "deployment", "userAgreement", "localeData", "theme", "ai"];
479
+
404
480
  type PlatformConfigType = typeof platformConfigTypes[number];
405
481
  interface IPlatformConfig extends Document {
406
482
  type: PlatformConfigType;
@@ -629,4 +705,4 @@ declare function GET_GLOBAL_BULLMQ_CONFIG({ env, redisCredentials }: {
629
705
  };
630
706
  }): Object;
631
707
 
632
- export { AIChatSchema, AnnotationSchema, BaseProducer, BaseWorker, ElasticSearchConnector, GET_GLOBAL_BULLMQ_CONFIG, MongoConnector, PlatformConfigsSchema, ProducerManager, TplSchema, WorkerManager, connectToRedis, deleteVal, extractAllBlocksFromTpl, genTagId, getAIChatModelByTenant, getAIConfigs, getAnnotationsModelByTenant, getDbByTenant, getModelByTenant, getPlatformConfigsModelByTenant, getRedisClient, getTpl, getTplModelByTenant, getVal, initializeGlobalConfig, _recursExtractBlocks as recursivelyExtractBlocks, setVal, toArray, updateGlobalConfig };
708
+ export { AIChatSchema, AnnotationSchema, BaseProducer, BaseWorker, ElasticSearchConnector, GET_GLOBAL_BULLMQ_CONFIG, MongoConnector, PlatformConfigsSchema, ProducerManager, RedisCacheConnector, TplSchema, WorkerManager, deleteVal, extractAllBlocksFromTpl, genTagId, getAIChatModelByTenant, getAnnotationsModelByTenant, getDbByTenant, getModelByTenant, getPlatformConfigsModelByTenant, getTplModelByTenant, getVal, initializeGlobalConfig, _recursExtractBlocks as recursivelyExtractBlocks, setVal, toArray, updateGlobalConfig };