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

* fix: convert insert loop to 1 insert stmt

* chore: lint
This commit is contained in:
Jonathan Waltz 2023-06-13 11:07:58 -10:00 committed by GitHub
parent c6f69fc0a6
commit 84a5707d6d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -31,17 +31,26 @@ const DocumentVectors = {
},
bulkInsert: async function (vectorRecords = []) {
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();
db.close();
return { documentsInserted: vectorRecords.length };
},
deleteForWorkspace: async function (workspaceId) {