@dnax/core 0.18.7 → 0.18.8

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.
@@ -633,6 +633,7 @@ class useRest {
633
633
  io: Cfg.io,
634
634
  driver: "mongodb",
635
635
  action: "find",
636
+ count: result?.docs?.length || 0,
636
637
  params: params,
637
638
  session: sessionStorage(),
638
639
  result: result.docs,
@@ -673,6 +674,109 @@ class useRest {
673
674
  }
674
675
  });
675
676
  }
677
+
678
+ async count(
679
+ collection: string,
680
+ params: findParam,
681
+ options?: optionCb
682
+ ): Promise<number> {
683
+ return new Promise(async (resolve, reject) => {
684
+ try {
685
+ if (options?.cleanDeep) {
686
+ params = cleanDeep(params);
687
+ }
688
+ let meta: { total: number; count: number } = {
689
+ total: 0,
690
+ count: 0,
691
+ };
692
+ let useHook = options?.useHook ?? this.#useHook;
693
+ let useCustomApi = options?.useCustomApi ?? this.#useCustomApi;
694
+ let sharedData = {};
695
+ let result = {
696
+ docs: [],
697
+ };
698
+
699
+ let col = getCollection(collection, this.#tenant_id);
700
+ if (col?.hooks?.beforeOperation && useHook) {
701
+ await col.hooks.beforeOperation({
702
+ sharedData: sharedData,
703
+ c: this.#c,
704
+ driver: "mongodb",
705
+ io: Cfg.io,
706
+ action: "find",
707
+ params: params,
708
+ session: sessionStorage(),
709
+ rest: new useRest({
710
+ useHook: false,
711
+ tenant_id: this.#tenant_id,
712
+ }),
713
+ });
714
+ }
715
+
716
+ if (col?.hooks?.beforeCount && useHook) {
717
+ await col.hooks.beforeCount({
718
+ sharedData: sharedData,
719
+ c: this.#c,
720
+ driver: "mongodb",
721
+ action: "find",
722
+ io: Cfg.io,
723
+ params: params,
724
+ session: sessionStorage(),
725
+ rest: new useRest({
726
+ useHook: false,
727
+ tenant_id: this.#tenant_id,
728
+ }),
729
+ });
730
+ }
731
+
732
+ if (col?.customApi?.count && useCustomApi) {
733
+ let result = (await col?.customApi?.count({
734
+ io: Cfg.io,
735
+ session: sessionStorage(),
736
+ params: params,
737
+ rest: new useRest({
738
+ useHook: false,
739
+ tenant_id: this.#tenant_id,
740
+ useCustomApi: false,
741
+ }),
742
+ })) as any;
743
+ return resolve(result);
744
+ }
745
+
746
+ result.docs = await this.#tenant.database.db
747
+ ?.collection(collection)
748
+ .aggregate(formatData(buildPipeline(params, col)), {
749
+ allowDiskUse: true,
750
+ })
751
+ .toArray();
752
+
753
+ result.docs = toJson(result.docs);
754
+
755
+ if (col?.hooks?.afterCount && useHook) {
756
+ await col.hooks.afterCount({
757
+ sharedData: sharedData,
758
+ c: this.#c,
759
+ io: Cfg.io,
760
+ driver: "mongodb",
761
+ action: "find",
762
+ params: params,
763
+ session: sessionStorage(),
764
+ result: result.docs,
765
+ count: result?.docs?.length || 0,
766
+ rest: new useRest({
767
+ useHook: false,
768
+ tenant_id: this.#tenant_id,
769
+ }),
770
+ });
771
+ }
772
+
773
+ let resultDocs = toJson([...(result?.docs || [])]);
774
+ return resolve(resultDocs?.length);
775
+ } catch (err) {
776
+ return reject(err);
777
+ }
778
+ });
779
+ }
676
780
  /**
677
781
  *
678
782
  * @param collection
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dnax/core",
3
- "version": "0.18.7",
3
+ "version": "0.18.8",
4
4
  "module": "index.ts",
5
5
  "type": "module",
6
6
  "bin": {
package/types/index.ts CHANGED
@@ -192,6 +192,7 @@ export type hooksCtx = (ctx: {
192
192
  data?: object;
193
193
  params?: findParam;
194
194
  options?: {};
195
+ count?: number;
195
196
  id?: string;
196
197
  ids?: string[];
197
198
  update?: updateParams;
@@ -250,6 +251,7 @@ export type Collection = {
250
251
  deleteMany?: (ctx: ctxApi) => object | null | undefined;
251
252
  find?: (ctx: ctxApi) => Array<object> | null | undefined;
252
253
  findOne?: (ctx: ctxApi) => object;
254
+ count: (ctx: ctxApi) => number;
253
255
  };
254
256
  cache?: {
255
257
  ttl?: string;
@@ -280,6 +282,8 @@ export type Collection = {
280
282
  afterInsert?: hooksCtx;
281
283
  beforeAggregate?: hooksCtx;
282
284
  afterAggregate?: hooksCtx;
285
+ beforeCount?: hooksCtx;
286
+ afterCount?: hooksCtx;
283
287
  };
284
288
  access?: {
285
289
  "*"?: accessCtx;
@@ -296,6 +300,7 @@ export type Collection = {
296
300
  deleteMany?: accessCtx;
297
301
  aggregate?: accessCtx;
298
302
  upload?: accessCtx;
303
+ count?: accessCtx;
299
304
  };
300
305
  tenant_id?: string;
301
306
  slug: string;