From 584ca4778af47adabdc4f434bfb69103454fa351 Mon Sep 17 00:00:00 2001 From: Joan Gallego Girona Date: Fri, 17 Jan 2025 13:04:46 +0100 Subject: [PATCH] Revert "Update mongoose and mongodb driver (#7588)" (#7594) This reverts commit 0cce9e8bfe298fa8a1b9968b15238784b69669f4. --- app/api/auth/privateInstanceMiddleware.js | 1 + .../common.v2/database/CollectionWrapper.ts | 57 ++---------- app/api/common.v2/database/MongoDataSource.ts | 10 +-- .../database/SessionScopedCollection.ts | 9 +- .../common.v2/database/SyncedCollection.ts | 12 +-- .../database/getConnectionForCurrentTenant.ts | 18 +--- .../database/specs/MongoResultSet.spec.ts | 20 ++--- app/api/config.ts | 3 - .../database/MongoEntitiesDataSource.ts | 2 +- ...ons-for-v2-relationships-migration.spec.ts | 4 +- .../18-fix-malformed-metadata/index.js | 2 +- app/api/odm/DB.ts | 5 -- app/api/odm/MultiTenantMongooseModel.ts | 8 +- app/api/odm/model.ts | 17 +--- app/api/odm/specs/DB.spec.ts | 5 +- .../odm/specs/EntitiesUpdateLogHelper.spec.ts | 2 +- app/api/odm/specs/model_multi_tenant.spec.ts | 7 +- .../MongoRelationshipTypesDataSource.ts | 5 +- .../specs/PDFSegmentation.spec.ts | 6 +- app/api/stats/routes.ts | 6 +- .../stats/specs/RetrieveStatsService.spec.ts | 2 +- app/api/sync/syncConfig.ts | 3 +- app/api/sync/synchronizer.ts | 4 +- app/api/tenants/specs/tenantsContext.spec.ts | 3 +- app/api/tenants/specs/tenantsModel.spec.ts | 3 +- app/api/tenants/tenantsModel.ts | 10 +-- app/api/updatelogs/updatelogsModel.ts | 2 - app/api/utils/testing_db.ts | 9 +- docker-compose.yml | 1 + package.json | 4 +- yarn.lock | 90 ++++++++++++------- 31 files changed, 136 insertions(+), 194 deletions(-) diff --git a/app/api/auth/privateInstanceMiddleware.js b/app/api/auth/privateInstanceMiddleware.js index 167f80a644..fe0f898edf 100644 --- a/app/api/auth/privateInstanceMiddleware.js +++ b/app/api/auth/privateInstanceMiddleware.js @@ -18,6 +18,7 @@ export default function (req, res, next) { if (req.user || req.url.match(allowedRoutesMatch)) { return next(); } + return settings .get() .then(result => { diff --git a/app/api/common.v2/database/CollectionWrapper.ts b/app/api/common.v2/database/CollectionWrapper.ts index 56ab0b119a..0b96f0ba76 100644 --- a/app/api/common.v2/database/CollectionWrapper.ts +++ b/app/api/common.v2/database/CollectionWrapper.ts @@ -27,8 +27,6 @@ import { OrderedBulkOperation, UnorderedBulkOperation, ListSearchIndexesCursor, - IndexDescriptionCompact, - IndexDescriptionInfo, } from 'mongodb'; export abstract class CollectionWrapper { @@ -129,12 +127,20 @@ export abstract class CollectionWrapper { throw new Error('Method not implemented.'); } + async indexInformation(_options?: IndexInformationOptions | undefined): Promise { + throw new Error('Method not implemented.'); + } + async estimatedDocumentCount( _options?: EstimatedDocumentCountOptions | undefined ): Promise { throw new Error('Method not implemented.'); } + async indexes(_options?: IndexInformationOptions | undefined): Promise { + throw new Error('Method not implemented.'); + } + watch>( _pipeline?: Document[] | undefined, _options?: ChangeStreamOptions | undefined @@ -169,10 +175,6 @@ export abstract class CollectionWrapper { throw new Error('Method not implemented.'); } - get timeoutMS(): number | undefined { - throw new Error('Method not implemented.'); - } - async dropSearchIndex(): Promise { throw new Error('Method not implemented.'); } @@ -180,47 +182,4 @@ export abstract class CollectionWrapper { async updateSearchIndex(): Promise { throw new Error('Method not implemented.'); } - - async indexInformation( - options: IndexInformationOptions & { - full: true; - } - ): Promise; - - async indexInformation( - options: IndexInformationOptions & { - full?: false; - } - ): Promise; - - async indexInformation(): Promise; - - async indexInformation( - options?: IndexInformationOptions - ): Promise { - return this.collection.indexInformation({ - ...options, - full: options?.full ?? false, - }); - } - - async indexes( - options: IndexInformationOptions & { full?: true } - ): Promise; - - async indexes( - options: IndexInformationOptions & { full: false } - ): Promise; - - async indexes( - options: IndexInformationOptions - ): Promise; - - async indexes(options?: ListIndexesOptions): Promise; - - async indexes( - options?: IndexInformationOptions - ): Promise { - return this.collection.indexes(options); - } } diff --git a/app/api/common.v2/database/MongoDataSource.ts b/app/api/common.v2/database/MongoDataSource.ts index 66c29dbf47..3399d1f67b 100644 --- a/app/api/common.v2/database/MongoDataSource.ts +++ b/app/api/common.v2/database/MongoDataSource.ts @@ -4,7 +4,7 @@ import { MongoTransactionManager } from './MongoTransactionManager'; import { SessionScopedCollection } from './SessionScopedCollection'; import { SyncedCollection } from './SyncedCollection'; -export abstract class MongoDataSource { +export abstract class MongoDataSource { private db: Db; protected abstract collectionName: string; @@ -17,9 +17,9 @@ export abstract class MongoDataSource { } protected getCollection(collectionName = this.collectionName) { - return new SyncedCollection( - new SessionScopedCollection( - this.db.collection(collectionName), + return new SyncedCollection( + new SessionScopedCollection( + this.db.collection(collectionName), this.transactionManager ), this.transactionManager, @@ -45,6 +45,6 @@ export abstract class MongoDataSource { } protected createBulkStream() { - return new BulkWriteStream(this.getCollection()); + return new BulkWriteStream(this.getCollection()); } } diff --git a/app/api/common.v2/database/SessionScopedCollection.ts b/app/api/common.v2/database/SessionScopedCollection.ts index 3bf0f0173a..7c13c203aa 100644 --- a/app/api/common.v2/database/SessionScopedCollection.ts +++ b/app/api/common.v2/database/SessionScopedCollection.ts @@ -72,8 +72,8 @@ export class SessionScopedCollection } async bulkWrite( - operations: ReadonlyArray>, - options?: BulkWriteOptions + operations: AnyBulkWriteOperation[], + options?: BulkWriteOptions | undefined ): Promise { return this.collection.bulkWrite(operations, this.appendSession(options)); } @@ -130,7 +130,10 @@ export class SessionScopedCollection return this.collection.find(filter || {}, this.appendSession(options)); } - async countDocuments(filter?: Filter, options?: CountDocumentsOptions): Promise { + async countDocuments( + filter?: Document | undefined, + options?: CountDocumentsOptions | undefined + ): Promise { return this.collection.countDocuments(filter, this.appendSession(options)); } diff --git a/app/api/common.v2/database/SyncedCollection.ts b/app/api/common.v2/database/SyncedCollection.ts index 031483a7c3..ffd8f2cd5b 100644 --- a/app/api/common.v2/database/SyncedCollection.ts +++ b/app/api/common.v2/database/SyncedCollection.ts @@ -121,15 +121,17 @@ export class SyncedCollection } async bulkWrite( - operations: ReadonlyArray>, + operations: AnyBulkWriteOperation[], options?: BulkWriteOptions | undefined ): Promise { const updateConditions = operations .map((op: any) => op.updateOne?.filter || op.updateMany?.filter) .filter((op: any) => op); + const deleteConditions = operations .map((op: any) => op.deleteOne?.filter || op.deleteMany?.filter) .filter((op: any) => op); + await this.upsertSyncLogs(deleteConditions, true); const result = await this.collection.bulkWrite(operations, options); await Promise.all([ @@ -143,8 +145,8 @@ export class SyncedCollection async updateOne( filter: Filter, - update: UpdateFilter | Document[], - options?: UpdateOptions + update: UpdateFilter | Partial, + options?: UpdateOptions | undefined ): Promise> { const result = await this.collection.updateOne(filter, update, options); await this.upsertSyncLogs([filter]); @@ -202,8 +204,8 @@ export class SyncedCollection } async countDocuments( - filter?: Filter, - options?: CountDocumentsOptions + filter?: Document | undefined, + options?: CountDocumentsOptions | undefined ): Promise { return this.collection.countDocuments(filter, options); } diff --git a/app/api/common.v2/database/getConnectionForCurrentTenant.ts b/app/api/common.v2/database/getConnectionForCurrentTenant.ts index 72e763da64..e34c9781bd 100644 --- a/app/api/common.v2/database/getConnectionForCurrentTenant.ts +++ b/app/api/common.v2/database/getConnectionForCurrentTenant.ts @@ -9,25 +9,11 @@ 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('DB object is undefined'); - } - return db; + return DB.connectionForDB(getTenant().dbName).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('DB object is undefined'); - } - return db; + return DB.connectionForDB(config.SHARED_DB).db; } function getClient(): MongoClient { diff --git a/app/api/common.v2/database/specs/MongoResultSet.spec.ts b/app/api/common.v2/database/specs/MongoResultSet.spec.ts index 0b4ba58d89..6335a7b0e9 100644 --- a/app/api/common.v2/database/specs/MongoResultSet.spec.ts +++ b/app/api/common.v2/database/specs/MongoResultSet.spec.ts @@ -111,9 +111,7 @@ describe('when built from a $type cursor', () => { const cursor = buildCursor(); const resultSet = new MongoResultSet(cursor!, elem => elem.name); expect(await resultSet.find(item => item.startsWith('doc2'))).toBe('doc2'); - //Due to a mongodb driver bug this is failing but its not affecting us for now, - //im leaving this as false so we know in case it gets fixed - expect(cursor?.closed).toBe(false); + expect(cursor?.closed).toBe(true); }); it('should return null if no item matches the query', async () => { @@ -136,9 +134,7 @@ describe('when built from a $type cursor', () => { const cursor = buildCursor(); const resultSet = new MongoResultSet(cursor!, elem => elem.name); expect(await resultSet.every(item => item.startsWith('doc1'))).toBe(false); - //Due to a mongodb driver bug this is failing but its not affecting us for now, - //im leaving this as false so we know in case it gets fixed - expect(cursor?.closed).toBe(false); + expect(cursor?.closed).toBe(true); }); it('should return true if there are no items', async () => { @@ -154,9 +150,7 @@ describe('when built from a $type cursor', () => { const cursor = buildCursor(); const resultSet = new MongoResultSet(cursor!, elem => elem.name); expect(await resultSet.some(item => item === 'doc3')).toBe(true); - //Due to a mongodb driver bug this is failing but its not affecting us for now, - //im leaving this as false so we know in case it gets fixed - expect(cursor?.closed).toBe(false); + expect(cursor?.closed).toBe(true); }); it('should return false if it is false for every item', async () => { @@ -225,9 +219,7 @@ describe('when built from a $type cursor', () => { } }); expect(visited).toEqual(['doc1', 'doc2']); - //Due to a mongodb driver bug this is failing but its not affecting us for now, - //im leaving this as false so we know in case it gets fixed - expect(cursor?.closed).toBe(false); + expect(cursor?.closed).toBe(true); }); }); @@ -280,9 +272,7 @@ describe('when built from a $type cursor', () => { ['doc1', 'doc2'], ['doc3', 'doc4'], ]); - //Due to a mongodb driver bug this is failing but its not affecting us for now, - //im leaving this as false so we know in case it gets fixed - expect(cursor?.closed).toBe(false); + expect(cursor?.closed).toBe(true); }); }); diff --git a/app/api/config.ts b/app/api/config.ts index ef7bb655d6..4131b97a7a 100644 --- a/app/api/config.ts +++ b/app/api/config.ts @@ -93,7 +93,4 @@ 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/entities.v2/database/MongoEntitiesDataSource.ts b/app/api/entities.v2/database/MongoEntitiesDataSource.ts index e580f99bd9..7dea6751b4 100644 --- a/app/api/entities.v2/database/MongoEntitiesDataSource.ts +++ b/app/api/entities.v2/database/MongoEntitiesDataSource.ts @@ -5,7 +5,6 @@ import { MongoResultSet } from 'api/common.v2/database/MongoResultSet'; import { MongoTransactionManager } from 'api/common.v2/database/MongoTransactionManager'; import entities from 'api/entities/entities'; import v1EntitiesModel from 'api/entities/entitiesModel'; -import { search } from 'api/search'; import { MongoSettingsDataSource } from 'api/settings.v2/database/MongoSettingsDataSource'; import { MongoTemplatesDataSource } from 'api/templates.v2/database/MongoTemplatesDataSource'; import { Db } from 'mongodb'; @@ -14,6 +13,7 @@ import { EntitiesDataSource } from '../contracts/EntitiesDataSource'; import { Entity, EntityMetadata, MetadataValue } from '../model/Entity'; import { EntityMappers } from './EntityMapper'; import { EntityDBO, EntityJoinTemplate } from './schemas/EntityTypes'; +import { search } from 'api/search'; export class MongoEntitiesDataSource extends MongoDataSource diff --git a/app/api/migrations/migrations/141-add-collections-for-v2-relationships-migration/specs/141-add-collections-for-v2-relationships-migration.spec.ts b/app/api/migrations/migrations/141-add-collections-for-v2-relationships-migration/specs/141-add-collections-for-v2-relationships-migration.spec.ts index d26a0c9f7b..4841b8b340 100644 --- a/app/api/migrations/migrations/141-add-collections-for-v2-relationships-migration/specs/141-add-collections-for-v2-relationships-migration.spec.ts +++ b/app/api/migrations/migrations/141-add-collections-for-v2-relationships-migration/specs/141-add-collections-for-v2-relationships-migration.spec.ts @@ -55,12 +55,12 @@ describe('migration add collections for v2 relationships migration', () => { }); it('should set unique index on the migration fields', async () => { - const relCollection = db.collection('relationshipMigrationFields'); + const relCollection = await db.collection('relationshipMigrationFields'); const indexInfo = await relCollection.indexInformation({ full: true }); const uniqueIndex = indexInfo.find( (index: any) => index.name === 'sourceTemplate_1_relationType_1_targetTemplate_1' ); - expect(uniqueIndex?.unique).toBe(true); + expect(uniqueIndex.unique).toBe(true); }); it('should check if a reindex is needed', async () => { diff --git a/app/api/migrations/migrations/18-fix-malformed-metadata/index.js b/app/api/migrations/migrations/18-fix-malformed-metadata/index.js index 9f91d31d71..1237854c83 100644 --- a/app/api/migrations/migrations/18-fix-malformed-metadata/index.js +++ b/app/api/migrations/migrations/18-fix-malformed-metadata/index.js @@ -24,7 +24,7 @@ const findLabel = (value, propertyData, thesauriById, translation) => { [] ); - const thesaurusElement = flattenedValues.find(v => v.id.toString() === value.toString()); + const thesaurusElement = flattenedValues.find(v => v.id === value.toString()); if (thesaurusElement) { const context = translation.contexts.find( ctx => ctx.id.toString() === propertyData.content.toString() diff --git a/app/api/odm/DB.ts b/app/api/odm/DB.ts index 4a1c50a2c0..948ceee3cb 100644 --- a/app/api/odm/DB.ts +++ b/app/api/odm/DB.ts @@ -1,6 +1,5 @@ import mongoose, { Connection, ConnectOptions } from 'mongoose'; import { config } from 'api/config'; -import { DbOptions } from 'mongodb'; let connection: Connection; @@ -26,10 +25,6 @@ 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/MultiTenantMongooseModel.ts b/app/api/odm/MultiTenantMongooseModel.ts index 99862a4d99..1da4ebc474 100644 --- a/app/api/odm/MultiTenantMongooseModel.ts +++ b/app/api/odm/MultiTenantMongooseModel.ts @@ -1,5 +1,5 @@ import { BulkWriteOptions } from 'mongodb'; -import mongoose, { ProjectionType, Schema } from 'mongoose'; +import mongoose, { Schema } from 'mongoose'; import { DataType, UwaziFilterQuery, @@ -36,11 +36,7 @@ class MultiTenantMongooseModel { return this.dbForCurrentTenant().findById(id, select, { lean: true }); } - find( - query: UwaziFilterQuery>, - select: ProjectionType> = {}, - options = {} - ) { + find(query: UwaziFilterQuery>, select = '', options = {}) { return this.dbForCurrentTenant().find(query, select, options); } diff --git a/app/api/odm/model.ts b/app/api/odm/model.ts index 0c9a22096e..26ace86cad 100644 --- a/app/api/odm/model.ts +++ b/app/api/odm/model.ts @@ -153,21 +153,10 @@ export class OdmModel implements SyncDBDataSource { }); const existingIds = new Set( ( - await this.db.find( - { _id: { $in: ids } }, - { _id: 1 }, - { - lean: true, - } - ) - ) - .map(d => { - if (d._id) { - return d._id.toString(); - } - return null; + await this.db.find({ _id: { $in: ids } } as UwaziFilterQuery>, '_id', { + lean: true, }) - .filter((id): id is string => typeof id === 'string') + ).map(d => d._id.toString()) ); const existingData = dataArray.filter(d => d._id && existingIds.has(d._id.toString())); diff --git a/app/api/odm/specs/DB.spec.ts b/app/api/odm/specs/DB.spec.ts index 85b78f9a22..85ac82fd1d 100644 --- a/app/api/odm/specs/DB.spec.ts +++ b/app/api/odm/specs/DB.spec.ts @@ -7,7 +7,6 @@ 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 }, @@ -28,8 +27,8 @@ describe('DB', () => { beforeEach(async () => { const uri = config.DBHOST; await DB.connect(`${uri}_DB_spec_ts`); - db1 = testingDB.db('db1'); - db2 = testingDB.db('db2'); + db1 = DB.getConnection().useDb('db1').db; + db2 = DB.getConnection().useDb('db2').db; }); afterAll(async () => { diff --git a/app/api/odm/specs/EntitiesUpdateLogHelper.spec.ts b/app/api/odm/specs/EntitiesUpdateLogHelper.spec.ts index c7f1945334..0cfd6e04f1 100644 --- a/app/api/odm/specs/EntitiesUpdateLogHelper.spec.ts +++ b/app/api/odm/specs/EntitiesUpdateLogHelper.spec.ts @@ -51,7 +51,7 @@ describe('EntitiesUpdateLogHelper', () => { fixtureFactory.id('files-thumbnail1'), fixtureFactory.id('files-document1'), ].forEach(id => { - const original = fixtures.updatelogs.find(log => log._id!.toString() === id.toString()); + const original = fixtures.updatelogs.find(log => log._id.toString() === id.toString()); const current = logs.find(log => log._id.toString() === id.toString()); expect(current!.timestamp).toBeGreaterThan(original!.timestamp! as number); diff --git a/app/api/odm/specs/model_multi_tenant.spec.ts b/app/api/odm/specs/model_multi_tenant.spec.ts index e3c9c36543..c6234e4139 100644 --- a/app/api/odm/specs/model_multi_tenant.spec.ts +++ b/app/api/odm/specs/model_multi_tenant.spec.ts @@ -5,6 +5,7 @@ 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, @@ -22,9 +23,9 @@ describe('ODM Model multi-tenant', () => { beforeAll(async () => { await testingDB.connect({ defaultTenant: false }); - defaultDB = testingDB.db(config.defaultTenant.dbName); - db1 = testingDB.db('db1'); - db2 = testingDB.db('db2'); + defaultDB = DB.connectionForDB(config.defaultTenant.dbName).db; + db1 = DB.connectionForDB('db1').db; + db2 = DB.connectionForDB('db2').db; }); beforeEach(async () => { diff --git a/app/api/relationshiptypes.v2/database/MongoRelationshipTypesDataSource.ts b/app/api/relationshiptypes.v2/database/MongoRelationshipTypesDataSource.ts index d7e59df44f..115b504e50 100644 --- a/app/api/relationshiptypes.v2/database/MongoRelationshipTypesDataSource.ts +++ b/app/api/relationshiptypes.v2/database/MongoRelationshipTypesDataSource.ts @@ -7,9 +7,8 @@ import { mapRelationshipTypeToApp } from './mappings/RelationshipTypeMappers'; import { RelationshipType } from '../model/RelationshipType'; export class MongoRelationshipTypesDataSource - extends MongoDataSource - implements RelationshipTypesDataSource -{ + extends MongoDataSource + implements RelationshipTypesDataSource { // eslint-disable-line protected collectionName = 'relationtypes'; async typesExist(ids: string[]): Promise { diff --git a/app/api/services/pdfsegmentation/specs/PDFSegmentation.spec.ts b/app/api/services/pdfsegmentation/specs/PDFSegmentation.spec.ts index 623b783a3c..745b7dd882 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 testingDB, { fixturer } from 'api/utils/testing_db'; +import { fixturer } from 'api/utils/testing_db'; import { fixturesOneFile, fixturesOtherFile, @@ -79,8 +79,8 @@ describe('PDFSegmentation', () => { beforeEach(async () => { segmentPdfs = new PDFSegmentation(); - dbOne = testingDB.db(tenantOne.dbName); - dbTwo = testingDB.db(tenantTwo.dbName); + dbOne = DB.connectionForDB(tenantOne.dbName).db; + dbTwo = DB.connectionForDB(tenantTwo.dbName).db; 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 3d1a081a20..4f2f9695ce 100644 --- a/app/api/stats/routes.ts +++ b/app/api/stats/routes.ts @@ -1,11 +1,13 @@ import { Application } from 'express'; import needsAuthorization from 'api/auth/authMiddleware'; import { RetrieveStatsService } from 'api/stats/services/RetrieveStatsService'; -import { getConnection } from 'api/common.v2/database/getConnectionForCurrentTenant'; +import { tenants } from 'api/tenants'; +import { DB } from 'api/odm'; export default (app: Application) => { app.get('/api/stats', needsAuthorization(['admin']), async (_req, res, _next) => { - const action = new RetrieveStatsService(getConnection()); + const { db } = DB.connectionForDB(tenants.current().dbName); + const action = new RetrieveStatsService(db); const stats = await action.execute(); res.json(stats); diff --git a/app/api/stats/specs/RetrieveStatsService.spec.ts b/app/api/stats/specs/RetrieveStatsService.spec.ts index ec3f166894..8036b8c6b1 100644 --- a/app/api/stats/specs/RetrieveStatsService.spec.ts +++ b/app/api/stats/specs/RetrieveStatsService.spec.ts @@ -10,7 +10,7 @@ describe('RetrieveStats', () => { let db: Db; beforeAll(async () => { - db = (await testingDB.connect()).db as Db; + db = (await testingDB.connect()).db; }); beforeEach(async () => { diff --git a/app/api/sync/syncConfig.ts b/app/api/sync/syncConfig.ts index 15e9f367fc..aec4e60baa 100644 --- a/app/api/sync/syncConfig.ts +++ b/app/api/sync/syncConfig.ts @@ -1,3 +1,4 @@ +import { DataType } from 'api/odm'; import { SyncConfig } from 'api/sync/syncWorker'; import templatesModel from 'api/templates/templatesModel'; import { model as updateLog, UpdateLog } from 'api/updatelogs'; @@ -162,7 +163,7 @@ export const createSyncConfig = async ( return changes; }, - async shouldSync(change: UpdateLog) { + async shouldSync(change: DataType) { if (change.deleted) return { skip: true }; const templatesConfig = this.config.templates || {}; diff --git a/app/api/sync/synchronizer.ts b/app/api/sync/synchronizer.ts index da71ac9e3c..f7c7552fae 100644 --- a/app/api/sync/synchronizer.ts +++ b/app/api/sync/synchronizer.ts @@ -22,7 +22,7 @@ const uploadFile = async ( }; export const synchronizer = { - async syncDelete(change: UpdateLog, url: string, cookie: string) { + async syncDelete(change: DataType, url: string, cookie: string) { await this.syncData( { url, @@ -40,7 +40,7 @@ export const synchronizer = { change, data, cookie, - }: { url: string; change: UpdateLog; data: DataType; cookie: string }, + }: { url: string; change: DataType; data: DataType; cookie: string }, action: keyof typeof request ) { await request[action]( diff --git a/app/api/tenants/specs/tenantsContext.spec.ts b/app/api/tenants/specs/tenantsContext.spec.ts index 68610c1fee..5378623a4d 100644 --- a/app/api/tenants/specs/tenantsContext.spec.ts +++ b/app/api/tenants/specs/tenantsContext.spec.ts @@ -1,3 +1,4 @@ +import { DB } from 'api/odm/DB'; import { Db } from 'mongodb'; import testingDB from 'api/utils/testing_db'; import { testingEnvironment } from 'api/utils/testingEnvironment'; @@ -25,7 +26,7 @@ describe('tenantsContext', () => { beforeAll(async () => { await testingDB.connect(); testingEnvironment.setRequestId(); - db = testingDB.db(config.SHARED_DB); + db = DB.connectionForDB(config.SHARED_DB).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 84d94ee36b..5d5c057ae7 100644 --- a/app/api/tenants/specs/tenantsModel.spec.ts +++ b/app/api/tenants/specs/tenantsModel.spec.ts @@ -1,4 +1,5 @@ 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'; @@ -16,7 +17,7 @@ describe('tenantsModel', () => { beforeAll(async () => { await testingDB.connect(); testingEnvironment.setRequestId(); - db = testingDB.db(config.SHARED_DB); + db = DB.connectionForDB(config.SHARED_DB).db; }); beforeEach(async () => { diff --git a/app/api/tenants/tenantsModel.ts b/app/api/tenants/tenantsModel.ts index ea5d228bb6..f8929762b2 100644 --- a/app/api/tenants/tenantsModel.ts +++ b/app/api/tenants/tenantsModel.ts @@ -76,19 +76,15 @@ class TenantsModel extends EventEmitter { } async initialize() { - const { db } = this.tenantsDB; - if (!db) { - throw new Error('Tenants db is undefined'); - } - const collections = (await db.listCollections().toArray()).map(c => c.name); + const collections = (await this.tenantsDB.db.listCollections().toArray()).map(c => c.name); if (collections.includes(this.collectionName)) { - await db.command({ + await this.tenantsDB.db.command({ collMod: this.collectionName, validator: schemaValidator, }); } else { - await db.createCollection(this.collectionName, { + await this.tenantsDB.db.createCollection(this.collectionName, { validator: schemaValidator, }); } diff --git a/app/api/updatelogs/updatelogsModel.ts b/app/api/updatelogs/updatelogsModel.ts index a11a5e117a..29c579b2ef 100644 --- a/app/api/updatelogs/updatelogsModel.ts +++ b/app/api/updatelogs/updatelogsModel.ts @@ -1,7 +1,6 @@ import mongoose from 'mongoose'; import { MultiTenantMongooseModel } from 'api/odm/MultiTenantMongooseModel'; import { ObjectIdSchema } from 'shared/types/commonTypes'; -import { ObjectId } from 'mongodb'; const updateLogSchema = new mongoose.Schema({ timestamp: { type: Number, index: true }, @@ -12,7 +11,6 @@ const updateLogSchema = new mongoose.Schema({ updateLogSchema.index({ namespace: 1, timestamp: 1 }); export interface UpdateLog extends mongoose.Document { - _id: ObjectId; timestamp: number; namespace: string; mongoId: ObjectIdSchema; diff --git a/app/api/utils/testing_db.ts b/app/api/utils/testing_db.ts index 43c814e49c..cfe6e55020 100644 --- a/app/api/utils/testing_db.ts +++ b/app/api/utils/testing_db.ts @@ -81,7 +81,6 @@ const testingDB: { mongodb: Db | null; dbName: string; UserInContextMockFactory: UserInContextMockFactory; - db: (dbName: string) => Db; connect: (options?: { defaultTenant: boolean } | undefined) => Promise; disconnect: () => Promise; tearDown: () => Promise; @@ -109,7 +108,7 @@ const testingDB: { .basename(expect.getState().testPath || '') .replace(/[.-]/g, '_')}`.substring(0, 63); await initMongoServer(this.dbName); - mongodb = this.db(this.dbName); + mongodb = mongooseConnection.db; this.mongodb = mongodb; if (options.defaultTenant) { @@ -125,10 +124,6 @@ const testingDB: { return mongooseConnection; }, - db(dbName: string) { - return DB.mongodb_Db(dbName); - }, - async tearDown() { await this.disconnect(); }, @@ -153,7 +148,7 @@ const testingDB: { await this.connect(); let optionalMongo: Db | null = null; if (dbName) { - optionalMongo = DB.connectionForDB(dbName).getClient().db(dbName); + optionalMongo = DB.connectionForDB(dbName).db; } await fixturer.clearAllAndLoad(optionalMongo || mongodb, fixtures); await this.createIndices(); diff --git a/docker-compose.yml b/docker-compose.yml index 3d77ee5311..268bf3289c 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,3 +1,4 @@ +version: '3' services: elasticsearch: build: diff --git a/package.json b/package.json index 1c1d4d5db3..ac8398eedd 100644 --- a/package.json +++ b/package.json @@ -175,8 +175,8 @@ "moment-timezone": "0.5.46", "monaco-editor": "0.50.0", "monaco-editor-webpack-plugin": "^7.1.0", - "mongodb": "6.12.0", - "mongoose": "8.9.4", + "mongodb": "6.3.0", + "mongoose": "8.1.2", "multer": "^1.4.5-lts.1", "node-uuid": "^1.4.7", "nodemailer": "6.9.16", diff --git a/yarn.lock b/yarn.lock index 8d6852daa7..a9c2c490f5 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2823,10 +2823,10 @@ dependencies: "@types/mdx" "^2.0.0" -"@mongodb-js/saslprep@^1.1.9": - version "1.1.9" - resolved "https://registry.yarnpkg.com/@mongodb-js/saslprep/-/saslprep-1.1.9.tgz#e974bab8eca9faa88677d4ea4da8d09a52069004" - integrity sha512-tVkljjeEaAhCqTzajSdgbQ6gE6f3oneVwa3iXR6csiEwXXOFsiC6Uh9iAjAhXPtqa/XMDHWjjeNH/77m/Yq2dw== +"@mongodb-js/saslprep@^1.1.0": + version "1.1.0" + resolved "https://registry.yarnpkg.com/@mongodb-js/saslprep/-/saslprep-1.1.0.tgz#022fa36620a7287d17acd05c4aae1e5f390d250d" + integrity sha512-Xfijy7HvfzzqiOAhAepF4SGN5e9leLkMvg/OPOF97XemjfVCYN/oWa75wnkc6mltMSTwY+XlbhWgUOJmkFspSw== dependencies: sparse-bitfield "^3.0.3" @@ -6098,10 +6098,10 @@ bser@^2.0.0: dependencies: node-int64 "^0.4.0" -bson@^6.10.1: - version "6.10.1" - resolved "https://registry.yarnpkg.com/bson/-/bson-6.10.1.tgz#dcd04703178f5ecf5b25de04edd2a95ec79385d3" - integrity sha512-P92xmHDQjSKPLHqFxefqMxASNq/aWJMEZugpCjf+AF/pgcUpMMQCg7t7+ewko0/u8AapvF3luf/FoehddEK+sA== +bson@^6.2.0: + version "6.5.0" + resolved "https://registry.yarnpkg.com/bson/-/bson-6.5.0.tgz#fc4828d065e64e48ea442b1a23099b2e52f7ff0b" + integrity sha512-DXf1BTAS8vKyR90BO4x5v3rKVarmkdkzwOrnYDFdjAY694ILNDkmA3uRh1xXJEl+C1DAh8XCvAQ+Gh3kzubtpg== buffer-crc32@^1.0.0: version "1.0.0" @@ -11424,10 +11424,10 @@ jvent@1.0.2: version "1.0.2" resolved "https://registry.npmjs.org/jvent/-/jvent-1.0.2.tgz" -kareem@2.6.3: - version "2.6.3" - resolved "https://registry.yarnpkg.com/kareem/-/kareem-2.6.3.tgz#23168ec8ffb6c1abfd31b7169a6fb1dd285992ac" - integrity sha512-C3iHfuGUXK2u8/ipq9LfjFfXFxAZMQJJq7vLS45r3D9Y2xQ/m4S8zaR4zMLFWh9AsNPXmcFfUDhTEO8UIC/V6Q== +kareem@2.5.1: + version "2.5.1" + resolved "https://registry.yarnpkg.com/kareem/-/kareem-2.5.1.tgz#7b8203e11819a8e77a34b3517d3ead206764d15d" + integrity sha512-7jFxRVm+jD+rkq3kY0iZDJfsO2/t4BBPeEb2qKn2lR/9KhuksYk5hxzfRYWMPV8P/x2d0kHD306YyWLzjjH+uA== keyv@^4.0.0: version "4.3.3" @@ -12203,27 +12203,27 @@ mongodb-connection-string-url@^3.0.0: "@types/whatwg-url" "^11.0.2" whatwg-url "^13.0.0" -mongodb@6.12.0, mongodb@~6.12.0: - version "6.12.0" - resolved "https://registry.yarnpkg.com/mongodb/-/mongodb-6.12.0.tgz#8b0bda1b18cbb3f0aec8ab4119c5dc535a43c444" - integrity sha512-RM7AHlvYfS7jv7+BXund/kR64DryVI+cHbVAy9P61fnb1RcWZqOW1/Wj2YhqMCx+MuYhqTRGv7AwHBzmsCKBfA== +mongodb@6.3.0: + version "6.3.0" + resolved "https://registry.yarnpkg.com/mongodb/-/mongodb-6.3.0.tgz#ec9993b19f7ed2ea715b903fcac6171c9d1d38ca" + integrity sha512-tt0KuGjGtLUhLoU263+xvQmPHEGTw5LbcNC73EoFRYgSHwZt5tsoJC110hDyO1kjQzpgNrpdcSza9PknWN4LrA== dependencies: - "@mongodb-js/saslprep" "^1.1.9" - bson "^6.10.1" + "@mongodb-js/saslprep" "^1.1.0" + bson "^6.2.0" mongodb-connection-string-url "^3.0.0" -mongoose@8.9.4: - version "8.9.4" - resolved "https://registry.yarnpkg.com/mongoose/-/mongoose-8.9.4.tgz#de93b9ec27a9384d013312e835667e540fd1dcef" - integrity sha512-DndoI01aV/q40P9DiYDXsYjhj8vZjmmuFwcC3Tro5wFznoE1z6Fe2JgMnbLR6ghglym5ziYizSfAJykp+UPZWg== +mongoose@8.1.2: + version "8.1.2" + resolved "https://registry.yarnpkg.com/mongoose/-/mongoose-8.1.2.tgz#f8a91da3e8c4b7489d4cbf35c20cd6908bbfcbce" + integrity sha512-5KMq7k6KmFCIB8/YMKMFsWdsdNkBwuARDRHDRpp5GKC78eT0LwHIaMEKo6gDUg3zBuMoy9OdcM/6f4dkW06C/A== dependencies: - bson "^6.10.1" - kareem "2.6.3" - mongodb "~6.12.0" + bson "^6.2.0" + kareem "2.5.1" + mongodb "6.3.0" mpath "0.9.0" mquery "5.0.0" ms "2.1.3" - sift "17.1.3" + sift "16.0.1" mpath@0.9.0: version "0.9.0" @@ -14945,7 +14945,12 @@ side-channel@^1.0.4, side-channel@^1.0.6: get-intrinsic "^1.2.4" object-inspect "^1.13.1" -sift@17.1.3, sift@^17.1.3: +sift@16.0.1: + version "16.0.1" + resolved "https://registry.yarnpkg.com/sift/-/sift-16.0.1.tgz#e9c2ccc72191585008cf3e36fc447b2d2633a053" + integrity sha512-Wv6BjQ5zbhW7VFefWusVP33T/EM0vYikCaQ2qR8yULbsilAT8/wQaXvuQ3ptGLpoKx+lihJE3y2UTgKDyyNHZQ== + +sift@^17.1.3: version "17.1.3" resolved "https://registry.yarnpkg.com/sift/-/sift-17.1.3.tgz#9d2000d4d41586880b0079b5183d839c7a142bf7" integrity sha512-Rtlj66/b0ICeFzYTuNvX/EF1igRbbnGSvEyT79McoZa/DeGhMyC5pWKOEsZKnpkqtSeovd5FL/bjHWC3CIIvCQ== @@ -15284,7 +15289,16 @@ string-length@^4.0.1: char-regex "^1.0.2" strip-ansi "^6.0.0" -"string-width-cjs@npm:string-width@^4.2.0", "string-width@^1.0.2 || 2 || 3 || 4", string-width@^4.1.0, string-width@^4.2.0, string-width@^4.2.3: +"string-width-cjs@npm:string-width@^4.2.0": + version "4.2.3" + resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010" + integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g== + dependencies: + emoji-regex "^8.0.0" + is-fullwidth-code-point "^3.0.0" + strip-ansi "^6.0.1" + +"string-width@^1.0.2 || 2 || 3 || 4", string-width@^4.1.0, string-width@^4.2.0, string-width@^4.2.3: version "4.2.3" resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010" integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g== @@ -15388,7 +15402,14 @@ string_decoder@~1.1.1: dependencies: safe-buffer "~5.1.0" -"strip-ansi-cjs@npm:strip-ansi@^6.0.1", strip-ansi@^6.0.0, strip-ansi@^6.0.1: +"strip-ansi-cjs@npm:strip-ansi@^6.0.1": + version "6.0.1" + resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-6.0.1.tgz#9e26c63d30f53443e9489495b2105d37b67a85d9" + integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A== + dependencies: + ansi-regex "^5.0.1" + +strip-ansi@^6.0.0, strip-ansi@^6.0.1: version "6.0.1" resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-6.0.1.tgz#9e26c63d30f53443e9489495b2105d37b67a85d9" integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A== @@ -16761,7 +16782,7 @@ world-countries@5.0.0: resolved "https://registry.yarnpkg.com/world-countries/-/world-countries-5.0.0.tgz#6f75ebcce3d5224d84e9117eaf0d75a7726b6501" integrity sha512-wAfOT9Y5i/xnxNOdKJKXdOCw9Q3yQLahBUeuRol+s+o20F6h2a4tLEbJ1lBCYwEQ30Sf9Meqeipk1gib3YwF5w== -"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0", wrap-ansi@^7.0.0: +"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0": version "7.0.0" resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43" integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q== @@ -16779,6 +16800,15 @@ wrap-ansi@^6.2.0: string-width "^4.1.0" strip-ansi "^6.0.0" +wrap-ansi@^7.0.0: + version "7.0.0" + resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43" + integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q== + dependencies: + ansi-styles "^4.0.0" + string-width "^4.1.0" + strip-ansi "^6.0.0" + wrap-ansi@^8.1.0: version "8.1.0" resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-8.1.0.tgz#56dc22368ee570face1b49819975d9b9a5ead214"