diff --git a/packages/backend-core/src/context/index.ts b/packages/backend-core/src/context/index.ts index 910bd192b4..15eed8d0ba 100644 --- a/packages/backend-core/src/context/index.ts +++ b/packages/backend-core/src/context/index.ts @@ -5,6 +5,7 @@ import { getDevelopmentAppID, getProdAppID, baseGlobalDBName, + getDB, PouchLike, } from "../db" import { Context } from "./localStorage" @@ -185,7 +186,10 @@ export function updateAppId(appId: string) { export function getGlobalDB(): PouchLike { const context = Context.get() - return new PouchLike(baseGlobalDBName(context?.tenantId)) + if (!context || (env.MULTI_TENANCY && !context.tenantId)) { + throw new Error("Global DB not found") + } + return getDB(baseGlobalDBName(context?.tenantId)) } /** @@ -194,7 +198,7 @@ export function getGlobalDB(): PouchLike { */ export function getAppDB(opts?: any): PouchLike { const appId = getAppId() - return new PouchLike(appId, opts) + return getDB(appId, opts) } /** @@ -206,7 +210,7 @@ export function getProdAppDB(opts?: any): PouchLike { if (!appId) { throw new Error("Unable to retrieve prod DB - no app ID.") } - return new PouchLike(getProdAppID(appId), opts) + return getDB(getProdAppID(appId), opts) } /** @@ -218,5 +222,5 @@ export function getDevAppDB(opts?: any): PouchLike { if (!appId) { throw new Error("Unable to retrieve dev DB - no app ID.") } - return new PouchLike(getDevelopmentAppID(appId), opts) + return getDB(getDevelopmentAppID(appId), opts) } diff --git a/packages/backend-core/src/context/tests/index.spec.ts b/packages/backend-core/src/context/tests/index.spec.js similarity index 80% rename from packages/backend-core/src/context/tests/index.spec.ts rename to packages/backend-core/src/context/tests/index.spec.js index 749231c3e5..6d09446059 100644 --- a/packages/backend-core/src/context/tests/index.spec.ts +++ b/packages/backend-core/src/context/tests/index.spec.js @@ -1,18 +1,10 @@ -import "../../../tests/utilities/TestConfiguration" -import * as context from ".." -import { DEFAULT_TENANT_ID } from "../../constants" -import env from "../../environment" - -// must use require to spy index file exports due to known issue in jest -const dbUtils = require("../../db") -jest.spyOn(dbUtils, "closePouchDB") -jest.spyOn(dbUtils, "getDB") +require("../../../tests/utilities/TestConfiguration") +const context = require("../") +const { DEFAULT_TENANT_ID } = require("../../constants") +const env = require("../../environment") +const dbCore = require("../../db") describe("context", () => { - beforeEach(() => { - jest.clearAllMocks() - }) - describe("doInTenant", () => { describe("single-tenancy", () => { it("defaults to the default tenant", () => { @@ -25,8 +17,6 @@ describe("context", () => { const db = context.getGlobalDB() expect(db.name).toBe("global-db") }) - expect(dbUtils.getDB).toHaveBeenCalledTimes(1) - expect(dbUtils.closePouchDB).toHaveBeenCalledTimes(1) }) }) @@ -40,7 +30,7 @@ describe("context", () => { let error try { context.getTenantId() - } catch (e: any) { + } catch (e) { error = e } expect(error.message).toBe("Tenant id not found") @@ -59,7 +49,7 @@ describe("context", () => { let error try { context.getGlobalDB() - } catch (e: any) { + } catch (e) { error = e } expect(error.message).toBe("Global DB not found") @@ -85,8 +75,6 @@ describe("context", () => { const db = context.getGlobalDB() expect(db.name).toBe("test_global-db") }) - expect(dbUtils.getDB).toHaveBeenCalledTimes(1) - expect(dbUtils.closePouchDB).toHaveBeenCalledTimes(1) }) it("sets the tenant id when nested with same tenant id", async () => { @@ -121,10 +109,6 @@ describe("context", () => { }) }) }) - - // only 1 db is opened and closed - expect(dbUtils.getDB).toHaveBeenCalledTimes(1) - expect(dbUtils.closePouchDB).toHaveBeenCalledTimes(1) }) it("sets different tenant id inside another context", () => { diff --git a/packages/backend-core/src/db/couch/index.ts b/packages/backend-core/src/db/couch/index.ts index 3730a1e248..b8cb0ab79f 100644 --- a/packages/backend-core/src/db/couch/index.ts +++ b/packages/backend-core/src/db/couch/index.ts @@ -1,4 +1,4 @@ export * from "./connections" export * from "./pouchLike" export * from "./utils" -export { init, getPouch, getPouchDB } from "./pouchDB" +export { init, getPouch, getPouchDB, closePouchDB } from "./pouchDB" diff --git a/packages/backend-core/src/db/db.ts b/packages/backend-core/src/db/db.ts index 497b747b80..909d286d32 100644 --- a/packages/backend-core/src/db/db.ts +++ b/packages/backend-core/src/db/db.ts @@ -11,7 +11,7 @@ const checkInitialised = () => { } } -export function getDB(dbName: string, opts?: any): PouchLike { +export function getDB(dbName?: string, opts?: any): PouchLike { if (env.isTest()) { dbList.add(dbName) }