|
| 1 | +// loaders/mongoose.js |
1 | 2 | import mongoose from 'mongoose'; |
2 | 3 | import { mongo as mongoConfig } from '../config.js'; |
3 | 4 | import { logger } from '../utils/logger.js'; |
4 | 5 |
|
| 6 | +// Important for compatibility with Mongoose v8+ |
| 7 | +// Prevents strict query errors when filtering by fields not in schema |
| 8 | +mongoose.set('strictQuery', false); |
| 9 | + |
5 | 10 | const log = logger.extend('mongoose'); |
6 | 11 |
|
| 12 | +// Cache for connections |
7 | 13 | const mongooseConnections = {}; |
8 | 14 |
|
| 15 | +/** |
| 16 | + * Returns a Mongoose connection for a given server and db name. |
| 17 | + * Caches connections for reuse across calls. |
| 18 | + */ |
9 | 19 | const getMongoose = async ({ server = mongoConfig.host, db } = {}) => { |
10 | | - try { |
11 | | - if (db === undefined) { |
12 | | - throw Error('missing db name'); |
13 | | - } |
14 | | - if (mongooseConnections[server] && mongooseConnections[server][db]) { |
15 | | - log(`using connection ${server}${db}`); |
16 | | - const connection = await mongooseConnections[server][db]; |
17 | | - return connection; |
18 | | - } |
19 | | - log(`creating connection ${server}${db}`); |
20 | | - mongooseConnections[server] = mongooseConnections[server] || {}; |
21 | | - mongooseConnections[server][db] = mongoose |
22 | | - .createConnection(`${server}${db}`, { |
23 | | - useNewUrlParser: true, |
24 | | - useUnifiedTopology: true, |
25 | | - autoIndex: mongoConfig.createIndex || false, |
26 | | - }) |
27 | | - .asPromise(); |
28 | | - const connection = await mongooseConnections[server][db]; |
29 | | - log(`opened connection ${server}${db}`); |
30 | | - return connection; |
31 | | - } catch (error) { |
32 | | - log('getMongoose', error); |
33 | | - throw error; |
| 20 | + if (!db) throw new Error('Missing db name'); |
| 21 | + |
| 22 | + // Return existing connection if present |
| 23 | + if (mongooseConnections[server]?.[db]) { |
| 24 | + log(`Using cached connection: ${server}${db}`); |
| 25 | + return mongooseConnections[server][db]; |
34 | 26 | } |
| 27 | + |
| 28 | + log(`Creating new connection: ${server}${db}`); |
| 29 | + const uri = `${server}${db}`; |
| 30 | + const connection = mongoose.createConnection(uri, { |
| 31 | + // Removed deprecated options |
| 32 | + // useNewUrlParser and useUnifiedTopology are defaults in Mongoose 8+ |
| 33 | + |
| 34 | + autoIndex: mongoConfig.createIndex ?? false, // Create indexes if needed |
| 35 | + autoCreate: true, // Ensure collections are auto-created |
| 36 | + bufferCommands: false, // ❗ Important for strict connection behavior in Mongoose 8+ |
| 37 | + }); |
| 38 | + |
| 39 | + // Cache the connection |
| 40 | + mongooseConnections[server] = mongooseConnections[server] || {}; |
| 41 | + mongooseConnections[server][db] = connection; |
| 42 | + |
| 43 | + // Return promise that resolves once connected |
| 44 | + return new Promise((resolve, reject) => { |
| 45 | + connection.once('open', () => { |
| 46 | + log(`Connected to ${server}${db}`); |
| 47 | + resolve(connection); |
| 48 | + }); |
| 49 | + |
| 50 | + connection.on('error', (err) => { |
| 51 | + log(`Connection error on ${server}${db}:`, err); |
| 52 | + reject(err); |
| 53 | + }); |
| 54 | + }); |
35 | 55 | }; |
36 | 56 |
|
37 | 57 | export { getMongoose }; |
0 commit comments