From c9688996e02496b38448e7678a303a59afde76d3 Mon Sep 17 00:00:00 2001 From: Daneryl Date: Thu, 16 Jan 2025 15:28:56 +0100 Subject: [PATCH] fix ts type errors --- ...1-add-collections-for-v2-relationships-migration.spec.ts | 4 ++-- app/api/odm/specs/DB.spec.ts | 4 ++-- app/api/odm/specs/EntitiesUpdateLogHelper.spec.ts | 2 +- app/api/odm/specs/model_multi_tenant.spec.ts | 6 +++--- .../services/pdfsegmentation/specs/PDFSegmentation.spec.ts | 4 ++-- app/api/stats/routes.ts | 2 +- app/api/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 | 2 +- app/api/tenants/specs/tenantsModel.spec.ts | 2 +- app/api/updatelogs/updatelogsModel.ts | 2 ++ app/api/utils/testing_db.ts | 4 ++-- 13 files changed, 21 insertions(+), 20 deletions(-) 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 4841b8b340..d26a0c9f7b 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 = await db.collection('relationshipMigrationFields'); + const relCollection = 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/odm/specs/DB.spec.ts b/app/api/odm/specs/DB.spec.ts index 85ac82fd1d..4ef6881d1f 100644 --- a/app/api/odm/specs/DB.spec.ts +++ b/app/api/odm/specs/DB.spec.ts @@ -27,8 +27,8 @@ describe('DB', () => { beforeEach(async () => { const uri = config.DBHOST; await DB.connect(`${uri}_DB_spec_ts`); - db1 = DB.getConnection().useDb('db1').db; - db2 = DB.getConnection().useDb('db2').db; + db1 = DB.getConnection().getClient().db('db1'); + db2 = DB.getConnection().getClient().db('db2'); }); afterAll(async () => { diff --git a/app/api/odm/specs/EntitiesUpdateLogHelper.spec.ts b/app/api/odm/specs/EntitiesUpdateLogHelper.spec.ts index 0cfd6e04f1..c7f1945334 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 c6234e4139..075128c39b 100644 --- a/app/api/odm/specs/model_multi_tenant.spec.ts +++ b/app/api/odm/specs/model_multi_tenant.spec.ts @@ -23,9 +23,9 @@ describe('ODM Model multi-tenant', () => { beforeAll(async () => { await testingDB.connect({ defaultTenant: false }); - defaultDB = DB.connectionForDB(config.defaultTenant.dbName).db; - db1 = DB.connectionForDB('db1').db; - db2 = DB.connectionForDB('db2').db; + defaultDB = DB.getConnection().getClient().db(config.defaultTenant.dbName); + db1 = DB.getConnection().getClient().db('db1'); + db2 = DB.getConnection().getClient().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 745b7dd882..c4183faef8 100644 --- a/app/api/services/pdfsegmentation/specs/PDFSegmentation.spec.ts +++ b/app/api/services/pdfsegmentation/specs/PDFSegmentation.spec.ts @@ -79,8 +79,8 @@ describe('PDFSegmentation', () => { beforeEach(async () => { segmentPdfs = new PDFSegmentation(); - dbOne = DB.connectionForDB(tenantOne.dbName).db; - dbTwo = DB.connectionForDB(tenantTwo.dbName).db; + dbOne = DB.getConnection().getClient().db(tenantOne.dbName); + dbTwo = DB.getConnection().getClient().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 4f2f9695ce..4dd344dd44 100644 --- a/app/api/stats/routes.ts +++ b/app/api/stats/routes.ts @@ -6,7 +6,7 @@ import { DB } from 'api/odm'; export default (app: Application) => { app.get('/api/stats', needsAuthorization(['admin']), async (_req, res, _next) => { - const { db } = DB.connectionForDB(tenants.current().dbName); + const db = DB.getConnection().getClient().db(tenants.current().dbName); const action = new RetrieveStatsService(db); const stats = await action.execute(); diff --git a/app/api/stats/specs/RetrieveStatsService.spec.ts b/app/api/stats/specs/RetrieveStatsService.spec.ts index 8036b8c6b1..ec3f166894 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; + db = (await testingDB.connect()).db as Db; }); beforeEach(async () => { diff --git a/app/api/sync/syncConfig.ts b/app/api/sync/syncConfig.ts index aec4e60baa..15e9f367fc 100644 --- a/app/api/sync/syncConfig.ts +++ b/app/api/sync/syncConfig.ts @@ -1,4 +1,3 @@ -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'; @@ -163,7 +162,7 @@ export const createSyncConfig = async ( return changes; }, - async shouldSync(change: DataType) { + async shouldSync(change: UpdateLog) { 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 f7c7552fae..da71ac9e3c 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: DataType, url: string, cookie: string) { + async syncDelete(change: UpdateLog, url: string, cookie: string) { await this.syncData( { url, @@ -40,7 +40,7 @@ export const synchronizer = { change, data, cookie, - }: { url: string; change: DataType; data: DataType; cookie: string }, + }: { url: string; change: UpdateLog; 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 5378623a4d..d72ec0a084 100644 --- a/app/api/tenants/specs/tenantsContext.spec.ts +++ b/app/api/tenants/specs/tenantsContext.spec.ts @@ -26,7 +26,7 @@ describe('tenantsContext', () => { beforeAll(async () => { await testingDB.connect(); testingEnvironment.setRequestId(); - db = DB.connectionForDB(config.SHARED_DB).db; + db = DB.getConnection().getClient().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 5d5c057ae7..885055ef69 100644 --- a/app/api/tenants/specs/tenantsModel.spec.ts +++ b/app/api/tenants/specs/tenantsModel.spec.ts @@ -17,7 +17,7 @@ describe('tenantsModel', () => { beforeAll(async () => { await testingDB.connect(); testingEnvironment.setRequestId(); - db = DB.connectionForDB(config.SHARED_DB).db; + db = DB.connectionForDB(config.SHARED_DB).getClient().db(config.SHARED_DB); }); beforeEach(async () => { diff --git a/app/api/updatelogs/updatelogsModel.ts b/app/api/updatelogs/updatelogsModel.ts index 29c579b2ef..a11a5e117a 100644 --- a/app/api/updatelogs/updatelogsModel.ts +++ b/app/api/updatelogs/updatelogsModel.ts @@ -1,6 +1,7 @@ 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 }, @@ -11,6 +12,7 @@ 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 cfe6e55020..a518f6a4ca 100644 --- a/app/api/utils/testing_db.ts +++ b/app/api/utils/testing_db.ts @@ -108,7 +108,7 @@ const testingDB: { .basename(expect.getState().testPath || '') .replace(/[.-]/g, '_')}`.substring(0, 63); await initMongoServer(this.dbName); - mongodb = mongooseConnection.db; + mongodb = mongooseConnection.getClient().db(); this.mongodb = mongodb; if (options.defaultTenant) { @@ -148,7 +148,7 @@ const testingDB: { await this.connect(); let optionalMongo: Db | null = null; if (dbName) { - optionalMongo = DB.connectionForDB(dbName).db; + optionalMongo = DB.connectionForDB(dbName).getClient().db(dbName); } await fixturer.clearAllAndLoad(optionalMongo || mongodb, fixtures); await this.createIndices();