From 3f565c7937b4478434866149f89fdd1574f1ed00 Mon Sep 17 00:00:00 2001 From: Tyler Hall Date: Fri, 27 Oct 2023 15:24:39 +0000 Subject: [PATCH] fix(updateDocument): corrected bug in cases where matchedCount and modifiedCount did not match #48 --- adapter.ts | 4 ++-- clients/atlas-data.ts | 1 + clients/native.ts | 2 ++ clients/types.ts | 1 + test/suite.ts | 23 +++++++++++++++++++++++ 5 files changed, 29 insertions(+), 2 deletions(-) diff --git a/adapter.ts b/adapter.ts index a661cb9..52e99c4 100644 --- a/adapter.ts +++ b/adapter.ts @@ -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}`, diff --git a/clients/atlas-data.ts b/clients/atlas-data.ts index 229117a..bbdbf95 100644 --- a/clients/atlas-data.ts +++ b/clients/atlas-data.ts @@ -147,6 +147,7 @@ export class Collection implements MongoCollectionClient return this.api<{ matchedCount: number modifiedCount: number + upsertedCount: number upsertedId?: string }>('replaceOne', { filter, diff --git a/clients/native.ts b/clients/native.ts index 8f5cb0a..0054f3d 100644 --- a/clients/native.ts +++ b/clients/native.ts @@ -86,6 +86,7 @@ class Collection implements MongoCollectionClient { ): Promise<{ matchedCount: number modifiedCount: number + upsertedCount: number upsertedId?: string | undefined }> { const res = await this.collection.replaceOne( @@ -97,6 +98,7 @@ class Collection implements MongoCollectionClient { return res as { matchedCount: number modifiedCount: number + upsertedCount: number upsertedId?: string | undefined } } diff --git a/clients/types.ts b/clients/types.ts index 60844aa..a4862ff 100644 --- a/clients/types.ts +++ b/clients/types.ts @@ -44,6 +44,7 @@ export interface MongoCollectionClient { ): Promise<{ matchedCount: number modifiedCount: number + upsertedCount: number upsertedId?: string }> diff --git a/test/suite.ts b/test/suite.ts index a4f56a5..27095f3 100644 --- a/test/suite.ts +++ b/test/suite.ts @@ -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 () => {