2023-09-28 23:00:03 +02:00
|
|
|
const prisma = require("../utils/prisma");
|
2023-06-08 06:31:35 +02:00
|
|
|
const { Document } = require("./documents");
|
2023-06-04 04:28:07 +02:00
|
|
|
|
|
|
|
const DocumentVectors = {
|
|
|
|
bulkInsert: async function (vectorRecords = []) {
|
|
|
|
if (vectorRecords.length === 0) return;
|
2023-06-13 23:07:58 +02:00
|
|
|
|
2023-07-27 03:06:53 +02:00
|
|
|
try {
|
2023-09-28 23:00:03 +02:00
|
|
|
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 };
|
2023-07-27 03:06:53 +02:00
|
|
|
}
|
2023-06-04 04:28:07 +02:00
|
|
|
},
|
2023-09-28 23:00:03 +02:00
|
|
|
|
2023-06-04 04:28:07 +02:00
|
|
|
deleteForWorkspace: async function (workspaceId) {
|
|
|
|
const documents = await Document.forWorkspace(workspaceId);
|
2023-06-08 06:31:35 +02:00
|
|
|
const docIds = [...new Set(documents.map((doc) => doc.docId))];
|
2023-09-28 23:00:03 +02:00
|
|
|
|
|
|
|
try {
|
|
|
|
await prisma.document_vectors.deleteMany({
|
|
|
|
where: { docId: { in: docIds } },
|
|
|
|
});
|
|
|
|
return true;
|
|
|
|
} catch (error) {
|
|
|
|
console.error("Delete for workspace failed", error);
|
|
|
|
return false;
|
|
|
|
}
|
2023-06-04 04:28:07 +02:00
|
|
|
},
|
|
|
|
|
2023-09-28 23:00:03 +02:00
|
|
|
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 [];
|
|
|
|
}
|
2023-06-04 04:28:07 +02:00
|
|
|
},
|
2023-09-28 23:00:03 +02:00
|
|
|
|
2023-06-04 04:28:07 +02:00
|
|
|
deleteIds: async function (ids = []) {
|
2023-09-28 23:00:03 +02:00
|
|
|
try {
|
|
|
|
await prisma.document_vectors.deleteMany({
|
|
|
|
where: { id: { in: ids } },
|
|
|
|
});
|
|
|
|
return true;
|
|
|
|
} catch (error) {
|
|
|
|
console.error("Delete IDs failed", error);
|
|
|
|
return false;
|
|
|
|
}
|
2023-06-08 06:31:35 +02:00
|
|
|
},
|
|
|
|
};
|
2023-06-04 04:28:07 +02:00
|
|
|
|
2023-06-08 06:31:35 +02:00
|
|
|
module.exports = { DocumentVectors };
|