anything-llm/server/models/vectors.js
Sean Hatfield a126b5f5aa
Replace custom sqlite dbms with prisma (#239)
* 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>
2023-09-28 14:00:03 -07:00

70 lines
1.7 KiB
JavaScript

const prisma = require("../utils/prisma");
const { Document } = require("./documents");
const DocumentVectors = {
bulkInsert: async function (vectorRecords = []) {
if (vectorRecords.length === 0) return;
try {
const inserts = [];
vectorRecords.forEach((record) => {
inserts.push(
prisma.document_vectors.create({
data: {
docId: record.docId,
vectorId: record.vectorId,
},
})
);
});
await prisma.$transaction(inserts);
return { documentsInserted: inserts.length };
} catch (error) {
console.error("Bulk insert failed", error);
return { documentsInserted: 0 };
}
},
deleteForWorkspace: async function (workspaceId) {
const documents = await Document.forWorkspace(workspaceId);
const docIds = [...new Set(documents.map((doc) => doc.docId))];
try {
await prisma.document_vectors.deleteMany({
where: { docId: { in: docIds } },
});
return true;
} catch (error) {
console.error("Delete for workspace failed", error);
return false;
}
},
where: async function (clause = {}, limit) {
try {
const results = await prisma.document_vectors.findMany({
where: clause,
take: limit || undefined,
});
return results;
} catch (error) {
console.error("Where query failed", error);
return [];
}
},
deleteIds: async function (ids = []) {
try {
await prisma.document_vectors.deleteMany({
where: { id: { in: ids } },
});
return true;
} catch (error) {
console.error("Delete IDs failed", error);
return false;
}
},
};
module.exports = { DocumentVectors };