From 048cf8f1887748816cc51a38dc0a5227935245d5 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Wed, 1 Feb 2023 13:01:38 +0000 Subject: [PATCH] PR comments. --- .../backend-core/src/db/couch/DatabaseImpl.ts | 11 ++++++++++ packages/server/src/integrations/couchdb.ts | 7 ++----- .../src/integrations/tests/couchdb.spec.ts | 20 ++++++++++++------- 3 files changed, 26 insertions(+), 12 deletions(-) diff --git a/packages/backend-core/src/db/couch/DatabaseImpl.ts b/packages/backend-core/src/db/couch/DatabaseImpl.ts index 2dbb061863..a60a748bdc 100644 --- a/packages/backend-core/src/db/couch/DatabaseImpl.ts +++ b/packages/backend-core/src/db/couch/DatabaseImpl.ts @@ -29,6 +29,17 @@ function buildNano(couchInfo: { url: string; cookie: string }) { }) } +export function DatabaseWithConnection( + dbName: string, + connection: string, + opts?: DatabaseOpts +) { + if (!connection) { + throw new Error("Must provide connection details") + } + return new DatabaseImpl(dbName, opts, connection) +} + export class DatabaseImpl implements Database { public readonly name: string private static nano: Nano.ServerScope diff --git a/packages/server/src/integrations/couchdb.ts b/packages/server/src/integrations/couchdb.ts index 8c1e71ef14..4da2253c77 100644 --- a/packages/server/src/integrations/couchdb.ts +++ b/packages/server/src/integrations/couchdb.ts @@ -6,6 +6,7 @@ import { QueryType, } from "@budibase/types" import { db as dbCore } from "@budibase/backend-core" +import { DatabaseWithConnection } from "@budibase/backend-core/src/db" interface CouchDBConfig { url: string @@ -66,11 +67,7 @@ class CouchDBIntegration implements IntegrationBase { constructor(config: CouchDBConfig) { this.config = config - this.client = new dbCore.DatabaseImpl( - config.database, - undefined, - config.url - ) + this.client = dbCore.DatabaseWithConnection(config.database, config.url) } async query( diff --git a/packages/server/src/integrations/tests/couchdb.spec.ts b/packages/server/src/integrations/tests/couchdb.spec.ts index 9bbb4fa211..0d744cd343 100644 --- a/packages/server/src/integrations/tests/couchdb.spec.ts +++ b/packages/server/src/integrations/tests/couchdb.spec.ts @@ -1,15 +1,19 @@ +import { DatabaseWithConnection } from "@budibase/backend-core/src/db" + jest.mock("@budibase/backend-core", () => { const core = jest.requireActual("@budibase/backend-core") return { ...core, db: { ...core.db, - DatabaseImpl: function () { - this.post = jest.fn() - this.allDocs = jest.fn().mockReturnValue({ rows: [] }) - this.put = jest.fn() - this.get = jest.fn().mockReturnValue({ _rev: "a" }) - this.remove = jest.fn() + DatabaseWithConnection: function () { + return { + post: jest.fn(), + allDocs: jest.fn().mockReturnValue({ rows: [] }), + put: jest.fn(), + get: jest.fn().mockReturnValue({ _rev: "a" }), + remove: jest.fn(), + } }, }, } @@ -20,7 +24,9 @@ import { default as CouchDBIntegration } from "../couchdb" class TestConfiguration { integration: any - constructor(config: any = {}) { + constructor( + config: any = { url: "http://somewhere", database: "something" } + ) { this.integration = new CouchDBIntegration.integration(config) } }