Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(updateDocument): corrected bug in cases where matchedCount and modifiedCount did not match #48 #49

Merged
merged 2 commits into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
52 changes: 18 additions & 34 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
export { default as crocks } from 'https://cdn.skypack.dev/[email protected]'
export * as R from 'https://cdn.skypack.dev/[email protected]?dts'

export { EJSON } from 'npm:bson@5.4.0'
export { type Collection, MongoClient } from 'npm:mongodb@5.7.0'
export { EJSON } from 'npm:bson@6.2.0'
export { type Collection, MongoClient } from 'npm:mongodb@6.2.0'
export { default as cuid } from 'npm:[email protected]'

export {
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