diff --git a/packages/backend-core/src/db/couch/DatabaseImpl.ts b/packages/backend-core/src/db/couch/DatabaseImpl.ts index 4db63ad695..274c09438d 100644 --- a/packages/backend-core/src/db/couch/DatabaseImpl.ts +++ b/packages/backend-core/src/db/couch/DatabaseImpl.ts @@ -80,6 +80,11 @@ export function DatabaseWithConnection( connection: string, opts?: DatabaseOpts ) { + if (!dbName || !connection) { + throw new Error( + "Unable to create database without database name or connection" + ) + } const db = new DatabaseImpl(dbName, opts, connection) return new DDInstrumentedDatabase(db) } diff --git a/packages/server/src/integrations/couchdb.ts b/packages/server/src/integrations/couchdb.ts index c271fb12b2..4004bc47c6 100644 --- a/packages/server/src/integrations/couchdb.ts +++ b/packages/server/src/integrations/couchdb.ts @@ -71,6 +71,9 @@ class CouchDBIntegration implements IntegrationBase { private readonly client: Database constructor(config: CouchDBConfig) { + if (!config.url || !config.database) { + throw new Error("Unable to connect without URL or database") + } this.client = dbCore.DatabaseWithConnection(config.database, config.url) } @@ -79,45 +82,30 @@ class CouchDBIntegration implements IntegrationBase { connected: false, } try { - const result = await this.query("exists", "validation error", {}) - response.connected = result === true + response.connected = await this.client.exists() } catch (e: any) { response.error = e.message as string } return response } - async query( - command: string, - errorMsg: string, - query: { json?: object; id?: string } - ) { - try { - return await (this.client as any)[command](query.id || query.json) - } catch (err) { - console.error(errorMsg, err) - throw err - } - } - private parse(query: { json: string | object }) { return typeof query.json === "string" ? JSON.parse(query.json) : query.json } async create(query: { json: string | object }) { const parsed = this.parse(query) - return this.query("post", "Error writing to couchDB", { json: parsed }) + return await this.client.put(parsed) } async read(query: { json: string | object }) { const parsed = this.parse(query) - const result = await this.query("allDocs", "Error querying couchDB", { - json: { - include_docs: true, - ...parsed, - }, - }) - return result.rows.map((row: { doc: object }) => row.doc) + const params = { + include_docs: true, + ...parsed, + } + const result = await this.client.allDocs(params) + return result.rows.map(row => row.doc) } async update(query: { json: string | object }) { @@ -126,22 +114,15 @@ class CouchDBIntegration implements IntegrationBase { const oldDoc = await this.get({ id: parsed._id }) parsed._rev = oldDoc._rev } - return this.query("put", "Error updating couchDB document", { - json: parsed, - }) + return await this.client.put(parsed) } async get(query: { id: string }) { - return this.query("get", "Error retrieving couchDB document by ID", { - id: query.id, - }) + return await this.client.get(query.id) } async delete(query: { id: string }) { - const doc = await this.query("get", "Cannot find doc to be deleted", query) - return this.query("remove", "Error deleting couchDB document", { - json: doc, - }) + return await this.client.remove(query.id) } } diff --git a/packages/server/src/integrations/tests/couchdb.spec.ts b/packages/server/src/integrations/tests/couchdb.spec.ts index f27713a81f..6cb0c438c0 100644 --- a/packages/server/src/integrations/tests/couchdb.spec.ts +++ b/packages/server/src/integrations/tests/couchdb.spec.ts @@ -6,7 +6,6 @@ jest.mock("@budibase/backend-core", () => { ...core.db, DatabaseWithConnection: function () { return { - post: jest.fn(), allDocs: jest.fn().mockReturnValue({ rows: [] }), put: jest.fn(), get: jest.fn().mockReturnValue({ _rev: "a" }), @@ -43,7 +42,7 @@ describe("CouchDB Integration", () => { await config.integration.create({ json: JSON.stringify(doc), }) - expect(config.integration.client.post).toHaveBeenCalledWith(doc) + expect(config.integration.client.put).toHaveBeenCalledWith(doc) }) it("calls the read method with the correct params", async () => { @@ -80,7 +79,6 @@ describe("CouchDB Integration", () => { it("calls the delete method with the correct params", async () => { const id = "1234" await config.integration.delete({ id }) - expect(config.integration.client.get).toHaveBeenCalledWith(id) - expect(config.integration.client.remove).toHaveBeenCalled() + expect(config.integration.client.remove).toHaveBeenCalledWith(id) }) })