Files
mattermost-mobile/app/database/models/server/group_membership.ts
Elias Nahum 8cd127a223 [Gekidou] Typings & PostMetadata structure (#5542)
* Typings & PostMetadata structure

* comment out unused code

* Remove duplicate interface

* Fix getPreferenceAsBool defaultValue
2021-07-15 11:49:02 -04:00

55 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 type GroupModel from '@typings/database/models/servers/group';
import type UserModel 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 GroupMembershipModel 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<GroupModel>;
/** memberUser : The related user in the group */
@immutableRelation(USER, 'user_id') memberUser!: Relation<UserModel>;
/**
* 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<GroupModel>;
/**
* 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<UserModel>;
}