Skip to content

Commit

Permalink
fix(updateDocument): corrected bug in cases where matchedCount and mo…
Browse files Browse the repository at this point in the history
…difiedCount did not match #48
  • Loading branch information
TillaTheHun0 committed Oct 27, 2023
1 parent df6b92f commit 3f565c7
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 2 deletions.
4 changes: 2 additions & 2 deletions adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,8 @@ export function adapter({
.chain(({ name: actualName }) =>
$database(actualName)
.replaceOne({ _id: id }, doc, { upsert: true })
.chain(({ matchedCount, modifiedCount }) =>
matchedCount + modifiedCount === 2 ? Async.Resolved({ ok: true, id }) : Async.Rejected(
.chain(({ matchedCount, upsertedCount }) =>
upsertedCount || matchedCount ? Async.Resolved({ ok: true, id }) : Async.Rejected(
HyperErr({
status: 404,
msg: `Could not update document with _id ${id}`,
Expand Down
1 change: 1 addition & 0 deletions clients/atlas-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ export class Collection<T extends Document> implements MongoCollectionClient<T>
return this.api<{
matchedCount: number
modifiedCount: number
upsertedCount: number
upsertedId?: string
}>('replaceOne', {
filter,
Expand Down
2 changes: 2 additions & 0 deletions clients/native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ class Collection<T extends Document> implements MongoCollectionClient<T> {
): Promise<{
matchedCount: number
modifiedCount: number
upsertedCount: number
upsertedId?: string | undefined
}> {
const res = await this.collection.replaceOne(
Expand All @@ -97,6 +98,7 @@ class Collection<T extends Document> implements MongoCollectionClient<T> {
return res as {
matchedCount: number
modifiedCount: number
upsertedCount: number
upsertedId?: string | undefined
}
}
Expand Down
1 change: 1 addition & 0 deletions clients/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export interface MongoCollectionClient<T extends Document> {
): Promise<{
matchedCount: number
modifiedCount: number
upsertedCount: number
upsertedId?: string
}>

Expand Down
23 changes: 23 additions & 0 deletions test/suite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,29 @@ async (
await a.removeDatabase(DB)
})

await t.step('should noop if the document was not modified', async () => {
await a.createDatabase(DB)

await a.createDocument({
db: DB,
id: 'foobar',
doc: { foo: 'bar' },
})

// Same document shape
const noop = await a.updateDocument({
db: DB,
id: 'foobar',
doc: { foo: 'bar' },
})

assertObjectMatch(noop as any, { ok: true, id: 'foobar' })
const res = await a.retrieveDocument({ db: DB, id: 'foobar' })
assertObjectMatch(res as any, { _id: 'foobar', foo: 'bar' })

await a.removeDatabase(DB)
})

await t.step(
'should return a HyperErr(404) if the database does not exist',
async () => {
Expand Down

0 comments on commit 3f565c7

Please sign in to comment.