Vona ORM分表全攻略

分表

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

分表规则

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

typescript 复制代码
const tableName = `Order_${129 % 16}`;  // Order_1

准备Models

先准备两个 Models:User/Order

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

查询数据

1. 直接查询订单列表

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

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

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

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

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

使用分表:动态方式

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

diff 复制代码
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 选项中动态指定表名:

diff 复制代码
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
diff 复制代码
@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. 查询数据

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

typescript 复制代码
class ServiceOrder {
  async selectOrdersDirectly() {
    const userId = 129;
    const orders = await this.scope.model.order.select({
      where: {
        userId,
      },
    });
  }
}  
typescript 复制代码
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

typescript 复制代码
// 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 时指定静态选项:

diff 复制代码
@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

相关推荐
天下代码客19 小时前
使用electronc框架调用dll动态链接库流程和避坑
前端·javascript·vue.js·electron·node.js
weixin1997010801619 小时前
【性能提升300%】仿1688首页的Webpack优化全记录
前端·webpack·node.js
不倒翁玩偶21 小时前
npm : 无法将“npm”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径正确,然后再试一次。
前端·npm·node.js
ArkPppp1 天前
NestJS全栈实战笔记:优雅处理 Entity 与 DTO 的映射与字段过滤
javascript·nestjs
Dragon Wu1 天前
Electron Forge集成React Typescript完整步骤
前端·javascript·react.js·typescript·electron·reactjs
一心赚狗粮的宇叔1 天前
03.Node.js依赖包补充说明及React&Node.Js项目
前端·react.js·node.js
-嘟囔着拯救世界-1 天前
【2026 最新版】OpenAI 祭出王炸 GPT-5.3-Codex!Win11 + VSCode 部署保姆级教程
vscode·gpt·chatgpt·node.js·node·codex·gpt5
We་ct2 天前
LeetCode 56. 合并区间:区间重叠问题的核心解法与代码解析
前端·算法·leetcode·typescript
全栈前端老曹2 天前
【MongoDB】Node.js 集成 —— Mongoose ORM、Schema 设计、Model 操作
前端·javascript·数据库·mongodb·node.js·nosql·全栈
行者无疆_ty2 天前
什么是Node.js,跟OpenCode/OpenClaw有什么关系?
人工智能·node.js·openclaw