From 134295605ef59395b08eb619fc397e1290acca21 Mon Sep 17 00:00:00 2001
From: peinz <s8923496@stud.uni-frankfurt.de>
Date: Thu, 23 Jan 2025 07:36:34 +0000
Subject: [PATCH] feat: allow passing aggregate options

---
 modules/baseql/server/resolver/mongo.ts | 13 ++++++-------
 1 file changed, 6 insertions(+), 7 deletions(-)

diff --git a/modules/baseql/server/resolver/mongo.ts b/modules/baseql/server/resolver/mongo.ts
index 5cebfbf8a9..a5dfae1cf9 100644
--- a/modules/baseql/server/resolver/mongo.ts
+++ b/modules/baseql/server/resolver/mongo.ts
@@ -1,5 +1,5 @@
 import { A } from '@adornis/base/env-info.js';
-import type { FindOptions } from 'mongodb';
+import type { AggregateOptions, FindOptions } from 'mongodb';
 import { from, map, switchMap, type Observable } from 'rxjs';
 import { constructValue } from '../../entities/construct.js';
 import type { MongoEntity } from '../../entities/mongoEntity.js';
@@ -228,6 +228,7 @@ export const subscribePaginatedAggregation = <T extends typeof MongoEntity>(
   limit: number,
   selector: Record<string, any>,
   aggregationStagesAfterSelection: object[],
+  aggregateOptions: AggregateOptions = {},
 ) => {
   return from(publishFilter(entity._class)).pipe(
     switchMap(filter => {
@@ -240,12 +241,10 @@ export const subscribePaginatedAggregation = <T extends typeof MongoEntity>(
         switchMap(async handle => {
           const col = await getRawCollection<T['prototype']>(entity._collectionName);
           const aggResults = await col
-            .aggregate<T['prototype']>([
-              { $match: fullSelector },
-              ...aggregationStagesAfterSelection,
-              { $skip: skip },
-              { $limit: limit },
-            ])
+            .aggregate<T['prototype']>(
+              [{ $match: fullSelector }, ...aggregationStagesAfterSelection, { $skip: skip }, { $limit: limit }],
+              aggregateOptions,
+            )
             .toArray();
           return aggResults;
         }),
-- 
GitLab