mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-17 00:40:14 +01:00
move db functions
This commit is contained in:
parent
586c162404
commit
4f5ee8b198
5 changed files with 80 additions and 69 deletions
|
|
@ -17,7 +17,7 @@ if (!cached) {
|
||||||
cached = global.mongoose = { conn: null, promise: null };
|
cached = global.mongoose = { conn: null, promise: null };
|
||||||
}
|
}
|
||||||
|
|
||||||
async function dbConnect() {
|
async function connectDb() {
|
||||||
if (cached.conn) {
|
if (cached.conn) {
|
||||||
return cached.conn;
|
return cached.conn;
|
||||||
}
|
}
|
||||||
|
|
@ -41,4 +41,4 @@ async function dbConnect() {
|
||||||
return cached.conn;
|
return cached.conn;
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = dbConnect;
|
module.exports = connectDb;
|
||||||
62
api/lib/db/migrateDb.js
Normal file
62
api/lib/db/migrateDb.js
Normal file
|
|
@ -0,0 +1,62 @@
|
||||||
|
const mongoose = require('mongoose');
|
||||||
|
const { Conversation, } = require('../../models/Conversation');
|
||||||
|
const { getMessages, } = require('../../models/');
|
||||||
|
|
||||||
|
async function migrateDb() {
|
||||||
|
try {
|
||||||
|
const conversations = await Conversation.find({ model: null }).exec();
|
||||||
|
|
||||||
|
if (!conversations || conversations.length === 0)
|
||||||
|
return { message: '[Migrate] No conversations to migrate' };
|
||||||
|
|
||||||
|
for (let convo of conversations) {
|
||||||
|
const messages = await getMessages({
|
||||||
|
conversationId: convo.conversationId,
|
||||||
|
messageId: { $exists: false }
|
||||||
|
});
|
||||||
|
|
||||||
|
let model;
|
||||||
|
let oldId;
|
||||||
|
const promises = [];
|
||||||
|
messages.forEach((message, i) => {
|
||||||
|
const msgObj = message.toObject();
|
||||||
|
const newId = msgObj.id;
|
||||||
|
if (i === 0) {
|
||||||
|
message.parentMessageId = '00000000-0000-0000-0000-000000000000';
|
||||||
|
} else {
|
||||||
|
message.parentMessageId = oldId;
|
||||||
|
}
|
||||||
|
|
||||||
|
oldId = newId;
|
||||||
|
message.messageId = newId;
|
||||||
|
if (message.sender.toLowerCase() !== 'user' && !model) {
|
||||||
|
model = message.sender.toLowerCase();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (message.sender.toLowerCase() === 'user') {
|
||||||
|
message.isCreatedByUser = true;
|
||||||
|
}
|
||||||
|
promises.push(message.save());
|
||||||
|
});
|
||||||
|
await Promise.all(promises);
|
||||||
|
|
||||||
|
await Conversation.findOneAndUpdate(
|
||||||
|
{ conversationId: convo.conversationId },
|
||||||
|
{ model },
|
||||||
|
{ new: true }
|
||||||
|
).exec();
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
await mongoose.connection.db.collection('messages').dropIndex('id_1');
|
||||||
|
} catch (error) {
|
||||||
|
console.log("[Migrate] Index doesn't exist or already dropped");
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.log(error);
|
||||||
|
return { message: '[Migrate] Error migrating conversations' };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
module.exports = migrateDb;
|
||||||
|
|
@ -1,5 +1,4 @@
|
||||||
const mongoose = require('mongoose');
|
const mongoose = require('mongoose');
|
||||||
const crypto = require('crypto');
|
|
||||||
const { getMessages, deleteMessages } = require('./Message');
|
const { getMessages, deleteMessages } = require('./Message');
|
||||||
|
|
||||||
const convoSchema = mongoose.Schema(
|
const convoSchema = mongoose.Schema(
|
||||||
|
|
@ -65,13 +64,14 @@ const getConvo = async (user, conversationId) => {
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
Conversation,
|
||||||
saveConvo: async (user, { conversationId, newConversationId, title, ...convo }) => {
|
saveConvo: async (user, { conversationId, newConversationId, title, ...convo }) => {
|
||||||
try {
|
try {
|
||||||
const messages = await getMessages({ conversationId });
|
const messages = await getMessages({ conversationId });
|
||||||
const update = { ...convo, messages };
|
const update = { ...convo, messages };
|
||||||
if (title) {
|
if (title) {
|
||||||
update.title = title;
|
update.title = title;
|
||||||
update.user = user
|
update.user = user;
|
||||||
}
|
}
|
||||||
if (newConversationId) {
|
if (newConversationId) {
|
||||||
update.conversationId = newConversationId;
|
update.conversationId = newConversationId;
|
||||||
|
|
@ -97,9 +97,13 @@ module.exports = {
|
||||||
},
|
},
|
||||||
updateConvo: async (user, { conversationId, ...update }) => {
|
updateConvo: async (user, { conversationId, ...update }) => {
|
||||||
try {
|
try {
|
||||||
return await Conversation.findOneAndUpdate({ conversationId: conversationId, user }, update, {
|
return await Conversation.findOneAndUpdate(
|
||||||
|
{ conversationId: conversationId, user },
|
||||||
|
update,
|
||||||
|
{
|
||||||
new: true
|
new: true
|
||||||
}).exec();
|
}
|
||||||
|
).exec();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log(error);
|
console.log(error);
|
||||||
return { message: 'Error updating conversation' };
|
return { message: 'Error updating conversation' };
|
||||||
|
|
@ -135,60 +139,5 @@ module.exports = {
|
||||||
let deleteCount = await Conversation.deleteMany({ ...filter, user }).exec();
|
let deleteCount = await Conversation.deleteMany({ ...filter, user }).exec();
|
||||||
deleteCount.messages = await deleteMessages(filter);
|
deleteCount.messages = await deleteMessages(filter);
|
||||||
return deleteCount;
|
return deleteCount;
|
||||||
},
|
|
||||||
migrateDb: async () => {
|
|
||||||
try {
|
|
||||||
const conversations = await Conversation.find({ model: null }).exec();
|
|
||||||
|
|
||||||
if (!conversations || conversations.length === 0)
|
|
||||||
return { message: '[Migrate] No conversations to migrate' };
|
|
||||||
|
|
||||||
for (let convo of conversations) {
|
|
||||||
const messages = await getMessages({
|
|
||||||
conversationId: convo.conversationId,
|
|
||||||
messageId: { $exists: false }
|
|
||||||
});
|
|
||||||
|
|
||||||
let model;
|
|
||||||
let oldId;
|
|
||||||
const promises = [];
|
|
||||||
messages.forEach((message, i) => {
|
|
||||||
const msgObj = message.toObject();
|
|
||||||
const newId = msgObj.id;
|
|
||||||
if (i === 0) {
|
|
||||||
message.parentMessageId = '00000000-0000-0000-0000-000000000000';
|
|
||||||
} else {
|
|
||||||
message.parentMessageId = oldId;
|
|
||||||
}
|
|
||||||
|
|
||||||
oldId = newId;
|
|
||||||
message.messageId = newId;
|
|
||||||
if (message.sender.toLowerCase() !== 'user' && !model) {
|
|
||||||
model = message.sender.toLowerCase();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (message.sender.toLowerCase() === 'user') {
|
|
||||||
message.isCreatedByUser = true;
|
|
||||||
}
|
|
||||||
promises.push(message.save());
|
|
||||||
});
|
|
||||||
await Promise.all(promises);
|
|
||||||
|
|
||||||
await Conversation.findOneAndUpdate(
|
|
||||||
{ conversationId: convo.conversationId },
|
|
||||||
{ model },
|
|
||||||
{ new: true }
|
|
||||||
).exec();
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
await mongoose.connection.db.collection('messages').dropIndex('id_1');
|
|
||||||
} catch (error) {
|
|
||||||
console.log("[Migrate] Index doesn't exist or already dropped");
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
console.log(error);
|
|
||||||
return { message: '[Migrate] Error migrating conversations' };
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -1,15 +1,15 @@
|
||||||
const { saveMessage, deleteMessagesSince, deleteMessages } = require('./Message');
|
const { getMessages, saveMessage, deleteMessagesSince, deleteMessages } = require('./Message');
|
||||||
const { getCustomGpts, updateCustomGpt, updateByLabel, deleteCustomGpts } = require('./CustomGpt');
|
const { getCustomGpts, updateCustomGpt, updateByLabel, deleteCustomGpts } = require('./CustomGpt');
|
||||||
const { getConvoTitle, getConvo, saveConvo, migrateDb } = require('./Conversation');
|
const { getConvoTitle, getConvo, saveConvo } = require('./Conversation');
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
getMessages,
|
||||||
saveMessage,
|
saveMessage,
|
||||||
deleteMessagesSince,
|
deleteMessagesSince,
|
||||||
deleteMessages,
|
deleteMessages,
|
||||||
getConvoTitle,
|
getConvoTitle,
|
||||||
getConvo,
|
getConvo,
|
||||||
saveConvo,
|
saveConvo,
|
||||||
migrateDb,
|
|
||||||
getCustomGpts,
|
getCustomGpts,
|
||||||
updateCustomGpt,
|
updateCustomGpt,
|
||||||
updateByLabel,
|
updateByLabel,
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
const express = require('express');
|
const express = require('express');
|
||||||
const session = require('express-session')
|
const session = require('express-session')
|
||||||
const dbConnect = require('../models/dbConnect');
|
const connectDb = require('../lib/db/connectDb');
|
||||||
const { migrateDb } = require('../models');
|
const migrateDb = require('../lib/db/migrateDb');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const cors = require('cors');
|
const cors = require('cors');
|
||||||
const routes = require('./routes');
|
const routes = require('./routes');
|
||||||
|
|
@ -10,7 +10,7 @@ const port = process.env.PORT || 3080;
|
||||||
const host = process.env.HOST || 'localhost'
|
const host = process.env.HOST || 'localhost'
|
||||||
const userSystemEnabled = process.env.ENABLE_USER_SYSTEM || false
|
const userSystemEnabled = process.env.ENABLE_USER_SYSTEM || false
|
||||||
const projectPath = path.join(__dirname, '..', '..', 'client');
|
const projectPath = path.join(__dirname, '..', '..', 'client');
|
||||||
dbConnect().then(() => {
|
connectDb().then(() => {
|
||||||
console.log('Connected to MongoDB');
|
console.log('Connected to MongoDB');
|
||||||
migrateDb();
|
migrateDb();
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue