diff --git a/app/api/common.v2/database/getConnectionForCurrentTenant.ts b/app/api/common.v2/database/getConnectionForCurrentTenant.ts index 7c4f257b7f..72e763da64 100644 --- a/app/api/common.v2/database/getConnectionForCurrentTenant.ts +++ b/app/api/common.v2/database/getConnectionForCurrentTenant.ts @@ -9,17 +9,23 @@ function getTenant(): Tenant { } function getConnection(): Db { + if (config.env_feature_flags.use_mongodb_instead_of_mongoose) { + return DB.mongodb_Db(getTenant().dbName); + } const { db } = DB.connectionForDB(getTenant().dbName); if (!db) { - throw new Error(); + throw new Error('DB object is undefined'); } return db; } function getSharedConnection(): Db { + if (config.env_feature_flags.use_mongodb_instead_of_mongoose) { + return DB.mongodb_Db(getTenant().dbName); + } const { db } = DB.connectionForDB(config.SHARED_DB); if (!db) { - throw new Error(); + throw new Error('DB object is undefined'); } return db; } diff --git a/app/api/config.ts b/app/api/config.ts index 4131b97a7a..ef7bb655d6 100644 --- a/app/api/config.ts +++ b/app/api/config.ts @@ -93,4 +93,7 @@ export const config = { }, githubToken: process.env.GITHUB_TOKEN || '', queueName: QUEUE_NAME || 'uwazi_jobs', + env_feature_flags: { + use_mongodb_instead_of_mongoose: process.env.MONGO_NOT_MONGOOSE || false, + }, }; diff --git a/app/api/odm/DB.ts b/app/api/odm/DB.ts index 948ceee3cb..4a1c50a2c0 100644 --- a/app/api/odm/DB.ts +++ b/app/api/odm/DB.ts @@ -1,5 +1,6 @@ import mongoose, { Connection, ConnectOptions } from 'mongoose'; import { config } from 'api/config'; +import { DbOptions } from 'mongodb'; let connection: Connection; @@ -25,6 +26,10 @@ const DB = { return this.getConnection().useDb(dbName, options); }, + mongodb_Db(dbName: string, options?: DbOptions) { + return this.getConnection().getClient().db(dbName, options); + }, + getConnection() { return connection; }, diff --git a/app/api/odm/specs/DB.spec.ts b/app/api/odm/specs/DB.spec.ts index 4ef6881d1f..85b78f9a22 100644 --- a/app/api/odm/specs/DB.spec.ts +++ b/app/api/odm/specs/DB.spec.ts @@ -7,6 +7,7 @@ import { testingTenants } from 'api/utils/testingTenants'; import { config } from 'api/config'; import { DB } from '../DB'; import { instanceModel } from '../model'; +import testingDB from 'api/utils/testing_db'; const testSchema = new mongoose.Schema({ name: { type: String, index: true }, @@ -27,8 +28,8 @@ describe('DB', () => { beforeEach(async () => { const uri = config.DBHOST; await DB.connect(`${uri}_DB_spec_ts`); - db1 = DB.getConnection().getClient().db('db1'); - db2 = DB.getConnection().getClient().db('db2'); + db1 = testingDB.db('db1'); + db2 = testingDB.db('db2'); }); afterAll(async () => { diff --git a/app/api/odm/specs/model_multi_tenant.spec.ts b/app/api/odm/specs/model_multi_tenant.spec.ts index 075128c39b..e3c9c36543 100644 --- a/app/api/odm/specs/model_multi_tenant.spec.ts +++ b/app/api/odm/specs/model_multi_tenant.spec.ts @@ -5,7 +5,6 @@ import { config } from 'api/config'; import { testingTenants } from 'api/utils/testingTenants'; import { instanceModel } from 'api/odm'; import testingDB from 'api/utils/testing_db'; -import { DB } from '../DB'; const testSchema = new mongoose.Schema({ name: String, @@ -23,9 +22,9 @@ describe('ODM Model multi-tenant', () => { beforeAll(async () => { await testingDB.connect({ defaultTenant: false }); - defaultDB = DB.getConnection().getClient().db(config.defaultTenant.dbName); - db1 = DB.getConnection().getClient().db('db1'); - db2 = DB.getConnection().getClient().db('db2'); + defaultDB = testingDB.db(config.defaultTenant.dbName); + db1 = testingDB.db('db1'); + db2 = testingDB.db('db2'); }); beforeEach(async () => { diff --git a/app/api/services/pdfsegmentation/specs/PDFSegmentation.spec.ts b/app/api/services/pdfsegmentation/specs/PDFSegmentation.spec.ts index c4183faef8..623b783a3c 100644 --- a/app/api/services/pdfsegmentation/specs/PDFSegmentation.spec.ts +++ b/app/api/services/pdfsegmentation/specs/PDFSegmentation.spec.ts @@ -1,7 +1,7 @@ /* eslint-disable camelcase */ /* eslint-disable max-lines */ -import { fixturer } from 'api/utils/testing_db'; +import testingDB, { fixturer } from 'api/utils/testing_db'; import { fixturesOneFile, fixturesOtherFile, @@ -79,8 +79,8 @@ describe('PDFSegmentation', () => { beforeEach(async () => { segmentPdfs = new PDFSegmentation(); - dbOne = DB.getConnection().getClient().db(tenantOne.dbName); - dbTwo = DB.getConnection().getClient().db(tenantTwo.dbName); + dbOne = testingDB.db(tenantOne.dbName); + dbTwo = testingDB.db(tenantTwo.dbName); tenants.tenants = { tenantOne }; fileA = await fs.readFile(`app/api/services/pdfsegmentation/specs/uploads/${fixturesPdfNameA}`); diff --git a/app/api/stats/routes.ts b/app/api/stats/routes.ts index 4dd344dd44..3d1a081a20 100644 --- a/app/api/stats/routes.ts +++ b/app/api/stats/routes.ts @@ -1,13 +1,11 @@ import { Application } from 'express'; import needsAuthorization from 'api/auth/authMiddleware'; import { RetrieveStatsService } from 'api/stats/services/RetrieveStatsService'; -import { tenants } from 'api/tenants'; -import { DB } from 'api/odm'; +import { getConnection } from 'api/common.v2/database/getConnectionForCurrentTenant'; export default (app: Application) => { app.get('/api/stats', needsAuthorization(['admin']), async (_req, res, _next) => { - const db = DB.getConnection().getClient().db(tenants.current().dbName); - const action = new RetrieveStatsService(db); + const action = new RetrieveStatsService(getConnection()); const stats = await action.execute(); res.json(stats); diff --git a/app/api/tenants/specs/tenantsContext.spec.ts b/app/api/tenants/specs/tenantsContext.spec.ts index d72ec0a084..68610c1fee 100644 --- a/app/api/tenants/specs/tenantsContext.spec.ts +++ b/app/api/tenants/specs/tenantsContext.spec.ts @@ -1,4 +1,3 @@ -import { DB } from 'api/odm/DB'; import { Db } from 'mongodb'; import testingDB from 'api/utils/testing_db'; import { testingEnvironment } from 'api/utils/testingEnvironment'; @@ -26,7 +25,7 @@ describe('tenantsContext', () => { beforeAll(async () => { await testingDB.connect(); testingEnvironment.setRequestId(); - db = DB.getConnection().getClient().db(config.SHARED_DB); + db = testingDB.db(config.SHARED_DB); await db.collection('tenants').deleteMany({}); await db.collection('tenants').insertMany([ diff --git a/app/api/tenants/specs/tenantsModel.spec.ts b/app/api/tenants/specs/tenantsModel.spec.ts index 885055ef69..84d94ee36b 100644 --- a/app/api/tenants/specs/tenantsModel.spec.ts +++ b/app/api/tenants/specs/tenantsModel.spec.ts @@ -1,5 +1,4 @@ import { config } from 'api/config'; -import { DB } from 'api/odm/DB'; import { Db, ObjectId } from 'mongodb'; import { Model } from 'mongoose'; import waitForExpect from 'wait-for-expect'; @@ -17,7 +16,7 @@ describe('tenantsModel', () => { beforeAll(async () => { await testingDB.connect(); testingEnvironment.setRequestId(); - db = DB.connectionForDB(config.SHARED_DB).getClient().db(config.SHARED_DB); + db = testingDB.db(config.SHARED_DB); }); beforeEach(async () => { diff --git a/app/api/utils/testing_db.ts b/app/api/utils/testing_db.ts index a518f6a4ca..8053e6ecf2 100644 --- a/app/api/utils/testing_db.ts +++ b/app/api/utils/testing_db.ts @@ -81,6 +81,7 @@ const testingDB: { mongodb: Db | null; dbName: string; UserInContextMockFactory: UserInContextMockFactory; + db: (dbName: string) => Db; connect: (options?: { defaultTenant: boolean } | undefined) => Promise; disconnect: () => Promise; tearDown: () => Promise; @@ -124,6 +125,10 @@ const testingDB: { return mongooseConnection; }, + db(dbName: string) { + return DB.mongodb_Db(dbName); + }, + async tearDown() { await this.disconnect(); },