@@ -131,10 +131,11 @@ export interface Model<TSchema extends BaseSchema, TOptions extends SchemaOption
131131 options ?: UpdateOptions
132132 ) => Promise < UpdateResult > ;
133133
134- upsert : (
134+ upsert : < TProjection extends Projection < TSchema > | undefined > (
135135 filter : PaprFilter < TSchema > ,
136- update : PaprUpdateFilter < TSchema >
137- ) => Promise < WithId < TSchema > > ;
136+ update : PaprUpdateFilter < TSchema > ,
137+ options ?: Omit < FindOneAndUpdateOptions , 'projection' | 'upsert' > & { projection ?: TProjection }
138+ ) => Promise < ProjectionType < TSchema , TProjection > > ;
138139}
139140
140141/* eslint-disable @typescript-eslint/ban-types */
@@ -1032,6 +1033,7 @@ export function build<TSchema extends BaseSchema, TOptions extends SchemaOptions
10321033 *
10331034 * @param filter {PaprFilter<TSchema>}
10341035 * @param update {PaprUpdateFilter<TSchema>}
1036+ * @param [options] {FindOneAndUpdateOptions}
10351037 *
10361038 * @returns {Promise<TSchema> }
10371039 *
@@ -1040,19 +1042,29 @@ export function build<TSchema extends BaseSchema, TOptions extends SchemaOptions
10401042 * { firstName: 'John', lastName: 'Wick' },
10411043 * { $set: { age: 40 } }
10421044 * );
1045+ *
1046+ * const userProjected = await User.upsert(
1047+ * { firstName: 'John', lastName: 'Wick' },
1048+ * { $set: { age: 40 } },
1049+ * { projection: { lastName: 1 } }
1050+ * );
1051+ * userProjected.firstName; // TypeScript error
1052+ * userProjected.lastName; // valid
10431053 */
1044- model . upsert = async function upsert (
1054+ model . upsert = async function upsert < TProjection extends Projection < TSchema > | undefined > (
10451055 filter : PaprFilter < TSchema > ,
1046- update : PaprUpdateFilter < TSchema >
1047- ) : Promise < WithId < TSchema > > {
1056+ update : PaprUpdateFilter < TSchema > ,
1057+ options ?: Omit < FindOneAndUpdateOptions , 'projection' | 'upsert' > & { projection ?: TProjection }
1058+ ) : Promise < ProjectionType < TSchema , TProjection > > {
10481059 const item = await model . findOneAndUpdate ( filter , update , {
1060+ ...options ,
10491061 upsert : true ,
10501062 } ) ;
10511063
10521064 if ( ! item ) {
10531065 throw new Error ( 'upsert failed' ) ;
10541066 }
10551067
1056- return item as unknown as WithId < TSchema > ;
1068+ return item ;
10571069 } ;
10581070}
0 commit comments