Skip to content

Commit

Permalink
use mongodb db instead of mongoose useDb
Browse files Browse the repository at this point in the history
use the mongodb one for all v2 things and tests
  • Loading branch information
daneryl committed Jan 17, 2025
1 parent c968899 commit e04b6b8
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 19 deletions.
10 changes: 8 additions & 2 deletions app/api/common.v2/database/getConnectionForCurrentTenant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
3 changes: 3 additions & 0 deletions app/api/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
};
5 changes: 5 additions & 0 deletions app/api/odm/DB.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import mongoose, { Connection, ConnectOptions } from 'mongoose';
import { config } from 'api/config';
import { DbOptions } from 'mongodb';

let connection: Connection;

Expand All @@ -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;
},
Expand Down
5 changes: 3 additions & 2 deletions app/api/odm/specs/DB.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand All @@ -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 () => {
Expand Down
7 changes: 3 additions & 4 deletions app/api/odm/specs/model_multi_tenant.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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}`);
Expand Down
6 changes: 2 additions & 4 deletions app/api/stats/routes.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
3 changes: 1 addition & 2 deletions app/api/tenants/specs/tenantsContext.spec.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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([
Expand Down
3 changes: 1 addition & 2 deletions app/api/tenants/specs/tenantsModel.spec.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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 () => {
Expand Down
5 changes: 5 additions & 0 deletions app/api/utils/testing_db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ const testingDB: {
mongodb: Db | null;
dbName: string;
UserInContextMockFactory: UserInContextMockFactory;
db: (dbName: string) => Db;
connect: (options?: { defaultTenant: boolean } | undefined) => Promise<Connection>;
disconnect: () => Promise<void>;
tearDown: () => Promise<void>;
Expand Down Expand Up @@ -124,6 +125,10 @@ const testingDB: {
return mongooseConnection;
},

db(dbName: string) {
return DB.mongodb_Db(dbName);
},

async tearDown() {
await this.disconnect();
},
Expand Down

0 comments on commit e04b6b8

Please sign in to comment.