mattermost-mobile/app/database/server/models/team_membership.ts
Avinash Lingaloo 89d4cf235f
MM_30476 [v2] Section 'Team' of the Server schema (#5071)
* MM_30476 : Added all isolated tables from the server schema

* MM_30476 : Updated 'test' script in package.json

* MM_30476 : ADDED team section of the server schema

* MM_30476 : Apply suggestions from code review

Co-authored-by: Miguel Alatzar <migbot@users.noreply.github.com>

* MM_30476 : Apply suggestions from code review

Co-authored-by: Elias Nahum <nahumhbl@gmail.com>

* MM_30476 : Updates to field name and description

Co-authored-by: Elias Nahum <nahumhbl@gmail.com>
Co-authored-by: Hossein <hahmadia@users.noreply.github.com>

* MM_30476 : Updated my_team and team_search_history description

* MM_30476 : Prefixing boolean fields with 'is'

* MM_30476 : Updated channel.d.ts

Co-authored-by: Hossein <hahmadia@users.noreply.github.com>
Co-authored-by: Elias Nahum <nahumhbl@gmail.com>

* MM_30476 : ADDED lazy queries to TeamMembership

Two methods that will retrieve all users in a team and all the teams that a user is part of

* MM_30476 : Updated descriptions for the associations

* MM_30476 : Updated tests as server schema was updated

* MM_30476 : Updated Team to have a 1:1 relationship with TeamChannelHistory

* MM_30476 : Updated team_membership and user

Co-authored-by: Miguel Alatzar <migbot@users.noreply.github.com>
Co-authored-by: Elias Nahum <nahumhbl@gmail.com>
Co-authored-by: Hossein <hahmadia@users.noreply.github.com>
2021-01-11 21:54:33 +04:00

53 lines
2 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 Model, {Associations} from '@nozbe/watermelondb/Model';
import {field, immutableRelation, lazy} from '@nozbe/watermelondb/decorators';
import {MM_TABLES} from '@constants/database';
import User from '@typings/database/user';
import Team from '@typings/database/team';
const {TEAM, TEAM_MEMBERSHIP, USER} = MM_TABLES.SERVER;
/**
* The TeamMembership model represents the 'association table' where many teams have users and many users are in
* teams (relationship type N:N)
*/
export default class TeamMembership extends Model {
/** table (entity name) : TeamMembership */
static table = TEAM_MEMBERSHIP;
/** associations : Describes every relationship to this entity. */
static associations: Associations = {
/** TEAM and TEAM_MEMBERSHIP share a 1:N relationship; USER can be part of multiple teams */
[TEAM]: {type: 'belongs_to', key: 'team_id'},
/** USER and TEAM_MEMBERSHIP share a 1:N relationship; A TEAM can regroup multiple users */
[USER]: {type: 'belongs_to', key: 'user_id'},
};
/** team_id : The foreign key to the related Team record */
@field('team_id') teamId!: string;
/* user_id: The foreign key to the related User record*/
@field('user_id') userId!: string;
/** memberUser: The related user in the team */
@immutableRelation(USER, 'user_id') memberUser!: Relation<User>;
/** memberTeam : The related team of users */
@immutableRelation(TEAM, 'team_id') memberTeam!: Relation<Team>;
/**
* getAllTeamsForUser - Retrieves all the teams that the user is part of
*/
@lazy getAllTeamsForUser = this.collections.get(TEAM).query(Q.on(USER, 'id', this.userId)) as Query<Team>
/**
* getAllUsersInTeam - Retrieves all the users who are part of this team
*/
@lazy getAllUsersInTeam = this.collections.get(USER).query(Q.on(TEAM, 'id', this.teamId)) as Query<User>
}