跳到主要内容

EntityMetadataOptions

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

实体定义元数据选项接口 用于配置 @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' },
{ 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/metadata-options.interface.ts:724

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


computedProperties?

optional computedProperties: EntityPropertyMetadataOptions[];

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

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

使用条件

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

计算来源

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

Example

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

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


displayName?

optional displayName: string;

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

显示名称

Example

"用户", "订单项"

extends?

optional extends: string[];

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

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


features?

optional features: EntityMetadataFeatures;

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

功能特性


indexes?

optional indexes: EntityIndexMetadataOptions[];

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

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


log?

optional log: boolean;

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

是否开启日志

Default

true

name

name: Capitalize<string>;

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

名称


namespace?

optional namespace: Lowercase<string>;

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

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

Example

"app", "system"

Default

"public"

properties?

optional properties: EntityPropertyMetadataOptions[];

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

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


relations?

optional relations: EntityRelationMetadataOptions[];

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

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

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

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

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


repository?

optional repository: string;

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

自定义 repository

Default

"Repository"

sync?

optional sync: SyncOptions;

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

实体同步配置


tableName?

optional tableName: string;

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

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