From faf4dbdca374a6fbc380e098acdfd60e1f18e0e6 Mon Sep 17 00:00:00 2001 From: timothycarambat Date: Thu, 27 Jul 2023 02:33:33 -0700 Subject: [PATCH] fix more SQL statments --- backend/endpoints/v1/organizations/index.js | 2 +- backend/models/workspaceDocument.js | 43 ++++++++++++++------- 2 files changed, 31 insertions(+), 14 deletions(-) diff --git a/backend/endpoints/v1/organizations/index.js b/backend/endpoints/v1/organizations/index.js index 4e74a63..7e37fbb 100644 --- a/backend/endpoints/v1/organizations/index.js +++ b/backend/endpoints/v1/organizations/index.js @@ -494,7 +494,7 @@ function organizationEndpoints(app) { const documents = await WorkspaceDocument.where( `organization_id = ${organization.id}`, - null, + 100, true ); response.status(200).json({ documents }); diff --git a/backend/models/workspaceDocument.js b/backend/models/workspaceDocument.js index 8760eeb..64524bb 100644 --- a/backend/models/workspaceDocument.js +++ b/backend/models/workspaceDocument.js @@ -132,29 +132,44 @@ const WorkspaceDocument = { }; }, where: async function (clause = null, limit = null, withReferences = false) { + if (!withReferences) { + const db = await this.db(); + const results = await db.all( + `SELECT * FROM ${this.tablename} ${clause ? `WHERE ${clause}` : ""} ${!!limit ? `LIMIT ${limit}` : "" + }` + ); + await db.close(); + return results; + } + + const { OrganizationWorkspace } = require("./organizationWorkspace"); const db = await this.db(); const results = await db.all( - `SELECT * FROM ${this.tablename} ${clause ? `WHERE ${clause}` : ""} ${ - !!limit ? `LIMIT ${limit}` : "" + `SELECT *, ow.slug as workspace_slug, ow.name as workspace_name + FROM ${this.tablename} as wd + LEFT JOIN ${OrganizationWorkspace.tablename} as ow ON ow.id = wd.workspace_id + ${clause ? `WHERE wd.${clause}` : ""} ${!!limit ? `LIMIT ${limit}` : "" }` ); await db.close(); - if (!withReferences) return results; - const { OrganizationWorkspace } = require("./organizationWorkspace"); - for (const res of results) { - res.workspace = await OrganizationWorkspace.get( - `id = ${res.workspace_id}` - ); - } + const completeResults = results.map((res) => { + const { workspace_slug, workspace_name, ...rest } = res + return { + ...rest, + workspace: { + slug: workspace_slug, + name: workspace_name, + } + } + }) - return results; + return completeResults; }, count: async function (clause = null) { const db = await this.db(); const { count } = await db.get( - `SELECT COUNT(*) as count FROM ${this.tablename} ${ - clause ? `WHERE ${clause}` : "" + `SELECT COUNT(*) as count FROM ${this.tablename} ${clause ? `WHERE ${clause}` : "" }` ); await db.close(); @@ -169,7 +184,9 @@ const WorkspaceDocument = { const documents = await this.where(`${field} = ${value}`); if (documents.length === 0) return 0; - const docIds = documents.map((doc) => doc.id); + const docIdSet = new Set(); + documents.forEach((doc) => docIdSet.add(doc.id)); + const docIds = Array.from(docIdSet); const vectorCount = await DocumentVectors.count( `document_id IN (${docIds.join(",")})` );