@dnax/core 0.30.6 → 0.31.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/app/hono.ts CHANGED
@@ -463,25 +463,50 @@ function HonoInstance(): typeof app {
463
463
  }
464
464
  // find
465
465
  if (action == "find") {
466
- let keyCache = bentoKey({
467
- data: body,
468
- query: c.req.query,
469
- });
470
- if (useCache) {
471
- let responseFromCache = await cache.get({
472
- key: keyCache,
466
+ if (col?.cache?.enabled && useCache) {
467
+ let keyCache = bentoKey({
468
+ data: body,
469
+ query: c.req.query,
473
470
  });
474
- if (responseFromCache) {
475
- response = responseFromCache;
476
- } else {
477
- response = await rest.find(collection, body?.params || {}, {
478
- withMeta: body?.withMeta || false,
471
+
472
+ // if customCache
473
+ if (col?.cache?.customCache) {
474
+ let fetchFromCache = await col?.cache?.customCache({
475
+ action: action,
476
+ rest: rest,
477
+ session: sessionStorage(),
478
+ c: c,
479
479
  });
480
- await cache.set({
480
+
481
+ if (!fetchFromCache.isStale) {
482
+ response = fetchFromCache.data;
483
+ } else {
484
+ response = await rest.find(collection, body?.params || {}, {
485
+ withMeta: body?.withMeta || false,
486
+ });
487
+ }
488
+ }
489
+ // if not customCache
490
+ if (!col?.cache?.customCache) {
491
+ let responseFromCache = await cache.get({
481
492
  key: keyCache,
482
- value: response,
483
- ttl: col?.cache?.ttl || "1m",
484
493
  });
494
+ if (responseFromCache) {
495
+ response = responseFromCache;
496
+ } else {
497
+ response = await rest.find(collection, body?.params || {}, {
498
+ withMeta: body?.withMeta || false,
499
+ });
500
+ await cache.set({
501
+ key: keyCache,
502
+ value: response,
503
+ ttl: col?.cache?.ttl || "1m",
504
+ });
505
+ }
506
+
507
+ /* response = await rest.find(collection, body?.params || {}, {
508
+ withMeta: body?.withMeta || false,
509
+ }); */
485
510
  }
486
511
  } else {
487
512
  response = await rest.find(collection, body?.params || {}, {
@@ -582,6 +607,7 @@ function HonoInstance(): typeof app {
582
607
 
583
608
  // Obtenir le type MIME du fichier
584
609
  const mimeType = mime.lookup(filePath);
610
+
585
611
  // Si ce n'est pas une image, retourner le fichier original
586
612
  if (!mimeType || !mimeType?.startsWith("image/")) {
587
613
  const originalFile = fs.readFileSync(filePath);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dnax/core",
3
- "version": "0.30.6",
3
+ "version": "0.31.0",
4
4
  "module": "index.ts",
5
5
  "type": "module",
6
6
  "bin": {
package/types/index.ts CHANGED
@@ -276,8 +276,27 @@ export type Collection = {
276
276
  findOne?: (ctx: ctxApi) => object;
277
277
  count?: (ctx: ctxApi) => number;
278
278
  };
279
+ /**
280
+ * Cache available for action 'find' | 'findOne'
281
+ */
279
282
  cache?: {
280
- ttl?: string;
283
+ enabled?: boolean;
284
+ ttl: string;
285
+ triggerEvents: ["find", "findOne"];
286
+ /**
287
+ * Overwrite cache bahavior
288
+ * @param ctx
289
+ * @returns <any>
290
+ */
291
+ customCache?: (ctx: {
292
+ action: "find" | "findOne";
293
+ rest: InstanceType<typeof useRest>;
294
+ c: Context;
295
+ session: sessionCtx;
296
+ }) => {
297
+ isStale: boolean;
298
+ data: any;
299
+ };
281
300
  };
282
301
  schema?: object;
283
302
  auth?: {
package/utils/index.ts CHANGED
@@ -10,6 +10,7 @@ import generateUniqueId from "generate-unique-id";
10
10
  import dayjs from "dayjs";
11
11
  import { Otp } from "../lib/opt";
12
12
  import collect from "collect.js";
13
+ import mime from "mime-types";
13
14
  import * as uuid from "uuid";
14
15
  import * as urlencode from "urlencode";
15
16
  import dotJson from "dot-object";
@@ -108,11 +109,15 @@ function isDate(date: string): boolean {
108
109
  const dateRegex3 = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}$/;
109
110
  // le regex pour cette forme 2024-11-30T21:36:12+00:00
110
111
  const dateRegex4 = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\+\d{2}:\d{2}$/;
112
+ const dateRegex5 = /^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\+\d{2}:\d{2}$/;
113
+ const dateRegex6 = /^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\+\d{2}$/;
111
114
  let isDate_ =
112
115
  !isNaN(Date.parse(date)) &&
113
116
  (dateRegex.test(date) ||
114
117
  dateRegex2.test(date) ||
115
118
  dateRegex4.test(date) ||
119
+ dateRegex5.test(date) ||
120
+ dateRegex6.test(date) ||
116
121
  dateRegex3.test(date));
117
122
  if (isDate_) return true;
118
123
  try {
@@ -369,6 +374,7 @@ const password = {
369
374
 
370
375
  const contextError = ContextError;
371
376
  export {
377
+ mime,
372
378
  dotJson,
373
379
  uuid,
374
380
  pick,