|
1 |
| -let mongoose; |
2 |
| -let isConnected = false; |
| 1 | +import mongoose, { Mongoose } from "mongoose"; |
3 | 2 |
|
4 |
| -if (typeof window === "undefined") { |
5 |
| - // Dynamically import mongoose only on the server-side |
6 |
| - mongoose = require("mongoose"); |
| 3 | +const MONGODB_URL = process.env.MONGODB_URL!; |
7 | 4 |
|
8 |
| - const MONGODB_URL = process.env.MONGODB_URL; |
9 |
| - |
10 |
| - interface MongooseConn { |
11 |
| - conn: typeof mongoose | null; |
12 |
| - promise: Promise<typeof mongoose> | null; |
13 |
| - } |
14 |
| - |
15 |
| - let cached: MongooseConn = (global as any).mongoose; |
16 |
| - |
17 |
| - if (!cached) { |
18 |
| - cached = (global as any).mongoose = { |
19 |
| - conn: null, |
20 |
| - promise: null, |
21 |
| - }; |
22 |
| - } |
| 5 | +interface MongooseConn { |
| 6 | + conn: Mongoose | null; |
| 7 | + promise: Promise<Mongoose> | null; |
| 8 | +} |
23 | 9 |
|
24 |
| - export const connect = async () => { |
25 |
| - if (isConnected) return; |
26 |
| - if (cached.conn) { |
27 |
| - isConnected = true; |
28 |
| - return cached.conn; |
29 |
| - } |
| 10 | +let cached: MongooseConn = (global as any).mongoose; |
30 | 11 |
|
31 |
| - if (!MONGODB_URL) { |
32 |
| - throw new Error("MONGODB_URL is not defined"); |
33 |
| - } |
| 12 | +if (!cached) { |
| 13 | + cached = (global as any).mongoose = { |
| 14 | + conn: null, |
| 15 | + promise: null, |
| 16 | + }; |
| 17 | +} |
34 | 18 |
|
35 |
| - try { |
36 |
| - cached.promise = |
37 |
| - cached.promise || |
38 |
| - mongoose.connect(MONGODB_URL, { |
39 |
| - dbName: "nexus", |
40 |
| - bufferCommands: false, |
41 |
| - connectTimeoutMS: 10000, |
42 |
| - }); |
| 19 | +export let isConnected = false; |
43 | 20 |
|
44 |
| - cached.conn = await cached.promise; |
45 |
| - isConnected = true; |
46 |
| - console.log("Database connected successfully"); |
47 |
| - return cached.conn; |
48 |
| - } catch (error) { |
49 |
| - isConnected = false; |
50 |
| - console.error("Database connection error:", error); |
51 |
| - throw error; |
52 |
| - } |
53 |
| - }; |
| 21 | +export const connect = async () => { |
| 22 | + if (isConnected) return; |
| 23 | + if (cached.conn) { |
| 24 | + isConnected = true; |
| 25 | + return cached.conn; |
| 26 | + } |
54 | 27 |
|
55 |
| - export function checkConnection() { |
56 |
| - return isConnected; |
| 28 | + if (!MONGODB_URL) { |
| 29 | + throw new Error("MONGODB_URL is not defined"); |
57 | 30 | }
|
58 |
| -} else { |
59 |
| - // Provide stubs for browser or environments where mongoose cannot be used |
60 |
| - export const connect = async () => { |
61 |
| - console.error("Mongoose cannot be used on the client-side."); |
62 |
| - }; |
63 |
| - export function checkConnection() { |
64 |
| - return false; |
| 31 | + |
| 32 | + try { |
| 33 | + cached.promise = |
| 34 | + cached.promise || |
| 35 | + mongoose.connect(MONGODB_URL, { |
| 36 | + dbName: "nexus", |
| 37 | + bufferCommands: false, |
| 38 | + connectTimeoutMS: 10000, |
| 39 | + }); |
| 40 | + |
| 41 | + cached.conn = await cached.promise; |
| 42 | + isConnected = true; |
| 43 | + console.log("Database connected successfully"); |
| 44 | + return cached.conn; |
| 45 | + } catch (error) { |
| 46 | + isConnected = false; |
| 47 | + console.error("Database connection error:", error); |
| 48 | + throw error; |
65 | 49 | }
|
| 50 | +}; |
| 51 | + |
| 52 | +export function checkConnection() { |
| 53 | + return isConnected; |
66 | 54 | }
|
0 commit comments