mirror of
https://github.com/labring/FastGPT.git
synced 2025-07-22 04:06:18 +00:00

* app list ui * feat: photo view * perf: app dataset filter * perf: app dataset filter * fix: chat recently apps * perf: workflow header phone * default templates * default templates * fix: input guide phone * fix: i18n * team chat history * remove code * perf: mongo connection * log level
62 lines
1.5 KiB
TypeScript
62 lines
1.5 KiB
TypeScript
import { addLog } from '../system/log';
|
|
import { connectionMongo } from './index';
|
|
import type { Mongoose } from 'mongoose';
|
|
|
|
const maxConnecting = Math.max(30, Number(process.env.DB_MAX_LINK || 20));
|
|
|
|
/**
|
|
* connect MongoDB and init data
|
|
*/
|
|
export async function connectMongo({
|
|
beforeHook,
|
|
afterHook
|
|
}: {
|
|
beforeHook?: () => any;
|
|
afterHook?: () => Promise<any>;
|
|
}): Promise<Mongoose> {
|
|
if (connectionMongo.connection.readyState !== 0) {
|
|
return connectionMongo;
|
|
}
|
|
|
|
beforeHook && beforeHook();
|
|
|
|
console.log('mongo start connect');
|
|
try {
|
|
connectionMongo.set('strictQuery', true);
|
|
|
|
connectionMongo.connection.on('error', (error) => {
|
|
console.log('mongo error', error);
|
|
connectionMongo.disconnect();
|
|
});
|
|
connectionMongo.connection.on('disconnected', () => {
|
|
console.log('mongo disconnected');
|
|
});
|
|
|
|
await connectionMongo.connect(process.env.MONGODB_URI as string, {
|
|
bufferCommands: true,
|
|
maxConnecting: maxConnecting,
|
|
maxPoolSize: maxConnecting,
|
|
minPoolSize: 20,
|
|
connectTimeoutMS: 60000,
|
|
waitQueueTimeoutMS: 60000,
|
|
socketTimeoutMS: 60000,
|
|
maxIdleTimeMS: 300000,
|
|
retryWrites: true,
|
|
retryReads: true
|
|
});
|
|
|
|
console.log('mongo connected');
|
|
} catch (error) {
|
|
connectionMongo.disconnect();
|
|
addLog.error('mongo connect error', error);
|
|
}
|
|
|
|
try {
|
|
afterHook && (await afterHook());
|
|
} catch (error) {
|
|
addLog.error('mongo connect after hook error', error);
|
|
}
|
|
|
|
return connectionMongo;
|
|
}
|