Skip to content

Commit

Permalink
fix: add support for mongodb@6
Browse files Browse the repository at this point in the history
  • Loading branch information
darrachequesne committed Jan 8, 2024
1 parent b39f5d5 commit 97e666f
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
Session,
} from "socket.io-adapter";
import { randomBytes } from "crypto";
import { ObjectId, MongoServerError } from "mongodb";
import { ObjectId, MongoServerError, WithId, Document } from "mongodb";
import type { Collection, ChangeStream, ResumeToken } from "mongodb";

const randomId = () => randomBytes(8).toString("hex");
Expand Down Expand Up @@ -821,11 +821,15 @@ export class MongoAdapter extends Adapter {
return Promise.reject("error while fetching session");
}

if (!results[0].value || !results[1]) {
const result = (results[0].ok
? results[0].value // mongodb@5
: results[0]) as unknown as WithId<Document>; // mongodb@6 (https://github.com/mongodb/node-mongodb-native/blob/HEAD/etc/notes/CHANGES_6.0.0.md#findoneandx-family-of-methods-will-now-return-only-the-found-document-or-null-by-default-includeresultmetadata-is-false-by-default)

if (!result || !results[1]) {
return Promise.reject("session or offset not found");
}

const session = results[0].value.data;
const session = result.data;

// could use a sparse index on [_id, nsp, data.opts.rooms, data.opts.except] (only index the documents whose type is EventType.BROADCAST)
const cursor = this.mongoCollection.find({
Expand Down

0 comments on commit 97e666f

Please sign in to comment.