Skip to content
Snippets Groups Projects
Unverified Commit 84a5707d authored by Jonathan Waltz's avatar Jonathan Waltz Committed by GitHub
Browse files

refactor: convert insert loop to 1 insert stmt (#19)

* fix: convert insert loop to 1 insert stmt

* chore: lint
parent c6f69fc0
No related branches found
No related tags found
No related merge requests found
......@@ -31,17 +31,26 @@ const DocumentVectors = {
},
bulkInsert: async function (vectorRecords = []) {
if (vectorRecords.length === 0) return;
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 (?, ?)`
`INSERT INTO ${this.tablename} (docId, vectorId) VALUES ${placeholders}`
);
for (const record of vectorRecords) {
const { docId, vectorId } = record;
stmt.run([docId, vectorId]);
}
// 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();
db.close();
return { documentsInserted: vectorRecords.length };
},
deleteForWorkspace: async function (workspaceId) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment