当前位置: 首页 > news >正文

Vona ORM分表全攻略

分表

针对高并发、数据量大的场景,通常会考虑采用分表机制进行优化。下面以 Model User/Order 为例,通过查询用户的订单列表,来演示分表的使用方法

分表规则

比如需要对订单表进行分表操作。可以根据实际业务需求设计分表规则,在这里,根据用户Id取模动态生成表名。比如,拆分为16张表,用户Id129,对应的表名如下:

const tableName = `Order_${129 % 16}`;  // Order_1

准备Models

先准备两个 Models:User/Order

  1. Model Order
@Model({entity: EntityOrder,
})
class ModelOrder{}
  1. Model User
@Model({entity: EntityUser,relations: {orders: $relation.hasMany(() => ModelOrder, 'userId'),},
})
class ModelUser {}

查询数据

1. 直接查询订单列表

class ServiceOrder {async selectOrdersDirectly() {const userId = 129;const orders = await this.scope.model.order.select({where: {userId,},});}
}  

到目前为止,使用默认表名查询userId=129的订单列表

2. 基于关系查询订单列表

class ServiceOrder {async selectOrdersByRelation() {const userId = 129;const userAndOrders = await this.scope.model.user.get({id: userId,}, {include: {orders: true,},});}
}  

到目前为止,使用默认表名查询userId=129的用户信息,使用默认表名查询该用户的订单列表

使用分表:动态方式

可以在代码中动态使用分表:

class ServiceOrder {async selectOrdersDirectly() {const userId = 129;
+   const tableName = `Order_${userId % 16}`;
+   const modelOrder = this.scope.model.order.newInstance(undefined, tableName as any);const orders = await modelOrder.select({where: {userId,},});}
}  
  • newInstance: 传入要使用的表名,返回新的 Model 实例

到目前为止,使用分表查询userId=129的订单列表

使用分表:Relation动态选项

可以在 relation 选项中动态指定表名:

class ServiceOrder {async selectOrdersByRelation() {const userId = 129;
+   const tableName = `Order_${userId % 16}`;const userAndOrders = await this.scope.model.user.get({id: userId,}, {include: {orders: {
+         meta: {
+           table: tableName as any,
+         },},},});}
}  
  • meta.table: 指定 relation orders要使用的表名

到目前为止,使用默认表名查询userId=129的用户信息,使用分表查询该用户的订单列表

使用分表:Model配置

也可以直接在 Model 中配置分表规则,从而简化查询代码

  1. Model Order
@Model({entity: EntityOrder,
+ table(_ctx: VonaContext, where: EntityOrder | undefined, defaultTable: keyof ITableRecord) {
+   const userId = where?.userId;
+   if (!userId) return defaultTable;
+   return `Order_${Number(userId) % 16}`;
+ },
})
class ModelOrder{}
  • table: 指定函数,实现分表规则
  1. 查询数据

现在,又可以使用常规的方式查询用户的订单列表

class ServiceOrder {async selectOrdersDirectly() {const userId = 129;const orders = await this.scope.model.order.select({where: {userId,},});}
}  
class ServiceOrder {async selectOrdersByRelation() {const userId = 129;const userAndOrders = await this.scope.model.user.get({id: userId,}, {include: {orders: true,},});}
}  

使用分表:App Config配置

也可以在 App config 中配置 Model options:

src/backend/config/config/config.ts

// onions
config.onions = {model: {'test-vona:order': {table(_ctx: VonaContext, where: EntityOrder | undefined, defaultTable: keyof ITableRecord) {const userId = where?.userId;if (!userId) return defaultTable;return `Order_${Number(userId) % 16}`;},},},
};

于是,也可以使用常规的方式查询用户的订单列表

使用分表:Relation静态选项

也可以在定义 Relation 时指定静态选项:

@Model({entity: EntityUser,relations: {orders: $relation.hasMany(() => ModelOrder, 'userId', {
+     meta: {
+       table(_ctx: VonaContext, where: EntityOrder | undefined, defaultTable: keyof ITableRecord) {
+         const userId = where?.userId;
+         if (!userId) return defaultTable;
+         return `Order_${Number(userId) % 16}`;
+       },
+     },}),},
})
class ModelUser {}

同样,也可以使用常规的方式查询用户的订单列表

Vona ORM已开源:github.com/vonajs/vona

http://www.hskmm.com/?act=detail&tid=16995

相关文章:

  • 如何在预算与风险之间做选择 iOS 混淆(源码混淆 vs IPA 混淆)的成本-收益分析与实战决策框架
  • 【兰州大学主办|EI稳定检索】第二届信息光学与光电技术国际学术会议(CIOT 2025)
  • 深入解析:设计模式-状态模式详解
  • 【IEEE出版】第五届网络通信与信息安全国际学术会议(ICNCIS 2025)
  • 第16章 Day19 Charles安装和使用---微信小程序逆向
  • DBLINK的创建和使用(总结)
  • Could not resolve host: mirrorlist.centos.org
  • axi 4k边界检测
  • GOSIM 开源出海工作坊:给开源创业者的忠告
  • 华为,让金融智能体月映千江 - 指南
  • 轻量级架构决策记录工具 - ADR Tools
  • 一文搞懂Flex弹性布局空间分配规则
  • AT_agc012_c [AGC012C] Tautonym Puzzle 题目分析
  • 详细介绍:回调函数与错误处理
  • Django系列(七)HttpRequest(请求)和HttpResponse(响应)对象
  • 工业主板:智能制造与严苛环境的坚实基石
  • 课上测试:C编程工具测试(AI)
  • 标题。
  • 虚拟机下的麒麟V10SP1与SP2进行iSCSI连接——基于MobaXterm
  • 中断的基本概念
  • AT_arc173_e [ARC173E] Rearrange and Adjacent XOR
  • 修复gradle8使用Transform第一个构建中断第二次构建失败的问题:java.io.IOException: Unable to delete directory xxxx\build
  • .NET操作Word/WPS打造专业文档 - 页面设置与打印控制完全指南
  • NORDIC蓝牙6.0新品NRF54L15多协议超低功耗高性能BLE芯片 - 动能世纪
  • 记录:git、.${index}. 滚动条
  • 使用springboot开发一个宿舍管理系统练习项目 - 实践
  • 元组
  • CF1542
  • Manim实现涟漪扩散特效
  • CRMEB标准版PHP移动订单功能深度解析:多端同步方案