Add getMostRecentServerUrl function (#5440)

* fix: add getMostRecentServerUrl func

* fix: add ts and tsx to editorconfig

* fix: rename functions

* fix: return type
This commit is contained in:
Miguel Alatzar
2021-06-09 10:16:52 -07:00
committed by GitHub
parent 160dae0d43
commit 48c7eaecf5
8 changed files with 80 additions and 69 deletions

View File

@@ -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

View File

@@ -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<void>}
*/
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<MostRecentConnection | undefined> => {
const defaultDatabase = await this.getDefaultDatabase();
getActiveServerUrl = async (): Promise<string|undefined> => {
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<DatabaseInstance | undefined>}
*/
getActiveServerDatabase = async (): Promise<DatabaseInstance> => {
const serverUrl = await this.getActiveServerUrl();
if (serverUrl) {
const serverDatabase = await this.getDatabaseConnection({serverUrl, setAsActiveDatabase: false});
return serverDatabase;
}
return undefined;
};
/**
* getDefaultDatabase : Returns the default database.

View File

@@ -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<void>}
*/
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<string|undefined> => {
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<MostRecentConnection | undefined>}
*/
getMostRecentServerConnection = async (): Promise<MostRecentConnection | undefined> => {
const defaultDatabase = await this.getDefaultDatabase();
getActiveServerDatabase = async (): Promise<DatabaseInstance> => {
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;
};

View File

@@ -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[];

View File

@@ -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.

View File

@@ -21,7 +21,7 @@ export const createTestConnection = async ({databaseName = 'db_name', setActive
});
if (setActive) {
await databaseClient.setMostRecentServerConnection(serverUrl);
await databaseClient.setActiveServerDatabase(serverUrl);
}
return database;

View File

@@ -81,7 +81,7 @@ describe('DataOperator: Utils tests', () => {
serverUrl,
},
});
await databaseManagerClient.setMostRecentServerConnection(serverUrl);
await databaseManagerClient.setActiveServerDatabase(serverUrl);
const operatorClient = new Operator(database!);

View File

@@ -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 {