@@ -3,6 +3,8 @@ const { MongoClient } = MONGO
33function wait ( time : number ) { return new Promise ( ( res ) => setTimeout ( res , time ) ) }
44
55export type ObjectId = MONGO . Bson . ObjectId
6+ export type InsertId < Type > = Type & { _id : ObjectId }
7+
68export default class Model < T > {
79 public client : MONGO . MongoClient
810 public db ?: MONGO . Database
@@ -61,9 +63,9 @@ export default class Model <T> {
6163 this . Model = this . db . collection ( this . collectionName )
6264 }
6365
64- public async insert ( document : MONGO . InsertDocument < T > , InsertOptions ?: MONGO . InsertOptions ) : Promise < MONGO . Bson . ObjectId >
65- public async insert ( document : MONGO . InsertDocument < T > [ ] , InsertOptions ?: MONGO . InsertOptions ) : Promise < MONGO . Bson . ObjectId [ ] >
66- public async insert ( document : MONGO . InsertDocument < T > [ ] | MONGO . InsertDocument < T > , InsertOptions ?: MONGO . InsertOptions ) : Promise < MONGO . Bson . ObjectId | Required < MONGO . InsertDocument < T > > [ "_id" ] | ( MONGO . Bson . ObjectId | Required < MONGO . InsertDocument < T > > [ "_id" ] ) [ ] > {
66+ public async insert ( document : MONGO . InsertDocument < T > , InsertOptions ?: MONGO . InsertOptions ) : Promise < ObjectId >
67+ public async insert ( document : MONGO . InsertDocument < T > [ ] , InsertOptions ?: MONGO . InsertOptions ) : Promise < ObjectId [ ] >
68+ public async insert ( document : MONGO . InsertDocument < T > [ ] | MONGO . InsertDocument < T > , InsertOptions ?: MONGO . InsertOptions ) : Promise < ObjectId | Required < MONGO . InsertDocument < T > > [ "_id" ] | ( ObjectId | Required < MONGO . InsertDocument < T > > [ "_id" ] ) [ ] > {
6769 const Model = await this . getModel ( )
6870 if ( Array . isArray ( document ) ) {
6971 return ( await Model . insertMany ( document , InsertOptions ) ) . insertedIds
@@ -72,9 +74,9 @@ export default class Model <T> {
7274 }
7375 }
7476
75- public async select ( filter ?: MONGO . Filter < T > , options ?: MONGO . FindOptions & { multiple ?: true , populate ?: { [ collection : string ] : string | string [ ] } } ) : Promise < ( T & { _id : MONGO . Bson . ObjectId } ) [ ] >
76- public async select ( filter ?: MONGO . Filter < T > , options ?: MONGO . FindOptions & { multiple ?: false , populate ?: { [ collection : string ] : string | string [ ] } } ) : Promise < T & { _id : MONGO . Bson . ObjectId } | undefined >
77- public async select ( filter ?: MONGO . Filter < T > , options ?: MONGO . FindOptions & { multiple ?: boolean , populate ?: { [ collection : string ] : string | string [ ] } } ) : Promise < T | undefined | T [ ] > {
77+ public async select < Populate > ( filter ?: MONGO . Filter < T > , options ?: MONGO . FindOptions & { multiple ?: true , populate ?: { [ collection : string ] : string | string [ ] } } ) : Promise < ( T & { _id : ObjectId } & ( Populate & { _id : ObjectId } ) ) [ ] >
78+ public async select < Populate > ( filter ?: MONGO . Filter < T > , options ?: MONGO . FindOptions & { multiple ?: false , populate ?: { [ collection : string ] : string | string [ ] } } ) : Promise < T & { _id : ObjectId } & ( Populate & { _id : ObjectId } ) | undefined >
79+ public async select < Populate > ( filter ?: MONGO . Filter < T > , options ?: MONGO . FindOptions & { multiple ?: boolean , populate ?: { [ collection : string ] : string | string [ ] } } ) : Promise < T & ( Populate & { _id : ObjectId } ) | undefined | ( T & ( Populate & { _id : ObjectId } ) ) [ ] > {
7880 const Model = await this . getModel ( )
7981 const _options = Object . assign ( { } , options )
8082 const populate = _options . populate
@@ -114,7 +116,7 @@ export default class Model <T> {
114116 } ) ( doc , path )
115117 pointers . forEach ( ( _p : any ) => {
116118 if ( Array . isArray ( _p [ pointer ] ) ) {
117- const promise = Promise . all ( _p [ pointer ] . map ( ( _id : MONGO . Bson . ObjectId ) => new Promise ( ( res ) => {
119+ const promise = Promise . all ( _p [ pointer ] . map ( ( _id : ObjectId ) => new Promise ( ( res ) => {
118120 res ( this . db ! . collection ( collection ) . findOne ( { _id } ) )
119121 } ) ) ) . then ( ( data ) => _p [ pointer ] = data )
120122 promises . push ( < any > promise )
@@ -133,11 +135,11 @@ export default class Model <T> {
133135 } )
134136 }
135137 await Promise . all ( promises )
136- return document
138+ return < T & ( Populate & { _id : ObjectId } ) | undefined | ( T & ( Populate & { _id : ObjectId } ) ) [ ] > document
137139 }
138140
139- public async update ( filter : MONGO . Filter < T > , document : Partial < T > & MONGO . Bson . Document , options ?: MONGO . FindOptions & { multiple ?: true } ) : Promise < MONGO . Bson . ObjectId [ ] >
140- public async update ( filter : MONGO . Filter < T > , document : Partial < T > & MONGO . Bson . Document , options ?: MONGO . FindOptions & { multiple ?: false } ) : Promise < MONGO . Bson . ObjectId | unknown >
141+ public async update ( filter : MONGO . Filter < T > , document : Partial < T > & MONGO . Bson . Document , options ?: MONGO . FindOptions & { multiple ?: true } ) : Promise < ObjectId [ ] >
142+ public async update ( filter : MONGO . Filter < T > , document : Partial < T > & MONGO . Bson . Document , options ?: MONGO . FindOptions & { multiple ?: false } ) : Promise < ObjectId | unknown >
141143 public async update ( filter : MONGO . Filter < T > , document : Partial < T > & MONGO . Bson . Document , options ?: MONGO . FindOptions & { multiple ?: boolean } ) {
142144 const Model = await this . getModel ( )
143145 const _options = Object . assign ( { } , options )
@@ -154,9 +156,9 @@ export default class Model <T> {
154156 }
155157 }
156158
157- public async delete ( filter ?: MONGO . Filter < T > , options ?: MONGO . FindOptions & { multiple ?: true } ) : Promise < MONGO . Bson . ObjectId [ ] >
158- public async delete ( filter ?: MONGO . Filter < T > , options ?: MONGO . FindOptions & { multiple ?: false } ) : Promise < MONGO . Bson . ObjectId | unknown >
159- public async delete ( filter ?: MONGO . Filter < T > , options ?: MONGO . FindOptions & { multiple ?: boolean } ) : Promise < MONGO . Bson . ObjectId | unknown | MONGO . Bson . ObjectId [ ] > {
159+ public async delete ( filter ?: MONGO . Filter < T > , options ?: MONGO . FindOptions & { multiple ?: true } ) : Promise < ObjectId [ ] >
160+ public async delete ( filter ?: MONGO . Filter < T > , options ?: MONGO . FindOptions & { multiple ?: false } ) : Promise < ObjectId | unknown >
161+ public async delete ( filter ?: MONGO . Filter < T > , options ?: MONGO . FindOptions & { multiple ?: boolean } ) : Promise < ObjectId | unknown | ObjectId [ ] > {
160162 const Model = await this . getModel ( )
161163 const _options = Object . assign ( { } , options )
162164 if ( _options . multiple || _options ?. multiple === undefined ) {
0 commit comments