mirror of
https://github.com/Mintplex-Labs/anything-llm.git
synced 2024-09-11 23:03:25 +02:00
* WIP converted all sqlite models into prisma calls * modify db setup and fix ApiKey model calls in admin.js * renaming function params to be consistent * converted adminEndpoints to utilize prisma orm * converted chatEndpoints to utilize prisma orm * converted inviteEndpoints to utilize prisma orm * converted systemEndpoints to utilize prisma orm * converted workspaceEndpoints to utilize prisma orm * converting sql queries to prisma calls * fixed default param bug for orderBy and limit * fixed typo for workspace chats * fixed order of deletion to account for sql relations * fix invite CRUD and workspace management CRUD * fixed CRUD for api keys * created prisma setup scripts/docs for understanding how to use prisma * prisma dependency change * removing unneeded console.logs * removing unneeded sql escape function * linting and creating migration script * migration from depreciated sqlite script update * removing unneeded migrations in prisma folder * create backup of old sqlite db and use transactions to ensure all operations complete successfully * adding migrations to gitignore * updated PRISMA.md docs for info on how to use sqlite migration script * comment changes * adding back migrations folder to repo * Reviewing SQL and prisma integraiton on fresh repo * update inline key replacement * ensure migration script executes and maps foreign_keys regardless of db ordering * run migration endpoint * support new prisma backend * bump version * change migration call --------- Co-authored-by: timothycarambat <rambat1010@gmail.com>
162 lines
3.9 KiB
JavaScript
162 lines
3.9 KiB
JavaScript
const prisma = require("../utils/prisma");
|
|
|
|
const WorkspaceChats = {
|
|
new: async function ({ workspaceId, prompt, response = {}, user = null }) {
|
|
try {
|
|
const chat = await prisma.workspace_chats.create({
|
|
data: {
|
|
workspaceId,
|
|
prompt,
|
|
response: JSON.stringify(response),
|
|
user_id: user?.id || null,
|
|
},
|
|
});
|
|
return { chat, message: null };
|
|
} catch (error) {
|
|
console.error(error.message);
|
|
return { chat: null, message: error.message };
|
|
}
|
|
},
|
|
|
|
forWorkspaceByUser: async function (
|
|
workspaceId = null,
|
|
userId = null,
|
|
limit = null
|
|
) {
|
|
if (!workspaceId || !userId) return [];
|
|
try {
|
|
const chats = await prisma.workspace_chats.findMany({
|
|
where: {
|
|
workspaceId,
|
|
user_id: userId,
|
|
},
|
|
...(limit !== null ? { take: limit } : {}),
|
|
orderBy: {
|
|
id: "asc",
|
|
},
|
|
});
|
|
return chats;
|
|
} catch (error) {
|
|
console.error(error.message);
|
|
return [];
|
|
}
|
|
},
|
|
|
|
forWorkspace: async function (workspaceId = null, limit = null) {
|
|
if (!workspaceId) return [];
|
|
try {
|
|
const chats = await prisma.workspace_chats.findMany({
|
|
where: {
|
|
workspaceId,
|
|
},
|
|
...(limit !== null ? { take: limit } : {}),
|
|
orderBy: {
|
|
id: "asc",
|
|
},
|
|
});
|
|
return chats;
|
|
} catch (error) {
|
|
console.error(error.message);
|
|
return [];
|
|
}
|
|
},
|
|
|
|
markHistoryInvalid: async function (workspaceId = null, user = null) {
|
|
if (!workspaceId) return;
|
|
try {
|
|
await prisma.workspace_chats.updateMany({
|
|
where: {
|
|
workspaceId,
|
|
user_id: user?.id,
|
|
},
|
|
data: {
|
|
include: false,
|
|
},
|
|
});
|
|
return;
|
|
} catch (error) {
|
|
console.error(error.message);
|
|
}
|
|
},
|
|
|
|
get: async function (clause = {}, limit = null, orderBy = null) {
|
|
try {
|
|
const chat = await prisma.workspace_chats.findFirst({
|
|
where: clause,
|
|
...(limit !== null ? { take: limit } : {}),
|
|
...(orderBy !== null ? { orderBy } : {}),
|
|
});
|
|
return chat || null;
|
|
} catch (error) {
|
|
console.error(error.message);
|
|
return null;
|
|
}
|
|
},
|
|
|
|
delete: async function (clause = {}) {
|
|
try {
|
|
await prisma.workspace_chats.deleteMany({
|
|
where: clause,
|
|
});
|
|
return true;
|
|
} catch (error) {
|
|
console.error(error.message);
|
|
return false;
|
|
}
|
|
},
|
|
|
|
where: async function (clause = {}, limit = null, orderBy = null) {
|
|
try {
|
|
const chats = await prisma.workspace_chats.findMany({
|
|
where: clause,
|
|
...(limit !== null ? { take: limit } : {}),
|
|
...(orderBy !== null ? { orderBy } : {}),
|
|
});
|
|
return chats;
|
|
} catch (error) {
|
|
console.error(error.message);
|
|
return [];
|
|
}
|
|
},
|
|
|
|
count: async function (clause = {}) {
|
|
try {
|
|
const count = await prisma.workspace_chats.count({
|
|
where: clause,
|
|
});
|
|
return count;
|
|
} catch (error) {
|
|
console.error(error.message);
|
|
return 0;
|
|
}
|
|
},
|
|
|
|
whereWithData: async function (clause = {}, limit = null, orderBy = null) {
|
|
const { Workspace } = require("./workspace");
|
|
const { User } = require("./user");
|
|
|
|
try {
|
|
const results = await this.where(clause, limit, orderBy);
|
|
|
|
for (const res of results) {
|
|
const workspace = await Workspace.get({ id: res.workspaceId });
|
|
res.workspace = workspace
|
|
? { name: workspace.name, slug: workspace.slug }
|
|
: { name: "deleted workspace", slug: null };
|
|
|
|
const user = await User.get({ id: res.user_id });
|
|
res.user = user
|
|
? { username: user.username }
|
|
: { username: "deleted user" };
|
|
}
|
|
|
|
return results;
|
|
} catch (error) {
|
|
console.error(error.message);
|
|
return [];
|
|
}
|
|
},
|
|
};
|
|
|
|
module.exports = { WorkspaceChats };
|