11import DataLoader from 'dataloader' ;
2- import { Db , ObjectId } from 'mongodb' ;
2+ import { Db , ObjectId , WithId } from 'mongodb' ;
33import { PlanDBScheme , UserDBScheme , WorkspaceDBScheme , ProjectDBScheme , EventData , EventAddons } from '@hawk.so/types' ;
44
55type EventDbScheme = {
@@ -47,7 +47,7 @@ export default class DataLoaders {
4747 */
4848 public userByEmail = new DataLoader < string , UserDBScheme | null > (
4949 ( userEmails ) =>
50- this . batchByField < UserDBScheme , string > ( 'users' , userEmails , 'email' ) ,
50+ this . batchByField < UserDBScheme , 'email' > ( 'users' , 'email' , userEmails ) ,
5151 { cache : false }
5252 ) ;
5353
@@ -69,41 +69,51 @@ export default class DataLoaders {
6969 * @param collectionName - collection name to get entities
7070 * @param ids - ids for resolving
7171 */
72- private async batchByIds < T extends { _id : ObjectId } > ( collectionName : string , ids : ReadonlyArray < string > ) : Promise < ( T | null | Error ) [ ] > {
73- return this . batchByField < T , ObjectId > ( collectionName , ids . map ( id => new ObjectId ( id ) ) , '_id' ) ;
72+ private async batchByIds < T extends { _id : ObjectId } > (
73+ collectionName : string ,
74+ ids : ReadonlyArray < string >
75+ ) : Promise < ( WithId < T > | null ) [ ] > {
76+ return this . batchByField < T , '_id' > ( collectionName , '_id' , ids . map ( id => new ObjectId ( id ) ) ) ;
7477 }
7578
7679 /**
7780 * Batching function for resolving entities by certain field
7881 * @param collectionName - collection name to get entities
79- * @param values - values for resolving
8082 * @param fieldName - field name to resolve
83+ * @param values - values for resolving
8184 */
8285 private async batchByField <
83- // eslint-disable-next-line @typescript-eslint/no-explicit-any
84- T extends { [ key : string ] : any } ,
85- FieldType extends ObjectId | string
86- > ( collectionName : string , values : ReadonlyArray < FieldType > , fieldName : string ) : Promise < ( T | null | Error ) [ ] > {
86+ T extends Record < string , any > ,
87+ FieldType extends keyof T
88+ > (
89+ collectionName : string ,
90+ fieldName : FieldType ,
91+ values : ReadonlyArray < T [ FieldType ] >
92+ ) : Promise < ( WithId < T > | null ) [ ] > {
93+ type Doc = WithId < T > ;
8794 const valuesMap = new Map < string , FieldType > ( ) ;
8895
8996 for ( const value of values ) {
9097 valuesMap . set ( value . toString ( ) , value ) ;
9198 }
9299
93- const queryResult = await this . dbConnection . collection ( collectionName )
100+ const queryResult = await this . dbConnection
101+ . collection < T > ( collectionName )
94102 . find ( {
95103 [ fieldName ] : { $in : Array . from ( valuesMap . values ( ) ) } ,
96- } )
104+ } as any )
97105 . toArray ( ) ;
98106
99107 /**
100108 * Map for making associations between given id and fetched entity
101109 * It's because MongoDB `find` mixed all entities
102110 */
103- const entitiesMap : Record < string , T > = { } ;
111+ const entitiesMap : Record < string , Doc > = { } ;
112+
113+ queryResult . forEach ( ( entity ) => {
114+ const key = entity [ fieldName as keyof Doc ] ;
104115
105- queryResult . forEach ( ( entity : T ) => {
106- entitiesMap [ entity [ fieldName ] . toString ( ) ] = entity ;
116+ entitiesMap [ key . toString ( ) ] = entity ;
107117 } , { } ) ;
108118
109119 return values . map ( ( field ) => entitiesMap [ field . toString ( ) ] || null ) ;
0 commit comments