tidbx-kysely

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

TiDB Cloud + Kysely

TiDB Cloud + Kysely

Use this skill when a user wants to connect Kysely to TiDB Cloud (TiDB X). Default to standard TCP (Node server/runtime). Only use the TiDB Cloud serverless driver over HTTP for serverless or edge runtimes.
当用户需要将Kysely连接到TiDB Cloud(TiDB X)时使用本技能。默认采用标准TCP连接(适用于Node服务器/运行时)。仅在无服务器或边缘运行时场景下,使用基于HTTP的TiDB Cloud无服务器驱动。

Workflow

工作流程

  1. Confirm runtime and deployment target (Node server vs serverless/edge).
  2. Confirm cluster type. The serverless HTTP driver applies to Starter/Essential clusters.
  3. Collect connection info (prefer a
    mysql://
    URL in
    DATABASE_URL
    ).
  4. Choose the path:
    • Normal usage (default): TCP +
      mysql2
      pool + Kysely
      MysqlDialect
      .
    • Serverless/edge:
      @tidbcloud/kysely
      dialect over HTTP.
  5. Show only the matching snippet first. Include the other path only if the user asks. Use
    references/kysely-usage.md
    for full examples.
  1. 确认运行时和部署目标(Node服务器 vs 无服务器/边缘环境)。
  2. 确认集群类型。无服务器HTTP驱动仅适用于Starter/Essential集群。
  3. 收集连接信息(优先使用
    DATABASE_URL
    中的
    mysql://
    格式URL)。
  4. 选择对应方案:
    • 常规使用(默认):TCP +
      mysql2
      连接池 + Kysely
      MysqlDialect
    • 无服务器/边缘环境:通过HTTP使用
      @tidbcloud/kysely
      方言。
  5. 首先仅展示匹配场景的代码片段。仅当用户要求时,再提供另一种方案的内容。完整示例请参考
    references/kysely-usage.md

Normal usage (default)

常规使用(默认)

Use this for Node servers, long-lived runtimes, or when TCP is available. This is the primary path unless the user explicitly needs serverless/edge. Uses TCP with a
mysql2
pool.
ts
import { Kysely, MysqlDialect } from 'kysely'
import { createPool } from 'mysql2'

const pool = createPool({ uri: process.env.DATABASE_URL })
const db = new Kysely({ dialect: new MysqlDialect({ pool }) })
适用于Node服务器、长生命周期运行时或TCP连接可用的场景。除非用户明确需要无服务器/边缘环境方案,否则这是首选方案。使用TCP和
mysql2
连接池。
ts
import { Kysely, MysqlDialect } from 'kysely'
import { createPool } from 'mysql2'

const pool = createPool({ uri: process.env.DATABASE_URL })
const db = new Kysely({ dialect: new MysqlDialect({ pool }) })

Serverless/edge usage (HTTP)

无服务器/边缘环境使用(HTTP)

Use this only when the runtime cannot keep TCP connections (serverless/edge). Requires the TiDB Cloud serverless driver and Starter/Essential clusters. Use from backend services only (browser origins may be blocked by CORS). See
references/serverless-kysely-tutorial.md
for the full walkthrough.
ts
import { Kysely } from 'kysely'
import { TiDBCloudServerlessDialect } from '@tidbcloud/kysely'

const db = new Kysely({
  dialect: new TiDBCloudServerlessDialect({ url: process.env.DATABASE_URL }),
})
仅当运行时无法保持TCP连接时(无服务器/边缘环境)使用本方案。需要TiDB Cloud无服务器驱动,且仅适用于Starter/Essential集群。仅可从后端服务使用(浏览器端可能会被CORS策略阻止)。完整教程请参考
references/serverless-kysely-tutorial.md
ts
import { Kysely } from 'kysely'
import { TiDBCloudServerlessDialect } from '@tidbcloud/kysely'

const db = new Kysely({
  dialect: new TiDBCloudServerlessDialect({ url: process.env.DATABASE_URL }),
})

Notes

注意事项

  • Many users say "instance" to mean "cluster"; treat them as the same.
  • Keep instructions concise; move any long docs into references.
  • 很多用户会用“实例”指代“集群”,二者视为同一概念。
  • 保持说明简洁;冗长文档请移至参考资料中。