You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: packages/adapter-mongodb/src/index.ts
+67-40Lines changed: 67 additions & 40 deletions
Original file line number
Diff line number
Diff line change
@@ -25,6 +25,16 @@ import type {
25
25
}from"@auth/core/adapters"
26
26
importtype{MongoClient}from"mongodb"
27
27
28
+
/**
29
+
* This adapter uses https://www.typescriptlang.org/docs/handbook/release-notes/typescript-5-2.html#using-declarations-and-explicit-resource-management.
30
+
* This feature is very new and requires runtime polyfills for `Symbol.asyncDispose` in order to work properly in all environments.
31
+
* It is also required to set in the `tsconfig.json` file the compilation target to `es2022` or below and configure the `lib` option to include `esnext` or `esnext.disposable`.
32
+
*
33
+
* You can find more information about this feature and the polyfills in the link above.
34
+
*/
35
+
// @ts-expect-error read only property is not assignable
* The name you want to give to the MongoDB database
41
51
*/
42
52
databaseName?: string
53
+
/**
54
+
* Callback function for managing the closing of the MongoDB client.
55
+
* This could be useful in serverless environments, especially when `client`
56
+
* is provided as a function returning Promise<MongoClient>, not just a simple promise.
57
+
* It allows for more sophisticated management of database connections,
58
+
* addressing persistence, container reuse, and connection closure issues.
59
+
*/
60
+
onClose?: (client: MongoClient)=>Promise<void>
43
61
}
44
62
45
63
exportconstdefaultCollections: Required<
@@ -89,83 +107,95 @@ export function _id(hex?: string) {
89
107
}
90
108
91
109
exportfunctionMongoDBAdapter(
92
-
client: Promise<MongoClient>,
110
+
/**
111
+
* The MongoDB client. You can either pass a promise that resolves to a `MongoClient` or a function that returns a promise that resolves to a `MongoClient`.
112
+
* Using a function that returns a `Promise<MongoClient>` could be useful in serverless environments, particularly when combined with `options.onClose`, to efficiently handle database connections and address challenges with persistence, container reuse, and connection closure.
113
+
* These functions enable either straightforward open-close database connections or more complex caching and connection reuse strategies.
0 commit comments