@okf/ootils 1.5.6 → 1.6.0
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 +381 -177
- package/dist/node.mjs +376 -169
- package/package.json +1 -1
package/dist/node.js
CHANGED
|
@@ -165,16 +165,11 @@ var init_Annotations = __esm({
|
|
|
165
165
|
}
|
|
166
166
|
});
|
|
167
167
|
|
|
168
|
-
// src/models/PlatformConfigs.ts
|
|
169
|
-
var
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
});
|
|
173
|
-
var import_mongoose2, platformConfigTypes, PlatformConfigsSchema, PlatformConfigs_default;
|
|
174
|
-
var init_PlatformConfigs = __esm({
|
|
175
|
-
"src/models/PlatformConfigs.ts"() {
|
|
168
|
+
// src/models/PlatformConfigs/platformConfigTypes.ts
|
|
169
|
+
var platformConfigTypes;
|
|
170
|
+
var init_platformConfigTypes = __esm({
|
|
171
|
+
"src/models/PlatformConfigs/platformConfigTypes.ts"() {
|
|
176
172
|
"use strict";
|
|
177
|
-
import_mongoose2 = __toESM(require("mongoose"));
|
|
178
173
|
platformConfigTypes = [
|
|
179
174
|
"roles",
|
|
180
175
|
"nav",
|
|
@@ -184,6 +179,20 @@ var init_PlatformConfigs = __esm({
|
|
|
184
179
|
"theme",
|
|
185
180
|
"ai"
|
|
186
181
|
];
|
|
182
|
+
}
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
// src/models/PlatformConfigs/index.ts
|
|
186
|
+
var PlatformConfigs_exports = {};
|
|
187
|
+
__export(PlatformConfigs_exports, {
|
|
188
|
+
default: () => PlatformConfigs_default
|
|
189
|
+
});
|
|
190
|
+
var import_mongoose2, PlatformConfigsSchema, PlatformConfigs_default;
|
|
191
|
+
var init_PlatformConfigs = __esm({
|
|
192
|
+
"src/models/PlatformConfigs/index.ts"() {
|
|
193
|
+
"use strict";
|
|
194
|
+
import_mongoose2 = __toESM(require("mongoose"));
|
|
195
|
+
init_platformConfigTypes();
|
|
187
196
|
PlatformConfigsSchema = new import_mongoose2.default.Schema(
|
|
188
197
|
{
|
|
189
198
|
type: {
|
|
@@ -583,46 +592,16 @@ var require_ElasticSearchConnector = __commonJS({
|
|
|
583
592
|
}
|
|
584
593
|
});
|
|
585
594
|
|
|
586
|
-
// src/db/getDbByTenant.js
|
|
587
|
-
var getDbByTenant_exports = {};
|
|
588
|
-
__export(getDbByTenant_exports, {
|
|
589
|
-
getDbByTenant: () => getDbByTenant
|
|
590
|
-
});
|
|
591
|
-
var import_mongoose4, import_MongoConnector, getDbByTenant;
|
|
592
|
-
var init_getDbByTenant = __esm({
|
|
593
|
-
"src/db/getDbByTenant.js"() {
|
|
594
|
-
"use strict";
|
|
595
|
-
import_mongoose4 = require("mongoose");
|
|
596
|
-
import_MongoConnector = __toESM(require_MongoConnector());
|
|
597
|
-
getDbByTenant = ({
|
|
598
|
-
tenant,
|
|
599
|
-
env: _env
|
|
600
|
-
}) => {
|
|
601
|
-
if (!tenant) throw new Error("tenant id has not been provided");
|
|
602
|
-
const env = _env || import_MongoConnector.MongoConnector.getEnv();
|
|
603
|
-
const dbConfigs = import_MongoConnector.MongoConnector.getDbConfigs();
|
|
604
|
-
const clusterConnections = import_MongoConnector.MongoConnector.getClusterConnections();
|
|
605
|
-
const { CLUSTER_NAME } = dbConfigs[env];
|
|
606
|
-
const dbName = `${tenant}_${env}`;
|
|
607
|
-
const connection = clusterConnections[CLUSTER_NAME];
|
|
608
|
-
if (!connection) {
|
|
609
|
-
throw new Error(`Connection not found for cluster: ${CLUSTER_NAME}`);
|
|
610
|
-
}
|
|
611
|
-
return connection.useDb(dbName, { useCache: true });
|
|
612
|
-
};
|
|
613
|
-
}
|
|
614
|
-
});
|
|
615
|
-
|
|
616
595
|
// src/models/AIChat.ts
|
|
617
|
-
var
|
|
596
|
+
var import_mongoose4, AIChatSchema, AIChat_default;
|
|
618
597
|
var init_AIChat = __esm({
|
|
619
598
|
"src/models/AIChat.ts"() {
|
|
620
599
|
"use strict";
|
|
621
|
-
|
|
600
|
+
import_mongoose4 = __toESM(require("mongoose"));
|
|
622
601
|
init_Annotations();
|
|
623
|
-
AIChatSchema = new
|
|
602
|
+
AIChatSchema = new import_mongoose4.default.Schema(
|
|
624
603
|
{
|
|
625
|
-
userId: { type:
|
|
604
|
+
userId: { type: import_mongoose4.default.Schema.Types.ObjectId, index: { unique: true } },
|
|
626
605
|
createdAt: Date,
|
|
627
606
|
lastAcitivity: Date,
|
|
628
607
|
messages: [
|
|
@@ -675,13 +654,43 @@ var init_models = __esm({
|
|
|
675
654
|
}
|
|
676
655
|
});
|
|
677
656
|
|
|
657
|
+
// src/db/getDbByTenant.js
|
|
658
|
+
var getDbByTenant_exports = {};
|
|
659
|
+
__export(getDbByTenant_exports, {
|
|
660
|
+
getDbByTenant: () => getDbByTenant
|
|
661
|
+
});
|
|
662
|
+
var import_mongoose5, import_MongoConnector, getDbByTenant;
|
|
663
|
+
var init_getDbByTenant = __esm({
|
|
664
|
+
"src/db/getDbByTenant.js"() {
|
|
665
|
+
"use strict";
|
|
666
|
+
import_mongoose5 = require("mongoose");
|
|
667
|
+
import_MongoConnector = __toESM(require_MongoConnector());
|
|
668
|
+
getDbByTenant = ({
|
|
669
|
+
tenant,
|
|
670
|
+
env: _env
|
|
671
|
+
}) => {
|
|
672
|
+
if (!tenant) throw new Error("tenant id has not been provided");
|
|
673
|
+
const env = _env || import_MongoConnector.MongoConnector.getEnv();
|
|
674
|
+
const dbConfigs = import_MongoConnector.MongoConnector.getDbConfigs();
|
|
675
|
+
const clusterConnections = import_MongoConnector.MongoConnector.getClusterConnections();
|
|
676
|
+
const { CLUSTER_NAME } = dbConfigs[env];
|
|
677
|
+
const dbName = `${tenant}_${env}`;
|
|
678
|
+
const connection = clusterConnections[CLUSTER_NAME];
|
|
679
|
+
if (!connection) {
|
|
680
|
+
throw new Error(`Connection not found for cluster: ${CLUSTER_NAME}`);
|
|
681
|
+
}
|
|
682
|
+
return connection.useDb(dbName, { useCache: true });
|
|
683
|
+
};
|
|
684
|
+
}
|
|
685
|
+
});
|
|
686
|
+
|
|
678
687
|
// src/db/getModelByTenant.js
|
|
679
688
|
var require_getModelByTenant = __commonJS({
|
|
680
689
|
"src/db/getModelByTenant.js"(exports2, module2) {
|
|
681
690
|
"use strict";
|
|
682
691
|
var { getDbByTenant: getDbByTenant2 } = (init_getDbByTenant(), __toCommonJS(getDbByTenant_exports));
|
|
683
692
|
var { AIChatSchema: AIChatSchema2, AnnotationSchema: AnnotationSchema2, PlatformConfigsSchema: PlatformConfigsSchema2, TplSchema: TplSchema2 } = (init_models(), __toCommonJS(models_exports));
|
|
684
|
-
var
|
|
693
|
+
var getModelByTenant3 = ({ tenant, modelName, schema, env }) => {
|
|
685
694
|
if (!tenant) {
|
|
686
695
|
throw new Error("tenant id has not been provided");
|
|
687
696
|
}
|
|
@@ -691,32 +700,32 @@ var require_getModelByTenant = __commonJS({
|
|
|
691
700
|
}
|
|
692
701
|
return db.model(modelName);
|
|
693
702
|
};
|
|
694
|
-
var getAnnotationsModelByTenant2 = ({ tenant, env, mongodb, dbConfigs, modelName }) =>
|
|
703
|
+
var getAnnotationsModelByTenant2 = ({ tenant, env, mongodb, dbConfigs, modelName }) => getModelByTenant3({
|
|
695
704
|
tenant,
|
|
696
705
|
modelName: modelName || "annotations",
|
|
697
706
|
schema: AnnotationSchema2,
|
|
698
707
|
env
|
|
699
708
|
});
|
|
700
|
-
var getPlatformConfigsModelByTenant2 = ({ tenant, env, mongodb, dbConfigs }) =>
|
|
709
|
+
var getPlatformConfigsModelByTenant2 = ({ tenant, env, mongodb, dbConfigs }) => getModelByTenant3({
|
|
701
710
|
tenant,
|
|
702
711
|
modelName: "platformConfigs",
|
|
703
712
|
schema: PlatformConfigsSchema2,
|
|
704
713
|
env
|
|
705
714
|
});
|
|
706
|
-
var getTplModelByTenant2 = ({ tenant, env, mongodb, dbConfigs }) =>
|
|
715
|
+
var getTplModelByTenant2 = ({ tenant, env, mongodb, dbConfigs }) => getModelByTenant3({
|
|
707
716
|
tenant,
|
|
708
717
|
modelName: "tpl",
|
|
709
718
|
schema: TplSchema2,
|
|
710
719
|
env
|
|
711
720
|
});
|
|
712
|
-
var getAIChatModelByTenant2 = ({ tenant, env, mongodb, dbConfigs }) =>
|
|
721
|
+
var getAIChatModelByTenant2 = ({ tenant, env, mongodb, dbConfigs }) => getModelByTenant3({
|
|
713
722
|
tenant,
|
|
714
723
|
modelName: "aiChat",
|
|
715
724
|
schema: AIChatSchema2,
|
|
716
725
|
env
|
|
717
726
|
});
|
|
718
727
|
module2.exports = {
|
|
719
|
-
getModelByTenant:
|
|
728
|
+
getModelByTenant: getModelByTenant3,
|
|
720
729
|
getAIChatModelByTenant: getAIChatModelByTenant2,
|
|
721
730
|
getAnnotationsModelByTenant: getAnnotationsModelByTenant2,
|
|
722
731
|
getPlatformConfigsModelByTenant: getPlatformConfigsModelByTenant2,
|
|
@@ -1253,10 +1262,10 @@ var init_GLOBAL_BULLMQ_CONFIG = __esm({
|
|
|
1253
1262
|
var require_GET_GLOBAL_BULLMQ_CONFIG = __commonJS({
|
|
1254
1263
|
"src/bullmq/GET_GLOBAL_BULLMQ_CONFIG.js"(exports2, module2) {
|
|
1255
1264
|
"use strict";
|
|
1256
|
-
var { default:
|
|
1265
|
+
var { default: Redis2 } = require("ioredis");
|
|
1257
1266
|
var { BASE_BULLMQ_CONFIG: BASE_BULLMQ_CONFIG2 } = (init_GLOBAL_BULLMQ_CONFIG(), __toCommonJS(GLOBAL_BULLMQ_CONFIG_exports));
|
|
1258
1267
|
function GET_GLOBAL_BULLMQ_CONFIG2({ env, redisCredentials }) {
|
|
1259
|
-
const redisConnectionForBullMQ = new
|
|
1268
|
+
const redisConnectionForBullMQ = new Redis2({
|
|
1260
1269
|
host: redisCredentials.REDIS_HOST,
|
|
1261
1270
|
port: redisCredentials.REDIS_PORT,
|
|
1262
1271
|
password: redisCredentials.REDIS_PASSWORD,
|
|
@@ -1295,21 +1304,18 @@ __export(node_exports, {
|
|
|
1295
1304
|
MongoConnector: () => import_MongoConnector2.MongoConnector,
|
|
1296
1305
|
PlatformConfigsSchema: () => PlatformConfigs_default,
|
|
1297
1306
|
ProducerManager: () => import_ProducerManager.ProducerManager,
|
|
1307
|
+
RedisCacheConnector: () => RedisCacheConnector,
|
|
1298
1308
|
TplSchema: () => Tpl_default,
|
|
1299
1309
|
WorkerManager: () => import_WorkerManager.WorkerManager,
|
|
1300
|
-
connectToRedis: () => connectToRedis,
|
|
1301
1310
|
deleteVal: () => deleteVal,
|
|
1302
1311
|
extractAllBlocksFromTpl: () => extractAllBlocksFromTpl,
|
|
1303
1312
|
genTagId: () => genTagId,
|
|
1304
|
-
getAIChatModelByTenant: () =>
|
|
1305
|
-
|
|
1306
|
-
getAnnotationsModelByTenant: () => import_getModelByTenant.getAnnotationsModelByTenant,
|
|
1313
|
+
getAIChatModelByTenant: () => import_getModelByTenant2.getAIChatModelByTenant,
|
|
1314
|
+
getAnnotationsModelByTenant: () => import_getModelByTenant2.getAnnotationsModelByTenant,
|
|
1307
1315
|
getDbByTenant: () => getDbByTenant,
|
|
1308
|
-
getModelByTenant: () =>
|
|
1309
|
-
getPlatformConfigsModelByTenant: () =>
|
|
1310
|
-
|
|
1311
|
-
getTpl: () => getTpl,
|
|
1312
|
-
getTplModelByTenant: () => import_getModelByTenant.getTplModelByTenant,
|
|
1316
|
+
getModelByTenant: () => import_getModelByTenant2.getModelByTenant,
|
|
1317
|
+
getPlatformConfigsModelByTenant: () => import_getModelByTenant2.getPlatformConfigsModelByTenant,
|
|
1318
|
+
getTplModelByTenant: () => import_getModelByTenant2.getTplModelByTenant,
|
|
1313
1319
|
getVal: () => getVal,
|
|
1314
1320
|
initializeGlobalConfig: () => initializeGlobalConfig,
|
|
1315
1321
|
recursivelyExtractBlocks: () => _recursExtractBlocks,
|
|
@@ -1597,6 +1603,319 @@ var _extractBlocksFromSomeBuilders = ({
|
|
|
1597
1603
|
// src/node.ts
|
|
1598
1604
|
var import_MongoConnector2 = __toESM(require_MongoConnector());
|
|
1599
1605
|
var import_ElasticSearchConnector = __toESM(require_ElasticSearchConnector());
|
|
1606
|
+
|
|
1607
|
+
// src/RedisCacheConnector.js
|
|
1608
|
+
var import_ioredis = __toESM(require("ioredis"));
|
|
1609
|
+
init_models();
|
|
1610
|
+
var import_getModelByTenant = __toESM(require_getModelByTenant());
|
|
1611
|
+
init_platformConfigTypes();
|
|
1612
|
+
var BASE_SETTINGS_FOR_CONFIGS_CACHE = [
|
|
1613
|
+
{
|
|
1614
|
+
modelName: "tpl",
|
|
1615
|
+
collectionName: "tpls",
|
|
1616
|
+
schema: Tpl_default,
|
|
1617
|
+
defaultQuery: [
|
|
1618
|
+
{
|
|
1619
|
+
status: {
|
|
1620
|
+
$ne: "unpublished"
|
|
1621
|
+
}
|
|
1622
|
+
},
|
|
1623
|
+
{
|
|
1624
|
+
drafts: 0
|
|
1625
|
+
}
|
|
1626
|
+
],
|
|
1627
|
+
typeKey: "kp_content_type"
|
|
1628
|
+
},
|
|
1629
|
+
{
|
|
1630
|
+
modelName: "platformConfigs",
|
|
1631
|
+
collectionName: "platformConfigs",
|
|
1632
|
+
schema: PlatformConfigs_default,
|
|
1633
|
+
defaultQuery: [],
|
|
1634
|
+
typeKey: "type"
|
|
1635
|
+
}
|
|
1636
|
+
];
|
|
1637
|
+
var RedisCacheConnector = class _RedisCacheConnector {
|
|
1638
|
+
constructor(options = {}) {
|
|
1639
|
+
/**
|
|
1640
|
+
* Initialize Redis client
|
|
1641
|
+
*/
|
|
1642
|
+
__publicField(this, "connect", async () => {
|
|
1643
|
+
try {
|
|
1644
|
+
this.client = new import_ioredis.default({
|
|
1645
|
+
host: this.host,
|
|
1646
|
+
port: this.port,
|
|
1647
|
+
password: this.password
|
|
1648
|
+
});
|
|
1649
|
+
this.client.on("connect", () => {
|
|
1650
|
+
console.log("\u2705 Connected to our redis cache instance!");
|
|
1651
|
+
});
|
|
1652
|
+
await new Promise((resolve, reject) => {
|
|
1653
|
+
this.client.on("ready", resolve);
|
|
1654
|
+
this.client.on("error", reject);
|
|
1655
|
+
});
|
|
1656
|
+
return this.client;
|
|
1657
|
+
} catch (err) {
|
|
1658
|
+
console.error(err.message);
|
|
1659
|
+
process.exit(1);
|
|
1660
|
+
}
|
|
1661
|
+
});
|
|
1662
|
+
if (_RedisCacheConnector.instance) {
|
|
1663
|
+
throw new Error(
|
|
1664
|
+
"RedisCacheConnector instance already exists. Use RedisCacheConnector.getInstance() instead."
|
|
1665
|
+
);
|
|
1666
|
+
}
|
|
1667
|
+
this.client = null;
|
|
1668
|
+
this.host = options.host;
|
|
1669
|
+
this.env = options.env;
|
|
1670
|
+
this.port = options.port;
|
|
1671
|
+
this.password = options.password;
|
|
1672
|
+
_RedisCacheConnector.instance = this;
|
|
1673
|
+
if (!this.host) {
|
|
1674
|
+
throw new Error("Host must be provided in constructor options");
|
|
1675
|
+
}
|
|
1676
|
+
if (!this.port) {
|
|
1677
|
+
throw new Error("Port must be provided in constructor options");
|
|
1678
|
+
}
|
|
1679
|
+
if (!this.password) {
|
|
1680
|
+
throw new Error("Password must be provided in constructor options");
|
|
1681
|
+
}
|
|
1682
|
+
if (!this.env) {
|
|
1683
|
+
throw new Error("Env must be provided in constructor options");
|
|
1684
|
+
}
|
|
1685
|
+
}
|
|
1686
|
+
static getEnv() {
|
|
1687
|
+
if (!_RedisCacheConnector.instance) {
|
|
1688
|
+
throw new Error("RedisCacheConnector not initialized");
|
|
1689
|
+
}
|
|
1690
|
+
return _RedisCacheConnector.instance.env;
|
|
1691
|
+
}
|
|
1692
|
+
// Static method to get the instance
|
|
1693
|
+
static getInstance() {
|
|
1694
|
+
if (!_RedisCacheConnector.instance) {
|
|
1695
|
+
throw new Error("RedisCacheConnector not initialized");
|
|
1696
|
+
}
|
|
1697
|
+
return _RedisCacheConnector.instance;
|
|
1698
|
+
}
|
|
1699
|
+
// Static method to get the client
|
|
1700
|
+
static getClient() {
|
|
1701
|
+
if (!_RedisCacheConnector.instance || !_RedisCacheConnector.instance.client) {
|
|
1702
|
+
throw new Error(
|
|
1703
|
+
"RedisCacheConnector not initialized or client not connected"
|
|
1704
|
+
);
|
|
1705
|
+
}
|
|
1706
|
+
return _RedisCacheConnector.instance.client;
|
|
1707
|
+
}
|
|
1708
|
+
/**
|
|
1709
|
+
* Find documents in Redis cache
|
|
1710
|
+
* @param {Object} params - Search parameters
|
|
1711
|
+
* @param {string} params.tenant - The tenant identifier
|
|
1712
|
+
* @param {'platformConfigs'|'tpl'} params.modelName - The collection to search in
|
|
1713
|
+
* @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.
|
|
1714
|
+
* @returns {Promise<Array>} Array of matching documents
|
|
1715
|
+
* @see {platformConfigTypes} for valid platformConfigs type values
|
|
1716
|
+
*/
|
|
1717
|
+
static async findConfigsInCache({ tenant, modelName, type }) {
|
|
1718
|
+
try {
|
|
1719
|
+
const client = this.getClient();
|
|
1720
|
+
const environment = this.getEnv();
|
|
1721
|
+
const keyPrefix = `${environment}:${tenant}:${modelName}:`;
|
|
1722
|
+
const keys = await client.keys(`${keyPrefix}${type || "*"}`);
|
|
1723
|
+
if (keys.length === 0) {
|
|
1724
|
+
const { schema, typeKey } = BASE_SETTINGS_FOR_CONFIGS_CACHE.find(
|
|
1725
|
+
(obj) => obj.modelName === modelName
|
|
1726
|
+
);
|
|
1727
|
+
const model = (0, import_getModelByTenant.getModelByTenant)({
|
|
1728
|
+
tenant,
|
|
1729
|
+
env: environment,
|
|
1730
|
+
modelName,
|
|
1731
|
+
schema
|
|
1732
|
+
});
|
|
1733
|
+
let findQuery = {};
|
|
1734
|
+
if (type) {
|
|
1735
|
+
findQuery = { [typeKey]: type };
|
|
1736
|
+
}
|
|
1737
|
+
const data = await model.find(findQuery);
|
|
1738
|
+
await Promise.all(
|
|
1739
|
+
data.map(
|
|
1740
|
+
(doc) => this.setOneConfigInCache({
|
|
1741
|
+
tenant,
|
|
1742
|
+
modelName,
|
|
1743
|
+
type: doc[typeKey],
|
|
1744
|
+
val: doc
|
|
1745
|
+
})
|
|
1746
|
+
)
|
|
1747
|
+
);
|
|
1748
|
+
return data;
|
|
1749
|
+
}
|
|
1750
|
+
const cachedDocs = await Promise.all(
|
|
1751
|
+
keys.map(async (key) => {
|
|
1752
|
+
const value = await client.get(key);
|
|
1753
|
+
return JSON.parse(value);
|
|
1754
|
+
})
|
|
1755
|
+
);
|
|
1756
|
+
return cachedDocs;
|
|
1757
|
+
} catch (error) {
|
|
1758
|
+
console.error("Error in findConfigsInCache:", error);
|
|
1759
|
+
throw error;
|
|
1760
|
+
}
|
|
1761
|
+
}
|
|
1762
|
+
/**
|
|
1763
|
+
* Find a single document in Redis cache (returns first match)
|
|
1764
|
+
* @param {Object} params - Search parameters
|
|
1765
|
+
* @param {string} params.tenant - The tenant identifier
|
|
1766
|
+
* @param {'platformConfigs'|'tpl'} params.modelName - The collection to search in
|
|
1767
|
+
* @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.
|
|
1768
|
+
* @returns {Promise<Object|undefined>} First matching document or undefined if none found
|
|
1769
|
+
* @see {platformConfigTypes} for valid platformConfigs type values
|
|
1770
|
+
*/
|
|
1771
|
+
static async findOneConfigInCache({ tenant, modelName, type }) {
|
|
1772
|
+
const cacheData = await this.findConfigsInCache({
|
|
1773
|
+
tenant,
|
|
1774
|
+
modelName,
|
|
1775
|
+
type
|
|
1776
|
+
});
|
|
1777
|
+
return cacheData?.[0];
|
|
1778
|
+
}
|
|
1779
|
+
/**
|
|
1780
|
+
* Generate Redis key for config caching
|
|
1781
|
+
* @private
|
|
1782
|
+
* @param {Object} params - Parameters for key generation
|
|
1783
|
+
* @param {string} params.tenant - The tenant identifier
|
|
1784
|
+
* @param {'platformConfigs'|'tpl'} params.modelName - The model/collection name
|
|
1785
|
+
* @param {string} params.type - The document type
|
|
1786
|
+
* @param {string} [params.env] - Environment (defaults to current env)
|
|
1787
|
+
* @returns {string} The Redis key
|
|
1788
|
+
*/
|
|
1789
|
+
static _getRedisKeyForConfig({ tenant, modelName, type, env }) {
|
|
1790
|
+
const environment = env || this.getEnv();
|
|
1791
|
+
return `${environment}:${tenant}:${modelName}:${type}`;
|
|
1792
|
+
}
|
|
1793
|
+
/**
|
|
1794
|
+
* Set a single config document in Redis cache
|
|
1795
|
+
* @param {Object} params - Cache parameters
|
|
1796
|
+
* @param {string} params.tenant - The tenant identifier
|
|
1797
|
+
* @param {'platformConfigs'|'tpl'} params.modelName - The model/collection name
|
|
1798
|
+
* @param {string} params.type - When modelName='platformConfigs': one of platformConfigTypes array values; when modelName='tpl': content type string
|
|
1799
|
+
* @param {Object} params.val - The value to cache
|
|
1800
|
+
* @param {string} [params.env] - Environment (defaults to current env)
|
|
1801
|
+
* @returns {Promise<void>}
|
|
1802
|
+
* @see {platformConfigTypes} for valid platformConfigs type values
|
|
1803
|
+
*/
|
|
1804
|
+
static async setOneConfigInCache({ tenant, modelName, type, val, env }) {
|
|
1805
|
+
const client = this.getClient();
|
|
1806
|
+
const cacheKey = this._getRedisKeyForConfig({
|
|
1807
|
+
tenant,
|
|
1808
|
+
modelName,
|
|
1809
|
+
type,
|
|
1810
|
+
env
|
|
1811
|
+
});
|
|
1812
|
+
await client.set(cacheKey, JSON.stringify(val));
|
|
1813
|
+
}
|
|
1814
|
+
/**
|
|
1815
|
+
* Invalidate (delete) a config from Redis cache and optionally set a new value
|
|
1816
|
+
* @param {Object} params - Cache parameters
|
|
1817
|
+
* @param {string} params.tenant - The tenant identifier
|
|
1818
|
+
* @param {'platformConfigs'|'tpl'} params.modelName - The model/collection name
|
|
1819
|
+
* @param {string} params.type - When modelName='platformConfigs': one of platformConfigTypes array values; when modelName='tpl': content type string
|
|
1820
|
+
* @param {Object} [params.newVal] - New value to set after deletion
|
|
1821
|
+
* @param {string} [params.env] - Environment (defaults to current env)
|
|
1822
|
+
* @returns {Promise<void>}
|
|
1823
|
+
* @see {platformConfigTypes} for valid platformConfigs type values
|
|
1824
|
+
* @note According to the current usecases this function is called only after a database operation.
|
|
1825
|
+
* We should include this function call within the MongoDB transaction.
|
|
1826
|
+
* Otherwise, if the database operation succeeds but cache deletion fails, the cache will hold the outdated data.
|
|
1827
|
+
* We can ignore setAsync failure as we are checking db also if cache is not present (inside findInCache function)
|
|
1828
|
+
*/
|
|
1829
|
+
static async invalidateOneConfigInCache({
|
|
1830
|
+
tenant,
|
|
1831
|
+
modelName,
|
|
1832
|
+
type,
|
|
1833
|
+
newVal,
|
|
1834
|
+
env
|
|
1835
|
+
}) {
|
|
1836
|
+
const client = this.getClient();
|
|
1837
|
+
const cacheKey = this._getRedisKeyForConfig({
|
|
1838
|
+
tenant,
|
|
1839
|
+
modelName,
|
|
1840
|
+
type,
|
|
1841
|
+
env
|
|
1842
|
+
});
|
|
1843
|
+
await client.del(cacheKey);
|
|
1844
|
+
if (newVal) {
|
|
1845
|
+
try {
|
|
1846
|
+
await client.set(cacheKey, JSON.stringify(newVal));
|
|
1847
|
+
} catch (err) {
|
|
1848
|
+
console.log("invalidateCache - Cache set failed: ", err);
|
|
1849
|
+
}
|
|
1850
|
+
}
|
|
1851
|
+
}
|
|
1852
|
+
/**
|
|
1853
|
+
* Load all collections and templates into Redis cache for all tenants
|
|
1854
|
+
* Clears existing cache and rebuilds it from database
|
|
1855
|
+
* @returns {Promise<void>}
|
|
1856
|
+
* @param {string} [params.tenant] - Tenant (either a single tenant or an array of tenants for whom the config cache should be refreshed)
|
|
1857
|
+
* @static
|
|
1858
|
+
*/
|
|
1859
|
+
static async loadTplsAndPlatformConfigsToCache({ tenant }) {
|
|
1860
|
+
try {
|
|
1861
|
+
const client = this.getClient();
|
|
1862
|
+
const environment = this.getEnv();
|
|
1863
|
+
if (!tenant) throw new Error("No tenant/s defined to recache");
|
|
1864
|
+
console.log(`Loading configs and templates into cache...`);
|
|
1865
|
+
const tenantCollectionPromises = toArray(tenant).map(async (tenant2) => {
|
|
1866
|
+
return Promise.all(
|
|
1867
|
+
BASE_SETTINGS_FOR_CONFIGS_CACHE.map(
|
|
1868
|
+
async ({ modelName, schema, defaultQuery, typeKey }) => {
|
|
1869
|
+
const model = (0, import_getModelByTenant.getModelByTenant)({
|
|
1870
|
+
tenant: tenant2,
|
|
1871
|
+
env: environment,
|
|
1872
|
+
modelName,
|
|
1873
|
+
schema
|
|
1874
|
+
});
|
|
1875
|
+
const keys = await client.keys(
|
|
1876
|
+
`${environment}:${tenant2}:${modelName}:*`
|
|
1877
|
+
);
|
|
1878
|
+
if (keys.length) await client.del(keys);
|
|
1879
|
+
const documents = await model.find(...defaultQuery);
|
|
1880
|
+
await Promise.all(
|
|
1881
|
+
documents.map(async (doc) => {
|
|
1882
|
+
const type = doc[typeKey];
|
|
1883
|
+
await this.setOneConfigInCache({
|
|
1884
|
+
tenant: tenant2,
|
|
1885
|
+
modelName,
|
|
1886
|
+
type,
|
|
1887
|
+
val: doc
|
|
1888
|
+
});
|
|
1889
|
+
})
|
|
1890
|
+
);
|
|
1891
|
+
console.log(
|
|
1892
|
+
`Successfully cached ${documents.length} documents from ${modelName} for tenant: ${tenant2} | env: ${environment}`
|
|
1893
|
+
);
|
|
1894
|
+
}
|
|
1895
|
+
)
|
|
1896
|
+
);
|
|
1897
|
+
});
|
|
1898
|
+
await Promise.all(tenantCollectionPromises);
|
|
1899
|
+
console.log(`Finished caching for all tenants`);
|
|
1900
|
+
} catch (error) {
|
|
1901
|
+
console.error("Error in loadTplsAndPlatformConfigsToCache: ", error);
|
|
1902
|
+
throw error;
|
|
1903
|
+
}
|
|
1904
|
+
}
|
|
1905
|
+
/**
|
|
1906
|
+
* Close the Redis client
|
|
1907
|
+
*/
|
|
1908
|
+
async close() {
|
|
1909
|
+
if (this.client) {
|
|
1910
|
+
await this.client.quit();
|
|
1911
|
+
console.log("\u2705 Redis client closed");
|
|
1912
|
+
this.client = null;
|
|
1913
|
+
}
|
|
1914
|
+
}
|
|
1915
|
+
};
|
|
1916
|
+
RedisCacheConnector.instance = null;
|
|
1917
|
+
|
|
1918
|
+
// src/node.ts
|
|
1600
1919
|
init_getDbByTenant();
|
|
1601
1920
|
|
|
1602
1921
|
// src/db/getGlobalConfig.ts
|
|
@@ -1621,119 +1940,7 @@ var updateGlobalConfig = (updates) => {
|
|
|
1621
1940
|
};
|
|
1622
1941
|
|
|
1623
1942
|
// src/node.ts
|
|
1624
|
-
var
|
|
1625
|
-
|
|
1626
|
-
// src/redis/functions.ts
|
|
1627
|
-
init_getDbByTenant();
|
|
1628
|
-
|
|
1629
|
-
// src/redis/index.ts
|
|
1630
|
-
var import_ioredis = __toESM(require("ioredis"));
|
|
1631
|
-
var redisClient = null;
|
|
1632
|
-
var REDIS_CONFIG = {
|
|
1633
|
-
port: Number(process.env.REDIS_PORT),
|
|
1634
|
-
host: process.env.REDIS_HOST || "",
|
|
1635
|
-
username: process.env.REDIS_USERNAME || "",
|
|
1636
|
-
password: process.env.REDIS_PASSWORD || ""
|
|
1637
|
-
};
|
|
1638
|
-
var connectToRedis = async () => {
|
|
1639
|
-
try {
|
|
1640
|
-
redisClient = new import_ioredis.default(REDIS_CONFIG);
|
|
1641
|
-
await redisClient.ping();
|
|
1642
|
-
console.log("Redis connected successfully");
|
|
1643
|
-
} catch (error) {
|
|
1644
|
-
console.error(
|
|
1645
|
-
"Redis connection failed:",
|
|
1646
|
-
error instanceof Error && error.message
|
|
1647
|
-
);
|
|
1648
|
-
if (redisClient) {
|
|
1649
|
-
redisClient.disconnect();
|
|
1650
|
-
}
|
|
1651
|
-
throw error;
|
|
1652
|
-
}
|
|
1653
|
-
};
|
|
1654
|
-
var getRedisClient = () => {
|
|
1655
|
-
if (!redisClient) {
|
|
1656
|
-
throw new Error("Redis client not initialized. Call connectToRedis first.");
|
|
1657
|
-
}
|
|
1658
|
-
return redisClient;
|
|
1659
|
-
};
|
|
1660
|
-
|
|
1661
|
-
// src/redis/functions.ts
|
|
1662
|
-
var findInCache = async ({
|
|
1663
|
-
tenant,
|
|
1664
|
-
modelName,
|
|
1665
|
-
type,
|
|
1666
|
-
query = {}
|
|
1667
|
-
}) => {
|
|
1668
|
-
const key = `${process.env.ENV}:${tenant}:${modelName}:${type}`;
|
|
1669
|
-
try {
|
|
1670
|
-
const redisClient2 = getRedisClient();
|
|
1671
|
-
const value = await redisClient2.get(key);
|
|
1672
|
-
if (value) {
|
|
1673
|
-
return JSON.parse(value);
|
|
1674
|
-
}
|
|
1675
|
-
} catch (error) {
|
|
1676
|
-
console.warn(
|
|
1677
|
-
`redis read failed for key ${key}`,
|
|
1678
|
-
error instanceof Error && error.message
|
|
1679
|
-
);
|
|
1680
|
-
}
|
|
1681
|
-
try {
|
|
1682
|
-
const collection = getDbByTenant({
|
|
1683
|
-
tenant,
|
|
1684
|
-
env: process.env.ENV
|
|
1685
|
-
}).collection(modelName);
|
|
1686
|
-
const value = await collection.findOne(query);
|
|
1687
|
-
if (!value) {
|
|
1688
|
-
throw new Error(`${type} value not found in ${modelName} for ${tenant}`);
|
|
1689
|
-
}
|
|
1690
|
-
try {
|
|
1691
|
-
const redisClient2 = getRedisClient();
|
|
1692
|
-
await redisClient2.set(key, JSON.stringify(value));
|
|
1693
|
-
} catch (error) {
|
|
1694
|
-
console.warn(
|
|
1695
|
-
`redis write failed for key ${key}:`,
|
|
1696
|
-
error instanceof Error && error.message
|
|
1697
|
-
);
|
|
1698
|
-
}
|
|
1699
|
-
return value;
|
|
1700
|
-
} catch (error) {
|
|
1701
|
-
throw new Error(
|
|
1702
|
-
`db read failed: ${error instanceof Error && error.message}`
|
|
1703
|
-
);
|
|
1704
|
-
}
|
|
1705
|
-
};
|
|
1706
|
-
var getTpl = async ({ name, tenant }) => {
|
|
1707
|
-
if (!name || !tenant) {
|
|
1708
|
-
throw new Error("Missing required parameters: name or tenant");
|
|
1709
|
-
}
|
|
1710
|
-
return await findInCache({
|
|
1711
|
-
modelName: "tpl",
|
|
1712
|
-
type: name,
|
|
1713
|
-
tenant,
|
|
1714
|
-
query: {
|
|
1715
|
-
kp_content_type: name,
|
|
1716
|
-
status: { $in: ["published", "editPublished"] }
|
|
1717
|
-
}
|
|
1718
|
-
});
|
|
1719
|
-
};
|
|
1720
|
-
var getAIConfigs = async ({
|
|
1721
|
-
tenant
|
|
1722
|
-
}) => {
|
|
1723
|
-
if (!tenant) {
|
|
1724
|
-
throw new Error("Missing required parameter: tenant");
|
|
1725
|
-
}
|
|
1726
|
-
return await findInCache({
|
|
1727
|
-
modelName: "platformConfigs",
|
|
1728
|
-
type: "ai",
|
|
1729
|
-
tenant,
|
|
1730
|
-
query: {
|
|
1731
|
-
type: "ai"
|
|
1732
|
-
}
|
|
1733
|
-
});
|
|
1734
|
-
};
|
|
1735
|
-
|
|
1736
|
-
// src/node.ts
|
|
1943
|
+
var import_getModelByTenant2 = __toESM(require_getModelByTenant());
|
|
1737
1944
|
init_models();
|
|
1738
1945
|
var import_WorkerManager = __toESM(require_WorkerManager());
|
|
1739
1946
|
var import_ProducerManager = __toESM(require_ProducerManager());
|
|
@@ -1751,20 +1958,17 @@ var import_GET_GLOBAL_BULLMQ_CONFIG = __toESM(require_GET_GLOBAL_BULLMQ_CONFIG()
|
|
|
1751
1958
|
MongoConnector,
|
|
1752
1959
|
PlatformConfigsSchema,
|
|
1753
1960
|
ProducerManager,
|
|
1961
|
+
RedisCacheConnector,
|
|
1754
1962
|
TplSchema,
|
|
1755
1963
|
WorkerManager,
|
|
1756
|
-
connectToRedis,
|
|
1757
1964
|
deleteVal,
|
|
1758
1965
|
extractAllBlocksFromTpl,
|
|
1759
1966
|
genTagId,
|
|
1760
1967
|
getAIChatModelByTenant,
|
|
1761
|
-
getAIConfigs,
|
|
1762
1968
|
getAnnotationsModelByTenant,
|
|
1763
1969
|
getDbByTenant,
|
|
1764
1970
|
getModelByTenant,
|
|
1765
1971
|
getPlatformConfigsModelByTenant,
|
|
1766
|
-
getRedisClient,
|
|
1767
|
-
getTpl,
|
|
1768
1972
|
getTplModelByTenant,
|
|
1769
1973
|
getVal,
|
|
1770
1974
|
initializeGlobalConfig,
|