docs/docs/cn/plugin-development/server/collection-options.md
export type MigrationRule =
| 'overwrite'
| 'skip'
| 'upsert'
| 'schema-only'
| 'insert-ignore';
export interface CollectionOptions {
name: string;
title?: string;
migrationRules?: MigrationRule[];
inherits?: string[] | string;
filterTargetKey?: string | string[];
fields?: FieldOptions[];
model?: string | ModelStatic<Model>;
repository?: string | RepositoryType;
autoGenId?: boolean;
timestamps?: boolean;
createdAt?: boolean;
updatedAt?: boolean;
deletedAt?: boolean;
paranoid?: boolean;
underscored?: boolean;
indexes?: ModelIndexesOptions[];
}
name - 数据表名称string{
name: 'users' // 用户数据表
}
title - 数据表标题string{
name: 'users',
title: '用户管理' // 在界面上显示为"用户管理"
}
migrationRules - 迁移规则MigrationRule[]{
name: 'users',
migrationRules: ['overwrite'], // 覆盖现有数据
fields: [...]
}
inherits - 继承数据表string[] | string// 单个继承
{
name: 'admin_users',
inherits: 'users', // 继承用户数据表的所有字段
fields: [
{
type: 'string',
name: 'admin_level'
}
]
}
// 多个继承
{
name: 'super_admin_users',
inherits: ['users', 'admin_users'], // 继承多个数据表
fields: [...]
}
filterTargetKey - 过滤目标键string | string[]{
name: 'user_posts',
filterTargetKey: 'userId', // 按用户ID过滤
fields: [...]
}
// 多个过滤键
{
name: 'user_category_posts',
filterTargetKey: ['userId', 'categoryId'], // 按用户ID和分类ID过滤
fields: [...]
}
fields - 字段定义FieldOptions[][]{
name: 'users',
fields: [
{
type: 'string',
name: 'username',
unique: true,
title: '用户名'
},
{
type: 'string',
name: 'email',
unique: true,
title: '邮箱'
},
{
type: 'password',
name: 'password',
title: '密码'
},
{
type: 'date',
name: 'createdAt',
title: '创建时间'
}
]
}
model - 自定义模型string | ModelStatic<Model>// 使用字符串指定模型类名
{
name: 'users',
model: 'UserModel',
fields: [...]
}
// 使用模型类
import { UserModel } from './models/UserModel';
{
name: 'users',
model: UserModel,
fields: [...]
}
repository - 自定义仓库string | RepositoryType// 使用字符串指定仓库类名
{
name: 'users',
repository: 'UserRepository',
fields: [...]
}
// 使用仓库类
import { UserRepository } from './repositories/UserRepository';
{
name: 'users',
repository: UserRepository,
fields: [...]
}
autoGenId - 自动生成IDbooleantrue{
name: 'users',
autoGenId: true, // 自动生成主键ID
fields: [...]
}
// 禁用自动生成ID(需要手动指定主键)
{
name: 'external_data',
autoGenId: false,
fields: [
{
type: 'string',
name: 'id',
primaryKey: true
}
]
}
timestamps - 启用时间戳booleantrue{
name: 'users',
timestamps: true, // 启用时间戳
fields: [...]
}
createdAt - 创建时间字段boolean | stringtrue{
name: 'users',
createdAt: 'created_at', // 自定义创建时间字段名
fields: [...]
}
updatedAt - 更新时间字段boolean | stringtrue{
name: 'users',
updatedAt: 'updated_at', // 自定义更新时间字段名
fields: [...]
}
deletedAt - 软删除字段boolean | stringfalse{
name: 'users',
deletedAt: 'deleted_at', // 启用软删除
paranoid: true,
fields: [...]
}
paranoid - 软删除模式booleanfalse{
name: 'users',
paranoid: true, // 启用软删除
deletedAt: 'deleted_at',
fields: [...]
}
underscored - 下划线命名booleanfalse{
name: 'users',
underscored: true, // 使用下划线命名风格
fields: [...]
}
indexes - 索引配置ModelIndexesOptions[]{
name: 'users',
indexes: [
{
fields: ['email'],
unique: true
},
{
fields: ['username', 'status']
}
],
fields: [...]
}
NocoBase 支持多种字段类型,所有字段都基于 FieldOptions 联合类型定义。字段配置包含基础属性、数据类型特定属性、关系属性以及前端渲染属性。
所有字段类型都继承自 BaseFieldOptions,提供通用的字段配置能力:
interface BaseFieldOptions<T extends BasicType = BasicType> {
// 通用参数
name?: string; // 字段名称
hidden?: boolean; // 是否隐藏
validation?: ValidationOptions<T>; // 验证规则
// 常用列字段属性
allowNull?: boolean;
defaultValue?: any;
unique?: boolean;
primaryKey?: boolean;
autoIncrement?: boolean;
field?: string;
comment?: string;
// 前端相关
title?: string;
description?: string;
interface?: string;
uiSchema?: any;
}
示例:
{
type: 'string',
name: 'username',
allowNull: false, // 不允许空值
unique: true, // 唯一约束
defaultValue: '', // 默认空字符串
index: true, // 创建索引
comment: '用户登录名' // 数据库注释
}
name - 字段名称string{
type: 'string',
name: 'username', // 字段名称
title: '用户名'
}
hidden - 隐藏字段booleanfalse{
type: 'string',
name: 'internalId',
hidden: true, // 隐藏内部ID字段
title: '内部ID'
}
validation - 验证规则interface ValidationOptions<T extends BasicType = BasicType> {
type: T; // 验证类型
rules: FieldValidationRule<T>[]; // 验证规则数组
[key: string]: any; // 其他验证选项
}
interface FieldValidationRule<T extends BasicType> {
key: string; // 规则键名
name: FieldValidationRuleName<T>; // 规则名称
args?: { // 规则参数
[key: string]: any;
};
paramsType?: 'object'; // 参数类型
}
ValidationOptions<T>{
type: 'string',
name: 'email',
validation: {
type: 'string',
rules: [
{ key: 'email', name: 'email' },
{ key: 'required', name: 'required' }
]
}
}
allowNull - 允许空值booleantrueNULL 值{
type: 'string',
name: 'username',
allowNull: false, // 不允许空值
title: '用户名'
}
defaultValue - 默认值any{
type: 'string',
name: 'status',
defaultValue: 'draft', // 默认为草稿状态
title: '状态'
}
unique - 唯一约束boolean | stringfalse{
type: 'string',
name: 'email',
unique: true, // 邮箱必须唯一
title: '邮箱'
}
primaryKey - 主键booleanfalse{
type: 'integer',
name: 'id',
primaryKey: true, // 设为主键
autoIncrement: true
}
autoIncrement - 自增booleanfalse{
type: 'integer',
name: 'id',
autoIncrement: true, // 自动递增
primaryKey: true
}
field - 数据库列名stringfield 一致){
type: 'string',
name: 'userId',
field: 'user_id', // 数据库中的列名
title: '用户ID'
}
comment - 数据库注释string{
type: 'string',
name: 'username',
comment: '用户登录名,用于系统登录', // 数据库注释
title: '用户名'
}
title - 显示标题string{
type: 'string',
name: 'username',
title: '用户名', // 前端显示的标题
allowNull: false
}
description - 字段描述string{
type: 'string',
name: 'email',
title: '邮箱',
description: '请输入有效的邮箱地址', // 字段描述
validation: {
type: 'string',
rules: [{ key: 'email', name: 'email' }]
}
}
interface - 界面组件string{
type: 'string',
name: 'content',
title: '内容',
interface: 'textarea', // 推荐使用文本域组件
uiSchema: {
'x-component': 'Input.TextArea'
}
}
type: 'string' - 字符串字段VARCHARlength: 字符串长度限制trim: 是否自动去除首尾空格interface StringFieldOptions extends BaseColumnFieldOptions<'string'> {
type: 'string';
length?: number; // 字符串长度限制
trim?: boolean; // 是否自动去除首尾空格
}
示例:
{
type: 'string',
name: 'username',
title: '用户名',
length: 50, // 最大50个字符
trim: true, // 自动去除空格
allowNull: false,
unique: true,
validation: {
type: 'string',
rules: [
{ key: 'min', name: 'min', args: { limit: 3 } },
{ key: 'max', name: 'max', args: { limit: 20 } }
]
}
}
type: 'text' - 文本字段TEXT、MEDIUMTEXT、LONGTEXTlength: MySQL 文本长度类型(tiny/medium/long)interface TextFieldOptions extends BaseColumnFieldOptions {
type: 'text';
length?: 'tiny' | 'medium' | 'long'; // MySQL 文本长度类型
}
示例:
{
type: 'text',
name: 'content',
title: '内容',
length: 'medium', // 使用 MEDIUMTEXT
allowNull: true
}
type: 'integer' - 整数字段INTEGERinterface IntegerFieldOptions extends BaseColumnFieldOptions<'number'> {
type: 'integer';
// 继承 Sequelize INTEGER 类型的所有选项
}
示例:
{
type: 'integer',
name: 'id',
title: 'ID',
primaryKey: true,
autoIncrement: true,
allowNull: false
}
type: 'bigInt' - 大整数字段BIGINTinterface BigIntFieldOptions extends BaseColumnFieldOptions<'number'> {
type: 'bigInt';
}
示例:
{
type: 'bigInt',
name: 'userId',
title: '用户ID',
allowNull: false,
unique: true
}
type: 'float' - 浮点数字段FLOATprecision: 精度(总位数)scale: 小数位数interface FloatFieldOptions extends BaseColumnFieldOptions<'number'> {
type: 'float';
precision?: number; // 精度
scale?: number; // 小数位数
}
示例:
{
type: 'float',
name: 'score',
title: '分数',
precision: 5,
scale: 2,
allowNull: true,
defaultValue: 0.0
}
type: 'double' - 双精度浮点数字段DOUBLEprecision: 精度(总位数)scale: 小数位数interface DoubleFieldOptions extends BaseColumnFieldOptions<'number'> {
type: 'double';
precision?: number;
scale?: number;
}
示例:
{
type: 'double',
name: 'price',
title: '价格',
precision: 10,
scale: 2,
allowNull: false,
defaultValue: 0.00
}
type: 'real' - 实数字段REALprecision: 精度(总位数)scale: 小数位数interface RealFieldOptions extends BaseColumnFieldOptions<'number'> {
type: 'real';
precision?: number;
scale?: number;
}
示例:
{
type: 'real',
name: 'rate',
title: '汇率',
precision: 8,
scale: 4,
allowNull: true
}
type: 'decimal' - 精确小数字段DECIMALprecision: 精度(总位数)scale: 小数位数interface DecimalFieldOptions extends BaseColumnFieldOptions<'number'> {
type: 'decimal';
precision?: number; // 精度(总位数)
scale?: number; // 小数位数
}
示例:
{
type: 'decimal',
name: 'amount',
title: '金额',
precision: 10,
scale: 2,
allowNull: false,
defaultValue: 0.00,
validation: {
type: 'number',
rules: [
{ key: 'min', name: 'min', args: { limit: 0 } }
]
}
}
type: 'boolean' - 布尔字段BOOLEAN 或 TINYINT(1)interface BooleanFieldOptions extends BaseColumnFieldOptions<'boolean'> {
type: 'boolean';
}
示例:
{
type: 'boolean',
name: 'isActive',
title: '是否激活',
defaultValue: true,
allowNull: false
}
type: 'radio' - 单选字段BOOLEAN 或 TINYINT(1)interface RadioFieldOptions extends BaseColumnFieldOptions<'boolean'> {
type: 'radio';
}
示例:
{
type: 'radio',
name: 'isDefault',
title: '是否默认',
defaultValue: false,
allowNull: false
}
type: 'date' - 日期字段DATEtimezone: 是否包含时区信息interface DateFieldOptions extends BaseColumnFieldOptions<'date'> {
type: 'date';
timezone?: boolean; // 是否包含时区信息
}
示例:
{
type: 'date',
name: 'birthday',
title: '生日',
allowNull: true,
timezone: false
}
type: 'time' - 时间字段TIMEtimezone: 是否包含时区信息interface TimeFieldOptions extends BaseColumnFieldOptions<'time'> {
type: 'time';
timezone?: boolean;
}
示例:
{
type: 'time',
name: 'startTime',
title: '开始时间',
allowNull: false,
timezone: false
}
type: 'datetimeTz' - 带时区日期时间字段TIMESTAMP WITH TIME ZONEtimezone: 是否包含时区信息interface DatetimeTzFieldOptions extends BaseColumnFieldOptions<'datetime'> {
type: 'datetimeTz';
timezone?: boolean;
}
示例:
{
type: 'datetimeTz',
name: 'createdAt',
title: '创建时间',
allowNull: false,
timezone: true,
defaultToCurrentTime: true,
onUpdateToCurrentTime: true
}
type: 'datetimeNoTz' - 不带时区日期时间字段TIMESTAMP 或 DATETIMEtimezone: 是否包含时区信息interface DatetimeNoTzFieldOptions extends BaseColumnFieldOptions<'datetime'> {
type: 'datetimeNoTz';
timezone?: boolean;
}
示例:
{
type: 'datetimeNoTz',
name: 'updatedAt',
title: '更新时间',
allowNull: false,
timezone: false,
defaultToCurrentTime: true,
onUpdateToCurrentTime: true
}
type: 'dateOnly' - 仅日期字段DATE{
type: 'dateOnly',
name: 'publishDate',
title: '发布日期',
allowNull: true
}
type: 'unixTimestamp' - Unix 时间戳字段BIGINTepoch: 纪元时间interface UnixTimestampFieldOptions extends BaseColumnFieldOptions<'unixTimestamp'> {
type: 'unixTimestamp';
epoch?: number; // 纪元时间
}
示例:
{
type: 'unixTimestamp',
name: 'lastLoginAt',
title: '最后登录时间',
allowNull: true,
epoch: 0
}
type: 'json' - JSON 字段JSON 或 TEXT{
type: 'json',
name: 'metadata',
title: '元数据',
allowNull: true,
defaultValue: {}
}
type: 'jsonb' - JSONB 字段JSONB(PostgreSQL){
type: 'jsonb',
name: 'config',
title: '配置',
allowNull: true,
defaultValue: {}
}
type: 'array' - 数组字段JSON 或 ARRAYdataType: 存储类型(json/array)elementType: 元素类型(STRING/INTEGER/BOOLEAN/JSON)interface ArrayFieldOptions extends BaseColumnFieldOptions<'array'> {
type: 'array';
dataType?: 'json' | 'array'; // 存储类型
elementType?: 'STRING' | 'INTEGER' | 'BOOLEAN' | 'JSON'; // 元素类型
}
示例:
{
type: 'array',
name: 'tags',
title: '标签',
dataType: 'json',
elementType: 'STRING',
allowNull: true,
defaultValue: []
}
type: 'set' - 集合字段JSON 或 ARRAYdataType: 存储类型(json/array)elementType: 元素类型(STRING/INTEGER/BOOLEAN/JSON)interface SetFieldOptions extends BaseColumnFieldOptions<'set'> {
type: 'set';
dataType?: 'json' | 'array';
elementType?: 'STRING' | 'INTEGER' | 'BOOLEAN' | 'JSON';
}
示例:
{
type: 'set',
name: 'categories',
title: '分类',
dataType: 'json',
elementType: 'STRING',
allowNull: true,
defaultValue: []
}
type: 'uuid' - UUID 字段UUID 或 VARCHAR(36)autoFill: 自动填充interface UUIDFieldOptions extends BaseColumnFieldOptions<'uuid'> {
type: 'uuid';
autoFill?: boolean; // 自动填充
}
示例:
{
type: 'uuid',
name: 'id',
title: 'ID',
autoFill: true,
allowNull: false,
primaryKey: true
}
type: 'nanoid' - Nanoid 字段VARCHARsize: ID 长度customAlphabet: 自定义字符集autoFill: 自动填充interface NanoidFieldOptions extends BaseColumnFieldOptions<'nanoid'> {
type: 'nanoid';
size?: number; // ID 长度
customAlphabet?: string; // 自定义字符集
autoFill?: boolean;
}
示例:
{
type: 'nanoid',
name: 'shortId',
title: '短ID',
size: 12,
customAlphabet: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789',
autoFill: true,
allowNull: false,
unique: true
}
type: 'uid' - 自定义 UID 字段VARCHARprefix: 前缀pattern: 验证模式interface UidFieldOptions extends BaseColumnFieldOptions<'uid'> {
type: 'uid';
prefix?: string; // 前缀
pattern?: string; // 验证模式
}
示例:
{
type: 'uid',
name: 'code',
title: '编码',
prefix: 'USR_',
pattern: '^[A-Za-z0-9_][A-Za-z0-9_-]*$',
allowNull: false,
unique: true
}
type: 'snowflakeId' - 雪花 ID 字段BIGINT{
type: 'snowflakeId',
name: 'snowflakeId',
title: '雪花ID',
allowNull: false,
unique: true
}
type: 'password' - 密码字段VARCHARlength: 哈希长度randomBytesSize: 随机字节大小interface PasswordFieldOptions extends BaseColumnFieldOptions<'password'> {
type: 'password';
length?: number; // 哈希长度
randomBytesSize?: number; // 随机字节大小
}
示例:
{
type: 'password',
name: 'password',
title: '密码',
length: 64,
randomBytesSize: 8,
allowNull: false,
hidden: true
}
type: 'encryption' - 加密字段VARCHAR{
type: 'encryption',
name: 'secret',
title: '密钥',
allowNull: true,
hidden: true
}
type: 'virtual' - 虚拟字段{
type: 'virtual',
name: 'fullName',
title: '全名'
}
type: 'context' - 上下文字段dataIndex: 数据索引路径dataType: 数据类型createOnly: 仅创建时设置interface ContextFieldOptions extends BaseFieldOptions {
type: 'context';
dataIndex?: string; // 数据索引路径
dataType?: string; // 数据类型
createOnly?: boolean; // 仅创建时设置
}
示例:
{
type: 'context',
name: 'currentUserId',
title: '当前用户ID',
dataIndex: 'user.id',
dataType: 'integer',
createOnly: true,
allowNull: false
}
type: 'belongsTo' - 属于关系target: 目标数据表名称foreignKey: 外键字段名targetKey: 目标表键字段名onDelete: 删除时的级联操作onUpdate: 更新时的级联操作constraints: 是否启用外键约束interface BelongsToFieldOptions extends BaseRelationFieldOptions {
type: 'belongsTo';
target: string; // 目标数据表名称
foreignKey?: string; // 外键字段名
targetKey?: string; // 目标表键字段名
onDelete?: 'CASCADE' | 'SET NULL' | 'RESTRICT' | 'NO ACTION';
onUpdate?: 'CASCADE' | 'SET NULL' | 'RESTRICT' | 'NO ACTION';
constraints?: boolean; // 是否启用外键约束
}
示例:
{
type: 'belongsTo',
name: 'author',
title: '作者',
target: 'users',
foreignKey: 'authorId',
targetKey: 'id',
onDelete: 'SET NULL',
onUpdate: 'CASCADE',
constraints: false
}
type: 'hasOne' - 拥有一个关系target: 目标数据表名称foreignKey: 外键字段名sourceKey: 源表键字段名onDelete: 删除时的级联操作onUpdate: 更新时的级联操作constraints: 是否启用外键约束interface HasOneFieldOptions extends BaseRelationFieldOptions {
type: 'hasOne';
target: string;
foreignKey?: string;
sourceKey?: string; // 源表键字段名
onDelete?: 'CASCADE' | 'SET NULL' | 'RESTRICT' | 'NO ACTION';
onUpdate?: 'CASCADE' | 'SET NULL' | 'RESTRICT' | 'NO ACTION';
constraints?: boolean;
}
示例:
{
type: 'hasOne',
name: 'profile',
title: '用户资料',
target: 'user_profiles',
foreignKey: 'userId',
sourceKey: 'id',
onDelete: 'CASCADE',
onUpdate: 'CASCADE',
constraints: false
}
type: 'hasMany' - 拥有多个关系target: 目标数据表名称foreignKey: 外键字段名sourceKey: 源表键字段名sortBy: 排序字段sortable: 是否可排序onDelete: 删除时的级联操作onUpdate: 更新时的级联操作constraints: 是否启用外键约束interface HasManyFieldOptions extends BaseRelationFieldOptions {
type: 'hasMany';
target: string;
foreignKey?: string;
sourceKey?: string;
sortBy?: string[]; // 排序字段
sortable?: boolean; // 是否可排序
onDelete?: 'CASCADE' | 'SET NULL' | 'RESTRICT' | 'NO ACTION';
onUpdate?: 'CASCADE' | 'SET NULL' | 'RESTRICT' | 'NO ACTION';
constraints?: boolean;
}
示例:
{
type: 'hasMany',
name: 'posts',
title: '文章列表',
target: 'articles',
foreignKey: 'authorId',
sourceKey: 'id',
sortBy: ['createdAt'],
sortable: true,
onDelete: 'CASCADE',
onUpdate: 'CASCADE',
constraints: false
}
type: 'belongsToMany' - 多对多关系target: 目标数据表名称through: 中间表名称foreignKey: 外键字段名otherKey: 中间表另一端外键sourceKey: 源表键字段名targetKey: 目标表键字段名onDelete: 删除时的级联操作onUpdate: 更新时的级联操作constraints: 是否启用外键约束interface BelongsToManyFieldOptions extends BaseRelationFieldOptions {
type: 'belongsToMany';
target: string;
through: string; // 中间表名称
foreignKey?: string;
otherKey?: string; // 中间表另一端外键
sourceKey?: string;
targetKey?: string;
onDelete?: 'CASCADE' | 'SET NULL' | 'RESTRICT' | 'NO ACTION';
onUpdate?: 'CASCADE' | 'SET NULL' | 'RESTRICT' | 'NO ACTION';
constraints?: boolean;
}
示例:
{
type: 'belongsToMany',
name: 'tags',
title: '标签',
target: 'article_tags',
through: 'article_tag_relations',
foreignKey: 'articleId',
otherKey: 'tagId',
sourceKey: 'id',
targetKey: 'id',
onDelete: 'CASCADE',
onUpdate: 'CASCADE',
constraints: false
}