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
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
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'},
],
},

View file

@ -53,6 +53,7 @@ const launchApp = async (props: LaunchProps, resetNavigation = true) => {
}
serverUrl = await getActiveServerUrl();
if (serverUrl) {
const credentials = await getServerCredentials(serverUrl);

View file

@ -204,7 +204,7 @@ class PushNotifications {
}
operator.handleGlobal({
global: [{name: 'deviceToken', value: `${prefix}:${deviceToken}`}],
global: [{id: 'deviceToken', value: `${prefix}:${deviceToken}`}],
prepareRecordsOnly: false,
});

View file

@ -9,6 +9,6 @@ import type Global from '@typings/database/models/app/global';
const {APP: {GLOBAL}} = MM_TABLES;
export const getDeviceToken = async (appDatabase: Database) => {
const tokens = (await appDatabase.collections.get(GLOBAL).query(Q.where('name', 'deviceToken')).fetch()) as Global[];
const tokens = (await appDatabase.collections.get(GLOBAL).query(Q.where('id', 'deviceToken')).fetch()) as Global[];
return tokens?.[0]?.value ?? '';
};

View file

@ -9,23 +9,23 @@ import System from '@typings/database/models/servers/system';
const {SERVER: {SYSTEM}} = MM_TABLES;
export const getCurrentUserId = async (serverDatabase: Database) => {
const currentUserId = await serverDatabase.collections.get(SYSTEM).query(Q.where('name', 'currentUserId')).fetch() as System[];
return currentUserId?.[0] ?? '';
const currentUserId = await serverDatabase.collections.get(SYSTEM).query(Q.where('id', 'currentUserId')).fetch() as System[];
return currentUserId?.[0];
};
export const getCommonSystemValues = async (database: Database) => {
const systemRecords = (await database.collections.get(SYSTEM).query(Q.where('name', Q.oneOf(['config', 'license', 'currentUserId']))).fetch()) as System[];
const systemRecords = (await database.collections.get(SYSTEM).query(Q.where('id', Q.oneOf(['config', 'license', 'currentUserId']))).fetch()) as System[];
let config = {};
let license = {};
let currentUserId = '';
systemRecords.forEach((systemRecord) => {
if (systemRecord.name === 'config') {
if (systemRecord.id === 'config') {
config = systemRecord.value;
}
if (systemRecord.name === 'license') {
if (systemRecord.id === 'license') {
license = systemRecord.value;
}
if (systemRecord.name === 'currentUserId') {
if (systemRecord.id === 'currentUserId') {
currentUserId = systemRecord.value;
}
});

View file

@ -11,12 +11,7 @@ export const createSessions = async (serverUrl: string, sessions: any) => {
}
await operator.handleSystem({
systems: [{
// id: string; // todo: to confirm value for session id ?
name: 'sessions',
value: sessions,
}],
systems: [{id: 'sessions', value: sessions}],
prepareRecordsOnly: false,
});

View file

@ -181,19 +181,19 @@ export const loadMe = async (serverUrl: string, {deviceToken, user}: LoadMeArgs)
const systemRecords = operator.handleSystem({
systems: [
{
name: 'config',
id: 'config',
value: JSON.stringify(config),
},
{
name: 'license',
id: 'license',
value: JSON.stringify(license),
},
{
name: 'currentUserId',
id: 'currentUserId',
value: currentUser.id,
},
{
name: 'url',
id: 'url',
value: Client4.getUrl(),
},
],
@ -271,7 +271,7 @@ export const completeLogin = async (serverUrl: string, user: RawUser) => {
// Data retention
if (config?.DataRetentionEnableMessageDeletion === 'true' && license?.IsLicensed === 'true' && license?.DataRetention === 'true') {
dataRetentionPolicy = await getDataRetentionPolicy(serverUrl);
await operator.handleSystem({systems: [{name: 'dataRetentionPolicy', value: dataRetentionPolicy}], prepareRecordsOnly: false});
await operator.handleSystem({systems: [{id: 'dataRetentionPolicy', value: dataRetentionPolicy}], prepareRecordsOnly: false});
}
return null;
@ -294,8 +294,8 @@ export const updateMe = async (serverUrl: string, user: User) => {
const systemRecords = operator.handleSystem({
systems: [
{name: 'currentUserId', value: data.id},
{name: 'locale', value: data?.locale},
{id: 'currentUserId', value: data.id},
{id: 'locale', value: data?.locale},
],
prepareRecordsOnly: true,
});

View file

@ -716,7 +716,7 @@ SPEC CHECKSUMS:
EXConstants: c4dd28acc12039c999612507a5f935556f2c86ce
EXFileSystem: dcf2273f49431e5037347c733a2dc5d08e0d0a9e
FBLazyVector: e686045572151edef46010a6f819ade377dfeb4b
FBReactNativeSpec: cef0cc6d50abc92e8cf52f140aa22b5371cfec0b
FBReactNativeSpec: d35931295aacfe996e833c01a3701d4aa7a80cb4
glog: 73c2498ac6884b13ede40eda8228cb1eee9d9d62
jail-monkey: 01cd0a75aa1034d08fd851869e6e6c3b063242d7
libwebp: e90b9c01d99205d03b6bb8f2c8c415e5a4ef66f0

View file

@ -279,7 +279,7 @@ export type ProcessRecordResults = {
}
export type RawGlobal = {
name: string;
id: string;
value: string;
};
@ -343,12 +343,14 @@ export type RawChannel = {
id: string;
last_post_at: number;
name: string;
policy_id: string;
props: Record<string, any> | null;
purpose: string;
scheme_id: string | null;
shared: boolean | null;
team_id: string;
total_msg_count: number;
total_msg_count_root: number;
type: ChannelType;
update_at: number;
};
@ -580,8 +582,7 @@ export type RawSlashCommand = {
};
export type RawSystem = {
id?: string;
name: string;
id: string;
value: string;
};

View file

@ -11,9 +11,6 @@ export default class Global extends Model {
/** table (name) : global */
static table: string;
/** name : The label/key to use to retrieve the special 'value' */
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 */
value: any;
}

View file

@ -12,9 +12,6 @@ export default class System extends Model {
/** table (name) : System */
static table: string;
/** name : The name or key value for the config */
name: string;
/** value : The value for that config/information */
/** value : The value for that config/information and whose key will be the id column */
value: any;
}