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 insert operation return types #398

Merged
merged 5 commits into from
Aug 14, 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
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];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why did you make this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typescript was complaining that the types were incompatible so had to declare a new variable

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
Loading