forked from Ivasoft/mattermost-mobile
[Gekidou] Typings & PostMetadata structure (#5542)
* Typings & PostMetadata structure * comment out unused code * Remove duplicate interface * Fix getPreferenceAsBool defaultValue
This commit is contained in:
@@ -1,19 +1,13 @@
|
||||
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
||||
// See LICENSE.txt for license information.
|
||||
|
||||
import Info from '@typings/database/models/app/info';
|
||||
import {RawInfo, RawGlobal, RawServers} from '@typings/database/database';
|
||||
import Global from '@typings/database/models/app/global';
|
||||
import Servers from '@typings/database/models/app/servers';
|
||||
import type InfoModel from '@typings/database/models/app/info';
|
||||
import type GlobalModel from '@typings/database/models/app/global';
|
||||
|
||||
export const isRecordInfoEqualToRaw = (record: Info, raw: RawInfo) => {
|
||||
export const isRecordInfoEqualToRaw = (record: InfoModel, raw: AppInfo) => {
|
||||
return (raw.build_number === record.buildNumber && raw.version_number === record.versionNumber);
|
||||
};
|
||||
|
||||
export const isRecordGlobalEqualToRaw = (record: Global, raw: RawGlobal) => {
|
||||
export const isRecordGlobalEqualToRaw = (record: GlobalModel, raw: IdValue) => {
|
||||
return raw.id === record.id && raw.value === record.value;
|
||||
};
|
||||
|
||||
export const isRecordServerEqualToRaw = (record: Servers, raw: RawServers) => {
|
||||
return raw.url === record.url && raw.db_path === record.dbPath;
|
||||
};
|
||||
|
||||
@@ -5,14 +5,11 @@ import DatabaseManager from '@database/manager';
|
||||
import {
|
||||
isRecordInfoEqualToRaw,
|
||||
isRecordGlobalEqualToRaw,
|
||||
isRecordServerEqualToRaw,
|
||||
} from '@database/operator/app_data_operator/comparator';
|
||||
import {
|
||||
transformInfoRecord,
|
||||
transformGlobalRecord,
|
||||
transformServersRecord,
|
||||
} from '@database/operator/app_data_operator/transformers';
|
||||
import {RawGlobal, RawServers} from '@typings/database/database';
|
||||
|
||||
describe('** APP DATA OPERATOR **', () => {
|
||||
beforeAll(async () => {
|
||||
@@ -72,7 +69,7 @@ describe('** APP DATA OPERATOR **', () => {
|
||||
expect(appOperator).toBeTruthy();
|
||||
|
||||
const spyOnHandleRecords = jest.spyOn(appOperator as any, 'handleRecords');
|
||||
const global: RawGlobal[] = [{id: 'global-1-name', value: 'global-1-value'}];
|
||||
const global: IdValue[] = [{id: 'global-1-name', value: 'global-1-value'}];
|
||||
|
||||
await appOperator?.handleGlobal({
|
||||
global,
|
||||
@@ -88,49 +85,4 @@ describe('** APP DATA OPERATOR **', () => {
|
||||
prepareRecordsOnly: false,
|
||||
});
|
||||
});
|
||||
|
||||
it('=> HandleServers: should write to SERVERS table', async () => {
|
||||
const appDatabase = DatabaseManager.appDatabase?.database;
|
||||
const appOperator = DatabaseManager.appDatabase?.operator;
|
||||
expect(appDatabase).toBeTruthy();
|
||||
expect(appOperator).toBeTruthy();
|
||||
|
||||
const spyOnHandleRecords = jest.spyOn(appOperator as any, 'handleRecords');
|
||||
|
||||
const servers: RawServers[] = [
|
||||
{
|
||||
db_path: 'server.db',
|
||||
display_name: 'community',
|
||||
mention_count: 0,
|
||||
unread_count: 0,
|
||||
url: 'https://community.mattermost.com',
|
||||
isSecured: true,
|
||||
lastActiveAt: 1623926359,
|
||||
},
|
||||
];
|
||||
|
||||
await appOperator?.handleServers({
|
||||
servers,
|
||||
prepareRecordsOnly: false,
|
||||
});
|
||||
|
||||
expect(spyOnHandleRecords).toHaveBeenCalledWith({
|
||||
fieldName: 'url',
|
||||
transformer: transformServersRecord,
|
||||
findMatchingRecordBy: isRecordServerEqualToRaw,
|
||||
createOrUpdateRawValues: [
|
||||
{
|
||||
db_path: 'server.db',
|
||||
display_name: 'community',
|
||||
mention_count: 0,
|
||||
unread_count: 0,
|
||||
url: 'https://community.mattermost.com',
|
||||
isSecured: true,
|
||||
lastActiveAt: 1623926359,
|
||||
},
|
||||
],
|
||||
tableName: 'Servers',
|
||||
prepareRecordsOnly: false,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -3,35 +3,24 @@
|
||||
|
||||
import {MM_TABLES} from '@constants/database';
|
||||
import DataOperatorException from '@database/exceptions/data_operator_exception';
|
||||
import {
|
||||
isRecordInfoEqualToRaw,
|
||||
isRecordGlobalEqualToRaw,
|
||||
isRecordServerEqualToRaw,
|
||||
} from '@database/operator/app_data_operator/comparator';
|
||||
import {
|
||||
transformInfoRecord,
|
||||
transformGlobalRecord,
|
||||
transformServersRecord,
|
||||
} from '@database/operator/app_data_operator/transformers';
|
||||
import {isRecordInfoEqualToRaw, isRecordGlobalEqualToRaw} from '@database/operator/app_data_operator/comparator';
|
||||
import {transformInfoRecord, transformGlobalRecord} from '@database/operator/app_data_operator/transformers';
|
||||
import BaseDataOperator from '@database/operator/base_data_operator';
|
||||
import {getUniqueRawsBy} from '@database/operator/utils/general';
|
||||
import {
|
||||
HandleInfoArgs,
|
||||
HandleGlobalArgs,
|
||||
HandleServersArgs,
|
||||
} from '@typings/database/database';
|
||||
|
||||
const {APP: {INFO, GLOBAL, SERVERS}} = MM_TABLES;
|
||||
import type {HandleInfoArgs, HandleGlobalArgs} from '@typings/database/database';
|
||||
|
||||
const {APP: {INFO, GLOBAL}} = MM_TABLES;
|
||||
|
||||
export default class AppDataOperator extends BaseDataOperator {
|
||||
handleInfo = async ({info, prepareRecordsOnly = true}: HandleInfoArgs) => {
|
||||
handleInfo = ({info, prepareRecordsOnly = true}: HandleInfoArgs) => {
|
||||
if (!info.length) {
|
||||
throw new DataOperatorException(
|
||||
'An empty "values" array has been passed to the handleInfo',
|
||||
);
|
||||
}
|
||||
|
||||
const records = await this.handleRecords({
|
||||
return this.handleRecords({
|
||||
fieldName: 'version_number',
|
||||
findMatchingRecordBy: isRecordInfoEqualToRaw,
|
||||
transformer: transformInfoRecord,
|
||||
@@ -39,8 +28,6 @@ export default class AppDataOperator extends BaseDataOperator {
|
||||
createOrUpdateRawValues: getUniqueRawsBy({raws: info, key: 'version_number'}),
|
||||
tableName: INFO,
|
||||
});
|
||||
|
||||
return records;
|
||||
}
|
||||
|
||||
handleGlobal = async ({global, prepareRecordsOnly = true}: HandleGlobalArgs) => {
|
||||
@@ -50,7 +37,7 @@ export default class AppDataOperator extends BaseDataOperator {
|
||||
);
|
||||
}
|
||||
|
||||
const records = await this.handleRecords({
|
||||
return this.handleRecords({
|
||||
fieldName: 'id',
|
||||
findMatchingRecordBy: isRecordGlobalEqualToRaw,
|
||||
transformer: transformGlobalRecord,
|
||||
@@ -58,26 +45,5 @@ export default class AppDataOperator extends BaseDataOperator {
|
||||
createOrUpdateRawValues: getUniqueRawsBy({raws: global, key: 'id'}),
|
||||
tableName: GLOBAL,
|
||||
});
|
||||
|
||||
return records;
|
||||
}
|
||||
|
||||
handleServers = async ({servers, prepareRecordsOnly = true}: HandleServersArgs) => {
|
||||
if (!servers.length) {
|
||||
throw new DataOperatorException(
|
||||
'An empty "values" array has been passed to the handleServers',
|
||||
);
|
||||
}
|
||||
|
||||
const records = await this.handleRecords({
|
||||
fieldName: 'url',
|
||||
findMatchingRecordBy: isRecordServerEqualToRaw,
|
||||
transformer: transformServersRecord,
|
||||
prepareRecordsOnly,
|
||||
createOrUpdateRawValues: getUniqueRawsBy({raws: servers, key: 'display_name'}),
|
||||
tableName: SERVERS,
|
||||
});
|
||||
|
||||
return records;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,13 +3,15 @@
|
||||
|
||||
import {MM_TABLES} from '@constants/database';
|
||||
import {prepareBaseRecord} from '@database/operator/server_data_operator/transformers';
|
||||
import Info from '@typings/database/models/app/info';
|
||||
import {TransformerArgs, RawInfo, RawGlobal, RawServers} from '@typings/database/database';
|
||||
import {OperationType} from '@typings/database/enums';
|
||||
import Global from '@typings/database/models/app/global';
|
||||
import Servers from '@typings/database/models/app/servers';
|
||||
|
||||
const {INFO, GLOBAL, SERVERS} = MM_TABLES.APP;
|
||||
import type {Model} from '@nozbe/watermelondb';
|
||||
|
||||
import type InfoModel from '@typings/database/models/app/info';
|
||||
import type {TransformerArgs} from '@typings/database/database';
|
||||
import {OperationType} from '@typings/database/enums';
|
||||
import type GlobalModel from '@typings/database/models/app/global';
|
||||
|
||||
const {INFO, GLOBAL} = MM_TABLES.APP;
|
||||
|
||||
/**
|
||||
* transformInfoRecord: Prepares a record of the APP database 'Info' table for update or create actions.
|
||||
@@ -18,12 +20,12 @@ const {INFO, GLOBAL, SERVERS} = MM_TABLES.APP;
|
||||
* @param {RecordPair} operator.value
|
||||
* @returns {Promise<Model>}
|
||||
*/
|
||||
export const transformInfoRecord = ({action, database, value}: TransformerArgs) => {
|
||||
const raw = value.raw as RawInfo;
|
||||
const record = value.record as Info;
|
||||
export const transformInfoRecord = ({action, database, value}: TransformerArgs): Promise<Model> => {
|
||||
const raw = value.raw as AppInfo;
|
||||
const record = value.record as InfoModel;
|
||||
const isCreateAction = action === OperationType.CREATE;
|
||||
|
||||
const fieldsMapper = (app: Info) => {
|
||||
const fieldsMapper = (app: InfoModel) => {
|
||||
app._raw.id = isCreateAction ? app.id : record.id;
|
||||
app.buildNumber = raw?.build_number;
|
||||
app.createdAt = raw?.created_at;
|
||||
@@ -46,10 +48,10 @@ export const transformInfoRecord = ({action, database, value}: TransformerArgs)
|
||||
* @param {RecordPair} operator.value
|
||||
* @returns {Promise<Model>}
|
||||
*/
|
||||
export const transformGlobalRecord = ({action, database, value}: TransformerArgs) => {
|
||||
const raw = value.raw as RawGlobal;
|
||||
export const transformGlobalRecord = ({action, database, value}: TransformerArgs): Promise<Model> => {
|
||||
const raw = value.raw as IdValue;
|
||||
|
||||
const fieldsMapper = (global: Global) => {
|
||||
const fieldsMapper = (global: GlobalModel) => {
|
||||
global._raw.id = raw?.id;
|
||||
global.value = raw?.value;
|
||||
};
|
||||
@@ -63,34 +65,3 @@ export const transformGlobalRecord = ({action, database, value}: TransformerArgs
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* transformServersRecord: Prepares a record of the APP database 'Servers' table for update or create actions.
|
||||
* @param {TransformerArgs} operator
|
||||
* @param {Database} operator.database
|
||||
* @param {RecordPair} operator.value
|
||||
* @returns {Promise<Model>}
|
||||
*/
|
||||
export const transformServersRecord = ({action, database, value}: TransformerArgs) => {
|
||||
const raw = value.raw as RawServers;
|
||||
const record = value.record as Servers;
|
||||
const isCreateAction = action === OperationType.CREATE;
|
||||
|
||||
const fieldsMapper = (servers: Servers) => {
|
||||
servers._raw.id = isCreateAction ? servers.id : record.id;
|
||||
servers.dbPath = raw?.db_path;
|
||||
servers.displayName = raw?.display_name;
|
||||
servers.mentionCount = raw?.mention_count;
|
||||
servers.unreadCount = raw?.unread_count;
|
||||
servers.url = raw?.url;
|
||||
servers.isSecured = raw?.isSecured;
|
||||
servers.lastActiveAt = raw?.lastActiveAt;
|
||||
};
|
||||
|
||||
return prepareBaseRecord({
|
||||
action,
|
||||
database,
|
||||
tableName: SERVERS,
|
||||
value,
|
||||
fieldsMapper,
|
||||
});
|
||||
};
|
||||
|
||||
@@ -5,7 +5,6 @@ import DatabaseManager from '@database/manager';
|
||||
import {
|
||||
transformInfoRecord,
|
||||
transformGlobalRecord,
|
||||
transformServersRecord,
|
||||
} from '@database/operator/app_data_operator/transformers/index';
|
||||
import {OperationType} from '@typings/database/enums';
|
||||
|
||||
@@ -14,33 +13,6 @@ describe('** APP DATA TRANSFORMER **', () => {
|
||||
await DatabaseManager.init([]);
|
||||
});
|
||||
|
||||
it('=> transformServersRecord: should return an array of type Servers', async () => {
|
||||
expect.assertions(3);
|
||||
|
||||
const database = DatabaseManager.appDatabase?.database;
|
||||
expect(database).toBeTruthy();
|
||||
|
||||
const preparedRecords = await transformServersRecord({
|
||||
action: OperationType.CREATE,
|
||||
database: database!,
|
||||
value: {
|
||||
record: undefined,
|
||||
raw: {
|
||||
db_path: 'mm-server',
|
||||
display_name: 's-displayName',
|
||||
mention_count: 1,
|
||||
unread_count: 0,
|
||||
url: 'https://community.mattermost.com',
|
||||
isSecured: true,
|
||||
lastActiveAt: 1623926359,
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
expect(preparedRecords).toBeTruthy();
|
||||
expect(preparedRecords!.collection.modelClass.name).toBe('Servers');
|
||||
});
|
||||
|
||||
it('=> transformInfoRecord: should return an array of type Info', async () => {
|
||||
expect.assertions(3);
|
||||
|
||||
@@ -61,7 +33,7 @@ describe('** APP DATA TRANSFORMER **', () => {
|
||||
});
|
||||
|
||||
expect(preparedRecords).toBeTruthy();
|
||||
expect(preparedRecords!.collection.modelClass.name).toBe('Info');
|
||||
expect(preparedRecords!.collection.modelClass.name).toBe('InfoModel');
|
||||
});
|
||||
|
||||
it('=> transformGlobalRecord: should return an array of type Global', async () => {
|
||||
@@ -80,6 +52,6 @@ describe('** APP DATA TRANSFORMER **', () => {
|
||||
});
|
||||
|
||||
expect(preparedRecords).toBeTruthy();
|
||||
expect(preparedRecords!.collection.modelClass.name).toBe('Global');
|
||||
expect(preparedRecords!.collection.modelClass.name).toBe('GlobalModel');
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user