From 420b9bc332ae19c45e0e00c5fd4e50e3c384cc10 Mon Sep 17 00:00:00 2001 From: Jonathan Waltz Date: Fri, 9 Jun 2023 23:05:28 -1000 Subject: [PATCH 1/2] fix: convert insert loop to 1 insert stmt --- server/models/vectors.js | 37 ++++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/server/models/vectors.js b/server/models/vectors.js index 6531ac4f71a..3f8e11bb54b 100644 --- a/server/models/vectors.js +++ b/server/models/vectors.js @@ -12,14 +12,13 @@ const DocumentVectors = { createdAt TEXT DEFAULT CURRENT_TIMESTAMP, lastUpdatedAt TEXT DEFAULT CURRENT_TIMESTAMP `, - db: async function () { + db: async function() { const sqlite3 = require("sqlite3").verbose(); const { open } = require("sqlite"); const db = await open({ - filename: `${ - !!process.env.STORAGE_DIR ? `${process.env.STORAGE_DIR}/` : "" - }anythingllm.db`, + filename: `${!!process.env.STORAGE_DIR ? `${process.env.STORAGE_DIR}/` : "" + }anythingllm.db`, driver: sqlite3.Database, }); @@ -29,22 +28,31 @@ const DocumentVectors = { db.on("trace", (sql) => console.log(sql)); return db; }, - bulkInsert: async function (vectorRecords = []) { + 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) { + deleteForWorkspace: async function(workspaceId) { const documents = await Document.forWorkspace(workspaceId); const docIds = [...new Set(documents.map((doc) => doc.docId))]; const ids = ( @@ -53,18 +61,17 @@ const DocumentVectors = { await this.deleteIds(ids); return true; }, - where: async function (clause = "", limit = null) { + where: async function(clause = "", limit = null) { const db = await this.db(); const results = await db.all( - `SELECT * FROM ${this.tablename} ${clause ? `WHERE ${clause}` : ""} ${ - !!limit ? `LIMIT ${limit}` : "" + `SELECT * FROM ${this.tablename} ${clause ? `WHERE ${clause}` : ""} ${!!limit ? `LIMIT ${limit}` : "" }` ); db.close(); return results; }, - deleteIds: async function (ids = []) { + deleteIds: async function(ids = []) { const db = await this.db(); await db.get( `DELETE FROM ${this.tablename} WHERE id IN (${ids.join(", ")}) ` From 31ce4faf916b21dfe74c7a5c0070efb1a07b71e5 Mon Sep 17 00:00:00 2001 From: Jonathan Waltz Date: Mon, 12 Jun 2023 11:31:37 -1000 Subject: [PATCH 2/2] chore: lint --- server/models/vectors.js | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/server/models/vectors.js b/server/models/vectors.js index 3f8e11bb54b..51b75e16cfe 100644 --- a/server/models/vectors.js +++ b/server/models/vectors.js @@ -12,13 +12,14 @@ const DocumentVectors = { createdAt TEXT DEFAULT CURRENT_TIMESTAMP, lastUpdatedAt TEXT DEFAULT CURRENT_TIMESTAMP `, - db: async function() { + db: async function () { const sqlite3 = require("sqlite3").verbose(); const { open } = require("sqlite"); const db = await open({ - filename: `${!!process.env.STORAGE_DIR ? `${process.env.STORAGE_DIR}/` : "" - }anythingllm.db`, + filename: `${ + !!process.env.STORAGE_DIR ? `${process.env.STORAGE_DIR}/` : "" + }anythingllm.db`, driver: sqlite3.Database, }); @@ -28,7 +29,7 @@ const DocumentVectors = { db.on("trace", (sql) => console.log(sql)); return db; }, - bulkInsert: async function(vectorRecords = []) { + bulkInsert: async function (vectorRecords = []) { if (vectorRecords.length === 0) return; const db = await this.db(); @@ -52,7 +53,7 @@ const DocumentVectors = { return { documentsInserted: vectorRecords.length }; }, - deleteForWorkspace: async function(workspaceId) { + deleteForWorkspace: async function (workspaceId) { const documents = await Document.forWorkspace(workspaceId); const docIds = [...new Set(documents.map((doc) => doc.docId))]; const ids = ( @@ -61,17 +62,18 @@ const DocumentVectors = { await this.deleteIds(ids); return true; }, - where: async function(clause = "", limit = null) { + where: async function (clause = "", limit = null) { const db = await this.db(); const results = await db.all( - `SELECT * FROM ${this.tablename} ${clause ? `WHERE ${clause}` : ""} ${!!limit ? `LIMIT ${limit}` : "" + `SELECT * FROM ${this.tablename} ${clause ? `WHERE ${clause}` : ""} ${ + !!limit ? `LIMIT ${limit}` : "" }` ); db.close(); return results; }, - deleteIds: async function(ids = []) { + deleteIds: async function (ids = []) { const db = await this.db(); await db.get( `DELETE FROM ${this.tablename} WHERE id IN (${ids.join(", ")}) `