实体索引
索引用于提升查询性能或实现联合唯一约束。Aiao RxDB 提供两种方式定义索引:
- 单字段唯一:在属性上设置
unique: true - 组合索引/组合唯一:在实体的
indexes中声明
组合索引
通过 indexes 配置属性组合索引(可唯一):
import { Entity, EntityBase, PropertyType } from '@aiao/rxdb';
@Entity({
name: 'Order',
properties: [
{ name: 'orderNo', type: PropertyType.string },
{ name: 'storeId', type: PropertyType.uuid },
{ name: 'buyerId', type: PropertyType.uuid }
],
indexes: [
// 非唯一组合索引
{ name: 'idx_order_store_buyer', properties: ['storeId', 'buyerId'] },
// 唯一组合索引(实现店内订单号唯一)
{ name: 'uk_store_orderNo', unique: true, properties: ['storeId', 'orderNo'] }
]
})
export class Order extends EntityBase {}
说明:
name: 索引名称properties: 参与索引的字段名数组(顺序生效)unique?: 是否唯一(默认 false)
单字段唯一场景请直接在属性定义中使用 unique: true;复杂约束(例如跨表)请在业务层实现。