使用 Node.js 连接 TiDB Cloud Lake
官方 Node.js 驱动为现代 JavaScript 应用提供 TypeScript 支持和基于 Promise 的 API。
安装
npm install tidbcloudlake-driver
Connection String:有关 DSN 格式和示例,请参见驱动概览。
主要特性
- ✅ TypeScript Support:包含完整的 TypeScript 定义
- ✅ Promise-based API:支持现代 async/await
- ✅ Streaming Results:高效处理大型结果集
- ✅ Connection Pooling:内置连接管理
数据类型映射
基本用法
const { Client } = require('tidbcloudlake-driver');
// Connect to TiDB Cloud Lake
const client = new Client('<your-dsn>');
const conn = await client.getConn();
// DDL: Create table
await conn.exec(`CREATE TABLE users (
id INT,
name STRING,
email STRING
)`);
// Write: Insert data
await conn.exec("INSERT INTO users VALUES (?, ?, ?)", [1, "Alice", "alice@example.com"]);
// Query: Select data
const rows = await conn.queryIter("SELECT id, name, email FROM users WHERE id = ?", [1]);
for await (const row of rows) {
console.log(row.values());
}
conn.close();
资源
- NPM Package:tidbcloudlake-driver
- GitHub Repository:tidbcloudlake-driver
- TypeScript Definitions:包中已包含