Files
mattermost-mobile/app/database/models/server/category.ts
Avinash Lingaloo 9a72837f04 Gekidou - Updated Server Database Diagrams/Schema/Models (#6119)
* started with the diagrams

* removed redundant tables

next step:
1. reconstruct id ( local id vs server id )
2. annotate fields with examples
3. recreate relationship

* work in progress

* work in progress

* fix association

* update postsInChannel

* removed SlashCommands from the Server database schema

* added missing associations in the models and updated docs/database

* exported server database

* update test

* code corrections following review

* update relationship

* update docs

* removed cyclic relationship

* Revert "removed cyclic relationship"

This reverts commit 4d784efb81.

* removed isOptional from Draft

* linked myChannelSettings to myChannel instead of Channel

* update diagrams

* store null instead of empty string

* update thread association

Co-authored-by: Elias Nahum <nahumhbl@gmail.com>
2022-04-07 10:14:28 -04:00

113 lines
4.3 KiB
TypeScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import {Relation, Query, Q} from '@nozbe/watermelondb';
import {children, field, immutableRelation, lazy} from '@nozbe/watermelondb/decorators';
import Model, {Associations} from '@nozbe/watermelondb/Model';
import {map, distinctUntilChanged} from 'rxjs';
import {MM_TABLES} from '@constants/database';
import type CategoryInterface from '@typings/database/models/servers/category';
import type CategoryChannelModel from '@typings/database/models/servers/category_channel';
import type ChannelModel from '@typings/database/models/servers/channel';
import type MyChannelModel from '@typings/database/models/servers/my_channel';
import type TeamModel from '@typings/database/models/servers/team';
const {
CATEGORY,
CATEGORY_CHANNEL,
CHANNEL,
MY_CHANNEL,
TEAM,
} = MM_TABLES.SERVER;
/**
* A Category holds channels for a given user in a team
*/
export default class CategoryModel extends Model implements CategoryInterface {
/** table (name) : Category */
static table = CATEGORY;
/** associations : Describes every relationship to this table. */
static associations: Associations = {
/** A CHANNEL can belong to several CATEGORY, and a CATEGORY posses multiple channels (N:N relationship).
* We use the intermediate table CATEGORY_CHANNEL for this relationship */
[CATEGORY_CHANNEL]: {type: 'has_many', foreignKey: 'category_id'},
/** A Category belongs to a Team, and a Team can have several categories (relationship 1:N) */
[TEAM]: {type: 'belongs_to', key: 'team_id'},
};
/** display_name : The display name for the category */
@field('display_name') displayName!: string;
/** type : The type of category ('channels' | 'direct_messages' | 'favorites' | 'custom') */
@field('type') type!: CategoryType;
/** sort_order : The index on which to sort and display categories */
@field('sort_order') sortOrder!: number;
/** sorting : The type of sorting applied to the category channels (alpha, recent, manual) */
@field('sorting') sorting!: CategorySorting;
/** collapsed : Boolean flag indicating if the category is collapsed */
@field('collapsed') collapsed!: boolean;
/** muted : Boolean flag indicating if the category is muted */
@field('muted') muted!: boolean;
/** teamId : The team in which this category lives */
@field('team_id') teamId!: string;
/** team : Retrieves information about the team that this category is a part of. */
@immutableRelation(TEAM, 'id') team!: Relation<TeamModel>;
/** categoryChannels : All the CategoryChannels associated with this team */
@children(CATEGORY_CHANNEL) categoryChannels!: Query<CategoryChannelModel>;
/** categoryChannelsBySortOrder : Retrieves assocated category channels sorted by sort_order */
@lazy categoryChannelsBySortOrder = this.categoryChannels.collection.
query(
Q.on(MY_CHANNEL,
Q.where('id', Q.notEq('')),
),
Q.where('category_id', this.id),
Q.sortBy('sort_order', Q.asc),
);
/** channels : Retrieves all the channels that are part of this category */
@lazy channels = this.collections.
get<ChannelModel>(CHANNEL).
query(
Q.experimentalJoinTables([MY_CHANNEL, CATEGORY_CHANNEL]),
Q.on(CATEGORY_CHANNEL,
Q.and(
Q.on(MY_CHANNEL, Q.where('id', Q.notEq(''))),
Q.where('category_id', this.id),
),
),
);
/** myChannels : Retrieves all myChannels that are part of this category */
@lazy myChannels = this.collections.
get<MyChannelModel>(MY_CHANNEL).
query(
Q.experimentalJoinTables([CHANNEL, CATEGORY_CHANNEL]),
Q.on(CATEGORY_CHANNEL,
Q.and(
Q.on(CHANNEL, Q.where('create_at', Q.gte(0))),
Q.where('category_id', this.id),
),
),
Q.sortBy('last_post_at', Q.desc),
);
/** hasChannels : Returns a boolean indicating if the category has channels */
@lazy hasChannels = this.categoryChannels.observeCount().pipe(
map((c) => c > 0),
distinctUntilChanged(),
);
}