-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
changed mongodb driver types, parsing insert results returns inserted count BREAKING CHANGE: parsing inserts no longer accepts a projection and returns inserted count instead of inserted documents
- Loading branch information
1 parent
0079176
commit 41439a1
Showing
9 changed files
with
12,635 additions
and
12,241 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
export { OmitProjection, parseResult } from './parse-result'; | ||
export { parseResult } from './parse-result'; | ||
export { Transaction, withTransaction } from './with-transaction'; | ||
export { throwIfNull } from './throw-if-null'; | ||
export { throwIfDuplicated } from './throw-if-duplicated'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,26 @@ | ||
/* eslint-disable @typescript-eslint/ban-types */ | ||
import omit from 'lodash/omit'; | ||
import keys from 'lodash/keys'; | ||
import { DeleteWriteOpResultObject, FindAndModifyWriteOpResultObject, InsertOneWriteOpResult, InsertWriteOpResult, UpdateWriteOpResult, WithId } from 'mongodb'; | ||
import { DeleteResult, InsertManyResult, InsertOneResult, ModifyResult, UpdateResult, Document } from 'mongodb'; | ||
|
||
type OptionalKeys<T> = { [K in keyof T]: {} extends Pick<T, K> ? K : never }[keyof T]; | ||
|
||
export type OmitProjection<T> = { [K in OptionalKeys<T>]?: 0 }; | ||
|
||
export async function parseResult<T>(promise: Promise<InsertOneWriteOpResult<WithId<T>>>, projection?: OmitProjection<WithId<T>>): Promise<T>; | ||
export async function parseResult<T>(promise: Promise<InsertWriteOpResult<WithId<T>>>, projection?: OmitProjection<WithId<T>>): Promise<T[]>; | ||
export async function parseResult<T>(promise: Promise<FindAndModifyWriteOpResultObject<T>>): Promise<T | null>; | ||
export async function parseResult(promise: Promise<UpdateWriteOpResult>): Promise<number>; | ||
export async function parseResult(promise: Promise<DeleteWriteOpResultObject>): Promise<number>; | ||
export async function parseResult<T extends { _id: unknown }>(promise: Promise<InsertOneResult<T>>): Promise<number>; | ||
export async function parseResult<T extends { _id: unknown }>(promise: Promise<InsertManyResult<T>>): Promise<number>; | ||
export async function parseResult<T>(promise: Promise<ModifyResult<T>>): Promise<T | null>; | ||
export async function parseResult(promise: Promise<UpdateResult | Document>): Promise<number>; | ||
export async function parseResult(promise: Promise<DeleteResult>): Promise<number>; | ||
|
||
export async function parseResult<T>( | ||
promise: | ||
| Promise<InsertOneWriteOpResult<WithId<T>>> | ||
| Promise<InsertWriteOpResult<WithId<T>>> | ||
| Promise<FindAndModifyWriteOpResultObject<T>> | ||
| Promise<UpdateWriteOpResult> | ||
| Promise<DeleteWriteOpResultObject>, | ||
projection?: OmitProjection<WithId<T>>, | ||
): Promise<T | T[] | number | null> { | ||
promise: Promise<InsertOneResult<T> | InsertManyResult<T> | ModifyResult<T> | Document | UpdateResult | DeleteResult>, | ||
): Promise<number | T | null> { | ||
const result = await promise; | ||
if ('modifiedCount' in result) { | ||
return result.modifiedCount; | ||
} | ||
if ('deletedCount' in result) { | ||
return result.deletedCount; | ||
} | ||
if ('insertedId' in result) { | ||
if (projection === undefined) { | ||
return result.ops[0] as T; | ||
} | ||
return omit(result.ops[0], keys(projection)) as T; | ||
return 1; | ||
} | ||
if ('insertedIds' in result) { | ||
if (projection === undefined) { | ||
return result.ops as T[]; | ||
} | ||
return result.ops.map(op => omit(op, keys(projection)) as T); | ||
} | ||
if ('result' in result) { | ||
return result.deletedCount as number; | ||
return result.insertedCount; | ||
} | ||
return result.value ?? null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters