Skip to content

Commit

Permalink
fix insert operation return types (#398)
Browse files Browse the repository at this point in the history
* fix insert types

* undo readme edit

* add ignore ban type

* format

* pass test
  • Loading branch information
hazelnutcloud authored Aug 14, 2023
1 parent 6e27e38 commit b4f6ad8
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 8 deletions.
6 changes: 3 additions & 3 deletions src/collection/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,16 +157,16 @@ export class Collection<T extends Document> {
docs: InsertDocument<T> | InsertDocument<T>[],
options?: InsertOptions,
) {
docs = Array.isArray(docs) ? docs : [docs];
return this.insertMany(docs, options);
const _docs = Array.isArray(docs) ? docs : [docs];
return this.insertMany(_docs, options);
}

async insertMany(
docs: InsertDocument<T>[],
options?: InsertOptions,
): Promise<
{
insertedIds: (ObjectId | Required<InsertDocument<T>>["_id"])[];
insertedIds: Required<InsertDocument<T>>["_id"][];
insertedCount: number;
}
> {
Expand Down
2 changes: 1 addition & 1 deletion src/protocol/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ export function deserializeMessage(
function parseDocuments(buffer: Uint8Array): Document[] {
let pos = 0;
const docs = [];
const view = new DataView(buffer);
const view = new DataView(buffer.buffer);
while (pos < buffer.byteLength) {
const docLen = view.getInt32(pos, true);
const doc = deserialize(buffer.slice(pos, pos + docLen));
Expand Down
9 changes: 5 additions & 4 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -797,10 +797,11 @@ type Flatten<T> = T extends Array<infer Item> ? Item : T;
type IsAny<T, Y, N> = 0 extends (1 & T) ? Y : N;

export type InsertDocument<TDocument extends Document> =
& Omit<TDocument, "_id">
& {
_id?: TDocument["_id"] | ObjectId;
};
Extract<TDocument["_id"], ObjectId> extends ObjectId
? Omit<TDocument, "_id"> & { _id?: TDocument["_id"] }
// deno-lint-ignore ban-types
: TDocument["_id"] extends {} ? TDocument
: TDocument & { _id?: ObjectId };

type KeysOfType<T, Type> = {
[Key in keyof T]: NonNullable<T[Key]> extends Type ? Key : never;
Expand Down

0 comments on commit b4f6ad8

Please sign in to comment.