File tree Expand file tree Collapse file tree 5 files changed +29
-2
lines changed Expand file tree Collapse file tree 5 files changed +29
-2
lines changed Original file line number Diff line number Diff line change @@ -221,8 +221,8 @@ export function adapter({
221
221
. chain ( ( { name : actualName } ) =>
222
222
$database ( actualName )
223
223
. replaceOne ( { _id : id } , doc , { upsert : true } )
224
- . chain ( ( { matchedCount, modifiedCount } ) =>
225
- matchedCount + modifiedCount === 2 ? Async . Resolved ( { ok : true , id } ) : Async . Rejected (
224
+ . chain ( ( { matchedCount, upsertedCount } ) =>
225
+ upsertedCount || matchedCount ? Async . Resolved ( { ok : true , id } ) : Async . Rejected (
226
226
HyperErr ( {
227
227
status : 404 ,
228
228
msg : `Could not update document with _id ${ id } ` ,
Original file line number Diff line number Diff line change @@ -147,6 +147,7 @@ export class Collection<T extends Document> implements MongoCollectionClient<T>
147
147
return this . api < {
148
148
matchedCount : number
149
149
modifiedCount : number
150
+ upsertedCount : number
150
151
upsertedId ?: string
151
152
} > ( 'replaceOne' , {
152
153
filter,
Original file line number Diff line number Diff line change @@ -86,6 +86,7 @@ class Collection<T extends Document> implements MongoCollectionClient<T> {
86
86
) : Promise < {
87
87
matchedCount : number
88
88
modifiedCount : number
89
+ upsertedCount : number
89
90
upsertedId ?: string | undefined
90
91
} > {
91
92
const res = await this . collection . replaceOne (
@@ -97,6 +98,7 @@ class Collection<T extends Document> implements MongoCollectionClient<T> {
97
98
return res as {
98
99
matchedCount : number
99
100
modifiedCount : number
101
+ upsertedCount : number
100
102
upsertedId ?: string | undefined
101
103
}
102
104
}
Original file line number Diff line number Diff line change @@ -44,6 +44,7 @@ export interface MongoCollectionClient<T extends Document> {
44
44
) : Promise < {
45
45
matchedCount : number
46
46
modifiedCount : number
47
+ upsertedCount : number
47
48
upsertedId ?: string
48
49
} >
49
50
Original file line number Diff line number Diff line change @@ -222,6 +222,29 @@ async (
222
222
await a . removeDatabase ( DB )
223
223
} )
224
224
225
+ await t . step ( 'should noop if the document was not modified' , async ( ) => {
226
+ await a . createDatabase ( DB )
227
+
228
+ await a . createDocument ( {
229
+ db : DB ,
230
+ id : 'foobar' ,
231
+ doc : { foo : 'bar' } ,
232
+ } )
233
+
234
+ // Same document shape
235
+ const noop = await a . updateDocument ( {
236
+ db : DB ,
237
+ id : 'foobar' ,
238
+ doc : { foo : 'bar' } ,
239
+ } )
240
+
241
+ assertObjectMatch ( noop as any , { ok : true , id : 'foobar' } )
242
+ const res = await a . retrieveDocument ( { db : DB , id : 'foobar' } )
243
+ assertObjectMatch ( res as any , { _id : 'foobar' , foo : 'bar' } )
244
+
245
+ await a . removeDatabase ( DB )
246
+ } )
247
+
225
248
await t . step (
226
249
'should return a HyperErr(404) if the database does not exist' ,
227
250
async ( ) => {
You can’t perform that action at this time.
0 commit comments