lucia-auth / lucia

Authentication, simple and clean
https://lucia-auth.com
MIT License
8.96k stars 460 forks source link

Fix bug with mongoDB #1382

Closed donsn closed 7 months ago

donsn commented 7 months ago

Fixes #1381

MongoDB (mongoose) returns a cursor and toArray() cannot be called directly after calling aggregate

https://mongoosejs.com/docs/api/aggregate.html

donsn commented 7 months ago

Is it okay if we takeout the import {Collection} from 'mongodb' and use mongoose instead? or would a separate adapter exist? const cursor = await this.Session.aggregate([ { $match: { _id: sessionId } }, { $lookup: { from: this.User.collectionName, localField: "user_id", // relies on _id being a String, not ObjectId. foreignField: "_id", as: "userDocs" } } ]); const sessionUsers = await cursor.toArray();

This patch currently works for me

"mongodb": "^6.3.0", "mongoose": "^8.1.0",

otodockal commented 7 months ago

@donsn Isn't it only a mongoose thing? becuase native mongodb driver returns AggregationCursor with toArray method on prototype. it's working for me.

I would not pollute mongodb adapter with a mongoose, honestly. Not everyone using mongoose, me included :-D.

donsn commented 7 months ago

@donsn Isn't it only a mongoose thing? becuase native mongodb driver returns AggregationCursor with toArray method on prototype. it's working for me.

I would not pollute mongodb adapter with a mongoose, honestly. Not everyone using mongoose, me included :-D.

@otodockal Do you think a separate mongoose driver will be fine?

The example on the docs uses mongoose

otodockal commented 7 months ago

@donsn It would make more sense to me if it's not working now because mongoose is not the only existing ODM/ORM, or it can be a custom adapter. For example, I use a pure mongodb driver, I don't want to install mongoose just for this adapter only.

https://lucia-auth.com/database/mongodb

donsn commented 7 months ago

@donsn It would make more sense to me if it's not working now because mongoose is not the only existing ODM/ORM, or it can be a custom adapter. For example, I use a pure mongodb driver, I don't want to install mongoose just for this adapter only.

https://lucia-auth.com/database/mongodb

@otodockal I think a custom adapter works

Because this guide on the docs will continue to break

https://lucia-auth.com/upgrade-v3/mongoose

donsn commented 7 months ago

Closing this for now