MM_36721 : Restructure Entities - Global & System (#5504)

This commit is contained in:
Avinash Lingaloo
2021-07-02 13:08:12 +04:00
committed by GitHub
parent 10bfc9d604
commit 7ff119fdc1
28 changed files with 57 additions and 83 deletions

View File

@@ -2,7 +2,7 @@
// See LICENSE.txt for license information.
import {Model} from '@nozbe/watermelondb';
import {field, json} from '@nozbe/watermelondb/decorators';
import {json} from '@nozbe/watermelondb/decorators';
import {MM_TABLES} from '@constants/database';
@@ -18,9 +18,6 @@ export default class Global extends Model {
/** table (name) : global */
static table = GLOBAL;
/** name : The label/key to use to retrieve the special 'value' */
@field('name') name!: string;
/** value : The value part of the key-value combination */
/** value : The value part of the key-value combination and whose key will be the id column */
@json('value', (rawJson) => rawJson) value!: any;
}

View File

@@ -2,7 +2,7 @@
// See LICENSE.txt for license information.
import {Model} from '@nozbe/watermelondb';
import {field, json} from '@nozbe/watermelondb/decorators';
import {json} from '@nozbe/watermelondb/decorators';
import {MM_TABLES} from '@constants/database';
@@ -17,9 +17,6 @@ export default class System extends Model {
/** table (name) : System */
static table = SYSTEM;
/** name : The name or key value for the config */
@field('name') name!: string;
/** value : The value for that config/information */
/** value : The value for that config/information and whose key will be the id column */
@json('value', (rawJson) => rawJson) value!: any;
}

View File

@@ -11,7 +11,7 @@ export const isRecordInfoEqualToRaw = (record: Info, raw: RawInfo) => {
};
export const isRecordGlobalEqualToRaw = (record: Global, raw: RawGlobal) => {
return raw.name === record.name && raw.value === record.value;
return raw.id === record.id && raw.value === record.value;
};
export const isRecordServerEqualToRaw = (record: Servers, raw: RawServers) => {

View File

@@ -72,7 +72,7 @@ describe('** APP DATA OPERATOR **', () => {
expect(appOperator).toBeTruthy();
const spyOnHandleRecords = jest.spyOn(appOperator as any, 'handleRecords');
const global: RawGlobal[] = [{name: 'global-1-name', value: 'global-1-value'}];
const global: RawGlobal[] = [{id: 'global-1-name', value: 'global-1-value'}];
await appOperator?.handleGlobal({
global,
@@ -81,7 +81,7 @@ describe('** APP DATA OPERATOR **', () => {
expect(spyOnHandleRecords).toHaveBeenCalledWith({
findMatchingRecordBy: isRecordGlobalEqualToRaw,
fieldName: 'name',
fieldName: 'id',
transformer: transformGlobalRecord,
createOrUpdateRawValues: global,
tableName: 'Global',

View File

@@ -51,11 +51,11 @@ export default class AppDataOperator extends BaseDataOperator {
}
const records = await this.handleRecords({
fieldName: 'name',
fieldName: 'id',
findMatchingRecordBy: isRecordGlobalEqualToRaw,
transformer: transformGlobalRecord,
prepareRecordsOnly,
createOrUpdateRawValues: getUniqueRawsBy({raws: global, key: 'name'}),
createOrUpdateRawValues: getUniqueRawsBy({raws: global, key: 'id'}),
tableName: GLOBAL,
});

View File

@@ -48,12 +48,9 @@ export const transformInfoRecord = ({action, database, value}: TransformerArgs)
*/
export const transformGlobalRecord = ({action, database, value}: TransformerArgs) => {
const raw = value.raw as RawGlobal;
const record = value.record as Global;
const isCreateAction = action === OperationType.CREATE;
const fieldsMapper = (global: Global) => {
global._raw.id = isCreateAction ? global.id : record.id;
global.name = raw?.name;
global._raw.id = raw?.id;
global.value = raw?.value;
};

View File

@@ -75,7 +75,7 @@ describe('** APP DATA TRANSFORMER **', () => {
database: database!,
value: {
record: undefined,
raw: {name: 'g-n1', value: 'g-v1'},
raw: {id: 'g-n1', value: 'g-v1'},
},
});

View File

@@ -50,10 +50,7 @@ export default class BaseDataOperator {
processRecords = async ({createOrUpdateRawValues, deleteRawValues = [], tableName, findMatchingRecordBy, fieldName}: ProcessRecordsArgs): Promise<ProcessRecordResults> => {
const getRecords = async (rawValues : RawValue[]) => {
// We will query a table where one of its fields can match a range of values. Hence, here we are extracting all those potential values.
const columnValues: string[] = getRangeOfValues({
fieldName,
raws: rawValues,
});
const columnValues: string[] = getRangeOfValues({fieldName, raws: rawValues});
if (!columnValues.length && rawValues.length) {
throw new DataOperatorException(

View File

@@ -62,7 +62,7 @@ export const isRecordRoleEqualToRaw = (record: Role, raw: RawRole) => {
};
export const isRecordSystemEqualToRaw = (record: System, raw: RawSystem) => {
return raw.name === record.name;
return raw.id === record.id;
};
export const isRecordTermsOfServiceEqualToRaw = (record: TermsOfService, raw: RawTermsOfService) => {

View File

@@ -32,24 +32,26 @@ describe('*** Operator: Channel Handlers tests ***', () => {
const spyOnHandleRecords = jest.spyOn(operator, 'handleRecords');
const channels: RawChannel[] = [
{
id: 'kjlw9j1ttnxwig7tnqgebg7dtipno',
create_at: 1600185541285,
update_at: 1604401077256,
delete_at: 0,
team_id: '',
type: 'D',
display_name: '',
name: 'gh781zkzkhh357b4bejephjz5u8daw__9ciscaqbrpd6d8s68k76xb9bte',
header: '(https://mattermost',
purpose: '',
last_post_at: 1617311494451,
total_msg_count: 585,
extra_update_at: 0,
creator_id: '',
delete_at: 0,
display_name: '',
extra_update_at: 0,
group_constrained: null,
shared: false,
header: '(https://mattermost',
id: 'kjlw9j1ttnxwig7tnqgebg7dtipno',
last_post_at: 1617311494451,
name: 'gh781zkzkhh357b4bejephjz5u8daw__9ciscaqbrpd6d8s68k76xb9bte',
policy_id: 'policy',
props: null,
purpose: '',
scheme_id: null,
shared: false,
team_id: '',
total_msg_count: 585,
total_msg_count_root: 1,
type: 'D',
update_at: 1604401077256,
},
];

View File

@@ -90,7 +90,7 @@ describe('*** DataOperator: Base Handlers tests ***', () => {
const spyOnHandleRecords = jest.spyOn(operator, 'handleRecords');
const systems = [{name: 'system-1', value: 'system-1'}];
const systems = [{id: 'system-1', value: 'system-1'}];
await operator.handleSystem({
systems,
@@ -99,7 +99,7 @@ describe('*** DataOperator: Base Handlers tests ***', () => {
expect(spyOnHandleRecords).toHaveBeenCalledWith({
findMatchingRecordBy: isRecordSystemEqualToRaw,
fieldName: 'name',
fieldName: 'id',
transformer: transformSystemRecord,
createOrUpdateRawValues: systems,
tableName: 'System',

View File

@@ -68,11 +68,11 @@ export default class ServerDataOperatorBase extends BaseDataOperator {
}
const records = await this.handleRecords({
fieldName: 'name',
fieldName: 'id',
findMatchingRecordBy: isRecordSystemEqualToRaw,
transformer: transformSystemRecord,
prepareRecordsOnly,
createOrUpdateRawValues: getUniqueRawsBy({raws: systems, key: 'name'}),
createOrUpdateRawValues: getUniqueRawsBy({raws: systems, key: 'id'}),
tableName: SYSTEM,
});

View File

@@ -90,7 +90,7 @@ export const transformSystemRecord = ({action, database, value}: TransformerArgs
// 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 = (system: System) => {
system.name = raw?.name;
system._raw.id = raw?.id;
system.value = raw?.value;
};

View File

@@ -20,7 +20,6 @@ export const schema: AppSchema = appSchema({
tableSchema({
name: GLOBAL,
columns: [
{name: 'name', type: 'string', isIndexed: true},
{name: 'value', type: 'string'},
],
}),

View File

@@ -10,7 +10,6 @@ const {GLOBAL} = MM_TABLES.APP;
export default tableSchema({
name: GLOBAL,
columns: [
{name: 'name', type: 'string', isIndexed: true},
{name: 'value', type: 'string'},
],
});

View File

@@ -28,11 +28,9 @@ describe('*** Test schema for DEFAULT database ***', () => {
[GLOBAL]: {
name: GLOBAL,
columns: {
name: {name: 'name', type: 'string', isIndexed: true},
value: {name: 'value', type: 'string'},
},
columnArray: [
{name: 'name', type: 'string', isIndexed: true},
{name: 'value', type: 'string'},
],
},

View File

@@ -10,7 +10,6 @@ const {SYSTEM} = MM_TABLES.SERVER;
export default tableSchema({
name: SYSTEM,
columns: [
{name: 'name', type: 'string'},
{name: 'value', type: 'string'},
],
});

View File

@@ -383,11 +383,9 @@ describe('*** Test schema for SERVER database ***', () => {
[SYSTEM]: {
name: SYSTEM,
columns: {
name: {name: 'name', type: 'string'},
value: {name: 'value', type: 'string'},
},
columnArray: [
{name: 'name', type: 'string'},
{name: 'value', type: 'string'},
],
},