forked from Ivasoft/mattermost-mobile
Move config to its own database table (#6744)
* Move config to its own database table * Address feedback * Fix test * Revert minimum version related changes
This commit is contained in:
committed by
GitHub
parent
887565423c
commit
1aa4188f8e
@@ -3,6 +3,7 @@
|
||||
|
||||
import DatabaseManager from '@database/manager';
|
||||
import {
|
||||
transformConfigRecord,
|
||||
transformCustomEmojiRecord,
|
||||
transformRoleRecord,
|
||||
transformSystemRecord,
|
||||
@@ -95,6 +96,30 @@ describe('*** DataOperator: Base Handlers tests ***', () => {
|
||||
});
|
||||
});
|
||||
|
||||
it('=> HandleConfig: should write to the CONFIG table', async () => {
|
||||
expect.assertions(1);
|
||||
|
||||
const spyOnHandleRecords = jest.spyOn(operator, 'handleRecords');
|
||||
|
||||
const configs = [{id: 'config-1', value: 'config-1'}];
|
||||
const configsToDelete = [{id: 'toDelete', value: 'toDelete'}];
|
||||
|
||||
await operator.handleConfigs({
|
||||
configs,
|
||||
configsToDelete,
|
||||
prepareRecordsOnly: false,
|
||||
});
|
||||
|
||||
expect(spyOnHandleRecords).toHaveBeenCalledWith({
|
||||
fieldName: 'id',
|
||||
transformer: transformConfigRecord,
|
||||
createOrUpdateRawValues: configs,
|
||||
tableName: 'Config',
|
||||
prepareRecordsOnly: false,
|
||||
deleteRawValues: configsToDelete,
|
||||
});
|
||||
});
|
||||
|
||||
it('=> No table name: should not call execute if tableName is invalid', async () => {
|
||||
expect.assertions(3);
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
import {MM_TABLES} from '@constants/database';
|
||||
import BaseDataOperator from '@database/operator/base_data_operator';
|
||||
import {
|
||||
transformConfigRecord,
|
||||
transformCustomEmojiRecord,
|
||||
transformRoleRecord,
|
||||
transformSystemRecord,
|
||||
@@ -12,12 +13,12 @@ import {getUniqueRawsBy} from '@database/operator/utils/general';
|
||||
import {logWarning} from '@utils/log';
|
||||
|
||||
import type {Model} from '@nozbe/watermelondb';
|
||||
import type {HandleCustomEmojiArgs, HandleRoleArgs, HandleSystemArgs, OperationArgs} from '@typings/database/database';
|
||||
import type {HandleConfigArgs, HandleCustomEmojiArgs, HandleRoleArgs, HandleSystemArgs, OperationArgs} from '@typings/database/database';
|
||||
import type CustomEmojiModel from '@typings/database/models/servers/custom_emoji';
|
||||
import type RoleModel from '@typings/database/models/servers/role';
|
||||
import type SystemModel from '@typings/database/models/servers/system';
|
||||
|
||||
const {SERVER: {CUSTOM_EMOJI, ROLE, SYSTEM}} = MM_TABLES;
|
||||
const {SERVER: {CONFIG, CUSTOM_EMOJI, ROLE, SYSTEM}} = MM_TABLES;
|
||||
|
||||
export default class ServerDataOperatorBase extends BaseDataOperator {
|
||||
handleRole = async ({roles, prepareRecordsOnly = true}: HandleRoleArgs) => {
|
||||
@@ -71,6 +72,24 @@ export default class ServerDataOperatorBase extends BaseDataOperator {
|
||||
}) as Promise<SystemModel[]>;
|
||||
};
|
||||
|
||||
handleConfigs = async ({configs, configsToDelete, prepareRecordsOnly = true}: HandleConfigArgs) => {
|
||||
if (!configs?.length && !configsToDelete?.length) {
|
||||
logWarning(
|
||||
'An empty or undefined "configs" and "configsToDelete" arrays has been passed to the handleConfigs',
|
||||
);
|
||||
return [];
|
||||
}
|
||||
|
||||
return this.handleRecords({
|
||||
fieldName: 'id',
|
||||
transformer: transformConfigRecord,
|
||||
prepareRecordsOnly,
|
||||
createOrUpdateRawValues: getUniqueRawsBy({raws: configs, key: 'id'}),
|
||||
tableName: CONFIG,
|
||||
deleteRawValues: configsToDelete,
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* execute: Handles the Create/Update operations on an table.
|
||||
* @param {OperationArgs} execute
|
||||
|
||||
@@ -5,6 +5,7 @@ import {MM_TABLES, OperationType} from '@constants/database';
|
||||
import {prepareBaseRecord} from '@database/operator/server_data_operator/transformers/index';
|
||||
|
||||
import type {TransformerArgs} from '@typings/database/database';
|
||||
import type ConfigModel from '@typings/database/models/servers/config';
|
||||
import type CustomEmojiModel from '@typings/database/models/servers/custom_emoji';
|
||||
import type RoleModel from '@typings/database/models/servers/role';
|
||||
import type SystemModel from '@typings/database/models/servers/system';
|
||||
@@ -13,6 +14,7 @@ const {
|
||||
CUSTOM_EMOJI,
|
||||
ROLE,
|
||||
SYSTEM,
|
||||
CONFIG,
|
||||
} = MM_TABLES.SERVER;
|
||||
|
||||
/**
|
||||
@@ -94,3 +96,28 @@ export const transformSystemRecord = ({action, database, value}: TransformerArgs
|
||||
fieldsMapper,
|
||||
}) as Promise<SystemModel>;
|
||||
};
|
||||
|
||||
/**
|
||||
* transformConfigRecord: Prepares a record of the SERVER database 'Config' table for update or create actions.
|
||||
* @param {TransformerArgs} operator
|
||||
* @param {Database} operator.database
|
||||
* @param {RecordPair} operator.value
|
||||
* @returns {Promise<ConfigModel>}
|
||||
*/
|
||||
export const transformConfigRecord = ({action, database, value}: TransformerArgs): Promise<ConfigModel> => {
|
||||
const raw = value.raw as IdValue;
|
||||
|
||||
// If isCreateAction is true, we will use the id (API response) from the RAW, else we shall use the existing record id from the database
|
||||
const fieldsMapper = (config: ConfigModel) => {
|
||||
config._raw.id = raw?.id;
|
||||
config.value = raw?.value as string;
|
||||
};
|
||||
|
||||
return prepareBaseRecord({
|
||||
action,
|
||||
database,
|
||||
tableName: CONFIG,
|
||||
value,
|
||||
fieldsMapper,
|
||||
}) as Promise<ConfigModel>;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user