资讯动态

Midway 集成阿里云 TableStore 实战指南:安装、多客户端配置与数据读写

发布时间:2026/10/8 8:13:05 来源:尧图企业网站定制
后端微服务云原生【免费下载链接】midway A Node.js Serverless Framework for front-end/full-stack developers. Build the application for next decade. Works on AWS, Alibaba Cloud, Tencent Cloud and traditional VM/Container. Super easy integrate with React and Vue. 项目地址https://gitcode.com/gh_mirrors/mi/midway点击查看免费下载本文介绍如何基于 Midway 框架标准项目、Serverless 与一体化项目均可接入阿里云 TableStore表格存储涵盖组件安装、客户端配置、依赖注入与工厂多实例管理以及 getRow、putRow、条件过滤、事务与搜索索引等数据操作。读完本文你将掌握在 Midway 应用中通过TableStoreService快速读写表格数据、通过TableStoreServiceFactory管理多实例并了解底层代理实现与可复用的示例代码。组件能力概览Midway 通过midwayjs/tablestore组件将阿里云 TableStore原 OTS官方 Node.js SDK 无缝接入依赖注入体系可用场景如下描述可用于标准项目✅可用于 Serverless✅可用于一体化✅包含独立主框架❌包含独立日志❌该组件不包含独立主框架与独立日志能力仅作为数据访问组件运行在 Midway 主框架之上。从源码看组件核心由 configuration.ts、manager.ts、proxy.ts 与 interface.ts 构成后文将逐一展开。安装依赖在项目根目录执行$ npm i midwayjs/tablestore4 --save或者在package.json中增加依赖后重新安装{ dependencies: { midwayjs/tablestore: ^4.0.0 } }midwayjs/tablestore底层依赖阿里云官方tablestoreSDK安装时会一并拉取。引入组件在src/configuration.ts中导入组件import { Configuration } from midwayjs/core; import * as tablestore from midwayjs/tablestore; import { join } from path; Configuration({ imports: [ tablestore, // 导入 tablestore 组件 ], importConfigs: [ join(__dirname, config), ], }) export class MainConfiguration { }组件内部通过 configuration.ts 以Configuration({ namespace: tableStore })注册命名空间并在onReady阶段异步获取TableStoreServiceFactory确保应用启动时客户端已被创建、后续注入即可直接使用。配置组件使用tableStore顶层配置项支持单客户端与多客户端两种形式。单客户端配置// src/config/config.default.ts export default { tableStore: { client: { accessKeyId: your access key id, secretAccessKey: your access key secret, stsToken: your stsToken, // 使用 STS 授权时需要填写 endpoint: your endpoint, instancename: your instance name, }, }, };多客户端配置// src/config/config.default.ts export default { tableStore: { clients: { db1: { accessKeyId: your access key id, secretAccessKey: your access key secret, stsToken: your stsToken, endpoint: your endpoint, instancename: your instance name, }, db2: { accessKeyId: your access key id, secretAccessKey: your access key secret, stsToken: your stsToken, endpoint: your endpoint, instancename: your instance name, }, }, }, };配置参数说明根据 interface.ts 中TableStoreConfig的定义客户端支持以下参数参数说明默认值accessKeyId阿里云访问密钥 ID必填无secretAccessKey/accessKeySecret访问密钥 Secret两者等价无stsToken/securityToken使用 STS 临时授权时填写两者等价无endpointTableStore 实例访问地址必填无instancename实例名称必填无logger自定义日志对象无httpOptions.timeoutHTTP 请求超时时间毫秒无httpOptions.maxSockets最大 socket 连接数300maxRetries请求失败最大重试次数无computeChecksums是否计算校验和true更多底层参数可查阅阿里云官方 TableStore Node.js SDK 文档midwayjs/tablestore直接以这些配置构造官方TableStore.Client。使用 TableStore 服务注入 TableStoreService在任意被依赖注入管理的类中注入即可使用import { Provide, Controller, Inject, Get } from midwayjs/core; import { TableStoreService } from midwayjs/tablestore; Provide() export class UserService { Inject() tableStoreService: TableStoreService; async invoke() { await this.tableStoreService.putRow(params); } }TableStoreService是单例ScopeEnum.Singleton内部持有默认客户端单客户端配置下的default实例并把官方TableStore.Client的全部方法putRow、getRow、getRange、batchGetRow等通过delegateTargetPrototypeMethod代理到自身见 manager.ts。若默认实例不存在初始化时会抛出TableStore default instance not found.错误。通过工厂获取多客户端实例多客户端场景下使用TableStoreServiceFactory按名称获取不同实例import { TableStoreServiceFactory } from midwayjs/tablestore; import { join } from path; Provide() export class UserService { Inject() tableStoreServiceFactory: TableStoreServiceFactory; async save() { const db1 await this.tableStoreServiceFactory.get(db1); const db2 await this.tableStoreServiceFactory.get(db2); // ... } }TableStoreServiceFactory继承自 Midway 的ServiceFactory在init阶段根据tableStore配置并发创建全部客户端见 manager.ts命名空间名为tableStore。单客户端配置时实例名为default多客户端配置时实例名为clients下的 key如db1、db2。完整示例getRow 与条件过滤以getRow为例midwayjs/tablestore已代理接管官方 SDK 导出的类型如Long、CompositeCondition、SingleColumnCondition、LogicalOperator、ComparatorType等可直接使用import { join } from path; import { TableStoreService, Long, CompositeCondition, SingleColumnCondition, LogicalOperator, ComparatorType, } from midwayjs/tablestore; Provide() export class UserService { Inject() tableStoreService: TableStoreService; async getInfo() { const data await this.tableStoreService.getRow({ tableName: sampleTable, primaryKey: [{ gid: Long.fromNumber(20013) }, { uid: Long.fromNumber(20013) }], maxVersions: 1, // 读取最新版本默认为 1 columnsToGet: [col1, col2], // 只读取指定列 columnFilter: condition, }); // TODO } }参照仓库示例 getRow.ts可以组合条件过滤器实现当col1 表格存储且col5 123456789时返回该行的读取语义const condition new CompositeCondition(LogicalOperator.AND); condition.addSubCondition( new SingleColumnCondition(col1, 表格存储, ComparatorType.EQUAL) ); condition.addSubCondition( new SingleColumnCondition(col5, Long.fromNumber(123456789), ComparatorType.EQUAL) );深入了解条件构造器与工具函数midwayjs/tablestore在 proxy.ts 中对官方 SDK 的条件类做了包装保持原有 API 形态CompositeCondition(combinator)组合条件combinator取LogicalOperator.NOT / AND / OR通过addSubCondition添加子条件Condition(rowExistenceExpectation, columnCondition?)行条件RowExistenceExpectation取IGNORE / EXPECT_EXIST / EXPECT_NOT_EXISTSingleColumnCondition(columnName, columnValue, comparator, passIfMissing true, latestVersionOnly true)单列条件ComparatorType提供EQUAL / NOT_EQUAL / GREATER_THAN / GREATER_EQUAL / LESS_THAN / LESS_EQUAL六种比较符passIfMissing控制目标列不存在时条件是否通过默认通过Long与官方TableStore.Long一致支持fromNumber/fromString构造 Int64 主键值。此外util.ts 提供了两个结果格式化工具formatRow(row, filterColumn)将 SDK 返回的主键与属性列扁平化为{ 列名: 值 }对象可按列名过滤值统一转为字符串formatRows(result, filterColumn)批量格式化getRange等接口返回的行列表并额外提取next_start_primary_key作为分页游标next。测试与运行验证组件自带测试与可运行示例可先于真实环境验证接入逻辑测试用例 index.test.ts 通过createLightApp加载轻量应用断言TableStoreService、TableStoreServiceFactory可正常获取且工厂命名为tableStore并验证条件构造器与默认实例缺失时抛错的行为示例代码位于 test/sample 目录覆盖putRow、getRow、getRange、updateRow、deleteRow、batchGetRow、batchWriteRow、createTable、createSearchIndex、search、transaction、autoIncrement等 27 个场景可作为各类数据操作的参考模板测试夹具 base-app 展示了最小配置形态tableStore.client下仅配置必填的accessKeyId即可完成客户端创建。结语通过midwayjs/tablestore组件Midway 应用可以像使用其他数据组件一样以声明式配置与依赖注入方式使用阿里云 TableStore单客户端直接注入TableStoreService多客户端通过TableStoreServiceFactory按名获取官方 SDK 的类型与条件构造器全部被代理接管配合仓库中的 27 个示例脚本可快速覆盖建表、读写、范围查询、批量操作、搜索索引与局部事务等完整场景。赞分享后端微服务云原生【免费下载链接】midway A Node.js Serverless Framework for front-end/full-stack developers. Build the application for next decade. Works on AWS, Alibaba Cloud, Tencent Cloud and traditional VM/Container. Super easy integrate with React and Vue. 项目地址https://gitcode.com/gh_mirrors/mi/midway点击查看免费下载相关推荐Midway 集成阿里云表格存储 Tablestore 组件实战midwayjs/tablestore 的配置、CRUD、搜索索引与演进全解Midway 集成阿里云表格存储 Tablestore 组件实战midwayjs/tablestore 的配置、CRUD、搜索索引与演进全解 导读 mid后端微服务云原生SeaTunnel Tablestore Sink 连接器实战指南将数据高效写入阿里云 TablestoreSeaTunnel Tablestore Sink 连接器实战指南将数据高效写入阿里云 Tablestore 本指南围绕 SeaTunnel 的 Tables数据集成ETL大数据批处理流处理变更数据捕获first-contributions 荷兰语版入门指南一次完整的 fork → clone → edit → pull request 开源贡献实战first contributions 荷兰语版入门指南一次完整的 fork → clone → edit → pull request 开源贡献实战 导读后端微服务云原生上一篇CoolProp 热力学物性计算一站式开源物性数据库5分钟告别查表下一篇如何解密 RPG Maker MV 资源文件三条路线快速解锁 rpgmvp 图片与 rpgmvm 音频创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

读完文章,也想定制专属网站?

尧图设计师 24 小时内与您沟通定制方案

免费获取报价 →
↑