refactor(db): replace connectDb import paths and introduce new connect module

- Updated import paths for connectDb across various files to use the new centralized connect module.
- Removed the old connectDb file to streamline the database connection logic.
- Ensured all tests and models reference the new connection method for consistency.
This commit is contained in:
Danny Avila 2025-05-30 13:04:09 -04:00
parent 7cf3f98475
commit eb368fcb70
No known key found for this signature in database
GPG key ID: BF31EEB2C5CA0956
17 changed files with 36 additions and 42 deletions

View file

@ -1,7 +1,7 @@
const { Constants } = require('librechat-data-provider'); const { Constants } = require('librechat-data-provider');
const { initializeFakeClient } = require('./FakeClient'); const { initializeFakeClient } = require('./FakeClient');
jest.mock('~/lib/db/connectDb'); jest.mock('~/db/connect');
jest.mock('~/models', () => ({ jest.mock('~/models', () => ({
User: jest.fn(), User: jest.fn(),
Key: jest.fn(), Key: jest.fn(),
@ -52,7 +52,7 @@ const messageHistory = [
{ {
role: 'user', role: 'user',
isCreatedByUser: true, isCreatedByUser: true,
text: 'What\'s up', text: "What's up",
messageId: '3', messageId: '3',
parentMessageId: '2', parentMessageId: '2',
}, },
@ -456,7 +456,7 @@ describe('BaseClient', () => {
const chatMessages2 = await TestClient.loadHistory(conversationId, '3'); const chatMessages2 = await TestClient.loadHistory(conversationId, '3');
expect(TestClient.currentMessages).toHaveLength(3); expect(TestClient.currentMessages).toHaveLength(3);
expect(chatMessages2[chatMessages2.length - 1].text).toEqual('What\'s up'); expect(chatMessages2[chatMessages2.length - 1].text).toEqual("What's up");
}); });
/* Most of the new sendMessage logic revolving around edited/continued AI messages /* Most of the new sendMessage logic revolving around edited/continued AI messages

View file

@ -5,7 +5,7 @@ const getLogStores = require('~/cache/getLogStores');
const OpenAIClient = require('../OpenAIClient'); const OpenAIClient = require('../OpenAIClient');
jest.mock('meilisearch'); jest.mock('meilisearch');
jest.mock('~/lib/db/connectDb'); jest.mock('~/db/connect');
jest.mock('~/models', () => ({ jest.mock('~/models', () => ({
User: jest.fn(), User: jest.fn(),
Key: jest.fn(), Key: jest.fn(),
@ -462,17 +462,17 @@ describe('OpenAIClient', () => {
role: 'system', role: 'system',
name: 'example_user', name: 'example_user',
content: content:
'Let\'s circle back when we have more bandwidth to touch base on opportunities for increased leverage.', "Let's circle back when we have more bandwidth to touch base on opportunities for increased leverage.",
}, },
{ {
role: 'system', role: 'system',
name: 'example_assistant', name: 'example_assistant',
content: 'Let\'s talk later when we\'re less busy about how to do better.', content: "Let's talk later when we're less busy about how to do better.",
}, },
{ {
role: 'user', role: 'user',
content: content:
'This late pivot means we don\'t have time to boil the ocean for the client deliverable.', "This late pivot means we don't have time to boil the ocean for the client deliverable.",
}, },
]; ];

View file

@ -3,7 +3,7 @@ const { Constants } = require('librechat-data-provider');
const { HumanMessage, AIMessage } = require('@langchain/core/messages'); const { HumanMessage, AIMessage } = require('@langchain/core/messages');
const PluginsClient = require('../PluginsClient'); const PluginsClient = require('../PluginsClient');
jest.mock('~/lib/db/connectDb'); jest.mock('~/db/connect');
jest.mock('~/models/Conversation', () => { jest.mock('~/models/Conversation', () => {
return function () { return function () {
return { return {

View file

@ -13,12 +13,10 @@ const mockPluginService = {
const mockModels = { const mockModels = {
User: mockUser, User: mockUser,
}; };
jest.mock('~/lib/db/connectDb', () => { jest.mock('~/db/connect', () => {
return { return {
connectDb: jest.fn(), connectDb: jest.fn(),
get models() { User: mockModels.mockUser,
return mockModels;
},
}; };
}); });
jest.mock('~/models/File', () => ({ jest.mock('~/models/File', () => ({
@ -60,7 +58,7 @@ describe('Tool Handlers', () => {
}, },
); );
fakeUser = await User.createUser({ fakeUser = await mockModels.User.createUser({
name: 'Fake User', name: 'Fake User',
username: 'fakeuser', username: 'fakeuser',
email: 'fakeuser@example.com', email: 'fakeuser@example.com',
@ -226,7 +224,6 @@ describe('Tool Handlers', () => {
try { try {
await loadTool2(); await loadTool2();
} catch (error) { } catch (error) {
// eslint-disable-next-line jest/no-conditional-expect
expect(error).toBeDefined(); expect(error).toBeDefined();
} }
}); });

View file

@ -6,7 +6,7 @@ const mockModels = {
}, },
}; };
jest.mock('~/lib/db/connectDb', () => { jest.mock('~/db/connect', () => {
return { return {
connectDb: jest.fn(), connectDb: jest.fn(),
get models() { get models() {

View file

@ -42,16 +42,6 @@ async function connectDb(mongoUri = process.env.MONGO_URI) {
return cached.conn; return cached.conn;
} }
function getModels() {
return cached.models;
}
module.exports = { module.exports = {
connectDb, connectDb,
getModels,
get models() {
if (!cached.models) {
throw new Error('Models not registered. ');
}
return cached.models;
},
}; };

8
api/db/index.js Normal file
View file

@ -0,0 +1,8 @@
const mongoose = require('mongoose');
const { createModels } = require('@librechat/data-schemas');
const { connectDb } = require('./connect');
const indexSync = require('./indexSync');
const models = createModels(mongoose);
module.exports = { connectDb, indexSync, ...models };

View file

@ -1,4 +0,0 @@
const { connectDb, getModels} = require('./connectDb');
const indexSync = require('./indexSync');
module.exports = { connectDb, getModels, indexSync };

View file

@ -1,6 +1,6 @@
const mongoose = require('mongoose'); const mongoose = require('mongoose');
const { v4: uuidv4 } = require('uuid'); const { v4: uuidv4 } = require('uuid');
const db = require('~/lib/db/connectDb'); const db = require('db/connect');
jest.mock('mongoose'); jest.mock('mongoose');
@ -35,7 +35,7 @@ const mockModels = {
}, },
}; };
jest.mock('~/lib/db/connectDb', () => { jest.mock('~/db/connect', () => {
return { return {
get models() { get models() {
return mockModels; return mockModels;

View file

@ -1,6 +1,5 @@
const mongoose = require('mongoose'); const mongoose = require('mongoose');
const { createModels, createMethods } = require('@librechat/data-schemas'); const { createMethods } = require('@librechat/data-schemas');
createModels(mongoose);
const methods = createMethods(mongoose); const methods = createMethods(mongoose);
const { comparePassword } = require('./userMethods'); const { comparePassword } = require('./userMethods');
const { const {

View file

@ -1,5 +1,5 @@
const { logger } = require('~/config'); const { logger } = require('~/config');
const db = require('~/lib/db/connectDb'); const db = require('db/connect');
const { createTransaction, createStructuredTransaction } = require('./Transaction'); const { createTransaction, createStructuredTransaction } = require('./Transaction');
/** /**
* Creates up to two transactions to record the spending of tokens. * Creates up to two transactions to record the spending of tokens.

View file

@ -10,7 +10,7 @@ const mongoSanitize = require('express-mongo-sanitize');
const fs = require('fs'); const fs = require('fs');
const cookieParser = require('cookie-parser'); const cookieParser = require('cookie-parser');
const { jwtLogin, passportLogin } = require('~/strategies'); const { jwtLogin, passportLogin } = require('~/strategies');
const { connectDb, indexSync, getModels } = require('~/lib/db'); const { connectDb, indexSync, getModels } = require('db');
const { isEnabled } = require('~/server/utils'); const { isEnabled } = require('~/server/utils');
const { ldapLogin } = require('~/strategies'); const { ldapLogin } = require('~/strategies');
const { logger } = require('~/config'); const { logger } = require('~/config');

View file

@ -38,7 +38,7 @@ class CustomOpenIDStrategy extends OpenIDStrategy {
} }
} }
const db = require('~/lib/db/connectDb'); const db = require('db/connect');
const { getBalanceConfig } = require('~/server/services/Config'); const { getBalanceConfig } = require('~/server/services/Config');
let crypto; let crypto;

View file

@ -17,7 +17,7 @@ const mockModels = {
}, },
}; };
jest.mock('~/lib/db/connectDb', () => { jest.mock('~/db/connect', () => {
return { return {
getModels: jest.fn(() => mockModels), getModels: jest.fn(() => mockModels),
connectDb: jest.fn(), connectDb: jest.fn(),

View file

@ -5,7 +5,7 @@ const moduleAlias = require('module-alias');
const basePath = path.resolve(__dirname, '..', 'api'); const basePath = path.resolve(__dirname, '..', 'api');
moduleAlias.addAlias('~', basePath); moduleAlias.addAlias('~', basePath);
const {connectDb }= require('~/lib/db/connectDb'); const { connectDb } = require('db/connect');
require('./helpers'); require('./helpers');
async function connect() { async function connect() {

View file

@ -1,6 +1,10 @@
import { connectDb, getModels } from '@librechat/backend/lib/db/connectDb'; import { connectDb } from '@librechat/backend/db/connect';
import { findUser, deleteAllUserSessions } from '@librechat/data-schemas'; import {
import { deleteMessages, deleteConvos } from '@librechat/backend/models'; findUser,
deleteConvos,
deleteMessages,
deleteAllUserSessions,
} from '@librechat/backend/models';
type TUser = { email: string; password: string }; type TUser = { email: string; password: string };