@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 +115 -39
- package/dist/node.d.ts +115 -39
- package/dist/node.js +383 -178
- package/dist/node.mjs +378 -170
- package/package.json +1 -1
package/dist/node.mjs
CHANGED
|
@@ -170,15 +170,10 @@ var init_Annotations = __esm({
|
|
|
170
170
|
}
|
|
171
171
|
});
|
|
172
172
|
|
|
173
|
-
// src/models/PlatformConfigs.ts
|
|
174
|
-
var
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
});
|
|
178
|
-
import mongoose2 from "mongoose";
|
|
179
|
-
var platformConfigTypes, PlatformConfigsSchema, PlatformConfigs_default;
|
|
180
|
-
var init_PlatformConfigs = __esm({
|
|
181
|
-
"src/models/PlatformConfigs.ts"() {
|
|
173
|
+
// src/models/PlatformConfigs/platformConfigTypes.ts
|
|
174
|
+
var platformConfigTypes;
|
|
175
|
+
var init_platformConfigTypes = __esm({
|
|
176
|
+
"src/models/PlatformConfigs/platformConfigTypes.ts"() {
|
|
182
177
|
"use strict";
|
|
183
178
|
platformConfigTypes = [
|
|
184
179
|
"roles",
|
|
@@ -189,6 +184,20 @@ var init_PlatformConfigs = __esm({
|
|
|
189
184
|
"theme",
|
|
190
185
|
"ai"
|
|
191
186
|
];
|
|
187
|
+
}
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
// src/models/PlatformConfigs/index.ts
|
|
191
|
+
var PlatformConfigs_exports = {};
|
|
192
|
+
__export(PlatformConfigs_exports, {
|
|
193
|
+
default: () => PlatformConfigs_default
|
|
194
|
+
});
|
|
195
|
+
import mongoose2 from "mongoose";
|
|
196
|
+
var PlatformConfigsSchema, PlatformConfigs_default;
|
|
197
|
+
var init_PlatformConfigs = __esm({
|
|
198
|
+
"src/models/PlatformConfigs/index.ts"() {
|
|
199
|
+
"use strict";
|
|
200
|
+
init_platformConfigTypes();
|
|
192
201
|
PlatformConfigsSchema = new mongoose2.Schema(
|
|
193
202
|
{
|
|
194
203
|
type: {
|
|
@@ -274,7 +283,8 @@ var init_Tpl = __esm({
|
|
|
274
283
|
title: String,
|
|
275
284
|
singular: String,
|
|
276
285
|
ctaText: String,
|
|
277
|
-
listingDesc: String
|
|
286
|
+
listingDesc: String,
|
|
287
|
+
aiDesc: String
|
|
278
288
|
},
|
|
279
289
|
allowQuickTagCreation: { enable: Boolean },
|
|
280
290
|
segment: String,
|
|
@@ -588,36 +598,6 @@ var require_ElasticSearchConnector = __commonJS({
|
|
|
588
598
|
}
|
|
589
599
|
});
|
|
590
600
|
|
|
591
|
-
// src/db/getDbByTenant.js
|
|
592
|
-
var getDbByTenant_exports = {};
|
|
593
|
-
__export(getDbByTenant_exports, {
|
|
594
|
-
getDbByTenant: () => getDbByTenant
|
|
595
|
-
});
|
|
596
|
-
import { Connection } from "mongoose";
|
|
597
|
-
var import_MongoConnector, getDbByTenant;
|
|
598
|
-
var init_getDbByTenant = __esm({
|
|
599
|
-
"src/db/getDbByTenant.js"() {
|
|
600
|
-
"use strict";
|
|
601
|
-
import_MongoConnector = __toESM(require_MongoConnector());
|
|
602
|
-
getDbByTenant = ({
|
|
603
|
-
tenant,
|
|
604
|
-
env: _env
|
|
605
|
-
}) => {
|
|
606
|
-
if (!tenant) throw new Error("tenant id has not been provided");
|
|
607
|
-
const env = _env || import_MongoConnector.MongoConnector.getEnv();
|
|
608
|
-
const dbConfigs = import_MongoConnector.MongoConnector.getDbConfigs();
|
|
609
|
-
const clusterConnections = import_MongoConnector.MongoConnector.getClusterConnections();
|
|
610
|
-
const { CLUSTER_NAME } = dbConfigs[env];
|
|
611
|
-
const dbName = `${tenant}_${env}`;
|
|
612
|
-
const connection = clusterConnections[CLUSTER_NAME];
|
|
613
|
-
if (!connection) {
|
|
614
|
-
throw new Error(`Connection not found for cluster: ${CLUSTER_NAME}`);
|
|
615
|
-
}
|
|
616
|
-
return connection.useDb(dbName, { useCache: true });
|
|
617
|
-
};
|
|
618
|
-
}
|
|
619
|
-
});
|
|
620
|
-
|
|
621
601
|
// src/models/AIChat.ts
|
|
622
602
|
import mongoose4 from "mongoose";
|
|
623
603
|
var AIChatSchema, AIChat_default;
|
|
@@ -680,13 +660,43 @@ var init_models = __esm({
|
|
|
680
660
|
}
|
|
681
661
|
});
|
|
682
662
|
|
|
663
|
+
// src/db/getDbByTenant.js
|
|
664
|
+
var getDbByTenant_exports = {};
|
|
665
|
+
__export(getDbByTenant_exports, {
|
|
666
|
+
getDbByTenant: () => getDbByTenant
|
|
667
|
+
});
|
|
668
|
+
import { Connection } from "mongoose";
|
|
669
|
+
var import_MongoConnector, getDbByTenant;
|
|
670
|
+
var init_getDbByTenant = __esm({
|
|
671
|
+
"src/db/getDbByTenant.js"() {
|
|
672
|
+
"use strict";
|
|
673
|
+
import_MongoConnector = __toESM(require_MongoConnector());
|
|
674
|
+
getDbByTenant = ({
|
|
675
|
+
tenant,
|
|
676
|
+
env: _env
|
|
677
|
+
}) => {
|
|
678
|
+
if (!tenant) throw new Error("tenant id has not been provided");
|
|
679
|
+
const env = _env || import_MongoConnector.MongoConnector.getEnv();
|
|
680
|
+
const dbConfigs = import_MongoConnector.MongoConnector.getDbConfigs();
|
|
681
|
+
const clusterConnections = import_MongoConnector.MongoConnector.getClusterConnections();
|
|
682
|
+
const { CLUSTER_NAME } = dbConfigs[env];
|
|
683
|
+
const dbName = `${tenant}_${env}`;
|
|
684
|
+
const connection = clusterConnections[CLUSTER_NAME];
|
|
685
|
+
if (!connection) {
|
|
686
|
+
throw new Error(`Connection not found for cluster: ${CLUSTER_NAME}`);
|
|
687
|
+
}
|
|
688
|
+
return connection.useDb(dbName, { useCache: true });
|
|
689
|
+
};
|
|
690
|
+
}
|
|
691
|
+
});
|
|
692
|
+
|
|
683
693
|
// src/db/getModelByTenant.js
|
|
684
694
|
var require_getModelByTenant = __commonJS({
|
|
685
695
|
"src/db/getModelByTenant.js"(exports, module) {
|
|
686
696
|
"use strict";
|
|
687
697
|
var { getDbByTenant: getDbByTenant2 } = (init_getDbByTenant(), __toCommonJS(getDbByTenant_exports));
|
|
688
698
|
var { AIChatSchema: AIChatSchema2, AnnotationSchema: AnnotationSchema2, PlatformConfigsSchema: PlatformConfigsSchema2, TplSchema: TplSchema2 } = (init_models(), __toCommonJS(models_exports));
|
|
689
|
-
var
|
|
699
|
+
var getModelByTenant3 = ({ tenant, modelName, schema, env }) => {
|
|
690
700
|
if (!tenant) {
|
|
691
701
|
throw new Error("tenant id has not been provided");
|
|
692
702
|
}
|
|
@@ -696,32 +706,32 @@ var require_getModelByTenant = __commonJS({
|
|
|
696
706
|
}
|
|
697
707
|
return db.model(modelName);
|
|
698
708
|
};
|
|
699
|
-
var getAnnotationsModelByTenant2 = ({ tenant, env, mongodb, dbConfigs, modelName }) =>
|
|
709
|
+
var getAnnotationsModelByTenant2 = ({ tenant, env, mongodb, dbConfigs, modelName }) => getModelByTenant3({
|
|
700
710
|
tenant,
|
|
701
711
|
modelName: modelName || "annotations",
|
|
702
712
|
schema: AnnotationSchema2,
|
|
703
713
|
env
|
|
704
714
|
});
|
|
705
|
-
var getPlatformConfigsModelByTenant2 = ({ tenant, env, mongodb, dbConfigs }) =>
|
|
715
|
+
var getPlatformConfigsModelByTenant2 = ({ tenant, env, mongodb, dbConfigs }) => getModelByTenant3({
|
|
706
716
|
tenant,
|
|
707
717
|
modelName: "platformConfigs",
|
|
708
718
|
schema: PlatformConfigsSchema2,
|
|
709
719
|
env
|
|
710
720
|
});
|
|
711
|
-
var getTplModelByTenant2 = ({ tenant, env, mongodb, dbConfigs }) =>
|
|
721
|
+
var getTplModelByTenant2 = ({ tenant, env, mongodb, dbConfigs }) => getModelByTenant3({
|
|
712
722
|
tenant,
|
|
713
723
|
modelName: "tpl",
|
|
714
724
|
schema: TplSchema2,
|
|
715
725
|
env
|
|
716
726
|
});
|
|
717
|
-
var getAIChatModelByTenant2 = ({ tenant, env, mongodb, dbConfigs }) =>
|
|
727
|
+
var getAIChatModelByTenant2 = ({ tenant, env, mongodb, dbConfigs }) => getModelByTenant3({
|
|
718
728
|
tenant,
|
|
719
729
|
modelName: "aiChat",
|
|
720
730
|
schema: AIChatSchema2,
|
|
721
731
|
env
|
|
722
732
|
});
|
|
723
733
|
module.exports = {
|
|
724
|
-
getModelByTenant:
|
|
734
|
+
getModelByTenant: getModelByTenant3,
|
|
725
735
|
getAIChatModelByTenant: getAIChatModelByTenant2,
|
|
726
736
|
getAnnotationsModelByTenant: getAnnotationsModelByTenant2,
|
|
727
737
|
getPlatformConfigsModelByTenant: getPlatformConfigsModelByTenant2,
|
|
@@ -1258,10 +1268,10 @@ var init_GLOBAL_BULLMQ_CONFIG = __esm({
|
|
|
1258
1268
|
var require_GET_GLOBAL_BULLMQ_CONFIG = __commonJS({
|
|
1259
1269
|
"src/bullmq/GET_GLOBAL_BULLMQ_CONFIG.js"(exports, module) {
|
|
1260
1270
|
"use strict";
|
|
1261
|
-
var { default:
|
|
1271
|
+
var { default: Redis2 } = __require("ioredis");
|
|
1262
1272
|
var { BASE_BULLMQ_CONFIG: BASE_BULLMQ_CONFIG2 } = (init_GLOBAL_BULLMQ_CONFIG(), __toCommonJS(GLOBAL_BULLMQ_CONFIG_exports));
|
|
1263
1273
|
function GET_GLOBAL_BULLMQ_CONFIG2({ env, redisCredentials }) {
|
|
1264
|
-
const redisConnectionForBullMQ = new
|
|
1274
|
+
const redisConnectionForBullMQ = new Redis2({
|
|
1265
1275
|
host: redisCredentials.REDIS_HOST,
|
|
1266
1276
|
port: redisCredentials.REDIS_PORT,
|
|
1267
1277
|
password: redisCredentials.REDIS_PASSWORD,
|
|
@@ -1566,6 +1576,319 @@ var _extractBlocksFromSomeBuilders = ({
|
|
|
1566
1576
|
// src/node.ts
|
|
1567
1577
|
var import_MongoConnector2 = __toESM(require_MongoConnector());
|
|
1568
1578
|
var import_ElasticSearchConnector = __toESM(require_ElasticSearchConnector());
|
|
1579
|
+
|
|
1580
|
+
// src/RedisCacheConnector.js
|
|
1581
|
+
init_models();
|
|
1582
|
+
import Redis from "ioredis";
|
|
1583
|
+
var import_getModelByTenant = __toESM(require_getModelByTenant());
|
|
1584
|
+
init_platformConfigTypes();
|
|
1585
|
+
var BASE_SETTINGS_FOR_CONFIGS_CACHE = [
|
|
1586
|
+
{
|
|
1587
|
+
modelName: "tpl",
|
|
1588
|
+
collectionName: "tpls",
|
|
1589
|
+
schema: Tpl_default,
|
|
1590
|
+
defaultQuery: [
|
|
1591
|
+
{
|
|
1592
|
+
status: {
|
|
1593
|
+
$ne: "unpublished"
|
|
1594
|
+
}
|
|
1595
|
+
},
|
|
1596
|
+
{
|
|
1597
|
+
drafts: 0
|
|
1598
|
+
}
|
|
1599
|
+
],
|
|
1600
|
+
typeKey: "kp_content_type"
|
|
1601
|
+
},
|
|
1602
|
+
{
|
|
1603
|
+
modelName: "platformConfigs",
|
|
1604
|
+
collectionName: "platformConfigs",
|
|
1605
|
+
schema: PlatformConfigs_default,
|
|
1606
|
+
defaultQuery: [],
|
|
1607
|
+
typeKey: "type"
|
|
1608
|
+
}
|
|
1609
|
+
];
|
|
1610
|
+
var RedisCacheConnector = class _RedisCacheConnector {
|
|
1611
|
+
constructor(options = {}) {
|
|
1612
|
+
/**
|
|
1613
|
+
* Initialize Redis client
|
|
1614
|
+
*/
|
|
1615
|
+
__publicField(this, "connect", async () => {
|
|
1616
|
+
try {
|
|
1617
|
+
this.client = new Redis({
|
|
1618
|
+
host: this.host,
|
|
1619
|
+
port: this.port,
|
|
1620
|
+
password: this.password
|
|
1621
|
+
});
|
|
1622
|
+
this.client.on("connect", () => {
|
|
1623
|
+
console.log("\u2705 Connected to our redis cache instance!");
|
|
1624
|
+
});
|
|
1625
|
+
await new Promise((resolve, reject) => {
|
|
1626
|
+
this.client.on("ready", resolve);
|
|
1627
|
+
this.client.on("error", reject);
|
|
1628
|
+
});
|
|
1629
|
+
return this.client;
|
|
1630
|
+
} catch (err) {
|
|
1631
|
+
console.error(err.message);
|
|
1632
|
+
process.exit(1);
|
|
1633
|
+
}
|
|
1634
|
+
});
|
|
1635
|
+
if (_RedisCacheConnector.instance) {
|
|
1636
|
+
throw new Error(
|
|
1637
|
+
"RedisCacheConnector instance already exists. Use RedisCacheConnector.getInstance() instead."
|
|
1638
|
+
);
|
|
1639
|
+
}
|
|
1640
|
+
this.client = null;
|
|
1641
|
+
this.host = options.host;
|
|
1642
|
+
this.env = options.env;
|
|
1643
|
+
this.port = options.port;
|
|
1644
|
+
this.password = options.password;
|
|
1645
|
+
_RedisCacheConnector.instance = this;
|
|
1646
|
+
if (!this.host) {
|
|
1647
|
+
throw new Error("Host must be provided in constructor options");
|
|
1648
|
+
}
|
|
1649
|
+
if (!this.port) {
|
|
1650
|
+
throw new Error("Port must be provided in constructor options");
|
|
1651
|
+
}
|
|
1652
|
+
if (!this.password) {
|
|
1653
|
+
throw new Error("Password must be provided in constructor options");
|
|
1654
|
+
}
|
|
1655
|
+
if (!this.env) {
|
|
1656
|
+
throw new Error("Env must be provided in constructor options");
|
|
1657
|
+
}
|
|
1658
|
+
}
|
|
1659
|
+
static getEnv() {
|
|
1660
|
+
if (!_RedisCacheConnector.instance) {
|
|
1661
|
+
throw new Error("RedisCacheConnector not initialized");
|
|
1662
|
+
}
|
|
1663
|
+
return _RedisCacheConnector.instance.env;
|
|
1664
|
+
}
|
|
1665
|
+
// Static method to get the instance
|
|
1666
|
+
static getInstance() {
|
|
1667
|
+
if (!_RedisCacheConnector.instance) {
|
|
1668
|
+
throw new Error("RedisCacheConnector not initialized");
|
|
1669
|
+
}
|
|
1670
|
+
return _RedisCacheConnector.instance;
|
|
1671
|
+
}
|
|
1672
|
+
// Static method to get the client
|
|
1673
|
+
static getClient() {
|
|
1674
|
+
if (!_RedisCacheConnector.instance || !_RedisCacheConnector.instance.client) {
|
|
1675
|
+
throw new Error(
|
|
1676
|
+
"RedisCacheConnector not initialized or client not connected"
|
|
1677
|
+
);
|
|
1678
|
+
}
|
|
1679
|
+
return _RedisCacheConnector.instance.client;
|
|
1680
|
+
}
|
|
1681
|
+
/**
|
|
1682
|
+
* Find documents in Redis cache
|
|
1683
|
+
* @param {Object} params - Search parameters
|
|
1684
|
+
* @param {string} params.tenant - The tenant identifier
|
|
1685
|
+
* @param {'platformConfigs'|'tpl'} params.modelName - The collection to search in
|
|
1686
|
+
* @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.
|
|
1687
|
+
* @returns {Promise<Array>} Array of matching documents
|
|
1688
|
+
* @see {platformConfigTypes} for valid platformConfigs type values
|
|
1689
|
+
*/
|
|
1690
|
+
static async findConfigsInCache({ tenant, modelName, type }) {
|
|
1691
|
+
try {
|
|
1692
|
+
const client = this.getClient();
|
|
1693
|
+
const environment = this.getEnv();
|
|
1694
|
+
const keyPrefix = `${environment}:${tenant}:${modelName}:`;
|
|
1695
|
+
const keys = await client.keys(`${keyPrefix}${type || "*"}`);
|
|
1696
|
+
if (keys.length === 0) {
|
|
1697
|
+
const { schema, typeKey } = BASE_SETTINGS_FOR_CONFIGS_CACHE.find(
|
|
1698
|
+
(obj) => obj.modelName === modelName
|
|
1699
|
+
);
|
|
1700
|
+
const model = (0, import_getModelByTenant.getModelByTenant)({
|
|
1701
|
+
tenant,
|
|
1702
|
+
env: environment,
|
|
1703
|
+
modelName,
|
|
1704
|
+
schema
|
|
1705
|
+
});
|
|
1706
|
+
let findQuery = {};
|
|
1707
|
+
if (type) {
|
|
1708
|
+
findQuery = { [typeKey]: type };
|
|
1709
|
+
}
|
|
1710
|
+
const data = await model.find(findQuery);
|
|
1711
|
+
await Promise.all(
|
|
1712
|
+
data.map(
|
|
1713
|
+
(doc) => this.setOneConfigInCache({
|
|
1714
|
+
tenant,
|
|
1715
|
+
modelName,
|
|
1716
|
+
type: doc[typeKey],
|
|
1717
|
+
val: doc
|
|
1718
|
+
})
|
|
1719
|
+
)
|
|
1720
|
+
);
|
|
1721
|
+
return data;
|
|
1722
|
+
}
|
|
1723
|
+
const cachedDocs = await Promise.all(
|
|
1724
|
+
keys.map(async (key) => {
|
|
1725
|
+
const value = await client.get(key);
|
|
1726
|
+
return JSON.parse(value);
|
|
1727
|
+
})
|
|
1728
|
+
);
|
|
1729
|
+
return cachedDocs;
|
|
1730
|
+
} catch (error) {
|
|
1731
|
+
console.error("Error in findConfigsInCache:", error);
|
|
1732
|
+
throw error;
|
|
1733
|
+
}
|
|
1734
|
+
}
|
|
1735
|
+
/**
|
|
1736
|
+
* Find a single document in Redis cache (returns first match)
|
|
1737
|
+
* @param {Object} params - Search parameters
|
|
1738
|
+
* @param {string} params.tenant - The tenant identifier
|
|
1739
|
+
* @param {'platformConfigs'|'tpl'} params.modelName - The collection to search in
|
|
1740
|
+
* @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.
|
|
1741
|
+
* @returns {Promise<Object|undefined>} First matching document or undefined if none found
|
|
1742
|
+
* @see {platformConfigTypes} for valid platformConfigs type values
|
|
1743
|
+
*/
|
|
1744
|
+
static async findOneConfigInCache({ tenant, modelName, type }) {
|
|
1745
|
+
const cacheData = await this.findConfigsInCache({
|
|
1746
|
+
tenant,
|
|
1747
|
+
modelName,
|
|
1748
|
+
type
|
|
1749
|
+
});
|
|
1750
|
+
return cacheData?.[0];
|
|
1751
|
+
}
|
|
1752
|
+
/**
|
|
1753
|
+
* Generate Redis key for config caching
|
|
1754
|
+
* @private
|
|
1755
|
+
* @param {Object} params - Parameters for key generation
|
|
1756
|
+
* @param {string} params.tenant - The tenant identifier
|
|
1757
|
+
* @param {'platformConfigs'|'tpl'} params.modelName - The model/collection name
|
|
1758
|
+
* @param {string} params.type - The document type
|
|
1759
|
+
* @param {string} [params.env] - Environment (defaults to current env)
|
|
1760
|
+
* @returns {string} The Redis key
|
|
1761
|
+
*/
|
|
1762
|
+
static _getRedisKeyForConfig({ tenant, modelName, type, env }) {
|
|
1763
|
+
const environment = env || this.getEnv();
|
|
1764
|
+
return `${environment}:${tenant}:${modelName}:${type}`;
|
|
1765
|
+
}
|
|
1766
|
+
/**
|
|
1767
|
+
* Set a single config document in Redis cache
|
|
1768
|
+
* @param {Object} params - Cache parameters
|
|
1769
|
+
* @param {string} params.tenant - The tenant identifier
|
|
1770
|
+
* @param {'platformConfigs'|'tpl'} params.modelName - The model/collection name
|
|
1771
|
+
* @param {string} params.type - When modelName='platformConfigs': one of platformConfigTypes array values; when modelName='tpl': content type string
|
|
1772
|
+
* @param {Object} params.val - The value to cache
|
|
1773
|
+
* @param {string} [params.env] - Environment (defaults to current env)
|
|
1774
|
+
* @returns {Promise<void>}
|
|
1775
|
+
* @see {platformConfigTypes} for valid platformConfigs type values
|
|
1776
|
+
*/
|
|
1777
|
+
static async setOneConfigInCache({ tenant, modelName, type, val, env }) {
|
|
1778
|
+
const client = this.getClient();
|
|
1779
|
+
const cacheKey = this._getRedisKeyForConfig({
|
|
1780
|
+
tenant,
|
|
1781
|
+
modelName,
|
|
1782
|
+
type,
|
|
1783
|
+
env
|
|
1784
|
+
});
|
|
1785
|
+
await client.set(cacheKey, JSON.stringify(val));
|
|
1786
|
+
}
|
|
1787
|
+
/**
|
|
1788
|
+
* Invalidate (delete) a config from Redis cache and optionally set a new value
|
|
1789
|
+
* @param {Object} params - Cache parameters
|
|
1790
|
+
* @param {string} params.tenant - The tenant identifier
|
|
1791
|
+
* @param {'platformConfigs'|'tpl'} params.modelName - The model/collection name
|
|
1792
|
+
* @param {string} params.type - When modelName='platformConfigs': one of platformConfigTypes array values; when modelName='tpl': content type string
|
|
1793
|
+
* @param {Object} [params.newVal] - New value to set after deletion
|
|
1794
|
+
* @param {string} [params.env] - Environment (defaults to current env)
|
|
1795
|
+
* @returns {Promise<void>}
|
|
1796
|
+
* @see {platformConfigTypes} for valid platformConfigs type values
|
|
1797
|
+
* @note According to the current usecases this function is called only after a database operation.
|
|
1798
|
+
* We should include this function call within the MongoDB transaction.
|
|
1799
|
+
* Otherwise, if the database operation succeeds but cache deletion fails, the cache will hold the outdated data.
|
|
1800
|
+
* We can ignore setAsync failure as we are checking db also if cache is not present (inside findInCache function)
|
|
1801
|
+
*/
|
|
1802
|
+
static async invalidateOneConfigInCache({
|
|
1803
|
+
tenant,
|
|
1804
|
+
modelName,
|
|
1805
|
+
type,
|
|
1806
|
+
newVal,
|
|
1807
|
+
env
|
|
1808
|
+
}) {
|
|
1809
|
+
const client = this.getClient();
|
|
1810
|
+
const cacheKey = this._getRedisKeyForConfig({
|
|
1811
|
+
tenant,
|
|
1812
|
+
modelName,
|
|
1813
|
+
type,
|
|
1814
|
+
env
|
|
1815
|
+
});
|
|
1816
|
+
await client.del(cacheKey);
|
|
1817
|
+
if (newVal) {
|
|
1818
|
+
try {
|
|
1819
|
+
await client.set(cacheKey, JSON.stringify(newVal));
|
|
1820
|
+
} catch (err) {
|
|
1821
|
+
console.log("invalidateCache - Cache set failed: ", err);
|
|
1822
|
+
}
|
|
1823
|
+
}
|
|
1824
|
+
}
|
|
1825
|
+
/**
|
|
1826
|
+
* Load all collections and templates into Redis cache for all tenants
|
|
1827
|
+
* Clears existing cache and rebuilds it from database
|
|
1828
|
+
* @returns {Promise<void>}
|
|
1829
|
+
* @param {string} [params.tenant] - Tenant (either a single tenant or an array of tenants for whom the config cache should be refreshed)
|
|
1830
|
+
* @static
|
|
1831
|
+
*/
|
|
1832
|
+
static async loadTplsAndPlatformConfigsToCache({ tenant }) {
|
|
1833
|
+
try {
|
|
1834
|
+
const client = this.getClient();
|
|
1835
|
+
const environment = this.getEnv();
|
|
1836
|
+
if (!tenant) throw new Error("No tenant/s defined to recache");
|
|
1837
|
+
console.log(`Loading configs and templates into cache...`);
|
|
1838
|
+
const tenantCollectionPromises = toArray(tenant).map(async (tenant2) => {
|
|
1839
|
+
return Promise.all(
|
|
1840
|
+
BASE_SETTINGS_FOR_CONFIGS_CACHE.map(
|
|
1841
|
+
async ({ modelName, schema, defaultQuery, typeKey }) => {
|
|
1842
|
+
const model = (0, import_getModelByTenant.getModelByTenant)({
|
|
1843
|
+
tenant: tenant2,
|
|
1844
|
+
env: environment,
|
|
1845
|
+
modelName,
|
|
1846
|
+
schema
|
|
1847
|
+
});
|
|
1848
|
+
const keys = await client.keys(
|
|
1849
|
+
`${environment}:${tenant2}:${modelName}:*`
|
|
1850
|
+
);
|
|
1851
|
+
if (keys.length) await client.del(keys);
|
|
1852
|
+
const documents = await model.find(...defaultQuery);
|
|
1853
|
+
await Promise.all(
|
|
1854
|
+
documents.map(async (doc) => {
|
|
1855
|
+
const type = doc[typeKey];
|
|
1856
|
+
await this.setOneConfigInCache({
|
|
1857
|
+
tenant: tenant2,
|
|
1858
|
+
modelName,
|
|
1859
|
+
type,
|
|
1860
|
+
val: doc
|
|
1861
|
+
});
|
|
1862
|
+
})
|
|
1863
|
+
);
|
|
1864
|
+
console.log(
|
|
1865
|
+
`Successfully cached ${documents.length} documents from ${modelName} for tenant: ${tenant2} | env: ${environment}`
|
|
1866
|
+
);
|
|
1867
|
+
}
|
|
1868
|
+
)
|
|
1869
|
+
);
|
|
1870
|
+
});
|
|
1871
|
+
await Promise.all(tenantCollectionPromises);
|
|
1872
|
+
console.log(`Finished caching for all tenants`);
|
|
1873
|
+
} catch (error) {
|
|
1874
|
+
console.error("Error in loadTplsAndPlatformConfigsToCache: ", error);
|
|
1875
|
+
throw error;
|
|
1876
|
+
}
|
|
1877
|
+
}
|
|
1878
|
+
/**
|
|
1879
|
+
* Close the Redis client
|
|
1880
|
+
*/
|
|
1881
|
+
async close() {
|
|
1882
|
+
if (this.client) {
|
|
1883
|
+
await this.client.quit();
|
|
1884
|
+
console.log("\u2705 Redis client closed");
|
|
1885
|
+
this.client = null;
|
|
1886
|
+
}
|
|
1887
|
+
}
|
|
1888
|
+
};
|
|
1889
|
+
RedisCacheConnector.instance = null;
|
|
1890
|
+
|
|
1891
|
+
// src/node.ts
|
|
1569
1892
|
init_getDbByTenant();
|
|
1570
1893
|
|
|
1571
1894
|
// src/db/getGlobalConfig.ts
|
|
@@ -1590,119 +1913,7 @@ var updateGlobalConfig = (updates) => {
|
|
|
1590
1913
|
};
|
|
1591
1914
|
|
|
1592
1915
|
// src/node.ts
|
|
1593
|
-
var
|
|
1594
|
-
|
|
1595
|
-
// src/redis/functions.ts
|
|
1596
|
-
init_getDbByTenant();
|
|
1597
|
-
|
|
1598
|
-
// src/redis/index.ts
|
|
1599
|
-
import IORedis from "ioredis";
|
|
1600
|
-
var redisClient = null;
|
|
1601
|
-
var REDIS_CONFIG = {
|
|
1602
|
-
port: Number(process.env.REDIS_PORT),
|
|
1603
|
-
host: process.env.REDIS_HOST || "",
|
|
1604
|
-
username: process.env.REDIS_USERNAME || "",
|
|
1605
|
-
password: process.env.REDIS_PASSWORD || ""
|
|
1606
|
-
};
|
|
1607
|
-
var connectToRedis = async () => {
|
|
1608
|
-
try {
|
|
1609
|
-
redisClient = new IORedis(REDIS_CONFIG);
|
|
1610
|
-
await redisClient.ping();
|
|
1611
|
-
console.log("Redis connected successfully");
|
|
1612
|
-
} catch (error) {
|
|
1613
|
-
console.error(
|
|
1614
|
-
"Redis connection failed:",
|
|
1615
|
-
error instanceof Error && error.message
|
|
1616
|
-
);
|
|
1617
|
-
if (redisClient) {
|
|
1618
|
-
redisClient.disconnect();
|
|
1619
|
-
}
|
|
1620
|
-
throw error;
|
|
1621
|
-
}
|
|
1622
|
-
};
|
|
1623
|
-
var getRedisClient = () => {
|
|
1624
|
-
if (!redisClient) {
|
|
1625
|
-
throw new Error("Redis client not initialized. Call connectToRedis first.");
|
|
1626
|
-
}
|
|
1627
|
-
return redisClient;
|
|
1628
|
-
};
|
|
1629
|
-
|
|
1630
|
-
// src/redis/functions.ts
|
|
1631
|
-
var findInCache = async ({
|
|
1632
|
-
tenant,
|
|
1633
|
-
modelName,
|
|
1634
|
-
type,
|
|
1635
|
-
query = {}
|
|
1636
|
-
}) => {
|
|
1637
|
-
const key = `${process.env.ENV}:${tenant}:${modelName}:${type}`;
|
|
1638
|
-
try {
|
|
1639
|
-
const redisClient2 = getRedisClient();
|
|
1640
|
-
const value = await redisClient2.get(key);
|
|
1641
|
-
if (value) {
|
|
1642
|
-
return JSON.parse(value);
|
|
1643
|
-
}
|
|
1644
|
-
} catch (error) {
|
|
1645
|
-
console.warn(
|
|
1646
|
-
`redis read failed for key ${key}`,
|
|
1647
|
-
error instanceof Error && error.message
|
|
1648
|
-
);
|
|
1649
|
-
}
|
|
1650
|
-
try {
|
|
1651
|
-
const collection = getDbByTenant({
|
|
1652
|
-
tenant,
|
|
1653
|
-
env: process.env.ENV
|
|
1654
|
-
}).collection(modelName);
|
|
1655
|
-
const value = await collection.findOne(query);
|
|
1656
|
-
if (!value) {
|
|
1657
|
-
throw new Error(`${type} value not found in ${modelName} for ${tenant}`);
|
|
1658
|
-
}
|
|
1659
|
-
try {
|
|
1660
|
-
const redisClient2 = getRedisClient();
|
|
1661
|
-
await redisClient2.set(key, JSON.stringify(value));
|
|
1662
|
-
} catch (error) {
|
|
1663
|
-
console.warn(
|
|
1664
|
-
`redis write failed for key ${key}:`,
|
|
1665
|
-
error instanceof Error && error.message
|
|
1666
|
-
);
|
|
1667
|
-
}
|
|
1668
|
-
return value;
|
|
1669
|
-
} catch (error) {
|
|
1670
|
-
throw new Error(
|
|
1671
|
-
`db read failed: ${error instanceof Error && error.message}`
|
|
1672
|
-
);
|
|
1673
|
-
}
|
|
1674
|
-
};
|
|
1675
|
-
var getTpl = async ({ name, tenant }) => {
|
|
1676
|
-
if (!name || !tenant) {
|
|
1677
|
-
throw new Error("Missing required parameters: name or tenant");
|
|
1678
|
-
}
|
|
1679
|
-
return await findInCache({
|
|
1680
|
-
modelName: "tpl",
|
|
1681
|
-
type: name,
|
|
1682
|
-
tenant,
|
|
1683
|
-
query: {
|
|
1684
|
-
kp_content_type: name,
|
|
1685
|
-
status: { $in: ["published", "editPublished"] }
|
|
1686
|
-
}
|
|
1687
|
-
});
|
|
1688
|
-
};
|
|
1689
|
-
var getAIConfigs = async ({
|
|
1690
|
-
tenant
|
|
1691
|
-
}) => {
|
|
1692
|
-
if (!tenant) {
|
|
1693
|
-
throw new Error("Missing required parameter: tenant");
|
|
1694
|
-
}
|
|
1695
|
-
return await findInCache({
|
|
1696
|
-
modelName: "platformConfigs",
|
|
1697
|
-
type: "ai",
|
|
1698
|
-
tenant,
|
|
1699
|
-
query: {
|
|
1700
|
-
type: "ai"
|
|
1701
|
-
}
|
|
1702
|
-
});
|
|
1703
|
-
};
|
|
1704
|
-
|
|
1705
|
-
// src/node.ts
|
|
1916
|
+
var import_getModelByTenant2 = __toESM(require_getModelByTenant());
|
|
1706
1917
|
init_models();
|
|
1707
1918
|
var import_WorkerManager = __toESM(require_WorkerManager());
|
|
1708
1919
|
var import_ProducerManager = __toESM(require_ProducerManager());
|
|
@@ -1716,11 +1927,11 @@ var export_GET_GLOBAL_BULLMQ_CONFIG = import_GET_GLOBAL_BULLMQ_CONFIG.GET_GLOBAL
|
|
|
1716
1927
|
var export_MongoConnector = import_MongoConnector2.MongoConnector;
|
|
1717
1928
|
var export_ProducerManager = import_ProducerManager.ProducerManager;
|
|
1718
1929
|
var export_WorkerManager = import_WorkerManager.WorkerManager;
|
|
1719
|
-
var export_getAIChatModelByTenant =
|
|
1720
|
-
var export_getAnnotationsModelByTenant =
|
|
1721
|
-
var export_getModelByTenant =
|
|
1722
|
-
var export_getPlatformConfigsModelByTenant =
|
|
1723
|
-
var export_getTplModelByTenant =
|
|
1930
|
+
var export_getAIChatModelByTenant = import_getModelByTenant2.getAIChatModelByTenant;
|
|
1931
|
+
var export_getAnnotationsModelByTenant = import_getModelByTenant2.getAnnotationsModelByTenant;
|
|
1932
|
+
var export_getModelByTenant = import_getModelByTenant2.getModelByTenant;
|
|
1933
|
+
var export_getPlatformConfigsModelByTenant = import_getModelByTenant2.getPlatformConfigsModelByTenant;
|
|
1934
|
+
var export_getTplModelByTenant = import_getModelByTenant2.getTplModelByTenant;
|
|
1724
1935
|
export {
|
|
1725
1936
|
AIChat_default as AIChatSchema,
|
|
1726
1937
|
Annotations_default as AnnotationSchema,
|
|
@@ -1731,20 +1942,17 @@ export {
|
|
|
1731
1942
|
export_MongoConnector as MongoConnector,
|
|
1732
1943
|
PlatformConfigs_default as PlatformConfigsSchema,
|
|
1733
1944
|
export_ProducerManager as ProducerManager,
|
|
1945
|
+
RedisCacheConnector,
|
|
1734
1946
|
Tpl_default as TplSchema,
|
|
1735
1947
|
export_WorkerManager as WorkerManager,
|
|
1736
|
-
connectToRedis,
|
|
1737
1948
|
deleteVal,
|
|
1738
1949
|
extractAllBlocksFromTpl,
|
|
1739
1950
|
genTagId,
|
|
1740
1951
|
export_getAIChatModelByTenant as getAIChatModelByTenant,
|
|
1741
|
-
getAIConfigs,
|
|
1742
1952
|
export_getAnnotationsModelByTenant as getAnnotationsModelByTenant,
|
|
1743
1953
|
getDbByTenant,
|
|
1744
1954
|
export_getModelByTenant as getModelByTenant,
|
|
1745
1955
|
export_getPlatformConfigsModelByTenant as getPlatformConfigsModelByTenant,
|
|
1746
|
-
getRedisClient,
|
|
1747
|
-
getTpl,
|
|
1748
1956
|
export_getTplModelByTenant as getTplModelByTenant,
|
|
1749
1957
|
getVal,
|
|
1750
1958
|
initializeGlobalConfig,
|