mirror of
https://github.com/Mintplex-Labs/anything-llm.git
synced 2025-03-16 07:02:22 +00:00
refactor: convert insert loop to 1 insert stmt (#19)
* fix: convert insert loop to 1 insert stmt * chore: lint
This commit is contained in:
parent
c6f69fc0a6
commit
84a5707d6d
1 changed files with 17 additions and 8 deletions
|
@ -31,17 +31,26 @@ const DocumentVectors = {
|
||||||
},
|
},
|
||||||
bulkInsert: async function (vectorRecords = []) {
|
bulkInsert: async function (vectorRecords = []) {
|
||||||
if (vectorRecords.length === 0) return;
|
if (vectorRecords.length === 0) return;
|
||||||
const db = await this.db();
|
|
||||||
const stmt = await db.prepare(
|
|
||||||
`INSERT INTO ${this.tablename} (docId, vectorId) VALUES (?, ?)`
|
|
||||||
);
|
|
||||||
for (const record of vectorRecords) {
|
|
||||||
const { docId, vectorId } = record;
|
|
||||||
stmt.run([docId, vectorId]);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
const db = await this.db();
|
||||||
|
|
||||||
|
// Build a single query string with multiple placeholders for the INSERT operation
|
||||||
|
const placeholders = vectorRecords.map(() => "(?, ?)").join(", ");
|
||||||
|
|
||||||
|
const stmt = await db.prepare(
|
||||||
|
`INSERT INTO ${this.tablename} (docId, vectorId) VALUES ${placeholders}`
|
||||||
|
);
|
||||||
|
|
||||||
|
// Flatten the vectorRecords array to match the order of placeholders
|
||||||
|
const values = vectorRecords.reduce(
|
||||||
|
(arr, record) => arr.concat([record.docId, record.vectorId]),
|
||||||
|
[]
|
||||||
|
);
|
||||||
|
|
||||||
|
stmt.run(values);
|
||||||
stmt.finalize();
|
stmt.finalize();
|
||||||
db.close();
|
db.close();
|
||||||
|
|
||||||
return { documentsInserted: vectorRecords.length };
|
return { documentsInserted: vectorRecords.length };
|
||||||
},
|
},
|
||||||
deleteForWorkspace: async function (workspaceId) {
|
deleteForWorkspace: async function (workspaceId) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue