mattermost-mobile-upstream/types/database/database.ts
Guillermo Vayá b541867fbf
Add BoardView, PropertyField, PropertyValue WatermelonDB tables (#9708)
* Add BoardView, PropertyField, PropertyValue WatermelonDB tables

Data-layer groundwork for the Integrated Boards feature. Bumps the
server schema from v19 to v20 and adds three new tables with their
models, transformers, handler mixin, and tests. No queries, actions,
or UI yet — those arrive with follow-up work.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* Align boards types with server contract (BO/BP, KanbanProps)

Widen ChannelType to include BO/BP, add OPEN_BOARD/PRIVATE_BOARD and
CHANNEL_PROPS.BOARD_LINKED_PROPERTIES constants, and replace the
free-form View.props with the typed KanbanProps/KanbanGroupBy/
KanbanColumn shape from mattermost/mattermost#35887.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* Allow null View.props to match transformer behavior

The boards transformer assigns null to view.props when neither the
incoming record nor existing row provides one, but the View type
declared props as non-nullable. Widen to KanbanProps | null so
consumers don't need unsafe non-null assertions.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* remove test that don't provide value

* Throw synchronously in boards transformers for consistency

Other server transformers (channel, category, post, custom_profile)
throw synchronously from their non-async Promise-returning signatures
when a non-create action is called without an existing record. The
boards transformer was the odd one out using Promise.reject; align
it with the prevailing pattern and update the two tests to assert the
synchronous throw via expect(() => ...).toThrow.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* Allow null View.description to match transformer behavior

The schema marks description as isOptional, the model types it as
string | null, and the transformer assigns null when the raw payload
omits it. The wire type was string | undefined only — widen to
string | null | undefined so callers handle the null the transformer
can write through.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* Make View.props optional to match nullable schema contract

Schema marks props as isOptional and model types it as KanbanProps | null,
so ViewPatch consumers should be able to omit or clear it.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* Rename View → BoardView across the boards data layer

Avoids the symbol clash with React Native's View component and gives
the migration/handler names a clearer boards-specific identity.

Renames:
- API type View → BoardView, ViewPatch → BoardViewPatch
- Model class ViewModel → BoardViewModel
- MM_TABLES.SERVER.VIEW → BOARD_VIEW (table name string unchanged)
- transformViewRecord → transformBoardViewRecord
- handleViews → handleBoardViews (views arg → boardViews)
- HandleViewsArgs → HandleBoardViewsArgs

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* Extract PropertyField/Value transformers and handlers into properties.ts

PropertyField and PropertyValue are shared primitives, not boards-specific
(future features such as cards and content flagging will reuse them).
Moving them out of the boards files keeps the boards namespace focused on
BoardView and lets other product features import the property handlers
without a boards dependency.

- transformers/properties.ts: transformPropertyFieldRecord, transformPropertyValueRecord
- handlers/properties.ts: PropertiesHandler mixin + PropertiesHandlerMix
- boards.ts files keep only BoardView code
- ServerDataOperator now composes PropertiesHandler alongside BoardsHandler
- Tests split into properties.test.ts and boards.test.ts accordingly

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* Move boards code under app/products/boards (mirroring playbooks layout)

BoardView is a boards-specific feature, so its model, schema, transformer,
and handler should live alongside the rest of the product code rather than
in the top-level database namespace. Adopt the same layout used by
playbooks and agents.

Structure:
  app/products/boards/
    constants/database.ts                       (BOARDS_TABLES)
    database/
      models/{index.ts,board_view.ts}
      schema/{index.ts,board_view.ts}
      operators/
        transformers/{index.ts,index.test.ts}
        handlers/{index.ts,index.test.ts}
    types/
      api.d.ts                                   (ambient BoardView/KanbanProps/...)
      database/models/board_view.ts              (BoardViewModel interface)

Wiring:
- tsconfig.json + babel.config.js: add @boards/* alias
- DatabaseManager (+ mock): import BoardViewModel from @boards
- Server schema index: import BoardViewSchema from @boards
- ServerDataOperator: import BoardsHandler from @boards
- Migration: import BOARD_VIEW from @boards/constants/database
- MM_TABLES.SERVER no longer carries BOARD_VIEW
- HandleBoardViewsArgs moved into the boards handler module
  (matches the playbooks pattern of co-locating handler args)

PropertyField/PropertyValue remain at the top-level operator because they
are shared infrastructure not tied to boards.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* update docs

* fix(boards): register board database imports

* test(boards): isolate handler database lifecycle

* test(properties): isolate handler database lifecycle

* snapshot

* fix snapshots back

---------

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-06-01 12:46:40 +02:00

355 lines
8.3 KiB
TypeScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
/* eslint-disable max-lines */
import type {DatabaseType} from '@constants/database';
import type AppDataOperator from '@database/operator/app_data_operator';
import type ServerDataOperator from '@database/operator/server_data_operator';
import type {Database} from '@nozbe/watermelondb';
import type Model from '@nozbe/watermelondb/Model';
import type {Clause} from '@nozbe/watermelondb/QueryDescription';
import type {Class} from '@nozbe/watermelondb/types';
import type {CustomProfileField, CustomProfileAttribute} from '@typings/api/custom_profile_attributes';
import type System from '@typings/database/models/servers/system';
export type SyncStatus = 'synced' | 'pending' | 'failed';
export type WithDatabaseArgs = { database: Database }
export type CreateServerDatabaseConfig = {
dbName: string;
dbType?: DatabaseType;
displayName?: string;
serverUrl?: string;
identifier?: string;
};
export type RegisterServerDatabaseArgs = {
databaseFilePath: string;
displayName: string;
serverUrl: string;
identifier?: string;
};
export type AppDatabase = {
database: Database;
operator: AppDataOperator;
};
export type ServerDatabase = {
database: Database;
operator: ServerDataOperator;
}
export type ServerDatabases = {
[x: string]: ServerDatabase | undefined;
};
export type TransformerArgs<T extends Model, R extends RawValue> = {
action: string;
database: Database;
fieldsMapper?: (model: Model) => void;
tableName?: string;
value: RecordPair<T, R>;
};
export type PrepareBaseRecordArgs<T extends Model, R extends RawValue> = TransformerArgs<T, R> & {
fieldsMapper: (model: Model) => void;
}
export type OperationArgs<T extends Model, R extends RawValue> = {
tableName: string;
createRaws?: Array<RecordPair<T, R>>;
updateRaws?: Array<RecordPair<T, R>>;
deleteRaws?: T[];
transformer: (args: TransformerArgs<T, R>) => Promise<T>;
};
export type Models = Array<Class<Model>>;
// The elements needed to create a new database
export type CreateServerDatabaseArgs = {
config: CreateServerDatabaseConfig;
shouldAddToAppDatabase?: boolean;
};
export type HandleReactionsArgs = {
prepareRecordsOnly: boolean;
postsReactions?: ReactionsPerPost[];
skipSync?: boolean;
};
export type HandleFilesArgs = {
files?: FileInfo[];
prepareRecordsOnly: boolean;
};
export type HandlePostsArgs = {
actionType: string;
order?: string[];
previousPostId?: string;
posts?: Post[];
prepareRecordsOnly?: boolean;
};
export type HandleThreadsArgs = {
threads?: ThreadWithLastFetchedAt[];
prepareRecordsOnly?: boolean;
teamId?: string;
};
export type HandleThreadParticipantsArgs = {
prepareRecordsOnly: boolean;
skipSync?: boolean;
threadsParticipants: ParticipantsPerThread[];
};
export type HandleThreadInTeamArgs = {
threadsMap?: Record<string, Thread[]>;
prepareRecordsOnly?: boolean;
};
export type HandleTeamThreadsSyncArgs = {
data: TeamThreadsSync[];
prepareRecordsOnly?: boolean;
};
export type SanitizeReactionsArgs = {
database: Database;
post_id: string;
rawReactions: Reaction[];
skipSync?: boolean;
};
export type SanitizeThreadParticipantsArgs = {
database: Database;
skipSync?: boolean;
thread_id: $ID<Thread>;
rawParticipants: ThreadParticipant[];
}
export type ChainPostsArgs = {
order?: string[];
previousPostId: string;
posts: Post[];
};
export type SanitizePostsArgs = {
orders: string[];
posts: Post[];
};
export type IdenticalRecordArgs<T extends Model, R extends RawValue> = {
existingRecord: T;
newValue: R;
tableName: string;
};
export type RetrieveRecordsArgs = {
database: Database;
tableName: string;
condition: Clause;
};
export type ProcessRecordsArgs<R extends RawValue> = {
createOrUpdateRawValues: R[];
deleteRawValues: R[];
tableName: string;
fieldName: string;
buildKeyRecordBy?: (obj: Record<string, any>) => string;
shouldUpdate?: (existing: Record<string, any>, newRaw: Record<string, any>) => boolean;
};
export type HandleRecordsArgs<T extends Model, R extends RawValue> = {
buildKeyRecordBy?: (obj: Record<string, any>) => string;
fieldName: string;
transformer: (args: TransformerArgs<T, R>) => Promise<T>;
createOrUpdateRawValues: R[];
deleteRawValues?: R[];
tableName: string;
prepareRecordsOnly: boolean;
shouldUpdate?: (existingRecord: T, newRaw: RawValue) => boolean;
};
export type RangeOfValueArgs = {
raws: RawValue[];
fieldName: string;
};
export type RecordPair<T extends Model, R extends RawValue> = {
record?: T;
raw: R;
};
type PrepareOnly = {
prepareRecordsOnly: boolean;
}
export type HandleInfoArgs = PrepareOnly & {
info?: AppInfo[];
}
export type HandleGlobalArgs = PrepareOnly & {
globals?: IdValue[];
}
export type HandleRoleArgs = PrepareOnly & {
roles?: Role[];
}
export type HandleCustomEmojiArgs = PrepareOnly & {
emojis?: CustomEmoji[];
}
export type HandleSystemArgs = PrepareOnly & {
systems?: IdValue[];
}
export type HandleConfigArgs = PrepareOnly & {
configs: IdValue[];
configsToDelete: IdValue[];
}
export type HandleMyChannelArgs = PrepareOnly & {
channels?: Channel[];
myChannels?: ChannelMembership[];
isCRTEnabled?: boolean;
};
export type HandleChannelInfoArgs = PrepareOnly &{
channelInfos?: Array<Partial<ChannelInfo>>;
};
export type HandleMyChannelSettingsArgs = PrepareOnly & {
settings?: ChannelMembership[];
};
export type HandleChannelArgs = PrepareOnly & {
channels?: Channel[];
};
export type HandleChannelBookmarkArgs = PrepareOnly & {
bookmarks?: ChannelBookmarkWithFileInfo[];
};
export type HandleCategoryArgs = PrepareOnly & {
categories?: Category[];
};
export type HandleGroupArgs = PrepareOnly & {
groups?: Group[];
};
export type HandleGroupChannelsForChannelArgs = PrepareOnly & {
channelId: string;
groups?: Array<Pick<Group, 'id'>>;
}
export type HandleGroupMembershipForMemberArgs = PrepareOnly & {
userId: string;
groups?: Array<Pick<Group, 'id'>>;
}
export type HandleGroupTeamsForTeamArgs = PrepareOnly & {
teamId: string;
groups?: Array<Pick<Group, 'id'>>;
}
export type HandleCategoryChannelArgs = PrepareOnly & {
categoryChannels?: CategoryChannel[];
};
export type HandleMyTeamArgs = PrepareOnly & {
myTeams?: MyTeam[];
};
export type HandleTeamSearchHistoryArgs = PrepareOnly &{
teamSearchHistories?: TeamSearchHistory[];
};
export type HandleTeamChannelHistoryArgs = PrepareOnly & {
teamChannelHistories?: TeamChannelHistory[];
};
export type HandleTeamArgs = PrepareOnly & {
teams?: Team[];
};
export type HandleChannelMembershipArgs = PrepareOnly & {
channelMemberships?: Array<Pick<ChannelMembership, 'user_id' | 'channel_id' | 'scheme_admin'>>;
};
export type HandleTeamMembershipArgs = PrepareOnly & {
teamMemberships?: TeamMembership[];
};
export type HandlePreferencesArgs = PrepareOnly & {
preferences?: PreferenceType[];
sync?: boolean;
};
export type HandleUsersArgs = PrepareOnly & {
users?: UserProfile[];
};
export type HandleDraftArgs = PrepareOnly & {
drafts?: Draft[];
};
export type HandleScheduledPostsArgs = PrepareOnly & {
actionType: string;
scheduledPosts?: ScheduledPost[];
includeDirectChannelPosts?: boolean;
};
export type HandleScheduledPostErrorCodeArgs = PrepareOnly & {
scheduledPostId: string;
errorCode: string;
}
export type HandleCustomProfileFieldsArgs = PrepareOnly & {
fields?: CustomProfileField[];
};
export type HandleCustomProfileAttributesArgs = PrepareOnly & {
attributes?: CustomProfileAttribute[];
};
export type HandlePropertyFieldsArgs = PrepareOnly & {
fields?: PropertyField[];
};
export type HandlePropertyValuesArgs = PrepareOnly & {
values?: PropertyValue[];
};
export type LoginArgs = {
config: Partial<ClientConfig>;
ldapOnly?: boolean;
license: Partial<ClientLicense>;
loginId: string;
mfaToken?: string;
password: string;
serverDisplayName: string;
};
export type ServerUrlChangedArgs = {
configRecord: System;
licenseRecord: System;
selectServerRecord: System;
serverUrl: string;
};
export type GetDatabaseConnectionArgs = {
serverUrl: string;
connectionName?: string;
setAsActiveDatabase: boolean;
}
export type ProcessRecordResults<T extends Model, R extends RawValue> = {
createRaws: Array<RecordPair<T, R>>;
updateRaws: Array<RecordPair<T, R>>;
deleteRaws: T[];
}