UNPKG

amis-model-design

Version:

amis模型设计器

544 lines (543 loc) 14.5 kB
import type { Schema, ApiObject } from 'amis-core'; import { JSONQL } from './jsonql'; import { FilterOP } from './utils/FilterUtils'; import type { ConditionValue } from 'amis'; import type { Strategy } from './schema-builder/ApiStrategyInterface'; import type { DSFeatureType } from 'amis-editor'; /** * 字段类型 */ export type FieldItemType = 'text' | 'textarea' | 'int' | 'float' | 'date' | 'datetime' | 'date-range' | 'enum' | 'boolean' | 'password' | 'ciphertext' | 'attachment' | 'image' | 'address' | 'location' | 'serial-number' | 'user' | 'department' | 'owner' | 'owner-department' | 'mpath' | 'json' | 'money' | 'parent' | 'relation' | string; /** * 字段配置 */ export interface FieldItem extends Record<string, any> { id?: string; name: string; key: string; type: FieldItemType; length?: number; isRequired?: boolean; isNullable?: boolean; isBuiltIn?: boolean; relationId?: string; relation?: RelationShipItem; /** * @deprecated 这个单词写错了,只做兼容,以后用 isPrimaryKey */ isPrimayKey?: boolean; isPrimaryKey?: boolean; isGenerated?: boolean; isForeignKey?: boolean; isUnique?: boolean; modified?: boolean; isArray?: boolean; defaultValue?: any; defaultValueMode?: 'null' | 'static' | 'expression' | 'current_user'; comment?: string; isCreateDate?: boolean; isUpdateDate?: boolean; isDeleteDate?: boolean; isCreateUser?: boolean; isUpdateUser?: boolean; isDeleteUser?: boolean; isOrderNumber?: boolean; isTreeParentId?: boolean; isTreePath?: boolean; isTenantId?: boolean; isAutoCreated?: boolean; referencedColumnName?: string; validations?: { [rule: string]: any; }; validationErrors?: { [rule: string]: string; }; parentId?: string; parentKey?: string; parentName?: string; parentModelKey?: string; parentModelName?: string; raw?: Record<string, any>; } export declare const FieldItemProps: string[]; /** * 关系类型 */ export type RelationMode = '1:1' | '1:n' | 'n:1' | 'n:n'; export type DialectType = 'mysql' | 'mssql' | 'postgres' | 'oracle' | string; export type DBEnv = { label: string; value: string; suffix?: string; }; export interface DatabaseDialectType { /** * 数据库类型 */ type: DialectType; /** * 类型名称 */ title: string; /** * 类型说明 */ description?: string; /** * 构建配置表, 不同的 db 配置属性不一样 */ buildSettingForm?: (schema: FormSchema, env: string) => void; /** * 是否支持关系,即是否为关系型数据库 */ isRdbms?: boolean; /** * 是否支持 json */ /** * 字段是否支持数组,比如 postgres 就支持。 */ /** * 是否支持动态定义字段 * 详见: https://www.mongodb.com/unstructured-data/schemaless */ schemaLess?: boolean; /** * Read/Write Splitting */ supportReadWriteSplitting?: boolean; } /** * 关系配置 */ export interface RelationShipItem extends Record<string, any> { id: string; relationMode: RelationMode; fieldId?: string; field?: FieldItem; key: string; target: string | number; targetName: string; targetKey: string; inverseSide?: string; modified?: boolean; isNullable?: boolean; withCustomProps?: boolean; isAutoCreated?: boolean; /** * 在一对一和多对多时有用 * * - 一对一是,就是字面意思,外键在对方身上 * - 多对多,意思是通过反向多对多关联,inverseSide 必须有值 * 且 joinTable 将无效 */ joinColumnAtTarget?: boolean; /** * @deprecated 请改用 joinTable.key */ thirdModelId?: string | number; foreignKey?: string; joinTable?: { key: string; joinColumn: { key: string; }; inverseJoinColumn: { key: string; }; }; } export declare const RelationShipItemProps: string[]; /** * 模型配置 */ export interface ModelItem extends Record<string, any> { /** 模型ID */ id: string; /** 模型中文名 */ name: string; /** 模型表名 */ key: string; /** 数据源ID */ dsId?: string; fields: Array<FieldItem>; relations: Array<RelationShipItem>; indices: Array<{ columns: Array<string>; unique?: boolean; name: string; }>; isRelationShip?: boolean; useSoftDelete?: boolean; isDeleted?: boolean; saveOperator?: boolean | { createdBy?: boolean | string; updatedBy?: boolean | string; deletedBy?: boolean | string; }; saveTimestamp?: boolean | { createdAt?: boolean | string; updatedAt?: boolean | string; deletedAt?: boolean | string; }; diagramInfo?: { x?: number; y?: number; }; description?: string; primaryField?: string; nameField?: string; titleTpl?: string; isTree?: boolean; treePattern?: // 树形结构的类型 'adjacency-list' | 'nested-set' | 'materialized-path' | 'closure-table'; customOrder?: boolean; /** * 是否启用多租户 */ multiTenant?: boolean; validateRules?: any; acl?: any; aclMode?: 'open' | 'write-limit' | 'delete-limit' | 'limit' | 'custom'; aclDefault?: any; fieldsAcl?: any; disabled?: boolean; } export interface DatabaseConfig extends Record<string, any> { host: string; port: number; schema?: string; username?: string; password?: string; poolLimit?: number; } export interface DataSourceItem extends Record<string, any> { id?: string; name: string; key: string; /** * 数据库类型 */ dialect: DialectType | keyof any; synchronizeMode?: number; /** * 数据具体的配置 */ config?: DatabaseConfig & { useBultinAccount?: boolean; otherEnv: { [propName: string]: Partial<DatabaseConfig> & { saveAsDev?: boolean; }; }; }; } export interface ModelCollection extends Record<string, any> { datasource?: DataSourceItem; /** * 数据模型集合 */ models: ModelItem[]; } export interface DBTableBrief { /** * 数据库名 */ database: string; /** * 表名 */ name: string; } export interface TableColumn extends FieldItem { dbType: string; rawType: string; } export interface DBTable extends DBTableBrief { /** * 字段集合 */ fields: Array<TableColumn>; /** * 唯一字段 */ uniques: Array<any>; /** * 索引 */ indices: Array<{ columns: Array<string>; unique?: boolean; name: string; }>; foreignKeys: Array<any>; exclusions: Array<any>; } export interface FieldType { type: FieldItemType; /** * 字段标题,比如,文本字段、图片字段 */ title: string; /** * 出现在 er 图中的简单名字,如果没配置用 title */ shortName?: string | ((field: FieldItem, model: ModelItem) => string); /** * 字段描述 */ description?: string; /** * 影响位置排序 */ order?: boolean; /** * 决定展示的分类 */ tag?: string | string[]; /** * 新增初始值 */ scaffold?: Partial<FieldItem>; /** * 禁用手动创建,也就是说只能自动创建 */ disableManualCreate?: boolean; /** * 检查DB能够映射的字段 */ acceptDBColumn?: (column: TableColumn, field?: FieldItem) => boolean; /** * 可作为插入字段 */ insertable?: boolean; /** * 可作为更新字段 */ updateable?: boolean; /** * 可作为查询字段 */ searchable?: boolean; /** * 可作为过滤字段 */ filterable?: boolean; /** * 允许的查询类型 */ allowedFilterOp?: Array<{ label: string; value: FilterOP | string; }>; /** * 默认的查询方式 */ defaultFilterOp?: FilterOP; /** * 作为CondtionBuilder的字段时的amis schema type, filterable为true的字段才支持 */ CBType?: string; /** * 不允许设置默认值的源 */ supportDefaultValue?: (dialect: DialectType) => boolean; /** * 可作为排序字段 */ sortable?: boolean; /** * 字段验证,有问题时返回错误 * @param value * @returns */ validate?: (value: FieldItem) => void | string | Promise<void | string>; /** * 是否支持索引 */ supportIndex?: boolean; } export interface RelationType { type: RelationMode; /** * 关系标题,比如:一对一、一对多 */ title: string; /** * 关系描述 */ description: string; /** * 影响位置排序 */ order?: boolean; /** * 新增初始值 */ scaffold?: RelationShipItem; } export interface FormSchema extends Record<string, any> { type: 'form'; body: Array<any>; /** Form脚手架关系实体默认配置 */ value?: Record<string, any>; } export type MayBeSchema = Partial<Schema>; export interface JSONQLAPI extends ApiObject { /** JSONQL结构体 */ jsonql?: JSONQL; vars?: any[]; /** 声明为模型接口 */ sourceType?: string; /** 非 GET 请求在运行时需要追加的 Query */ runtimeQuery?: Record<string, string>; } export type Api = string | (ApiObject & { strategy?: Exclude<Strategy, 'mixed'>; }) | JSONQLAPI; /** CRUD或Form支持的功能场景 */ export type FeatType = Exclude<DSFeatureType, 'Import' | 'Export'>; export interface ExtraFieldItem extends FieldItem { /** modelKey + fieldKey全路径 */ keyPath?: string; /** 模型ID */ modelValue?: string; /** 模型名称 */ modelLabel?: string; /** 模型Key */ modelKey?: string; /** 可查看 */ viewable?: boolean; /** 可新增 */ insertable?: boolean; /** 可更新 */ updateable?: boolean; /** 可过滤 */ filterable?: boolean; /** 可搜索 */ searchable?: boolean; /** 可删除 */ removable?: boolean; /** 可搜索的条件 */ allowedFilterOp?: any[]; /** 作为CB成员的schema */ CBFieldSchema?: Schema; /** 是否参与构建 */ checked?: boolean; /** 是否为级联表平铺的字段 */ isCascadingField?: boolean; /** 简单查询匹配规则 */ filterOp?: string; } export interface ExtraRelationShipItem extends RelationShipItem { /** modelKey + fieldKey全路径 */ keyPath?: string; /** 模型ID */ modelValue?: string; /** 模型名称 */ modelLabel?: string; /** 模型Key */ modelKey?: string; /** 可查看 */ viewable?: boolean; /** 可新增 */ insertable?: boolean; /** 可更新 */ updateable?: boolean; /** 可过滤 */ filterable?: boolean; /** 可搜索 */ searchable?: boolean; /** 可删除 */ removable?: boolean; /** 可搜索的条件 */ allowedFilterOp?: any[]; /** 作为CB成员的schema */ CBFieldSchema?: Schema; /** 是否参与构建 */ checked?: boolean; /** 关系字段schema构建配置 */ relationBuildSetting?: Record<string, any>; /** 关系字段配置Schema */ settingSchema?: Record<string, any>; /** 是否为级联表平铺的字段 */ isCascadingField?: boolean; } /** 附加了额外信息的字段结构体 */ export type ExtraField = ExtraFieldItem | ExtraRelationShipItem; export interface SelectFieldItem { /** 字段ID */ id: string; /** 字段类型 */ type: FieldItemType; /** 字段类型中文名称 */ typeLabel?: string; /** 字段中文名 */ name: string; /** 字段名 */ key: string; /** name字段的alias,用于前端展示, 区别是级联字段会以modelName.fieldName形式展示 */ label: string; /** key字段的alias,区别是级联字段会以modelKey.fieldKey形式 */ value: string; /** 主表实体 ID */ masterModelId?: string; } export interface SelectRelationShipItem extends SelectFieldItem { /** 目标实体ID */ targetId: string; /** 目标实体key */ targetKey: string; /** 目标实体名称 */ targetName: string; /** 关系类型模式 */ relationMode: string; inverseSide?: string; } export type SelectField = SelectFieldItem | SelectRelationShipItem; /** 模型实体的API Schema格式 */ export interface ModelEntityApiSchema { method?: string; url?: string; /** JSONQL 结构体 */ jsonql?: any; /** 实体接口的功能场景,可以用于脚手架重新构建时识别配置,也用于运行态时转换请求 method */ action?: FeatType; /** JSONQL构建的来源 */ origin?: 'sdk' | 'saas'; /** 数据源的类型 */ sourceType?: 'model-entity' | 'api' | 'apicenter'; /** 数据源为实体时的构建策略 */ strategy?: Extract<Strategy, 'restful' | 'jsonql'>; /** 需要获取实体数据的各类场景 CRUD | 参照录入|自动填充 | 关联他表,这个字段只是为了区分普通 JSONQL 和前面几个场景 */ scene?: 'list' | 'suggestion' | 'autoFill' | 'options'; /** 实体 */ entity?: { /** 实体字段值, 格式: 数据源.表名 */ value: string; /** 实体字段Label, 格式: 数据源 / 表名 */ label: string; /** 数据源ID */ dsId: string; dsKey: string; dsLabel: string; /** 实体模型ID */ mId: string; mKey: string; mLabel: string; }; /** 需要查询的字段集合 */ select?: SelectField[]; /** 数据条数, 不设置默认为multiple */ limit?: 'piece' | 'multiple'; /** 条件过滤器,格式为 ConditionBuilder 组件,无法直接用于 JSONQL 的 where 子句 */ filters?: ConditionValue; /** 字段排序方式 */ orders?: Array<{ field: string; order: boolean; }>; /** 运行态时需要追加的query映射 */ runtimeQuery?: Record<string, string>; }