From e7bbd878d1d2c53bb8f26cb28d496ea2ee293a09 Mon Sep 17 00:00:00 2001 From: Simone Cervini Date: Mon, 15 Jul 2024 16:19:32 +0200 Subject: [PATCH] init --- .../getting-started/adapters/mongodb.mdx | 33 +++++++++---------- packages/adapter-mongodb/src/index.ts | 26 ++++++++++----- packages/adapter-mongodb/test/custom.test.ts | 3 +- packages/adapter-mongodb/test/index.test.ts | 3 +- .../adapter-mongodb/test/serverless.test.ts | 9 +---- 5 files changed, 36 insertions(+), 38 deletions(-) diff --git a/docs/pages/getting-started/adapters/mongodb.mdx b/docs/pages/getting-started/adapters/mongodb.mdx index 4bd9b4e975..a6bd2e87be 100644 --- a/docs/pages/getting-started/adapters/mongodb.mdx +++ b/docs/pages/getting-started/adapters/mongodb.mdx @@ -30,10 +30,10 @@ MONGODB_URI= ```ts filename="./auth.ts" import NextAuth from "next-auth" import { MongoDBAdapter } from "@auth/mongodb-adapter" -import clientPromise from "./lib/db" +import client from "./lib/db" export const { handlers, auth, signIn, signOut } = NextAuth({ - adapter: MongoDBAdapter(clientPromise), + adapter: MongoDBAdapter(client), }) ``` @@ -43,12 +43,12 @@ export const { handlers, auth, signIn, signOut } = NextAuth({ ```ts filename="/src/routes/plugin@auth.ts" import { QwikAuth$ } from "@auth/qwik" import { MongoDBAdapter } from "@auth/mongodb-adapter" -import clientPromise from "./lib/db" +import client from "./lib/db" export const { onRequest, useSession, useSignIn, useSignOut } = QwikAuth$( () => ({ providers: [], - adapter: MongoDBAdapter(clientPromise), + adapter: MongoDBAdapter(client), }) ) ``` @@ -59,10 +59,10 @@ export const { onRequest, useSession, useSignIn, useSignOut } = QwikAuth$( ```ts filename="./src/auth.ts" import { SvelteKitAuth } from "@auth/sveltekit" import { MongoDBAdapter } from "@auth/mongodb-adapter" -import clientPromise from "./lib/db" +import client from "./lib/db" export const { handle, signIn, signOut } = SvelteKitAuth({ - adapter: MongoDBAdapter(clientPromise), + adapter: MongoDBAdapter(client), }) ``` @@ -72,7 +72,7 @@ export const { handle, signIn, signOut } = SvelteKitAuth({ ```ts filename="./src/routes/auth.route.ts" import { ExpressAuth } from "@auth/express" import { MongoDBAdapter } from "@auth/mongodb-adapter" -import clientPromise from "./lib/db" +import client from "./lib/db" const app = express() @@ -81,7 +81,7 @@ app.use( "/auth/*", ExpressAuth({ providers: [], - adapter: MongoDBAdapter(clientPromise), + adapter: MongoDBAdapter(client), }) ) ``` @@ -110,28 +110,25 @@ const options = { }, } -let client -let clientPromise: Promise +let client: MongoClient if (process.env.NODE_ENV === "development") { // In development mode, use a global variable so that the value // is preserved across module reloads caused by HMR (Hot Module Replacement). let globalWithMongo = global as typeof globalThis & { - _mongoClientPromise?: Promise + _mongoClient?: MongoClient } - if (!globalWithMongo._mongoClientPromise) { - client = new MongoClient(uri, options) - globalWithMongo._mongoClientPromise = client.connect() + if (!globalWithMongo._mongoClient) { + globalWithMongo._mongoClient = new MongoClient(uri, options) } - clientPromise = globalWithMongo._mongoClientPromise + client = globalWithMongo._mongoClient } else { // In production mode, it's best to not use a global variable. client = new MongoClient(uri, options) - clientPromise = client.connect() } -// Export a module-scoped MongoClient promise. By doing this in a +// Export a module-scoped MongoClient. By doing this in a // separate module, the client can be shared across functions. -export default clientPromise +export default client ``` diff --git a/packages/adapter-mongodb/src/index.ts b/packages/adapter-mongodb/src/index.ts index a2a92dc853..986c8644d7 100644 --- a/packages/adapter-mongodb/src/index.ts +++ b/packages/adapter-mongodb/src/index.ts @@ -52,9 +52,8 @@ export interface MongoDBAdapterOptions { databaseName?: string /** * Callback function for managing the closing of the MongoDB client. - * This could be useful in serverless environments, especially when `client` - * is provided as a function returning Promise, not just a simple promise. - * It allows for more sophisticated management of database connections, + * This could be useful when `client` is provided as a function returning MongoClient | Promise. + * It allows for more customized management of database connections, * addressing persistence, container reuse, and connection closure issues. */ onClose?: (client: MongoClient) => Promise @@ -108,18 +107,29 @@ export function _id(hex?: string) { export function MongoDBAdapter( /** - * 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`. - * Using a function that returns a `Promise` 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. - * These functions enable either straightforward open-close database connections or more complex caching and connection reuse strategies. + * The MongoDB client. + * + * The MongoDB team recommends providing a non-connected `MongoClient` instance to avoid unhandled promise rejections if the client fails to connect. + * + * Alternatively, you can also pass: + * - A promise that resolves to a connected `MongoClient` (not recommended). + * - A function, to handle more complex and custom connection strategies. + * + * Using a function that returns `MongoClient | Promise`, combined with `options.onClose`, can be useful when you want a more advanced and customized connection strategy to address challenges related to persistence, container reuse, and connection closure. */ - client: Promise | (() => Promise), + client: + | MongoClient + | Promise + | (() => MongoClient | Promise), options: MongoDBAdapterOptions = {} ): Adapter { const { collections } = options const { from, to } = format const getDb = async () => { - const _client = await (typeof client === "function" ? client() : client) + const _client: MongoClient = await (typeof client === "function" + ? client() + : client) const _db = _client.db(options.databaseName) const c = { ...defaultCollections, ...collections } return { diff --git a/packages/adapter-mongodb/test/custom.test.ts b/packages/adapter-mongodb/test/custom.test.ts index acfe357f83..055b089acc 100644 --- a/packages/adapter-mongodb/test/custom.test.ts +++ b/packages/adapter-mongodb/test/custom.test.ts @@ -3,12 +3,11 @@ import { defaultCollections, format, MongoDBAdapter, _id } from "../src" import { MongoClient } from "mongodb" const name = "custom-test" const client = new MongoClient(`mongodb://localhost:27017/${name}`) -const clientPromise = client.connect() const collections = { ...defaultCollections, Users: "some_userz" } runBasicTests({ - adapter: MongoDBAdapter(clientPromise, { + adapter: MongoDBAdapter(client, { collections, }), db: { diff --git a/packages/adapter-mongodb/test/index.test.ts b/packages/adapter-mongodb/test/index.test.ts index 0dc8482049..e8b46d0a55 100644 --- a/packages/adapter-mongodb/test/index.test.ts +++ b/packages/adapter-mongodb/test/index.test.ts @@ -4,10 +4,9 @@ import { MongoClient } from "mongodb" const name = "test" const client = new MongoClient(`mongodb://localhost:27017/${name}`) -const clientPromise = client.connect() runBasicTests({ - adapter: MongoDBAdapter(clientPromise), + adapter: MongoDBAdapter(client), db: { async disconnect() { await client.db().dropDatabase() diff --git a/packages/adapter-mongodb/test/serverless.test.ts b/packages/adapter-mongodb/test/serverless.test.ts index a6ad2d5bfd..73528fd08c 100644 --- a/packages/adapter-mongodb/test/serverless.test.ts +++ b/packages/adapter-mongodb/test/serverless.test.ts @@ -4,9 +4,7 @@ import { MongoClient } from "mongodb" import { expect, test, vi } from "vitest" const name = "serverless-test" -const clientPromise = new MongoClient( - `mongodb://localhost:27017/${name}` -).connect() +const client = new MongoClient(`mongodb://localhost:27017/${name}`) const onClose = vi.fn(async (client: MongoClient) => { await client.close() @@ -29,12 +27,10 @@ runBasicTests({ ), db: { async disconnect() { - const client = await clientPromise await client.db().dropDatabase() await client.close() }, async user(id) { - const client = await clientPromise const user = await client .db() .collection(defaultCollections.Users) @@ -44,7 +40,6 @@ runBasicTests({ return format.from(user) }, async account(provider_providerAccountId) { - const client = await clientPromise const account = await client .db() .collection(defaultCollections.Accounts) @@ -53,7 +48,6 @@ runBasicTests({ return format.from(account) }, async session(sessionToken) { - const client = await clientPromise const session = await client .db() .collection(defaultCollections.Sessions) @@ -62,7 +56,6 @@ runBasicTests({ return format.from(session) }, async verificationToken(identifier_token) { - const client = await clientPromise const token = await client .db() .collection(defaultCollections.VerificationTokens)