forked from Ivasoft/mattermost-mobile
* Refactored storage layer - in progress * Refactored DatabaseManager & Operators * Renamed isRecordAppEqualToRaw to isRecordInfoEqualToRaw * Review feedback * Update app/database/models/app/info.ts Co-authored-by: Miguel Alatzar <migbot@users.noreply.github.com> * Update app/database/models/server/my_team.ts Co-authored-by: Miguel Alatzar <migbot@users.noreply.github.com> Co-authored-by: Avinash Lingaloo <> Co-authored-by: Miguel Alatzar <migbot@users.noreply.github.com>
54 lines
2.0 KiB
TypeScript
54 lines
2.0 KiB
TypeScript
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
// See LICENSE.txt for license information.
|
|
|
|
import {Q, Query, Relation} from '@nozbe/watermelondb';
|
|
import {field, immutableRelation, lazy} from '@nozbe/watermelondb/decorators';
|
|
import Model, {Associations} from '@nozbe/watermelondb/Model';
|
|
|
|
import {MM_TABLES} from '@constants/database';
|
|
import Group from '@typings/database/models/servers/group';
|
|
import User from '@typings/database/models/servers/user';
|
|
|
|
const {GROUP, GROUP_MEMBERSHIP, USER} = MM_TABLES.SERVER;
|
|
|
|
/**
|
|
* The GroupMembership model represents the 'association table' where many groups have users and many users are in
|
|
* groups (relationship type N:N)
|
|
*/
|
|
export default class GroupMembership extends Model {
|
|
/** table (name) : GroupMembership */
|
|
static table = GROUP_MEMBERSHIP;
|
|
|
|
/** associations : Describes every relationship to this table */
|
|
static associations: Associations = {
|
|
|
|
/** A GROUP can have multiple users in it */
|
|
[GROUP]: {type: 'belongs_to', key: 'group_id'},
|
|
|
|
/** A USER can be part of multiple groups */
|
|
[USER]: {type: 'belongs_to', key: 'user_id'},
|
|
};
|
|
|
|
/* group_id: The foreign key to the related Group record*/
|
|
@field('group_id') groupId!: string;
|
|
|
|
/* user_id: The foreign key to the related User record*/
|
|
@field('user_id') userId!: string;
|
|
|
|
/** memberGroup : The related group this user belongs to */
|
|
@immutableRelation(GROUP, 'group_id') memberGroup!: Relation<Group>;
|
|
|
|
/** memberUser : The related user in the group */
|
|
@immutableRelation(USER, 'user_id') memberUser!: Relation<User>;
|
|
|
|
/**
|
|
* getAllGroupsForUser : Retrieves all the groups that the user is part of
|
|
*/
|
|
@lazy getAllGroupsForUser = this.collections.get(GROUP).query(Q.on(USER, 'id', this.userId)) as Query<Group>;
|
|
|
|
/**
|
|
* getAllUsersInGroup : Retrieves all the users who are part of this group
|
|
*/
|
|
@lazy getAllUsersInGroup = this.collections.get(USER).query(Q.on(GROUP, 'id', this.groupId)) as Query<User>;
|
|
}
|