@dnax/core 0.8.17 → 0.8.19

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 CHANGED
@@ -14,6 +14,7 @@ import { useRest } from "../driver/mongo/rest";
14
14
  import moment from "moment";
15
15
  import { Cfg } from "../config";
16
16
  import { getService } from "../lib/service";
17
+ import { bentoCache, bentoKey } from "../lib/bento";
17
18
  import { MediaDrive } from "../lib/media";
18
19
  import { isStudio } from "../lib/studio";
19
20
  import { pick } from "radash";
@@ -23,8 +24,8 @@ import { checkPermission, getPermission } from "../lib/permissions";
23
24
  import { ipRestriction } from "hono/ip-restriction";
24
25
  import { logger } from "hono/logger";
25
26
  import { v4 } from "uuid";
27
+ const cache = bentoCache.namespace("DNAX_API");
26
28
  const app = new Hono();
27
-
28
29
  const API_PATH = "/api";
29
30
  function HonoInstance(): typeof app {
30
31
  if (Cfg?.server?.logger) {
@@ -430,7 +431,23 @@ function HonoInstance(): typeof app {
430
431
  // udpateOne
431
432
 
432
433
  if (action == "find") {
433
- response = await rest.find(collection, body?.params || {});
434
+ let key = bentoKey({
435
+ data: body,
436
+ query: c.req.query,
437
+ });
438
+ if (useCache) {
439
+ let responseCache = await cache.get(key);
440
+ if (responseCache) {
441
+ return responseCache;
442
+ } else {
443
+ response = await rest.find(collection, body?.params || {});
444
+ await cache.set(key, response, {
445
+ ttl: col?.cache?.ttl || 60,
446
+ });
447
+ }
448
+ } else {
449
+ response = await rest.find(collection, body?.params || {});
450
+ }
434
451
  }
435
452
  if (action == "findOne") {
436
453
  response = await rest.findOne(
@@ -547,6 +564,7 @@ function HonoInstance(): typeof app {
547
564
  Cfg?.endpoints?.map((e) => {
548
565
  if (e?.enabled) {
549
566
  e.handler({
567
+ io: Cfg.io,
550
568
  router: app,
551
569
  });
552
570
  }
@@ -0,0 +1,30 @@
1
+ import { BentoCache, bentostore } from "bentocache";
2
+ import { memoryDriver } from "bentocache/drivers/memory";
3
+ const bentoCache = new BentoCache({
4
+ default: "dnaxCache",
5
+ stores: {
6
+ dnaxCache: bentostore().useL1Layer(
7
+ memoryDriver({
8
+ maxSize: 100 * 1024 * 1024,
9
+ })
10
+ ),
11
+ },
12
+ });
13
+
14
+ function bentoKey(data: object | string) {
15
+ let key = "";
16
+ if (data) {
17
+ if (typeof data == "object") {
18
+ key = JSON.stringify(data);
19
+ key = key.replace(/\s/g, "").trim();
20
+ }
21
+
22
+ if (typeof data == "string") {
23
+ key = data.replace(/\s/g, "").trim();
24
+ }
25
+ }
26
+
27
+ return key;
28
+ }
29
+
30
+ export { bentoCache, bentoKey };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dnax/core",
3
- "version": "0.8.17",
3
+ "version": "0.8.19",
4
4
  "module": "index.ts",
5
5
  "type": "module",
6
6
  "bin": {
@@ -20,6 +20,7 @@
20
20
  "@lukeed/ms": "^2.0.2",
21
21
  "@orama/orama": "^2.0.23",
22
22
  "@types/jsonwebtoken": "^9.0.6",
23
+ "bentocache": "^1.0.0-beta.9",
23
24
  "boxen": "^7.1.1",
24
25
  "chokidar": "^3.6.0",
25
26
  "clean-deep": "^3.4.0",
package/types/index.ts CHANGED
@@ -239,6 +239,9 @@ export type Collection = {
239
239
  find?: (ctx: ctxApi) => Array<object> | null | undefined;
240
240
  findOne?: (ctx: ctxApi) => object;
241
241
  };
242
+ cache: {
243
+ ttl?: number;
244
+ };
242
245
  schema?: object;
243
246
  auth?: {
244
247
  enabled?: boolean;
@@ -425,6 +428,7 @@ export type Q = {
425
428
  export type endpointCtx = {
426
429
  enabled: boolean;
427
430
  handler: (ctx: {
431
+ io: Io;
428
432
  router: {
429
433
  post: (path: string, clb: (c: Context) => void) => {};
430
434
  get: (path: string, clb: (c: Context) => void) => {};