mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-19 18:00:15 +01:00
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:
parent
7cf3f98475
commit
eb368fcb70
17 changed files with 36 additions and 42 deletions
|
|
@ -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
|
||||||
|
|
|
||||||
|
|
@ -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.",
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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 {
|
||||||
|
|
|
||||||
|
|
@ -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();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
|
||||||
2
api/cache/banViolation.spec.js
vendored
2
api/cache/banViolation.spec.js
vendored
|
|
@ -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() {
|
||||||
|
|
|
||||||
|
|
@ -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
8
api/db/index.js
Normal 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 };
|
||||||
|
|
@ -1,4 +0,0 @@
|
||||||
const { connectDb, getModels} = require('./connectDb');
|
|
||||||
const indexSync = require('./indexSync');
|
|
||||||
|
|
||||||
module.exports = { connectDb, getModels, indexSync };
|
|
||||||
|
|
@ -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;
|
||||||
|
|
|
||||||
|
|
@ -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 {
|
||||||
|
|
|
||||||
|
|
@ -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.
|
||||||
|
|
|
||||||
|
|
@ -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');
|
||||||
|
|
@ -36,7 +36,7 @@ const startServer = async () => {
|
||||||
axios.defaults.headers.common['Accept-Encoding'] = 'gzip';
|
axios.defaults.headers.common['Accept-Encoding'] = 'gzip';
|
||||||
}
|
}
|
||||||
await connectDb();
|
await connectDb();
|
||||||
|
|
||||||
logger.info('Connected to MongoDB');
|
logger.info('Connected to MongoDB');
|
||||||
await indexSync();
|
await indexSync();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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;
|
||||||
|
|
|
||||||
|
|
@ -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(),
|
||||||
|
|
|
||||||
|
|
@ -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() {
|
||||||
|
|
|
||||||
|
|
@ -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 };
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue