Skip to content

Commit 3f565c7

Browse files
committed
fix(updateDocument): corrected bug in cases where matchedCount and modifiedCount did not match #48
1 parent df6b92f commit 3f565c7

File tree

5 files changed

+29
-2
lines changed

5 files changed

+29
-2
lines changed

adapter.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,8 @@ export function adapter({
221221
.chain(({ name: actualName }) =>
222222
$database(actualName)
223223
.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(
226226
HyperErr({
227227
status: 404,
228228
msg: `Could not update document with _id ${id}`,

clients/atlas-data.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ export class Collection<T extends Document> implements MongoCollectionClient<T>
147147
return this.api<{
148148
matchedCount: number
149149
modifiedCount: number
150+
upsertedCount: number
150151
upsertedId?: string
151152
}>('replaceOne', {
152153
filter,

clients/native.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ class Collection<T extends Document> implements MongoCollectionClient<T> {
8686
): Promise<{
8787
matchedCount: number
8888
modifiedCount: number
89+
upsertedCount: number
8990
upsertedId?: string | undefined
9091
}> {
9192
const res = await this.collection.replaceOne(
@@ -97,6 +98,7 @@ class Collection<T extends Document> implements MongoCollectionClient<T> {
9798
return res as {
9899
matchedCount: number
99100
modifiedCount: number
101+
upsertedCount: number
100102
upsertedId?: string | undefined
101103
}
102104
}

clients/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ export interface MongoCollectionClient<T extends Document> {
4444
): Promise<{
4545
matchedCount: number
4646
modifiedCount: number
47+
upsertedCount: number
4748
upsertedId?: string
4849
}>
4950

test/suite.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,29 @@ async (
222222
await a.removeDatabase(DB)
223223
})
224224

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+
225248
await t.step(
226249
'should return a HyperErr(404) if the database does not exist',
227250
async () => {

0 commit comments

Comments
 (0)