@dnax/core 0.70.3 → 0.70.5
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/app/hono.ts +7 -1
- package/driver/mongo/rest.ts +131 -5
- package/lib/bento/index.ts +3 -0
- package/package.json +1 -1
- package/types/index.ts +15 -3
- package/utils/index.ts +22 -0
package/app/hono.ts
CHANGED
|
@@ -578,7 +578,13 @@ function HonoInstance(): typeof app {
|
|
|
578
578
|
|
|
579
579
|
// find
|
|
580
580
|
if (action == "find") {
|
|
581
|
-
|
|
581
|
+
if (col?.cache?.fetch && useCache) {
|
|
582
|
+
// la fonctionnalité de cache n'est pas configuré
|
|
583
|
+
return fn.error(`Cache features not configured`, 400);
|
|
584
|
+
}
|
|
585
|
+
response = await rest.find(collection, body?.params || {}, {
|
|
586
|
+
useCache: useCache ?? false,
|
|
587
|
+
});
|
|
582
588
|
}
|
|
583
589
|
|
|
584
590
|
if (action == "findWithMeta") {
|
package/driver/mongo/rest.ts
CHANGED
|
@@ -18,7 +18,6 @@ import {
|
|
|
18
18
|
import { contextError, fn, toJson, dotJson, getEntryBykeys } from "../../utils";
|
|
19
19
|
import v, { type Schema } from "joi";
|
|
20
20
|
import type { Context } from "hono";
|
|
21
|
-
import { MeiliSearch } from "../../lib/meilisearch";
|
|
22
21
|
import type { SearchParams } from "meilisearch";
|
|
23
22
|
|
|
24
23
|
import type {
|
|
@@ -41,6 +40,7 @@ import {
|
|
|
41
40
|
} from "./utils";
|
|
42
41
|
import { Cfg } from "../../config";
|
|
43
42
|
import cleanDeep from "clean-deep";
|
|
43
|
+
import { useCache } from "../../lib/bento";
|
|
44
44
|
|
|
45
45
|
type options = {
|
|
46
46
|
tenant_id: string;
|
|
@@ -55,7 +55,6 @@ type optionCb = {
|
|
|
55
55
|
cleanDeep?: boolean;
|
|
56
56
|
useCustomApi?: boolean;
|
|
57
57
|
elementAt?: Number | null;
|
|
58
|
-
withMeta?: boolean;
|
|
59
58
|
};
|
|
60
59
|
|
|
61
60
|
type QueryBatchType<T extends "findOne" | "find"> = {
|
|
@@ -571,6 +570,20 @@ class useRest {
|
|
|
571
570
|
state: sessionStorage()?.get()?.state,
|
|
572
571
|
uuid: sessionStorage()?.get()?.uuid, */
|
|
573
572
|
} as activityInput);
|
|
573
|
+
if (col?.cache?.invalidate) {
|
|
574
|
+
col.cache.invalidate({
|
|
575
|
+
operationType: "insert",
|
|
576
|
+
rest: new useRest({
|
|
577
|
+
useCustomApi: false,
|
|
578
|
+
session: this.#session,
|
|
579
|
+
useHook: false,
|
|
580
|
+
tenant_id: this.#tenant_id,
|
|
581
|
+
}),
|
|
582
|
+
action: "insertOne",
|
|
583
|
+
ids: [String(data?._id)],
|
|
584
|
+
tags: [String(data?._id)],
|
|
585
|
+
});
|
|
586
|
+
}
|
|
574
587
|
return resolve(toJson(data));
|
|
575
588
|
} catch (err) {
|
|
576
589
|
return reject(err);
|
|
@@ -738,6 +751,20 @@ class useRest {
|
|
|
738
751
|
state: sessionStorage()?.get()?.state,
|
|
739
752
|
uuid: sessionStorage()?.get()?.uuid, */
|
|
740
753
|
});
|
|
754
|
+
if (col?.cache?.invalidate) {
|
|
755
|
+
col.cache.invalidate({
|
|
756
|
+
operationType: "insert",
|
|
757
|
+
rest: new useRest({
|
|
758
|
+
useCustomApi: false,
|
|
759
|
+
session: this.#session,
|
|
760
|
+
useHook: false,
|
|
761
|
+
tenant_id: this.#tenant_id,
|
|
762
|
+
}),
|
|
763
|
+
action: "insertMany",
|
|
764
|
+
ids: data.map((d) => String(d?._id)) || [],
|
|
765
|
+
tags: data.map((d) => String(d?._id)) || [],
|
|
766
|
+
});
|
|
767
|
+
}
|
|
741
768
|
return resolve(toJson(data));
|
|
742
769
|
} catch (err) {
|
|
743
770
|
reject(err);
|
|
@@ -793,8 +820,10 @@ class useRest {
|
|
|
793
820
|
async find(
|
|
794
821
|
collection: string,
|
|
795
822
|
params?: findParam,
|
|
796
|
-
options?: optionCb
|
|
797
|
-
|
|
823
|
+
options?: optionCb & {
|
|
824
|
+
useCache?: boolean;
|
|
825
|
+
}
|
|
826
|
+
): Promise<{ _id: string; [key: string]: any }[] | Array<any>> {
|
|
798
827
|
return new Promise(async (resolve, reject) => {
|
|
799
828
|
try {
|
|
800
829
|
if (!params) params = {};
|
|
@@ -802,6 +831,8 @@ class useRest {
|
|
|
802
831
|
params = cleanDeep(params);
|
|
803
832
|
}
|
|
804
833
|
|
|
834
|
+
let useCacheBool = options?.useCache ?? false;
|
|
835
|
+
|
|
805
836
|
let useHook = options?.useHook ?? this.#useHook;
|
|
806
837
|
let useCustomApi = options?.useCustomApi ?? this.#useCustomApi;
|
|
807
838
|
let sharedData = {};
|
|
@@ -810,6 +841,17 @@ class useRest {
|
|
|
810
841
|
};
|
|
811
842
|
|
|
812
843
|
let col = getCollection(collection, this.#tenant_id);
|
|
844
|
+
if (useCacheBool && col?.cache?.fetch) {
|
|
845
|
+
let cacheResult = await col.cache.fetch({
|
|
846
|
+
rest: new useRest({
|
|
847
|
+
useHook: false,
|
|
848
|
+
tenant_id: this.#tenant_id,
|
|
849
|
+
}),
|
|
850
|
+
params: params,
|
|
851
|
+
});
|
|
852
|
+
return cacheResult || [];
|
|
853
|
+
}
|
|
854
|
+
|
|
813
855
|
if (col?.hooks?.beforeOperation && useHook) {
|
|
814
856
|
await col.hooks.beforeOperation({
|
|
815
857
|
sharedData: sharedData,
|
|
@@ -1511,6 +1553,20 @@ class useRest {
|
|
|
1511
1553
|
state: sessionStorage()?.get()?.state,
|
|
1512
1554
|
uuid: sessionStorage()?.get()?.uuid, */
|
|
1513
1555
|
});
|
|
1556
|
+
if (col?.cache?.invalidate) {
|
|
1557
|
+
col.cache.invalidate({
|
|
1558
|
+
operationType: "update",
|
|
1559
|
+
rest: new useRest({
|
|
1560
|
+
useCustomApi: false,
|
|
1561
|
+
session: this.#session,
|
|
1562
|
+
useHook: false,
|
|
1563
|
+
tenant_id: this.#tenant_id,
|
|
1564
|
+
}),
|
|
1565
|
+
action: "updateOne",
|
|
1566
|
+
ids: [id],
|
|
1567
|
+
tags: [id],
|
|
1568
|
+
});
|
|
1569
|
+
}
|
|
1514
1570
|
return resolve(toJson(result?.doc || {}));
|
|
1515
1571
|
} catch (err) {
|
|
1516
1572
|
return reject(err);
|
|
@@ -1651,7 +1707,20 @@ class useRest {
|
|
|
1651
1707
|
state: sessionStorage()?.get()?.state,
|
|
1652
1708
|
uuid: sessionStorage()?.get()?.uuid, */
|
|
1653
1709
|
});
|
|
1654
|
-
|
|
1710
|
+
if (col?.cache?.invalidate) {
|
|
1711
|
+
col.cache.invalidate({
|
|
1712
|
+
operationType: "update",
|
|
1713
|
+
rest: new useRest({
|
|
1714
|
+
useCustomApi: false,
|
|
1715
|
+
session: this.#session,
|
|
1716
|
+
useHook: false,
|
|
1717
|
+
tenant_id: this.#tenant_id,
|
|
1718
|
+
}),
|
|
1719
|
+
action: "findAndUpdate",
|
|
1720
|
+
ids: result?.docs?.map((d) => String(d?._id)) || [],
|
|
1721
|
+
tags: result?.docs.map((d) => String(d?._id)) || [],
|
|
1722
|
+
});
|
|
1723
|
+
}
|
|
1655
1724
|
return resolve({
|
|
1656
1725
|
docs: result.docs,
|
|
1657
1726
|
matchedCount: up_?.matchedCount || 0,
|
|
@@ -1798,6 +1867,20 @@ class useRest {
|
|
|
1798
1867
|
state: sessionStorage()?.get()?.state,
|
|
1799
1868
|
uuid: sessionStorage()?.get()?.uuid, */
|
|
1800
1869
|
});
|
|
1870
|
+
if (col?.cache?.invalidate) {
|
|
1871
|
+
col.cache.invalidate({
|
|
1872
|
+
operationType: "update",
|
|
1873
|
+
rest: new useRest({
|
|
1874
|
+
useCustomApi: false,
|
|
1875
|
+
session: this.#session,
|
|
1876
|
+
useHook: false,
|
|
1877
|
+
tenant_id: this.#tenant_id,
|
|
1878
|
+
}),
|
|
1879
|
+
action: "findOneAndUpdate",
|
|
1880
|
+
ids: [doc?._id],
|
|
1881
|
+
tags: [doc?._id],
|
|
1882
|
+
});
|
|
1883
|
+
}
|
|
1801
1884
|
return resolve(toJson(result?.doc || {}));
|
|
1802
1885
|
} catch (err) {
|
|
1803
1886
|
return reject(err);
|
|
@@ -2025,6 +2108,20 @@ class useRest {
|
|
|
2025
2108
|
state: sessionStorage()?.get()?.state,
|
|
2026
2109
|
uuid: sessionStorage()?.get()?.uuid, */
|
|
2027
2110
|
});
|
|
2111
|
+
if (col?.cache?.invalidate) {
|
|
2112
|
+
col.cache.invalidate({
|
|
2113
|
+
operationType: "update",
|
|
2114
|
+
rest: new useRest({
|
|
2115
|
+
useCustomApi: false,
|
|
2116
|
+
session: this.#session,
|
|
2117
|
+
useHook: false,
|
|
2118
|
+
tenant_id: this.#tenant_id,
|
|
2119
|
+
}),
|
|
2120
|
+
action: "updateMany",
|
|
2121
|
+
ids: ids,
|
|
2122
|
+
tags: ids,
|
|
2123
|
+
});
|
|
2124
|
+
}
|
|
2028
2125
|
return resolve(result.docs);
|
|
2029
2126
|
} else {
|
|
2030
2127
|
throw new contextError("List of id required", 400);
|
|
@@ -2187,6 +2284,21 @@ class useRest {
|
|
|
2187
2284
|
uuid: sessionStorage()?.get()?.uuid, */
|
|
2188
2285
|
});
|
|
2189
2286
|
|
|
2287
|
+
if (col?.cache?.invalidate) {
|
|
2288
|
+
col.cache.invalidate({
|
|
2289
|
+
operationType: "delete",
|
|
2290
|
+
rest: new useRest({
|
|
2291
|
+
useCustomApi: false,
|
|
2292
|
+
session: this.#session,
|
|
2293
|
+
useHook: false,
|
|
2294
|
+
tenant_id: this.#tenant_id,
|
|
2295
|
+
}),
|
|
2296
|
+
action: "deleteOne",
|
|
2297
|
+
ids: [id],
|
|
2298
|
+
tags: [id],
|
|
2299
|
+
});
|
|
2300
|
+
}
|
|
2301
|
+
|
|
2190
2302
|
return resolve(doc);
|
|
2191
2303
|
} catch (err) {
|
|
2192
2304
|
return resolve(err);
|
|
@@ -2345,6 +2457,20 @@ class useRest {
|
|
|
2345
2457
|
state: sessionStorage()?.get()?.state,
|
|
2346
2458
|
uuid: sessionStorage()?.get()?.uuid, */
|
|
2347
2459
|
});
|
|
2460
|
+
if (col?.cache?.invalidate) {
|
|
2461
|
+
col.cache.invalidate({
|
|
2462
|
+
operationType: "delete",
|
|
2463
|
+
rest: new useRest({
|
|
2464
|
+
useCustomApi: false,
|
|
2465
|
+
session: this.#session,
|
|
2466
|
+
useHook: false,
|
|
2467
|
+
tenant_id: this.#tenant_id,
|
|
2468
|
+
}),
|
|
2469
|
+
action: "deleteMany",
|
|
2470
|
+
ids: ids,
|
|
2471
|
+
tags: ids,
|
|
2472
|
+
});
|
|
2473
|
+
}
|
|
2348
2474
|
return resolve(deletedIds);
|
|
2349
2475
|
} catch (err) {
|
|
2350
2476
|
return reject(err);
|
package/lib/bento/index.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { BentoCache, bentostore } from "bentocache";
|
|
2
2
|
import { memoryDriver } from "bentocache/drivers/memory";
|
|
3
3
|
import { fileDriver } from "bentocache/drivers/file";
|
|
4
|
+
import { useKey } from "../../utils";
|
|
4
5
|
import { v4 } from "uuid";
|
|
5
6
|
import fs from "fs-extra";
|
|
6
7
|
import path from "path";
|
|
@@ -43,4 +44,6 @@ function useCache(
|
|
|
43
44
|
return bento;
|
|
44
45
|
}
|
|
45
46
|
|
|
47
|
+
useCache.Key = useKey;
|
|
48
|
+
|
|
46
49
|
export { useCache };
|
package/package.json
CHANGED
package/types/index.ts
CHANGED
|
@@ -3,7 +3,7 @@ import { Cron } from "croner";
|
|
|
3
3
|
//import type{ updateParams } from "./../driver/mongo/@types";
|
|
4
4
|
//import * as v from "valibot";
|
|
5
5
|
import type { Db, MongoClient } from "mongodb";
|
|
6
|
-
import {
|
|
6
|
+
import { BentoCache, bentostore } from "bentocache";
|
|
7
7
|
|
|
8
8
|
import { useRest } from "../driver/mongo/rest";
|
|
9
9
|
import { sessionStorage } from "../lib/asyncLocalStorage";
|
|
@@ -308,6 +308,19 @@ export type Collection = {
|
|
|
308
308
|
accept?: Array<string> | string;
|
|
309
309
|
filesystemAdapter?: any;
|
|
310
310
|
};
|
|
311
|
+
cache?:{
|
|
312
|
+
fetch?:(ctx:{
|
|
313
|
+
rest:InstanceType<typeof useRest>;
|
|
314
|
+
params:findParam;
|
|
315
|
+
})=>Promise<Array<any>>;
|
|
316
|
+
invalidate?:(ctx:{
|
|
317
|
+
rest:InstanceType<typeof useRest>;
|
|
318
|
+
action:Actions;
|
|
319
|
+
ids:Array<string>;
|
|
320
|
+
tags:Array<string>;
|
|
321
|
+
operationType:"insert"|"update"|"delete";
|
|
322
|
+
})=>Promise<any>;
|
|
323
|
+
};
|
|
311
324
|
customApi?: {
|
|
312
325
|
aggregate?: (ctx: ctxApi) => Array<object> | null | undefined | typeof fn.error;
|
|
313
326
|
insertOne?: (ctx: ctxApi) => object | null | undefined | typeof fn.error;
|
|
@@ -316,11 +329,10 @@ export type Collection = {
|
|
|
316
329
|
updateMany?: (ctx: ctxApi) => object | null | undefined | typeof fn.error;
|
|
317
330
|
deleteOne?: (ctx: ctxApi) => object | null | undefined | typeof fn.error;
|
|
318
331
|
deleteMany?: (ctx: ctxApi) => object | null | undefined | typeof fn.error;
|
|
319
|
-
find?: (ctx: ctxApi) => Array<
|
|
332
|
+
find?: (ctx: ctxApi) => Array<any> | undefined | typeof fn.error;
|
|
320
333
|
findWithMeta?: (ctx: ctxApi) => { meta: object; data: Array<object> } | null | undefined | typeof fn.error;
|
|
321
334
|
findOne?: (ctx: ctxApi) => object | null | undefined | typeof fn.error;
|
|
322
335
|
count?: (ctx: ctxApi) => number | null | undefined | typeof fn.error;
|
|
323
|
-
|
|
324
336
|
};
|
|
325
337
|
|
|
326
338
|
schema?: object;
|
package/utils/index.ts
CHANGED
|
@@ -21,6 +21,7 @@ import * as _ from "radash";
|
|
|
21
21
|
import { email } from "../lib/mail";
|
|
22
22
|
|
|
23
23
|
import { deepEqual } from "json-joy/lib/json-equal/deepEqual";
|
|
24
|
+
import type { SupportedCryptoAlgorithms } from "bun";
|
|
24
25
|
//import {applyPatch} from "json-joy/lib/json-patch"
|
|
25
26
|
|
|
26
27
|
function copy(data: object): object | any {
|
|
@@ -549,6 +550,26 @@ function containsForbiddenOperators(
|
|
|
549
550
|
);
|
|
550
551
|
}
|
|
551
552
|
|
|
553
|
+
// create key hash with bun hash
|
|
554
|
+
function useKey(
|
|
555
|
+
key: object | string,
|
|
556
|
+
algorithm: SupportedCryptoAlgorithms = "sha256"
|
|
557
|
+
): string {
|
|
558
|
+
if (!key) throw new Error("key is required");
|
|
559
|
+
let hashKey = null;
|
|
560
|
+
if (typeof key == "string") {
|
|
561
|
+
hashKey = key;
|
|
562
|
+
}
|
|
563
|
+
if (typeof key == "object") {
|
|
564
|
+
hashKey = JSON.stringify(key);
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
let hasher = new Bun.CryptoHasher(algorithm || "sha256");
|
|
568
|
+
hasher.update(hashKey as any);
|
|
569
|
+
let n = hasher.digest("base64");
|
|
570
|
+
return n;
|
|
571
|
+
}
|
|
572
|
+
|
|
552
573
|
const contextError = ContextError;
|
|
553
574
|
export {
|
|
554
575
|
file,
|
|
@@ -585,4 +606,5 @@ export {
|
|
|
585
606
|
applyPatch,
|
|
586
607
|
createPatch,
|
|
587
608
|
containsForbiddenOperators,
|
|
609
|
+
useKey,
|
|
588
610
|
};
|