@dnax/core 0.69.5 → 0.69.7

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
@@ -578,9 +578,11 @@ function HonoInstance(): typeof app {
578
578
 
579
579
  // find
580
580
  if (action == "find") {
581
- response = await rest.find(collection, body?.params || {}, {
582
- withMeta: body?.withMeta || false,
583
- });
581
+ response = await rest.find(collection, body?.params || {});
582
+ }
583
+
584
+ if (action == "findWithMeta") {
585
+ response = await rest.findWithMeta(collection, body?.params || {});
584
586
  }
585
587
 
586
588
  if (action == "listActivity") {
@@ -618,10 +620,7 @@ function HonoInstance(): typeof app {
618
620
  response = await rest.findOne(
619
621
  collection,
620
622
  body?.id || body._id,
621
- body?.params || {},
622
- {
623
- withMeta: body?.withMeta || false,
624
- }
623
+ body?.params || {}
625
624
  );
626
625
  }
627
626
  if (action == "insertOne") {
@@ -673,7 +672,7 @@ function HonoInstance(): typeof app {
673
672
  Array.isArray(col?.api?.fields?.select)
674
673
  ) {
675
674
  const selectedFields = [];
676
- if (body?.withMeta) {
675
+ if (action == "findWithMeta") {
677
676
  response.data = pick(response.data, col?.api?.fields?.select);
678
677
  } else {
679
678
  response = pick(response, col?.api?.fields?.select);
@@ -685,23 +684,16 @@ function HonoInstance(): typeof app {
685
684
  typeof col?.api?.fields?.select == "function"
686
685
  ) {
687
686
  let selectedFields = [];
688
- if (body?.withMeta) {
689
- selectedFields = await col?.api?.fields?.select({
690
- action: action,
691
- c: c,
692
- rest: rest,
693
- session: sessionStorage() as any,
694
- });
695
- selectedFields = [...new Set(selectedFields)];
687
+ selectedFields = await col?.api?.fields?.select({
688
+ action: action,
689
+ c: c,
690
+ rest: rest,
691
+ session: sessionStorage() as any,
692
+ });
693
+ selectedFields = [...new Set(selectedFields)];
694
+ if (action == "findWithMeta") {
696
695
  response.data = pick(response.data, selectedFields);
697
696
  } else {
698
- selectedFields = await col?.api?.fields?.select({
699
- action: action,
700
- c: c,
701
- rest: rest,
702
- session: sessionStorage() as any,
703
- });
704
- selectedFields = [...new Set(selectedFields)];
705
697
  response = pick(response, selectedFields);
706
698
  }
707
699
  }
@@ -714,17 +706,11 @@ function HonoInstance(): typeof app {
714
706
  let privateFields = col?.api?.fields?.hidden || [];
715
707
  privateFields.push("password");
716
708
  privateFields = [...new Set(privateFields)];
717
- if (body?.withMeta) {
718
- // If use Meta are included
719
- try {
720
- response.data = omit(response.data, privateFields);
721
- } catch (e) {}
709
+ if (action == "findWithMeta") {
710
+ response.data = omit(response.data, privateFields);
722
711
  } else {
723
712
  response = omit(response, privateFields);
724
713
  }
725
- if (response.hits) {
726
- response.hits = omit(response.hits, privateFields);
727
- }
728
714
  }
729
715
 
730
716
  // hidden is function
@@ -734,54 +720,28 @@ function HonoInstance(): typeof app {
734
720
  ) {
735
721
  let privateFields = [];
736
722
 
737
- if (body?.withMeta) {
738
- // If use Meta are included
739
- try {
740
- privateFields = await col?.api?.fields?.hidden({
741
- action: action,
742
- c: c,
743
- rest: rest,
744
- session: sessionStorage() as any,
745
- });
746
- privateFields.push("password");
747
- privateFields = [...new Set(privateFields)];
748
- response.data = omit(response.data, privateFields);
749
- if (response?.hits) {
750
- response.hits = omit(response.hits, privateFields);
751
- }
752
- } catch (e) {}
753
- } else {
754
- privateFields = await col?.api?.fields?.hidden({
755
- action: action,
756
- c: c,
757
- rest: rest,
758
- session: sessionStorage() as any,
759
- });
760
- privateFields.push("password");
761
- privateFields = [...new Set(privateFields)];
723
+ privateFields = await col?.api?.fields?.hidden({
724
+ action: action,
725
+ c: c,
726
+ rest: rest,
727
+ session: sessionStorage() as any,
728
+ });
729
+ privateFields.push("password");
730
+ privateFields = [...new Set(privateFields)];
762
731
 
763
- if (response?.hits) {
764
- response.hits = omit(response?.hits, privateFields);
765
- }
732
+ if (action == "findWithMeta") {
733
+ response.data = omit(response.data, privateFields);
734
+ } else {
766
735
  response = omit(response, privateFields);
767
736
  }
768
737
  }
769
738
 
770
- if (
771
- !col?.api?.fields?.hidden &&
772
- !col?.api?.fields?.select &&
773
- body?.withMeta
774
- ) {
775
- response.data = omit(response.data, ["password"]);
776
- response.meta = response.meta || {};
777
- }
778
-
779
- if (response?.hits && col?.privateFields?.length) {
780
- response = omit(response, col?.privateFields);
739
+ if (action == "findWithMeta" && col?.privateFields?.length) {
740
+ response.data = omit(response.data, col?.privateFields);
781
741
  }
782
742
 
783
743
  if (response?.data && col?.privateFields?.length) {
784
- response = omit(response, col?.privateFields);
744
+ response.data = omit(response.data, col?.privateFields);
785
745
  }
786
746
 
787
747
  return c.json(response);
@@ -8,15 +8,23 @@ export type Lookup = {
8
8
  };
9
9
 
10
10
  export type findParam = {
11
- $match?: object;
12
- $sort?: object;
11
+ $match?: {
12
+ _id?: string;
13
+ [key: string]: any;
14
+ };
15
+ $sort?: {
16
+ [key: string]: 1 | -1;
17
+ };
13
18
  $skip?: number;
14
19
  $limit?: number;
15
20
  $include?: Array<string | Lookup>;
16
21
  $project?: object;
17
- $matchInclude?: object;
22
+ $matchInclude?: {
23
+ _id?: string;
24
+ [key: string]: any;
25
+ };
18
26
  $group?: {
19
- _id: any;
27
+ _id?: any;
20
28
  [key: string]: any;
21
29
  };
22
30
  $sample?: {
@@ -6,11 +6,13 @@ import type { Actions, Collection, Tenant } from "./../../types/index";
6
6
  import { isArray } from "radash";
7
7
  import { omit } from "../../utils";
8
8
  import {
9
+ AggregationCursor,
9
10
  ChangeStream,
10
11
  ClientSession,
11
12
  MongoClient,
12
13
  ObjectId,
13
14
  type ChangeStreamOptions,
15
+ type Document,
14
16
  type FilterOperations,
15
17
  } from "mongodb";
16
18
  import { contextError, fn, toJson, dotJson, getEntryBykeys } from "../../utils";
@@ -798,6 +800,152 @@ class useRest {
798
800
  });
799
801
  }
800
802
 
803
+ async findWithMeta(
804
+ collection: string,
805
+ params?: findParam,
806
+ options?: optionCb
807
+ ): Promise<{ meta: object; data: Array<any> }> {
808
+ return new Promise(async (resolve, reject) => {
809
+ try {
810
+ let meta = {
811
+ ts: 0, // time duration
812
+ };
813
+ let start = Date.now();
814
+
815
+ if (!params) params = {};
816
+ if (options?.cleanDeep) {
817
+ params = cleanDeep(params);
818
+ }
819
+
820
+ let useHook = options?.useHook ?? this.#useHook;
821
+ let useCustomApi = options?.useCustomApi ?? this.#useCustomApi;
822
+ let sharedData = {};
823
+ let result = {
824
+ docs: [] as any[] | Document[],
825
+ };
826
+
827
+ let col = getCollection(collection, this.#tenant_id);
828
+ if (col?.hooks?.beforeOperation && useHook) {
829
+ await col.hooks.beforeOperation({
830
+ sharedData: sharedData,
831
+ c: this.#c,
832
+ error: fn.error,
833
+ driver: "mongodb",
834
+ io: Cfg.io,
835
+ action: "find",
836
+ params: params,
837
+ session: sessionStorage(),
838
+ rest: new useRest({
839
+ useHook: false,
840
+ tenant_id: this.#tenant_id,
841
+ }),
842
+ });
843
+ }
844
+
845
+ if (col?.hooks?.beforeFindWithMeta && useHook) {
846
+ await col.hooks.beforeFindWithMeta({
847
+ sharedData: sharedData,
848
+ c: this.#c,
849
+ driver: "mongodb",
850
+ action: "find",
851
+ io: Cfg.io,
852
+ params: params,
853
+ session: sessionStorage(),
854
+ rest: new useRest({
855
+ useHook: false,
856
+ tenant_id: this.#tenant_id,
857
+ }),
858
+ });
859
+ }
860
+
861
+ if (col?.customApi?.findWithMeta && useCustomApi) {
862
+ let result = (await col?.customApi?.findWithMeta({
863
+ io: Cfg.io,
864
+ error: fn.error,
865
+ session: sessionStorage(),
866
+ params: params,
867
+ rest: new useRest({
868
+ useHook: false,
869
+ tenant_id: this.#tenant_id,
870
+ useCustomApi: false,
871
+ }),
872
+ })) as any;
873
+ // Temp dexecution en ms
874
+ meta.ts = Date.now() - start;
875
+ return resolve({
876
+ meta: meta,
877
+ data: result,
878
+ });
879
+ }
880
+
881
+ result.docs =
882
+ (await this.#tenant.database.db
883
+ ?.collection(collection)
884
+ .aggregate(formatData(buildPipeline(params, col)), {
885
+ allowDiskUse: true,
886
+ })
887
+ .toArray()) || [];
888
+
889
+ result.docs = toJson(result.docs);
890
+ meta.ts = Date.now() - start;
891
+ if (col?.hooks?.afterFindWithMeta && useHook) {
892
+ await col.hooks.afterFindWithMeta({
893
+ sharedData: sharedData,
894
+ c: this.#c,
895
+ io: Cfg.io,
896
+ driver: "mongodb",
897
+ action: "find",
898
+ meta: meta,
899
+ count: result?.docs?.length || 0,
900
+ params: toJson(params),
901
+ session: sessionStorage(),
902
+ result: result.docs,
903
+ rest: new useRest({
904
+ useHook: false,
905
+ tenant_id: this.#tenant_id,
906
+ }),
907
+ });
908
+ }
909
+
910
+ let resultDocs = toJson([...(result?.docs || [])]);
911
+ return resolve({
912
+ meta: meta,
913
+ data: resultDocs,
914
+ });
915
+ } catch (err) {
916
+ return reject(err);
917
+ }
918
+ });
919
+ }
920
+
921
+ async findWithCursor(
922
+ collection: string,
923
+ params?: findParam,
924
+ options?: Pick<optionCb, "cleanDeep"> & {
925
+ batchSize?: number;
926
+ }
927
+ ): Promise<AggregationCursor<Document>> {
928
+ return new Promise(async (resolve, reject) => {
929
+ try {
930
+ if (!params) params = {};
931
+ if (options?.cleanDeep) {
932
+ params = cleanDeep(params);
933
+ }
934
+
935
+ let col = getCollection(collection, this.#tenant_id);
936
+
937
+ await this.#tenant.database.db
938
+ ?.collection(collection)
939
+ .aggregate(formatData(buildPipeline(params, col)), {
940
+ allowDiskUse: true,
941
+ batchSize: options?.batchSize,
942
+ });
943
+ } catch (err) {
944
+ return reject(err);
945
+ }
946
+ });
947
+ }
948
+
801
949
  async count(
802
950
  collection: string,
803
951
  params: findParam,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dnax/core",
3
- "version": "0.69.5",
3
+ "version": "0.69.7",
4
4
  "module": "index.ts",
5
5
  "type": "module",
6
6
  "bin": {},
@@ -22,7 +22,6 @@
22
22
  "dependencies": {
23
23
  "@colors/colors": "^1.6.0",
24
24
  "@lukeed/ms": "^2.0.2",
25
- "@types/blessed": "^0.1.25",
26
25
  "bentocache": "^1.5.0",
27
26
  "boxen": "^7.1.1",
28
27
  "chokidar": "3.6.0",
package/types/index.ts CHANGED
@@ -237,7 +237,7 @@ export type hooksCtx = (ctx: {
237
237
  sharedData?: any;
238
238
  action?: Actions;
239
239
  c?: Context;
240
- meta?: { total?: number; count?: number; [key: string]: any };
240
+ meta?: { ts:number; [key: string]: any };
241
241
  rest: InstanceType<typeof useRest>;
242
242
  session?: sessionCtx;
243
243
  io: socketIoType;
@@ -317,8 +317,10 @@ export type Collection = {
317
317
  deleteOne?: (ctx: ctxApi) => object | null | undefined | typeof fn.error;
318
318
  deleteMany?: (ctx: ctxApi) => object | null | undefined | typeof fn.error;
319
319
  find?: (ctx: ctxApi) => Array<object> | null | undefined | typeof fn.error;
320
+ findWithMeta?: (ctx: ctxApi) => { meta: object; data: Array<object> } | null | undefined | typeof fn.error;
320
321
  findOne?: (ctx: ctxApi) => object | null | undefined | typeof fn.error;
321
322
  count?: (ctx: ctxApi) => number | null | undefined | typeof fn.error;
323
+
322
324
  };
323
325
 
324
326
  schema?: object;
@@ -345,6 +347,8 @@ export type Collection = {
345
347
  afterDelete?: hooksCtx;
346
348
  beforeInsert?: hooksCtx;
347
349
  afterInsert?: hooksCtx;
350
+ beforeFindWithMeta?: hooksCtx;
351
+ afterFindWithMeta?: hooksCtx;
348
352
  beforeAggregate?: hooksCtx;
349
353
  afterAggregate?: hooksCtx;
350
354
  beforeCount?: hooksCtx;
@@ -374,6 +378,7 @@ export type Collection = {
374
378
  count?: accessCtx;
375
379
  search?: accessCtx;
376
380
  listActivity?: accessCtx;
381
+ findWithMeta?: accessCtx;
377
382
  };
378
383
  tenant_id?: string;
379
384
  slug: string;
@@ -565,6 +570,7 @@ export type Q = {
565
570
  | "execService"
566
571
  | "batch"
567
572
  | "count"
573
+ | "findWithMeta"
568
574
  | "execToolkit"
569
575
  | "search"
570
576
  | "listActivity";