diff --git a/.editorconfig b/.editorconfig index be3c347761..8496df79fc 100644 --- a/.editorconfig +++ b/.editorconfig @@ -7,7 +7,7 @@ end_of_line = lf insert_final_newline = true charset = utf-8 -[*.{js,jsx,json,html}] +[*.{js,jsx,json,html,ts,tsx}] indent_style = space indent_size = 4 diff --git a/app/database/manager/__mocks__/index.ts b/app/database/manager/__mocks__/index.ts index ec7ad1da27..a065be1361 100644 --- a/app/database/manager/__mocks__/index.ts +++ b/app/database/manager/__mocks__/index.ts @@ -48,7 +48,6 @@ import { DefaultNewServerArgs, GetDatabaseConnectionArgs, Models, - MostRecentConnection, RetrievedDatabase, } from '@typings/database/database'; import {DatabaseType} from '@typings/database/enums'; @@ -151,7 +150,7 @@ class DatabaseManager { } if (setAsActiveDatabase) { - await this.setMostRecentServerConnection(serverUrl); + await this.setActiveServerDatabase(serverUrl); } return connection; @@ -196,12 +195,12 @@ class DatabaseManager { }; /** - * setMostRecentServerConnection: Set the new active server database. The serverUrl is used to ensure that we do not duplicate entries in the default database. + * setActiveServerDatabase: Set the new active server database. The serverUrl is used to ensure that we do not duplicate entries in the default database. * This method should be called when switching to another server. * @param {string} serverUrl * @returns {Promise} */ - setMostRecentServerConnection = async (serverUrl: string) => { + setActiveServerDatabase = async (serverUrl: string) => { const defaultDatabase = await this.getDefaultDatabase(); if (defaultDatabase) { @@ -245,29 +244,38 @@ class DatabaseManager { }; /** - * getMostRecentServerConnection: The DatabaseManager should be the only one setting the active database. Hence, we have made the activeDatabase property private. - * Use this getter method to retrieve the active database if it has been set in your code. - * @returns {DatabaseInstance} + * getActiveServerUrl: Use this getter method to retrieve the active server URL. + * @returns {string} */ - getMostRecentServerConnection = async (): Promise => { - const defaultDatabase = await this.getDefaultDatabase(); + getActiveServerUrl = async (): Promise => { + const defaultDatabase = await this.getDefaultDatabase(); - if (defaultDatabase) { - const serverRecords = await defaultDatabase.collections.get(GLOBAL).query(Q.where('name', RECENTLY_VIEWED_SERVERS)).fetch() as IGlobal[]; + if (defaultDatabase) { + const serverRecords = await defaultDatabase.collections.get(GLOBAL).query(Q.where('name', RECENTLY_VIEWED_SERVERS)).fetch() as IGlobal[]; - if (serverRecords.length) { - const activeServer = serverRecords[0].value as string[]; - const activeServerUrl = activeServer[0]; - const activeServerDatabase = await this.getDatabaseConnection({serverUrl: activeServerUrl, setAsActiveDatabase: false}); - return { - connection: activeServerDatabase, - serverUrl: activeServerUrl, - }; - } - return undefined; - } - return undefined; - }; + if (serverRecords.length) { + const recentServers = serverRecords[0].value as string[]; + return recentServers[0]; + } + return undefined; + } + return undefined; + }; + + /** + * getActiveServerDatabase: The DatabaseManager should be the only one setting the active database. Hence, we have made the activeDatabase property private. + * Use this getter method to retrieve the active database if it has been set in your code. + * @returns {Promise} + */ + getActiveServerDatabase = async (): Promise => { + const serverUrl = await this.getActiveServerUrl(); + + if (serverUrl) { + const serverDatabase = await this.getDatabaseConnection({serverUrl, setAsActiveDatabase: false}); + return serverDatabase; + } + return undefined; + }; /** * getDefaultDatabase : Returns the default database. diff --git a/app/database/manager/index.ts b/app/database/manager/index.ts index 9c125120fc..afc68b5da7 100644 --- a/app/database/manager/index.ts +++ b/app/database/manager/index.ts @@ -51,7 +51,6 @@ import { GetDatabaseConnectionArgs, MigrationEvents, Models, - MostRecentConnection, RetrievedDatabase, } from '@typings/database/database'; import {DatabaseType} from '@typings/database/enums'; @@ -146,7 +145,7 @@ class DatabaseManager { } if (setAsActiveDatabase) { - await this.setMostRecentServerConnection(serverUrl); + await this.setActiveServerDatabase(serverUrl); } return connection; @@ -196,12 +195,12 @@ class DatabaseManager { }; /** - * setMostRecentServerConnection: Set the new active server database. The serverUrl is used to ensure that we do not duplicate entries in the default database. + * setActiveServerDatabase: Set the new active server database. The serverUrl is used to ensure that we do not duplicate entries in the default database. * This method should be called when switching to another server. * @param {string} serverUrl * @returns {Promise} */ - setMostRecentServerConnection = async (serverUrl: string) => { + setActiveServerDatabase = async (serverUrl: string) => { const defaultDatabase = await this.getDefaultDatabase(); if (defaultDatabase) { // retrieve recentlyViewedServers from Global entity @@ -234,26 +233,35 @@ class DatabaseManager { }; /** - * getMostRecentServerConnection: The DatabaseManager should be the only one setting the active database. Hence, we have made the activeDatabase property private. + * getActiveServerUrl: Use this getter method to retrieve the active server URL. + * @returns {string} + */ + getActiveServerUrl = async (): Promise => { + const defaultDatabase = await this.getDefaultDatabase(); + + if (defaultDatabase) { + const serverRecords = await defaultDatabase.collections.get(GLOBAL).query(Q.where('name', RECENTLY_VIEWED_SERVERS)).fetch() as IGlobal[]; + + if (serverRecords.length) { + const recentServers = serverRecords[0].value as string[]; + return recentServers[0]; + } + return undefined; + } + return undefined; + }; + + /** + * getActiveServerDatabase: The DatabaseManager should be the only one setting the active database. Hence, we have made the activeDatabase property private. * Use this getter method to retrieve the active database if it has been set in your code. * @returns {Promise} */ - getMostRecentServerConnection = async (): Promise => { - const defaultDatabase = await this.getDefaultDatabase(); + getActiveServerDatabase = async (): Promise => { + const serverUrl = await this.getActiveServerUrl(); - if (defaultDatabase) { - const serverRecords = await defaultDatabase.collections.get(GLOBAL).query(Q.where('name', RECENTLY_VIEWED_SERVERS)).fetch() as IGlobal[]; - - if (serverRecords.length) { - const activeServer = serverRecords[0].value as string[]; - const activeServerUrl = activeServer[0]; - const activeServerDatabase = await this.getDatabaseConnection({serverUrl: activeServerUrl, setAsActiveDatabase: false}); - return { - connection: activeServerDatabase, - serverUrl: activeServerUrl, - }; - } - return undefined; + if (serverUrl) { + const serverDatabase = await this.getDatabaseConnection({serverUrl, setAsActiveDatabase: false}); + return serverDatabase; } return undefined; }; diff --git a/app/database/manager/test.ts b/app/database/manager/test.ts index 3b52e06217..c3cb696f7f 100644 --- a/app/database/manager/test.ts +++ b/app/database/manager/test.ts @@ -4,7 +4,6 @@ import {Database, Q} from '@nozbe/watermelondb'; import {MM_TABLES} from '@constants/database'; -import {DatabaseInstance} from '@typings/database/database'; import {DatabaseType} from '@typings/database/enums'; import IGlobal from '@typings/database/global'; import IServers from '@typings/database/servers'; @@ -86,38 +85,34 @@ describe('*** Database Manager tests ***', () => { it('=> should switch between active server connections', async () => { expect.assertions(6); - let activeServer: DatabaseInstance; let adapter; - const recentConnectionA = await databaseManagerClient!.getMostRecentServerConnection(); - activeServer = recentConnectionA?.connection; + const activeServerA = await databaseManagerClient!.getActiveServerDatabase(); // as we haven't set an active server yet, we should be getting undefined in the activeServer variable - expect(activeServer).toBeUndefined(); + expect(activeServerA).toBeUndefined(); const setActiveServer = async (serverUrl: string) => { // now we set the active database - await databaseManagerClient!.setMostRecentServerConnection(serverUrl); + await databaseManagerClient!.setActiveServerDatabase(serverUrl); }; await setActiveServer('https://appv1.mattermost.com'); // let's verify if we now have a value for activeServer - const recentConnectionB = await databaseManagerClient!.getMostRecentServerConnection(); - activeServer = recentConnectionB?.connection; - expect(activeServer).toBeDefined(); + const activeServerB = await databaseManagerClient!.getActiveServerDatabase(); + expect(activeServerB).toBeDefined(); - adapter = activeServer!.adapter as any; + adapter = activeServerB!.adapter as any; const currentDBName = adapter.underlyingAdapter._dbName; expect(currentDBName).toStrictEqual('appv1.mattermost.com'); // spice things up; we'll set a new server and verify if the value of activeServer changes await setActiveServer('https://appv2.mattermost.com'); - const recentConnectionC = await databaseManagerClient!.getMostRecentServerConnection(); - activeServer = recentConnectionC?.connection; - expect(activeServer).toBeDefined(); + const activeServerC = await databaseManagerClient!.getActiveServerDatabase(); + expect(activeServerC).toBeDefined(); - adapter = activeServer!.adapter as any; + adapter = activeServerC!.adapter as any; const newDBName = adapter.underlyingAdapter._dbName; expect(newDBName).toStrictEqual('appv2.mattermost.com'); @@ -170,7 +165,7 @@ describe('*** Database Manager tests ***', () => { const serversRecords = await defaultDB!.collections.get(SERVERS).query().fetch() as IServers[]; expect(serversRecords).toBeDefined(); - // We have call the 'DatabaseManager.setMostRecentServerConnection' twice in the previous test case; that implies that we have 2 records in the 'servers' table + // We have call the 'DatabaseManager.setActiveServerDatabase' twice in the previous test case; that implies that we have 2 records in the 'servers' table expect(serversRecords.length).toEqual(2); }); @@ -180,8 +175,8 @@ describe('*** Database Manager tests ***', () => { const defaultDatabase = await databaseManagerClient!.getDefaultDatabase(); - await databaseManagerClient?.setMostRecentServerConnection('https://appv1.mattermost.com'); - await databaseManagerClient?.setMostRecentServerConnection('https://appv2.mattermost.com'); + await databaseManagerClient?.setActiveServerDatabase('https://appv1.mattermost.com'); + await databaseManagerClient?.setActiveServerDatabase('https://appv2.mattermost.com'); const fetchGlobalRecords = async () => { const initialGlobalRecords = await defaultDatabase!.collections.get(GLOBAL).query(Q.where('name', RECENTLY_VIEWED_SERVERS)).fetch() as IGlobal[]; diff --git a/app/database/manager/test_manual.ts b/app/database/manager/test_manual.ts index 3d2176a876..7d2a1a3354 100644 --- a/app/database/manager/test_manual.ts +++ b/app/database/manager/test_manual.ts @@ -38,12 +38,12 @@ export default async () => { // Test: It should return the current active server database const testGetActiveServerConnection = () => { - // const activeServer = DatabaseManager.getMostRecentServerConnection(); + // const activeServer = DatabaseManager.getActiveServerDatabase(); }; // Test: It should set the current active server database to the provided server url. const testSetActiveServerConnection = async () => { - await databaseClient.setMostRecentServerConnection('https://comm4.mattermost.com'); + await databaseClient.setActiveServerDatabase('https://comm4.mattermost.com'); }; // Test: It should return database instance(s) if there are valid server urls in the provided list. diff --git a/app/database/operator/utils/create_test_connection.ts b/app/database/operator/utils/create_test_connection.ts index 58b4b3ee17..0cc6e0bea8 100644 --- a/app/database/operator/utils/create_test_connection.ts +++ b/app/database/operator/utils/create_test_connection.ts @@ -21,7 +21,7 @@ export const createTestConnection = async ({databaseName = 'db_name', setActive }); if (setActive) { - await databaseClient.setMostRecentServerConnection(serverUrl); + await databaseClient.setActiveServerDatabase(serverUrl); } return database; diff --git a/app/database/operator/utils/test.ts b/app/database/operator/utils/test.ts index 35a2e9f9a2..e07e67f533 100644 --- a/app/database/operator/utils/test.ts +++ b/app/database/operator/utils/test.ts @@ -81,7 +81,7 @@ describe('DataOperator: Utils tests', () => { serverUrl, }, }); - await databaseManagerClient.setMostRecentServerConnection(serverUrl); + await databaseManagerClient.setActiveServerDatabase(serverUrl); const operatorClient = new Operator(database!); diff --git a/app/utils/database.ts b/app/utils/database.ts index d8e2e44019..57aa77a3d9 100644 --- a/app/utils/database.ts +++ b/app/utils/database.ts @@ -47,11 +47,11 @@ export const getDefaultDatabase = async () => { export const getActiveServerDatabase = async () => { try { const databaseClient = new DatabaseManager(); - const recentConnection = await databaseClient.getMostRecentServerConnection(); + const activeServerDatabase = await databaseClient.getActiveServerDatabase(); return { - error: recentConnection?.connection ? null : 'Unable to retrieve the current active server database.', - activeServerDatabase: recentConnection?.connection ?? null, + error: activeServerDatabase ? null : 'Unable to retrieve the current active server database.', + activeServerDatabase, }; } catch (e) { return {