UNPKG

amis-model-design

Version:

amis模型设计器

359 lines (358 loc) 12.8 kB
/** * @file FieldManager.ts * @desc 字段管理相关 */ import { Manager } from './Manager'; import { ApiBuilder } from '../schema-builder/ApiBuilder'; import type { JSONSchema } from 'amis'; import type { DataSourceItem, FieldItem, ModelItem, FieldItemType, RelationShipItem, FeatType, ExtraField, FieldType } from '../type'; import type { ManagerOptions } from './Manager'; import type { ModelStore } from './ModelStore'; import type { BuildFieldSchemaContextOthers } from '../base/PluginInterface'; import type { Strategy, ApiStrategyInterface } from '../schema-builder/ApiStrategyInterface'; interface ReqFieldItem { label: string; value: string; type: FieldItemType; } type GenericSchema = Record<string, any>; export interface ReqCondition { /** 是否返回系统字段 */ withSystemFields?: boolean; /** 是否返回关系相关字段(relation,外键) */ withRelationFields?: boolean; /** 是否返回字段在CondtionBuilder中的 */ withCBFieldSchema?: boolean; } export type FieldTreeItem<T = any> = { children?: FieldTreeItem<T>[]; } & T; export interface FieldManagerInterface { manager: Manager; fieldTypeMap: Record<string, string>; /** 系统字段排序优先级 */ systemFieldOrderMap: Record<string, number>; /** 获取字段类型名称 */ getTypeLabel(field: FieldItem | RelationShipItem): string; /** 切换数据源 */ switch2Datasource(params: { /** 考虑到应用导入导出后会数据源/实体的 ID 会变化,所以后面统一用 key 作为标识符(应用内唯一) */ dsKey: string; ds?: DataSourceItem; forceUpdate?: string | boolean; }): Promise<void>; /** 获取当前实体的字段集合 */ getModelFields(params: { dsKey: string; mKey: string; }, options?: ReqCondition): Promise<{ fields: FieldItem[]; relations: RelationShipItem[]; }>; /** 获取当前实体的字段集合,包含深层对一关系字段 */ getAvailableFieldsDeep(params: { dsKey: string; mKey: string; options?: { /** 列表的展示方式,默认平铺,支持分组、树形 */ listMode?: 'group' | 'tree'; /** 字段使用的功能场景,默认为 List */ feat?: FeatType; /** 是否排除本表字段 */ excludeSelf?: boolean; /** 需要排除的实体表,通常用于过滤主表 */ excludeModelKeys?: string[]; /** 当 listMode 为 tree 时,开启此选项会将外键和系统字段收纳为子分类 */ enableSubGroup?: boolean; defaultChecked?: boolean; }; }): Promise<FieldTreeItem<ExtraField | { label: string; value: string; }>[] | ExtraField[]>; /** 获取当前实体ConditionBuilder组件的字段定义集合 */ getConditionBuilderFields(params: { dsKey: string; mKey: string; options?: { /** 列表的展示方式,默认平铺,支持树形 */ listMode?: 'tree'; }; }): Promise<any[]>; /** 获取指定 ID 字段的信息 */ getFieldInfo(params: { dsKey: string; mKey: string; fieldId: string; }): Promise<(FieldItem & { filterable: boolean; allowedFilterOp: FieldType['allowedFilterOp']; }) | void>; /** 构建 CRUD 列的快速编辑配置 */ buildFieldQuickEditSettingForm(params: { dsKey: string; mKey: string; fieldId: string; config?: BuildFieldSchemaContextOthers & { [propName: string]: any; }; }): Promise<GenericSchema | undefined>; /** 构建脚手架中关系字段编辑状态的配置表单 */ buildRelationFieldEditSettingForm(params: { dsKey: string; mKey: string; fieldId: string; schema: any; config?: BuildFieldSchemaContextOthers & { [propName: string]: any; }; }): Promise<any>; /** 根据策略构建 API 请求结构 */ buildApiBySchema(params: { apiSchema: Record<string, any>; entity: { dsKey: string; mKey: string; }; options: { strategy: Strategy; apiPrefix: string; useApiSchema?: boolean; }; }, /** 外部来控制api的构建更灵活一点 */ hookFn: (fields: ExtraField[], ab: ApiBuilder, apiStrategy: ApiStrategyInterface) => Promise<any>): Promise<any>; /** 获取模型字段树,表格结构 */ getFieldTableTree(dsKey: string, mKey: string, options: { fields: ReqFieldItem[] | '*'; }): Promise<any[]>; /** 获取当前实体字段JSONSchema结构 */ getFieldJsonSchema(dsKey: string, mKey: string, options: { rootId?: string; fields: ReqFieldItem[] | '*'; }): Promise<JSONSchema>; } export declare class FieldManager implements FieldManagerInterface { /** 模型管理 */ manager: Manager; /** 字段名称映射 */ fieldTypeMap: Record<string, string>; /** 系统字段排序优先级 */ systemFieldOrderMap: Record<string, number>; constructor(options: ManagerOptions); /** 获取已经注册实体字段插件 */ get registeredFieldPlugins(): import("../base/PluginInterface").PluginInterface[]; /** 初始化类型映射 */ _initFieldTypeMap(): { /** 补充一下关系字段具体名称 */ '1:1': string; '1:n': string; 'n:1': string; 'n:n': string; /** 分类字段,不需要展示类型 */ subGroup: string; }; /** 切换数据源 & 对应的模型列表 */ switch2Datasource(params: { dsKey: string; ds?: DataSourceItem; forceUpdate?: string | boolean; }): Promise<void>; /** 获取字段类型名称 */ getTypeLabel(field: FieldItem | RelationShipItem): string; /** 将字段类型转化为前端可用的结构 */ _normalizeField2FE(field: FieldItem, model: ModelItem): { value: string; label: string; typeLabel: string; id?: string | undefined; name: string; key: string; type: string; length?: number | undefined; isRequired?: boolean | undefined; isNullable?: boolean | undefined; isBuiltIn?: boolean | undefined; relationId?: string | undefined; relation?: RelationShipItem | undefined; isPrimayKey?: boolean | undefined; /** 是否返回关系相关字段(relation,外键) */ isPrimaryKey?: boolean | undefined; isGenerated?: boolean | undefined; isForeignKey?: boolean | undefined; isUnique?: boolean | undefined; modified?: boolean | undefined; isArray?: boolean | undefined; defaultValue?: any; defaultValueMode?: "static" | "null" | "expression" | "current_user" | undefined; comment?: string | undefined; isCreateDate?: boolean | undefined; isUpdateDate?: boolean | undefined; isDeleteDate?: boolean | undefined; isCreateUser?: boolean | undefined; isUpdateUser?: boolean | undefined; isDeleteUser?: boolean | undefined; isOrderNumber?: boolean | undefined; isTreeParentId?: boolean | undefined; isTreePath?: boolean | undefined; isTenantId?: boolean | undefined; isAutoCreated?: boolean | undefined; referencedColumnName?: string | undefined; validations?: { [rule: string]: any; } | undefined; validationErrors?: { [rule: string]: string; } | undefined; parentId?: string | undefined; parentKey?: string | undefined; parentName?: string | undefined; /** 需要排除的实体表,通常用于过滤主表 */ parentModelKey?: string | undefined; parentModelName?: string | undefined; raw?: Record<string, any> | undefined; }; /** * 获取当前模型的字段集合 */ getModelFields(params: { dsKey: string; mKey: string; }, options?: ReqCondition): Promise<{ fields: FieldItem[]; relations: RelationShipItem[]; }>; /** 处理字段集合输出 */ _normalizeFieldsByType(params: { model?: ModelStore; } & ReqCondition): never[] | Promise<any[]>; /** 获取可以使用的全部字段,包括可以展开的关联表字段 */ getAvailableFieldsDeep(params: { dsKey: string; mKey: string; options?: { /** 列表的展示方式,默认平铺,支持分组、树形 */ listMode?: 'group' | 'tree'; /** 字段使用的功能场景,默认为 List */ feat?: FeatType; /** 是否排除本表字段 */ excludeSelf?: boolean; /** 需要排除的实体表,通常用于过滤主表 */ excludeModelKeys?: string[]; /** 当 listMode 为 tree 时,开启此选项会将外键和系统字段收纳为子分类 */ enableSubGroup?: boolean; defaultChecked?: boolean; }; }): Promise<FieldTreeItem<ExtraField | { label: string; value: string; }>[] | ExtraField[]>; /** * 构建可用的字段树 */ _buildAvailableFieldsTree<T = ExtraField>(fields: ExtraField[], mapper?: (field: ExtraField) => any): FieldTreeItem<T>[]; /** * 构建可用的字段树,并支持子分类 * * @param fields 额外字段数组 * @returns 字段树项数组 */ _buildAvailableFieldsTreeWithSubGroup(fields: ExtraField[]): any[]; getConditionBuilderFields(params: { dsKey: string; mKey: string; options?: { /** 列表的展示方式,默认平铺,支持树形 */ listMode?: 'tree'; }; }): Promise<any[]>; getFieldInfo(params: { dsKey: string; mKey: string; fieldId: string; }): Promise<any>; /** * 构建字段快速编辑配置 */ buildFieldQuickEditSettingForm(params: { dsKey: string; mKey: string; fieldId: string; config?: BuildFieldSchemaContextOthers & { [propName: string]: any; }; }): Promise<{ type: string; className: string; wrapperBody: boolean; visibleOn: string; body: { type: string; name: string; label: boolean; items: any[]; multiLine: boolean; multiple: boolean; noBorder: boolean; subFormMode: string; itemClassName: string; subFormHorizontal: { justify: boolean; }; }[]; } | undefined>; /** * 构建脚手架中关系字段编辑状态的配置表单 */ buildRelationFieldEditSettingForm(params: { dsKey: string; mKey: string; fieldId: string; schema: any; config?: BuildFieldSchemaContextOthers & { [propName: string]: any; }; }): Promise<any>; /** 根据策略构建 API 请求结构 */ buildApiBySchema(params: { apiSchema: Record<string, any>; entity: { dsKey: string; mKey: string; }; options: { strategy: Strategy; apiPrefix: string; useApiSchema?: boolean; }; }, /** 外部来控制api的构建更灵活一点 */ hookFn: (fields: ExtraField[], ab: ApiBuilder, apiStrategy: ApiStrategyInterface) => Promise<any>): Promise<any>; /** * 获取模型表字段树表格 */ getFieldTableTree(dsKey: string, mKey: string, options: { fields: ReqFieldItem[] | '*'; }): Promise<any[]>; _recursiveBuildFieldTableTree(model: ModelStore, modelMap: Record<string, boolean>, prefix: string, level: number, config: { depth: number; models: ModelStore[]; fieldValues: string[]; }): any[]; /** * 获取当前实体字段JsonSchema结构 */ getFieldJsonSchema(dsKey: string, mKey: string, options: { rootId?: string; fields: ReqFieldItem[] | '*'; }): Promise<any>; _recursiveBuildFieldJsonSchema(model: ModelStore, modelMap: Record<string, boolean>, prefix: string, level: number, config: { depth: number; models: ModelStore[]; fieldValues: string[] | '*'; }): Record<string, any>; /** * 接口相关的报错使用toast,内部相关错误使用log输出 */ log(msg: string, type?: 'error' | 'warn' | 'log'): void; } export {};