Agent skill
tidbx-javascript-mysqljs
Install this agent skill to your Project
npx add-skill https://github.com/pingcap/agent-rules/tree/main/skills/tidbx-javascript-mysqljs
SKILL.md
TiDB + JavaScript (mysqljs/mysql)
Use this skill to connect to TiDB from Node.js with the mysql package (aka mysqljs/mysql), configure TLS correctly for TiDB Cloud, and provide small runnable snippets you can copy into a project.
Important: mysql vs mysql2
These two drivers are easy to mix up:
- This skill is for mysqljs/mysql (
npm i mysql) which is primarily callback-based. - If you want the promise-native driver mysql2 (
npm i mysql2/mysql2/promise), jump to thetidbx-javascript-mysql2skill instead.
Recommendation: prefer an ORM for app code
For most application code (models, migrations, types), prefer an ORM/query builder:
- Prisma ORM: use
tidbx-prisma - Kysely query builder: use
tidbx-kysely
When to use this skill
- Connect to TiDB from JavaScript/TypeScript (Node.js runtime) via
mysql(mysqljs/mysql). - Need TLS guidance (TiDB Cloud public endpoint) or CA certificate setup (TiDB Cloud Dedicated).
- Want a minimal "connect -> SELECT VERSION() -> CRUD" template using mysqljs/mysql.
Code generation rules (Node.js)
- Never hardcode credentials; use
DATABASE_URLorTIDB_*env vars. - Use parameterized queries with
?placeholders; never string-concatenate untrusted input. - Prefer a pool for apps/services (
createPool) andpool.end()on shutdown. - mysqljs/mysql is callback-first; wrap callback APIs with
util.promisifyor smallnew Promise(...)helpers.
Install
npm i mysql
If you want .env support:
npm i dotenv
Minimal snippets
Option A: connect with DATABASE_URL (recommended)
import mysql from 'mysql'
import util from 'node:util'
const conn = mysql.createConnection(process.env.DATABASE_URL)
const query = util.promisify(conn.query).bind(conn)
const rows = await query('SELECT VERSION() AS v')
console.log(rows[0].v)
await util.promisify(conn.end).bind(conn)()
Option B: connect with connection options (TLS / CA)
Use this when you want explicit TLS config (common for TiDB Cloud Dedicated with a downloaded CA).
import mysql from 'mysql'
import fs from 'node:fs'
const conn = mysql.createConnection({
host: process.env.TIDB_HOST,
port: Number(process.env.TIDB_PORT ?? 4000),
user: process.env.TIDB_USER,
password: process.env.TIDB_PASSWORD,
database: process.env.TIDB_DATABASE ?? 'test',
ssl: process.env.TIDB_ENABLE_SSL === 'true'
? {
minVersion: 'TLSv1.2',
ca: process.env.TIDB_CA_PATH ? fs.readFileSync(process.env.TIDB_CA_PATH) : undefined,
}
: undefined,
})
Suggested env vars:
TIDB_HOST=...
TIDB_PORT=4000
TIDB_USER=...
TIDB_PASSWORD=...
TIDB_DATABASE=test
TIDB_ENABLE_SSL=true
# Optional (commonly used for TiDB Cloud Dedicated):
TIDB_CA_PATH=/absolute/path/to/ca.pem
Transactions (mysqljs/mysql)
import util from 'node:util'
const begin = util.promisify(conn.beginTransaction).bind(conn)
const commit = util.promisify(conn.commit).bind(conn)
const rollback = util.promisify(conn.rollback).bind(conn)
const query = util.promisify(conn.query).bind(conn)
try {
await begin()
await query('UPDATE players SET coins = coins + ? WHERE id = ?', [50, id])
await commit()
} catch (e) {
await rollback()
throw e
}
Templates & scripts in this skill
scripts/validate_connection.mjs-- readsDATABASE_URLorTIDB_*, connects, printsSELECT VERSION(), then exits.templates/quickstart.mjs-- minimal end-to-end: connect -> create table -> insert -> query -> update -> delete.
Recommended Agent Skills
Expand your agent's capabilities with these related and highly-rated skills.
tidbx-kysely
Set up Kysely with TiDB Cloud (TiDB X), including @tidbcloud/kysely over the TiDB Cloud serverless HTTP driver for serverless or edge environments, plus standard TCP usage. Use for Kysely + TiDB Cloud connection setup, demo snippets, and environment-specific guidance.
pytidb
PyTiDB (pytidb) setup and usage for TiDB from Python. Covers connecting, table modeling (TableModel), CRUD, raw SQL, transactions, vector/full-text/hybrid search, auto-embedding, custom embedding functions, and reference templates/snippets (vector/hybrid/image) plus agent-oriented examples (RAG/memory/text2sql).
tidb-query-tuning
Diagnose and optimize slow TiDB queries using optimizer hints, session variables, join strategy selection, subquery optimization, and index tuning. Use when a query is slow, produces a bad plan, or needs performance guidance on TiDB.
tidbx
Provision TiDB Cloud Serverless clusters and related resources. Use when creating, deleting, or listing clusters/branches, or managing SQL users via the console.
tidb-cloud-zero
Provision a disposable MySQL-compatible database instantly with no auth required. Includes a claim URL to convert Zero instances into regular TiDB Starter instances when the user needs persistence or more quota.
tidbx-javascript-mysql2
Connect to TiDB from JavaScript/Node.js using the mysql2 driver (mysql2/promise). Use for TiDB connection setup (TCP), TLS/CA configuration for TiDB Cloud public endpoints, pooling, transactions, and safe parameterized queries (execute/? placeholders) plus small runnable connection/CRUD templates.
Didn't find tool you were looking for?