ghostguild-org/server/utils/mongoose.js
Jennie Robinson Faber 6bfb078e45
Some checks failed
Test / vitest (push) Successful in 11m8s
Test / playwright (push) Failing after 6m57s
Test / visual (push) Failing after 6m47s
Test / Notify on failure (push) Successful in 2s
fix(mongoose): fall back to process.env when run outside Nitro
connectDB() called useRuntimeConfig() unconditionally — works inside
the Nuxt/Nitro runtime but throws ReferenceError for standalone Node
scripts (seed-members.js, seed-tags.js, etc.). CI exposed this when
trying to run seed-all.js. Detect the auto-import and fall back to
process.env when it's not available; preserves Nitro behavior.
2026-05-01 10:46:47 +01:00

27 lines
No EOL
602 B
JavaScript

import mongoose from 'mongoose';
let isConnected = false;
export const connectDB = async () => {
if (isConnected) {
return;
}
const MONGODB_URI =
typeof useRuntimeConfig === 'function'
? useRuntimeConfig().mongodbUri
: process.env.NUXT_MONGODB_URI || process.env.MONGODB_URI;
try {
await mongoose.connect(MONGODB_URI, {
serverSelectionTimeoutMS: 5000,
});
isConnected = true;
console.log('MongoDB connected successfully');
} catch (error) {
console.error('MongoDB connection error:', error);
throw error;
}
};
export default connectDB;