@okf/ootils 1.3.1 → 1.3.3
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/index.d.mts +36 -23
- package/dist/index.d.ts +36 -23
- package/dist/index.js +136 -105
- package/dist/index.mjs +134 -105
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Connection, Document, Schema, Model } from 'mongoose';
|
|
2
|
+
import IORedis from 'ioredis';
|
|
2
3
|
|
|
3
4
|
declare function add(a: number, b: number): number;
|
|
4
5
|
|
|
@@ -108,28 +109,6 @@ declare const getTplModelByTenant: ({ tenant, env, mongodb, dbConfigs }: GetMode
|
|
|
108
109
|
__v: number;
|
|
109
110
|
}, any>;
|
|
110
111
|
|
|
111
|
-
declare const deleteVal: (data: any, valuePath: string) => any;
|
|
112
|
-
|
|
113
|
-
type ValuePath = string | string[];
|
|
114
|
-
type DataValue = any;
|
|
115
|
-
interface GetValOptions {
|
|
116
|
-
flattenIfValueIsArray?: boolean;
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
declare const setVal: (data: any, valuePath: string, value: DataValue) => any;
|
|
120
|
-
|
|
121
|
-
/**
|
|
122
|
-
*
|
|
123
|
-
* @param {*} data
|
|
124
|
-
* @param {*} valuePath
|
|
125
|
-
* @returns
|
|
126
|
-
* - if target value is an array found inside an array, it will flatten them out so that there is a single array
|
|
127
|
-
* - if target value is an array NOT found inside an array, that will be returned as is.
|
|
128
|
-
* - if target value is a string/object/number/boolean found inside an array of arrays, then the inner arrays will be flattened so that there is a single array
|
|
129
|
-
* - if target value is a string/object/number/boolean NOT found inside an array, it will be returned as is.
|
|
130
|
-
*/
|
|
131
|
-
declare const getVal: (data: any, valuePath: ValuePath, options?: GetValOptions, depthIdx?: number) => any;
|
|
132
|
-
|
|
133
112
|
interface GetTplParams {
|
|
134
113
|
name: string;
|
|
135
114
|
tenant: string;
|
|
@@ -163,5 +142,39 @@ declare const getTpl: ({ name, tenant }: GetTplParams) => Promise<Template>;
|
|
|
163
142
|
declare const getAIConfigs: ({ tenant }: GetAIConfigsParams) => Promise<AIconfig>;
|
|
164
143
|
|
|
165
144
|
declare const connectToRedis: () => Promise<void>;
|
|
145
|
+
declare const getRedisClient: () => IORedis;
|
|
146
|
+
|
|
147
|
+
declare const deleteVal: (data: any, valuePath: string) => any;
|
|
148
|
+
|
|
149
|
+
type ValuePath = string | string[];
|
|
150
|
+
type DataValue = any;
|
|
151
|
+
interface GetValOptions {
|
|
152
|
+
flattenIfValueIsArray?: boolean;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
declare const setVal: (data: any, valuePath: string, value: DataValue) => any;
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
*
|
|
159
|
+
* @param {*} data
|
|
160
|
+
* @param {*} valuePath
|
|
161
|
+
* @returns
|
|
162
|
+
* - if target value is an array found inside an array, it will flatten them out so that there is a single array
|
|
163
|
+
* - if target value is an array NOT found inside an array, that will be returned as is.
|
|
164
|
+
* - if target value is a string/object/number/boolean found inside an array of arrays, then the inner arrays will be flattened so that there is a single array
|
|
165
|
+
* - if target value is a string/object/number/boolean NOT found inside an array, it will be returned as is.
|
|
166
|
+
*/
|
|
167
|
+
declare const getVal: (data: any, valuePath: ValuePath, options?: GetValOptions, depthIdx?: number) => any;
|
|
168
|
+
|
|
169
|
+
interface RichTextBlock {
|
|
170
|
+
text: string;
|
|
171
|
+
[key: string]: any;
|
|
172
|
+
}
|
|
173
|
+
interface RichTextValue {
|
|
174
|
+
blocks: RichTextBlock[];
|
|
175
|
+
[key: string]: any;
|
|
176
|
+
}
|
|
177
|
+
type TagNameInput = string | number | RichTextValue | null | undefined;
|
|
178
|
+
declare const genTagId: (tagName: TagNameInput) => string;
|
|
166
179
|
|
|
167
|
-
export { add, connectToRedis, deleteVal, getAIConfigs, getAnnotationsModelByTenant, getDbByTenant, getModelByTenant, getPlatformConfigsModelByTenant, getTpl, getTplModelByTenant, getVal, initializeGlobalConfig, multiConnectToMongoDB, setVal, updateGlobalConfig };
|
|
180
|
+
export { add, connectToRedis, deleteVal, genTagId, getAIConfigs, getAnnotationsModelByTenant, getDbByTenant, getModelByTenant, getPlatformConfigsModelByTenant, getRedisClient, getTpl, getTplModelByTenant, getVal, initializeGlobalConfig, multiConnectToMongoDB, setVal, updateGlobalConfig };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Connection, Document, Schema, Model } from 'mongoose';
|
|
2
|
+
import IORedis from 'ioredis';
|
|
2
3
|
|
|
3
4
|
declare function add(a: number, b: number): number;
|
|
4
5
|
|
|
@@ -108,28 +109,6 @@ declare const getTplModelByTenant: ({ tenant, env, mongodb, dbConfigs }: GetMode
|
|
|
108
109
|
__v: number;
|
|
109
110
|
}, any>;
|
|
110
111
|
|
|
111
|
-
declare const deleteVal: (data: any, valuePath: string) => any;
|
|
112
|
-
|
|
113
|
-
type ValuePath = string | string[];
|
|
114
|
-
type DataValue = any;
|
|
115
|
-
interface GetValOptions {
|
|
116
|
-
flattenIfValueIsArray?: boolean;
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
declare const setVal: (data: any, valuePath: string, value: DataValue) => any;
|
|
120
|
-
|
|
121
|
-
/**
|
|
122
|
-
*
|
|
123
|
-
* @param {*} data
|
|
124
|
-
* @param {*} valuePath
|
|
125
|
-
* @returns
|
|
126
|
-
* - if target value is an array found inside an array, it will flatten them out so that there is a single array
|
|
127
|
-
* - if target value is an array NOT found inside an array, that will be returned as is.
|
|
128
|
-
* - if target value is a string/object/number/boolean found inside an array of arrays, then the inner arrays will be flattened so that there is a single array
|
|
129
|
-
* - if target value is a string/object/number/boolean NOT found inside an array, it will be returned as is.
|
|
130
|
-
*/
|
|
131
|
-
declare const getVal: (data: any, valuePath: ValuePath, options?: GetValOptions, depthIdx?: number) => any;
|
|
132
|
-
|
|
133
112
|
interface GetTplParams {
|
|
134
113
|
name: string;
|
|
135
114
|
tenant: string;
|
|
@@ -163,5 +142,39 @@ declare const getTpl: ({ name, tenant }: GetTplParams) => Promise<Template>;
|
|
|
163
142
|
declare const getAIConfigs: ({ tenant }: GetAIConfigsParams) => Promise<AIconfig>;
|
|
164
143
|
|
|
165
144
|
declare const connectToRedis: () => Promise<void>;
|
|
145
|
+
declare const getRedisClient: () => IORedis;
|
|
146
|
+
|
|
147
|
+
declare const deleteVal: (data: any, valuePath: string) => any;
|
|
148
|
+
|
|
149
|
+
type ValuePath = string | string[];
|
|
150
|
+
type DataValue = any;
|
|
151
|
+
interface GetValOptions {
|
|
152
|
+
flattenIfValueIsArray?: boolean;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
declare const setVal: (data: any, valuePath: string, value: DataValue) => any;
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
*
|
|
159
|
+
* @param {*} data
|
|
160
|
+
* @param {*} valuePath
|
|
161
|
+
* @returns
|
|
162
|
+
* - if target value is an array found inside an array, it will flatten them out so that there is a single array
|
|
163
|
+
* - if target value is an array NOT found inside an array, that will be returned as is.
|
|
164
|
+
* - if target value is a string/object/number/boolean found inside an array of arrays, then the inner arrays will be flattened so that there is a single array
|
|
165
|
+
* - if target value is a string/object/number/boolean NOT found inside an array, it will be returned as is.
|
|
166
|
+
*/
|
|
167
|
+
declare const getVal: (data: any, valuePath: ValuePath, options?: GetValOptions, depthIdx?: number) => any;
|
|
168
|
+
|
|
169
|
+
interface RichTextBlock {
|
|
170
|
+
text: string;
|
|
171
|
+
[key: string]: any;
|
|
172
|
+
}
|
|
173
|
+
interface RichTextValue {
|
|
174
|
+
blocks: RichTextBlock[];
|
|
175
|
+
[key: string]: any;
|
|
176
|
+
}
|
|
177
|
+
type TagNameInput = string | number | RichTextValue | null | undefined;
|
|
178
|
+
declare const genTagId: (tagName: TagNameInput) => string;
|
|
166
179
|
|
|
167
|
-
export { add, connectToRedis, deleteVal, getAIConfigs, getAnnotationsModelByTenant, getDbByTenant, getModelByTenant, getPlatformConfigsModelByTenant, getTpl, getTplModelByTenant, getVal, initializeGlobalConfig, multiConnectToMongoDB, setVal, updateGlobalConfig };
|
|
180
|
+
export { add, connectToRedis, deleteVal, genTagId, getAIConfigs, getAnnotationsModelByTenant, getDbByTenant, getModelByTenant, getPlatformConfigsModelByTenant, getRedisClient, getTpl, getTplModelByTenant, getVal, initializeGlobalConfig, multiConnectToMongoDB, setVal, updateGlobalConfig };
|
package/dist/index.js
CHANGED
|
@@ -49,8 +49,9 @@ var require_Annotations = __commonJS({
|
|
|
49
49
|
tagId: String
|
|
50
50
|
}
|
|
51
51
|
],
|
|
52
|
-
collectionId: String
|
|
53
|
-
|
|
52
|
+
collectionId: String,
|
|
53
|
+
_id: false
|
|
54
|
+
})
|
|
54
55
|
},
|
|
55
56
|
// Meta information
|
|
56
57
|
meta: {
|
|
@@ -73,6 +74,7 @@ var require_Annotations = __commonJS({
|
|
|
73
74
|
annotations: {
|
|
74
75
|
tags: {
|
|
75
76
|
type: Map,
|
|
77
|
+
_id: false,
|
|
76
78
|
of: new Schema({
|
|
77
79
|
collectionId: String,
|
|
78
80
|
data: [
|
|
@@ -82,7 +84,7 @@ var require_Annotations = __commonJS({
|
|
|
82
84
|
tagId: String
|
|
83
85
|
}
|
|
84
86
|
]
|
|
85
|
-
}
|
|
87
|
+
})
|
|
86
88
|
},
|
|
87
89
|
fragment: {
|
|
88
90
|
isLexical: Boolean,
|
|
@@ -318,11 +320,13 @@ __export(index_exports, {
|
|
|
318
320
|
add: () => add,
|
|
319
321
|
connectToRedis: () => connectToRedis,
|
|
320
322
|
deleteVal: () => deleteVal,
|
|
323
|
+
genTagId: () => genTagId,
|
|
321
324
|
getAIConfigs: () => getAIConfigs,
|
|
322
325
|
getAnnotationsModelByTenant: () => getAnnotationsModelByTenant,
|
|
323
326
|
getDbByTenant: () => getDbByTenant,
|
|
324
327
|
getModelByTenant: () => getModelByTenant,
|
|
325
328
|
getPlatformConfigsModelByTenant: () => getPlatformConfigsModelByTenant,
|
|
329
|
+
getRedisClient: () => getRedisClient,
|
|
326
330
|
getTpl: () => getTpl,
|
|
327
331
|
getTplModelByTenant: () => getTplModelByTenant,
|
|
328
332
|
getVal: () => getVal,
|
|
@@ -373,7 +377,7 @@ var updateGlobalConfig = (updates) => {
|
|
|
373
377
|
var mongoOptions = {
|
|
374
378
|
// Note: These legacy options are no longer needed in newer versions of mongoose
|
|
375
379
|
// useUnifiedTopology, useNewUrlParser, useCreateIndex, useFindAndModify are deprecated
|
|
376
|
-
maxPoolSize:
|
|
380
|
+
maxPoolSize: 20
|
|
377
381
|
// replaces poolSize
|
|
378
382
|
};
|
|
379
383
|
var multiConnectToMongoDB = ({ env, dbConfigs } = {}) => {
|
|
@@ -474,6 +478,113 @@ var getTplModelByTenant = ({ tenant, env, mongodb, dbConfigs }) => getModelByTen
|
|
|
474
478
|
env
|
|
475
479
|
});
|
|
476
480
|
|
|
481
|
+
// src/redis/index.ts
|
|
482
|
+
var import_ioredis = __toESM(require("ioredis"));
|
|
483
|
+
var redisClient = null;
|
|
484
|
+
var REDIS_CONFIG = {
|
|
485
|
+
port: Number(process.env.REDIS_PORT),
|
|
486
|
+
host: process.env.REDIS_HOST || "",
|
|
487
|
+
username: process.env.REDIS_USERNAME || "",
|
|
488
|
+
password: process.env.REDIS_PASSWORD || ""
|
|
489
|
+
};
|
|
490
|
+
var connectToRedis = async () => {
|
|
491
|
+
try {
|
|
492
|
+
redisClient = new import_ioredis.default(REDIS_CONFIG);
|
|
493
|
+
await redisClient.ping();
|
|
494
|
+
console.log("Redis connected successfully");
|
|
495
|
+
} catch (error) {
|
|
496
|
+
console.error(
|
|
497
|
+
"Redis connection failed:",
|
|
498
|
+
error instanceof Error && error.message
|
|
499
|
+
);
|
|
500
|
+
if (redisClient) {
|
|
501
|
+
redisClient.disconnect();
|
|
502
|
+
}
|
|
503
|
+
throw error;
|
|
504
|
+
}
|
|
505
|
+
};
|
|
506
|
+
var getRedisClient = () => {
|
|
507
|
+
if (!redisClient) {
|
|
508
|
+
throw new Error("Redis client not initialized. Call connectToRedis first.");
|
|
509
|
+
}
|
|
510
|
+
return redisClient;
|
|
511
|
+
};
|
|
512
|
+
|
|
513
|
+
// src/redis/functions.ts
|
|
514
|
+
var findInCache = async ({
|
|
515
|
+
tenant,
|
|
516
|
+
modelName,
|
|
517
|
+
type,
|
|
518
|
+
query = {}
|
|
519
|
+
}) => {
|
|
520
|
+
const key = `${process.env.ENV}:${tenant}:${modelName}:${type}`;
|
|
521
|
+
try {
|
|
522
|
+
const redisClient2 = getRedisClient();
|
|
523
|
+
const value = await redisClient2.get(key);
|
|
524
|
+
if (value) {
|
|
525
|
+
return JSON.parse(value);
|
|
526
|
+
}
|
|
527
|
+
} catch (error) {
|
|
528
|
+
console.warn(
|
|
529
|
+
`redis read failed for key ${key}`,
|
|
530
|
+
error instanceof Error && error.message
|
|
531
|
+
);
|
|
532
|
+
}
|
|
533
|
+
try {
|
|
534
|
+
const collection = getDbByTenant({
|
|
535
|
+
tenant,
|
|
536
|
+
env: process.env.ENV
|
|
537
|
+
}).collection(modelName);
|
|
538
|
+
const value = await collection.findOne(query);
|
|
539
|
+
if (!value) {
|
|
540
|
+
throw new Error(`${type} value not found in ${modelName} for ${tenant}`);
|
|
541
|
+
}
|
|
542
|
+
try {
|
|
543
|
+
const redisClient2 = getRedisClient();
|
|
544
|
+
await redisClient2.set(key, JSON.stringify(value));
|
|
545
|
+
} catch (error) {
|
|
546
|
+
console.warn(
|
|
547
|
+
`redis write failed for key ${key}:`,
|
|
548
|
+
error instanceof Error && error.message
|
|
549
|
+
);
|
|
550
|
+
}
|
|
551
|
+
return value;
|
|
552
|
+
} catch (error) {
|
|
553
|
+
throw new Error(
|
|
554
|
+
`db read failed: ${error instanceof Error && error.message}`
|
|
555
|
+
);
|
|
556
|
+
}
|
|
557
|
+
};
|
|
558
|
+
var getTpl = async ({ name, tenant }) => {
|
|
559
|
+
if (!name || !tenant) {
|
|
560
|
+
throw new Error("Missing required parameters: name or tenant");
|
|
561
|
+
}
|
|
562
|
+
return await findInCache({
|
|
563
|
+
modelName: "tpls",
|
|
564
|
+
type: name,
|
|
565
|
+
tenant,
|
|
566
|
+
query: {
|
|
567
|
+
kp_content_type: name,
|
|
568
|
+
status: { $in: ["published", "editPublished"] }
|
|
569
|
+
}
|
|
570
|
+
});
|
|
571
|
+
};
|
|
572
|
+
var getAIConfigs = async ({
|
|
573
|
+
tenant
|
|
574
|
+
}) => {
|
|
575
|
+
if (!tenant) {
|
|
576
|
+
throw new Error("Missing required parameter: tenant");
|
|
577
|
+
}
|
|
578
|
+
return await findInCache({
|
|
579
|
+
modelName: "platformConfigs",
|
|
580
|
+
type: "ai",
|
|
581
|
+
tenant,
|
|
582
|
+
query: {
|
|
583
|
+
type: "ai"
|
|
584
|
+
}
|
|
585
|
+
});
|
|
586
|
+
};
|
|
587
|
+
|
|
477
588
|
// src/utils/getterSetterDeleter/utils/set_deleteVal.ts
|
|
478
589
|
var set_deleteVal = (action, data, valuePath, value) => {
|
|
479
590
|
if (valuePath === void 0) return;
|
|
@@ -605,122 +716,42 @@ var getValV2_getter = (data, valuePath, options, depthIdx) => {
|
|
|
605
716
|
return dataRef[keysArray[len - 1]];
|
|
606
717
|
};
|
|
607
718
|
|
|
608
|
-
// src/
|
|
609
|
-
var
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
username: process.env.REDIS_USERNAME || "",
|
|
615
|
-
password: process.env.REDIS_PASSWORD || ""
|
|
616
|
-
};
|
|
617
|
-
var connectToRedis = async () => {
|
|
618
|
-
try {
|
|
619
|
-
redisClient = new import_ioredis.default(REDIS_CONFIG);
|
|
620
|
-
await redisClient.ping();
|
|
621
|
-
console.log("Redis connected successfully");
|
|
622
|
-
} catch (error) {
|
|
623
|
-
console.error(
|
|
624
|
-
"Redis connection failed:",
|
|
625
|
-
error instanceof Error && error.message
|
|
626
|
-
);
|
|
627
|
-
if (redisClient) {
|
|
628
|
-
redisClient.disconnect();
|
|
629
|
-
}
|
|
630
|
-
throw error;
|
|
631
|
-
}
|
|
632
|
-
};
|
|
633
|
-
var getRedisClient = () => {
|
|
634
|
-
if (!redisClient) {
|
|
635
|
-
throw new Error("Redis client not initialized. Call connectToRedis first.");
|
|
636
|
-
}
|
|
637
|
-
return redisClient;
|
|
638
|
-
};
|
|
639
|
-
|
|
640
|
-
// src/redis/functions.ts
|
|
641
|
-
var findInCache = async ({
|
|
642
|
-
tenant,
|
|
643
|
-
modelName,
|
|
644
|
-
type,
|
|
645
|
-
query = {}
|
|
646
|
-
}) => {
|
|
647
|
-
const key = `${process.env.ENV}:${tenant}:${modelName}:${type}`;
|
|
648
|
-
try {
|
|
649
|
-
const redisClient2 = getRedisClient();
|
|
650
|
-
const value = await redisClient2.get(key);
|
|
651
|
-
if (value) {
|
|
652
|
-
return JSON.parse(value);
|
|
653
|
-
}
|
|
654
|
-
} catch (error) {
|
|
655
|
-
console.warn(
|
|
656
|
-
`redis read failed for key ${key}`,
|
|
657
|
-
error instanceof Error && error.message
|
|
658
|
-
);
|
|
719
|
+
// src/utils/genTagId.ts
|
|
720
|
+
var convertFromRichText = (value) => {
|
|
721
|
+
if (!value) return "";
|
|
722
|
+
let val = "";
|
|
723
|
+
if (typeof value === "string" || typeof value === "number") {
|
|
724
|
+
return String(value);
|
|
659
725
|
}
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
if (!value) {
|
|
667
|
-
throw new Error(`${type} value not found in ${modelName} for ${tenant}`);
|
|
668
|
-
}
|
|
669
|
-
try {
|
|
670
|
-
const redisClient2 = getRedisClient();
|
|
671
|
-
await redisClient2.set(key, JSON.stringify(value));
|
|
672
|
-
} catch (error) {
|
|
673
|
-
console.warn(
|
|
674
|
-
`redis write failed for key ${key}:`,
|
|
675
|
-
error instanceof Error && error.message
|
|
676
|
-
);
|
|
726
|
+
if (typeof value === "object" && "blocks" in value && Array.isArray(value.blocks)) {
|
|
727
|
+
for (let i = 0; i < value.blocks.length; i++) {
|
|
728
|
+
const block = value.blocks[i];
|
|
729
|
+
if (block && block.text && block.text.length > 0) {
|
|
730
|
+
val = val + block.text;
|
|
731
|
+
}
|
|
677
732
|
}
|
|
678
|
-
return value;
|
|
679
|
-
} catch (error) {
|
|
680
|
-
throw new Error(
|
|
681
|
-
`db read failed: ${error instanceof Error && error.message}`
|
|
682
|
-
);
|
|
683
|
-
}
|
|
684
|
-
};
|
|
685
|
-
var getTpl = async ({ name, tenant }) => {
|
|
686
|
-
if (!name || !tenant) {
|
|
687
|
-
throw new Error("Missing required parameters: name or tenant");
|
|
688
733
|
}
|
|
689
|
-
return
|
|
690
|
-
modelName: "tpls",
|
|
691
|
-
type: name,
|
|
692
|
-
tenant,
|
|
693
|
-
query: {
|
|
694
|
-
kp_content_type: name,
|
|
695
|
-
status: { $in: ["published", "editPublished"] }
|
|
696
|
-
}
|
|
697
|
-
});
|
|
734
|
+
return val;
|
|
698
735
|
};
|
|
699
|
-
var
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
return await findInCache({
|
|
706
|
-
modelName: "platformConfigs",
|
|
707
|
-
type: "ai",
|
|
708
|
-
tenant,
|
|
709
|
-
query: {
|
|
710
|
-
type: "ai"
|
|
711
|
-
}
|
|
712
|
-
});
|
|
736
|
+
var genTagId = (tagName) => {
|
|
737
|
+
let toReturn = convertFromRichText(tagName);
|
|
738
|
+
const regex = /[^\p{L}\p{N}\+]+/gui;
|
|
739
|
+
toReturn = toReturn.trim().toLowerCase().replace(regex, "_");
|
|
740
|
+
toReturn = toReturn.replace(/\+/g, "plus");
|
|
741
|
+
return toReturn.replace(/^_+|_+$/, "");
|
|
713
742
|
};
|
|
714
743
|
// Annotate the CommonJS export names for ESM import in node:
|
|
715
744
|
0 && (module.exports = {
|
|
716
745
|
add,
|
|
717
746
|
connectToRedis,
|
|
718
747
|
deleteVal,
|
|
748
|
+
genTagId,
|
|
719
749
|
getAIConfigs,
|
|
720
750
|
getAnnotationsModelByTenant,
|
|
721
751
|
getDbByTenant,
|
|
722
752
|
getModelByTenant,
|
|
723
753
|
getPlatformConfigsModelByTenant,
|
|
754
|
+
getRedisClient,
|
|
724
755
|
getTpl,
|
|
725
756
|
getTplModelByTenant,
|
|
726
757
|
getVal,
|
package/dist/index.mjs
CHANGED
|
@@ -28,8 +28,9 @@ var require_Annotations = __commonJS({
|
|
|
28
28
|
tagId: String
|
|
29
29
|
}
|
|
30
30
|
],
|
|
31
|
-
collectionId: String
|
|
32
|
-
|
|
31
|
+
collectionId: String,
|
|
32
|
+
_id: false
|
|
33
|
+
})
|
|
33
34
|
},
|
|
34
35
|
// Meta information
|
|
35
36
|
meta: {
|
|
@@ -52,6 +53,7 @@ var require_Annotations = __commonJS({
|
|
|
52
53
|
annotations: {
|
|
53
54
|
tags: {
|
|
54
55
|
type: Map,
|
|
56
|
+
_id: false,
|
|
55
57
|
of: new Schema({
|
|
56
58
|
collectionId: String,
|
|
57
59
|
data: [
|
|
@@ -61,7 +63,7 @@ var require_Annotations = __commonJS({
|
|
|
61
63
|
tagId: String
|
|
62
64
|
}
|
|
63
65
|
]
|
|
64
|
-
}
|
|
66
|
+
})
|
|
65
67
|
},
|
|
66
68
|
fragment: {
|
|
67
69
|
isLexical: Boolean,
|
|
@@ -331,7 +333,7 @@ var updateGlobalConfig = (updates) => {
|
|
|
331
333
|
var mongoOptions = {
|
|
332
334
|
// Note: These legacy options are no longer needed in newer versions of mongoose
|
|
333
335
|
// useUnifiedTopology, useNewUrlParser, useCreateIndex, useFindAndModify are deprecated
|
|
334
|
-
maxPoolSize:
|
|
336
|
+
maxPoolSize: 20
|
|
335
337
|
// replaces poolSize
|
|
336
338
|
};
|
|
337
339
|
var multiConnectToMongoDB = ({ env, dbConfigs } = {}) => {
|
|
@@ -432,6 +434,113 @@ var getTplModelByTenant = ({ tenant, env, mongodb, dbConfigs }) => getModelByTen
|
|
|
432
434
|
env
|
|
433
435
|
});
|
|
434
436
|
|
|
437
|
+
// src/redis/index.ts
|
|
438
|
+
import IORedis from "ioredis";
|
|
439
|
+
var redisClient = null;
|
|
440
|
+
var REDIS_CONFIG = {
|
|
441
|
+
port: Number(process.env.REDIS_PORT),
|
|
442
|
+
host: process.env.REDIS_HOST || "",
|
|
443
|
+
username: process.env.REDIS_USERNAME || "",
|
|
444
|
+
password: process.env.REDIS_PASSWORD || ""
|
|
445
|
+
};
|
|
446
|
+
var connectToRedis = async () => {
|
|
447
|
+
try {
|
|
448
|
+
redisClient = new IORedis(REDIS_CONFIG);
|
|
449
|
+
await redisClient.ping();
|
|
450
|
+
console.log("Redis connected successfully");
|
|
451
|
+
} catch (error) {
|
|
452
|
+
console.error(
|
|
453
|
+
"Redis connection failed:",
|
|
454
|
+
error instanceof Error && error.message
|
|
455
|
+
);
|
|
456
|
+
if (redisClient) {
|
|
457
|
+
redisClient.disconnect();
|
|
458
|
+
}
|
|
459
|
+
throw error;
|
|
460
|
+
}
|
|
461
|
+
};
|
|
462
|
+
var getRedisClient = () => {
|
|
463
|
+
if (!redisClient) {
|
|
464
|
+
throw new Error("Redis client not initialized. Call connectToRedis first.");
|
|
465
|
+
}
|
|
466
|
+
return redisClient;
|
|
467
|
+
};
|
|
468
|
+
|
|
469
|
+
// src/redis/functions.ts
|
|
470
|
+
var findInCache = async ({
|
|
471
|
+
tenant,
|
|
472
|
+
modelName,
|
|
473
|
+
type,
|
|
474
|
+
query = {}
|
|
475
|
+
}) => {
|
|
476
|
+
const key = `${process.env.ENV}:${tenant}:${modelName}:${type}`;
|
|
477
|
+
try {
|
|
478
|
+
const redisClient2 = getRedisClient();
|
|
479
|
+
const value = await redisClient2.get(key);
|
|
480
|
+
if (value) {
|
|
481
|
+
return JSON.parse(value);
|
|
482
|
+
}
|
|
483
|
+
} catch (error) {
|
|
484
|
+
console.warn(
|
|
485
|
+
`redis read failed for key ${key}`,
|
|
486
|
+
error instanceof Error && error.message
|
|
487
|
+
);
|
|
488
|
+
}
|
|
489
|
+
try {
|
|
490
|
+
const collection = getDbByTenant({
|
|
491
|
+
tenant,
|
|
492
|
+
env: process.env.ENV
|
|
493
|
+
}).collection(modelName);
|
|
494
|
+
const value = await collection.findOne(query);
|
|
495
|
+
if (!value) {
|
|
496
|
+
throw new Error(`${type} value not found in ${modelName} for ${tenant}`);
|
|
497
|
+
}
|
|
498
|
+
try {
|
|
499
|
+
const redisClient2 = getRedisClient();
|
|
500
|
+
await redisClient2.set(key, JSON.stringify(value));
|
|
501
|
+
} catch (error) {
|
|
502
|
+
console.warn(
|
|
503
|
+
`redis write failed for key ${key}:`,
|
|
504
|
+
error instanceof Error && error.message
|
|
505
|
+
);
|
|
506
|
+
}
|
|
507
|
+
return value;
|
|
508
|
+
} catch (error) {
|
|
509
|
+
throw new Error(
|
|
510
|
+
`db read failed: ${error instanceof Error && error.message}`
|
|
511
|
+
);
|
|
512
|
+
}
|
|
513
|
+
};
|
|
514
|
+
var getTpl = async ({ name, tenant }) => {
|
|
515
|
+
if (!name || !tenant) {
|
|
516
|
+
throw new Error("Missing required parameters: name or tenant");
|
|
517
|
+
}
|
|
518
|
+
return await findInCache({
|
|
519
|
+
modelName: "tpls",
|
|
520
|
+
type: name,
|
|
521
|
+
tenant,
|
|
522
|
+
query: {
|
|
523
|
+
kp_content_type: name,
|
|
524
|
+
status: { $in: ["published", "editPublished"] }
|
|
525
|
+
}
|
|
526
|
+
});
|
|
527
|
+
};
|
|
528
|
+
var getAIConfigs = async ({
|
|
529
|
+
tenant
|
|
530
|
+
}) => {
|
|
531
|
+
if (!tenant) {
|
|
532
|
+
throw new Error("Missing required parameter: tenant");
|
|
533
|
+
}
|
|
534
|
+
return await findInCache({
|
|
535
|
+
modelName: "platformConfigs",
|
|
536
|
+
type: "ai",
|
|
537
|
+
tenant,
|
|
538
|
+
query: {
|
|
539
|
+
type: "ai"
|
|
540
|
+
}
|
|
541
|
+
});
|
|
542
|
+
};
|
|
543
|
+
|
|
435
544
|
// src/utils/getterSetterDeleter/utils/set_deleteVal.ts
|
|
436
545
|
var set_deleteVal = (action, data, valuePath, value) => {
|
|
437
546
|
if (valuePath === void 0) return;
|
|
@@ -563,121 +672,41 @@ var getValV2_getter = (data, valuePath, options, depthIdx) => {
|
|
|
563
672
|
return dataRef[keysArray[len - 1]];
|
|
564
673
|
};
|
|
565
674
|
|
|
566
|
-
// src/
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
username: process.env.REDIS_USERNAME || "",
|
|
573
|
-
password: process.env.REDIS_PASSWORD || ""
|
|
574
|
-
};
|
|
575
|
-
var connectToRedis = async () => {
|
|
576
|
-
try {
|
|
577
|
-
redisClient = new IORedis(REDIS_CONFIG);
|
|
578
|
-
await redisClient.ping();
|
|
579
|
-
console.log("Redis connected successfully");
|
|
580
|
-
} catch (error) {
|
|
581
|
-
console.error(
|
|
582
|
-
"Redis connection failed:",
|
|
583
|
-
error instanceof Error && error.message
|
|
584
|
-
);
|
|
585
|
-
if (redisClient) {
|
|
586
|
-
redisClient.disconnect();
|
|
587
|
-
}
|
|
588
|
-
throw error;
|
|
589
|
-
}
|
|
590
|
-
};
|
|
591
|
-
var getRedisClient = () => {
|
|
592
|
-
if (!redisClient) {
|
|
593
|
-
throw new Error("Redis client not initialized. Call connectToRedis first.");
|
|
594
|
-
}
|
|
595
|
-
return redisClient;
|
|
596
|
-
};
|
|
597
|
-
|
|
598
|
-
// src/redis/functions.ts
|
|
599
|
-
var findInCache = async ({
|
|
600
|
-
tenant,
|
|
601
|
-
modelName,
|
|
602
|
-
type,
|
|
603
|
-
query = {}
|
|
604
|
-
}) => {
|
|
605
|
-
const key = `${process.env.ENV}:${tenant}:${modelName}:${type}`;
|
|
606
|
-
try {
|
|
607
|
-
const redisClient2 = getRedisClient();
|
|
608
|
-
const value = await redisClient2.get(key);
|
|
609
|
-
if (value) {
|
|
610
|
-
return JSON.parse(value);
|
|
611
|
-
}
|
|
612
|
-
} catch (error) {
|
|
613
|
-
console.warn(
|
|
614
|
-
`redis read failed for key ${key}`,
|
|
615
|
-
error instanceof Error && error.message
|
|
616
|
-
);
|
|
675
|
+
// src/utils/genTagId.ts
|
|
676
|
+
var convertFromRichText = (value) => {
|
|
677
|
+
if (!value) return "";
|
|
678
|
+
let val = "";
|
|
679
|
+
if (typeof value === "string" || typeof value === "number") {
|
|
680
|
+
return String(value);
|
|
617
681
|
}
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
if (!value) {
|
|
625
|
-
throw new Error(`${type} value not found in ${modelName} for ${tenant}`);
|
|
626
|
-
}
|
|
627
|
-
try {
|
|
628
|
-
const redisClient2 = getRedisClient();
|
|
629
|
-
await redisClient2.set(key, JSON.stringify(value));
|
|
630
|
-
} catch (error) {
|
|
631
|
-
console.warn(
|
|
632
|
-
`redis write failed for key ${key}:`,
|
|
633
|
-
error instanceof Error && error.message
|
|
634
|
-
);
|
|
682
|
+
if (typeof value === "object" && "blocks" in value && Array.isArray(value.blocks)) {
|
|
683
|
+
for (let i = 0; i < value.blocks.length; i++) {
|
|
684
|
+
const block = value.blocks[i];
|
|
685
|
+
if (block && block.text && block.text.length > 0) {
|
|
686
|
+
val = val + block.text;
|
|
687
|
+
}
|
|
635
688
|
}
|
|
636
|
-
return value;
|
|
637
|
-
} catch (error) {
|
|
638
|
-
throw new Error(
|
|
639
|
-
`db read failed: ${error instanceof Error && error.message}`
|
|
640
|
-
);
|
|
641
|
-
}
|
|
642
|
-
};
|
|
643
|
-
var getTpl = async ({ name, tenant }) => {
|
|
644
|
-
if (!name || !tenant) {
|
|
645
|
-
throw new Error("Missing required parameters: name or tenant");
|
|
646
689
|
}
|
|
647
|
-
return
|
|
648
|
-
modelName: "tpls",
|
|
649
|
-
type: name,
|
|
650
|
-
tenant,
|
|
651
|
-
query: {
|
|
652
|
-
kp_content_type: name,
|
|
653
|
-
status: { $in: ["published", "editPublished"] }
|
|
654
|
-
}
|
|
655
|
-
});
|
|
690
|
+
return val;
|
|
656
691
|
};
|
|
657
|
-
var
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
return await findInCache({
|
|
664
|
-
modelName: "platformConfigs",
|
|
665
|
-
type: "ai",
|
|
666
|
-
tenant,
|
|
667
|
-
query: {
|
|
668
|
-
type: "ai"
|
|
669
|
-
}
|
|
670
|
-
});
|
|
692
|
+
var genTagId = (tagName) => {
|
|
693
|
+
let toReturn = convertFromRichText(tagName);
|
|
694
|
+
const regex = /[^\p{L}\p{N}\+]+/gui;
|
|
695
|
+
toReturn = toReturn.trim().toLowerCase().replace(regex, "_");
|
|
696
|
+
toReturn = toReturn.replace(/\+/g, "plus");
|
|
697
|
+
return toReturn.replace(/^_+|_+$/, "");
|
|
671
698
|
};
|
|
672
699
|
export {
|
|
673
700
|
add,
|
|
674
701
|
connectToRedis,
|
|
675
702
|
deleteVal,
|
|
703
|
+
genTagId,
|
|
676
704
|
getAIConfigs,
|
|
677
705
|
getAnnotationsModelByTenant,
|
|
678
706
|
getDbByTenant,
|
|
679
707
|
getModelByTenant,
|
|
680
708
|
getPlatformConfigsModelByTenant,
|
|
709
|
+
getRedisClient,
|
|
681
710
|
getTpl,
|
|
682
711
|
getTplModelByTenant,
|
|
683
712
|
getVal,
|