跳到主要内容

EntityMetadataOptions

Defined in: packages/rxdb/src/entity/entity-options.interface.ts:59

实体定义元数据选项接口 用于配置 @Entity 装饰器的完整选项,定义实体的结构和行为

这个接口是实体定义的核心,包含了实体的所有配置信息:

  • 基本信息:名称、命名空间、显示名称
  • 结构信息:属性、关系、索引
  • 行为信息:抽象类标记、日志配置、同步策略

Example​

@Entity({
name: 'User',
displayName: '用户',
properties: [
{ name: 'name', type: PropertyType.string, displayName: '姓名' },
`{ name: 'age', type: PropertyType.number, displayName: '年龄' }`
],
relations: [
{ name: 'profile', kind: RelationKind.ONE_TO_ONE, mappedEntity: 'Profile', mappedProperty: 'user' },
`{ name: 'posts', kind: RelationKind.ONE_TO_MANY, mappedEntity: 'Post', mappedProperty: 'author' }`
]
})
class User extends EntityBase {}

Properties​

abstract?​

optional abstract?: boolean;

Defined in: packages/rxdb/src/entity/entity-options.interface.ts:122

是否为抽象实体 抽象类是不能被实例化的


computedProperties?​

optional computedProperties?: EntityPropertyMetadataOptions[];

Defined in: packages/rxdb/src/entity/entity-options.interface.ts:153

计算属性 动态计算的只读属性,不存储在数据库中

使用条件​

  • 必须在 features 中启用对应功能(如 tree:{ hasChildren: true })
  • 仅在实体上可读,不可修改

计算来源​

  1. 数据库查询:例如树结构的 hasChildren 属性
  2. 类方法:通过 getter 方法计算,如 fullName = firstName + lastName
  3. 复杂逻辑:基于业务规则动态生成(待实现)

Example​

// 树结构中的计算属性
computedProperties: [
`{ name: 'hasChildren', type: PropertyType.boolean }`
]

注意: 当前仅在树结构(tree feature)中使用


displayName?​

optional displayName?: string;

Defined in: packages/rxdb/src/entity/entity-options.interface.ts:86

显示名称

Example​

"用户", "订单项"

extends?​

optional extends?: string[];

Defined in: packages/rxdb/src/entity/entity-options.interface.ts:92

继承的实体名称列表 表示当前实体继承自哪些实体


features?​

optional features?: EntityMetadataFeatures;

Defined in: packages/rxdb/src/entity/entity-options.interface.ts:185

功能特性


foreignKeys?​

optional foreignKeys?: EntityForeignKeyMetadataOptions[];

Defined in: packages/rxdb/src/entity/entity-options.interface.ts:180

实体级多列外键约束。

单列关系仍使用 relations;只有需要把多列作为一个整体校验时才使用本配置。


indexes?​

optional indexes?: EntityIndexMetadataOptions[];

Defined in: packages/rxdb/src/entity/entity-options.interface.ts:173

实体的索引配置 自己定义的索引,不包括继承的


log?​

optional log?: boolean;

Defined in: packages/rxdb/src/entity/entity-options.interface.ts:111

是否开启日志

Default​

true

name​

name: Capitalize<string>;

Defined in: packages/rxdb/src/entity/entity-options.interface.ts:73

名称


namespace?​

optional namespace?: Lowercase<string>;

Defined in: packages/rxdb/src/entity/entity-options.interface.ts:68

命名空间 只能包含小写的英文字母 在 postgres 里会变成 schema 在 sqlite 会变成 table 的前缀

Example​

"app", "system"

Default​

"public"

properties?​

optional properties?: EntityPropertyMetadataOptions[];

Defined in: packages/rxdb/src/entity/entity-options.interface.ts:128

实体的属性表 自己定义的属性,不包括继承的


relations?​

optional relations?: EntityRelationMetadataOptions[];

Defined in: packages/rxdb/src/entity/entity-options.interface.ts:167

实体的关系配置 自己定义的关系,不包括继承的

定义实体与其他实体之间的关联关系,支持四种关系类型:

  • ONE_TO_ONE:一对一关系,如用户和用户资料
  • ONE_TO_MANY:一对多关系,如用户和用户的多篇文章
  • MANY_TO_ONE:多对一关系,如多篇文章和一个作者
  • MANY_TO_MANY:多对多关系,如学生和课程

系统会根据关系配置自动处理外键、查询和关联操作


repository?​

optional repository?: RxDBRepositoryName;

Defined in: packages/rxdb/src/entity/entity-options.interface.ts:105

自定义 repository

Remarks​

取值来自门面轴注册表 RxDBRepositoryName:核心的 Repository 与插件经 declare module 注册的门面(如 TreeRepository / GraphRepository) 在补全里同为一等公民。 名字没经 RxDB.repository() 登记过时,EntityManager.init() 仍会抛错拦下。

Default​

"Repository"

sync?​

optional sync?: SyncOptions;

Defined in: packages/rxdb/src/entity/entity-options.interface.ts:116

实体同步配置


tableName?​

optional tableName?: string;

Defined in: packages/rxdb/src/entity/entity-options.interface.ts:80

表名称 数据库中的表名称 没有填写就是 name 一样