
@tidbcloud/serverless
TiDB Cloud Serverless Driver
About
TiDB Cloud Serverless Driver
Live mirror of the GitHub README. Updated whenever the repo's default branch changes.
TiDB Cloud Serverless Driver for JavaScript
This driver is for serverless and edge compute platforms that require HTTP external connections, such as Vercel Edge Functions or Cloudflare Workers.
There are three ways to use the driver:
- Stateless connection (default): each query is independent, ideal for edge environments with short-lived, frequently created connections.
- Stateful connection (experimental): use it when you require session.
- Transaction (experimental): use it when you require interactive transaction.
Usage
Install
You can install the driver with npm:
npm install @tidbcloud/serverless
Stateless Connection
To query from TiDB Serverless, you need to create a connection first. Then you can use the connection to execute raw SQL queries.
import { connect } from '@tidbcloud/serverless'
const conn = connect({url: 'mysql://username:password@host/database'})
const results = await conn.execute('select * from test where id = ?',[1])
Stateful Connection (experimental)
If you want to keep session state across multiple queries, create a stateful connection. Remember to call close() to release the connection, or you may reach the connection limits.
Note:
Connections idle for 10 minutes will be closed automatically. The Stateful connection is not concurrent-safe. You are not allowed to run SQLs parallel in the same stateful connection.
import { connect } from '@tidbcloud/serverless'
const conn = connect({url: 'mysql://username:password@host/database'})
const stateful = await conn.persist()
try {
const r1 = await stateful.execute('use db2')
const r2 = await stateful.execute('select * from test where id = ?', [2])
} finally {
await stateful.close()
}
Transaction (experimental)
You can also perform interactive transactions with the serverless driver. For example:
Note:
Transactions idle for 10 minutes will be rolled back automatically if it has not been committed or rolled back. The transaction is not concurrent-safe. You are not allowed to run SQLs parallel in the same transaction.
import { connect } from '@tidbcloud/serverless'
const conn = connect({url: 'mysql://username:password@host/database'})
const tx = await conn.begin()
try {
await tx.execute('insert into test values (1)')
await tx.execute('select * from test')
await tx.commit()
}catch (err) {
await tx.rollback()
throw err
}
Edge example
The serverless driver is suitable for the edge environments. See how to use it with Vercel Edge Functions:
import { NextResponse } from 'next/server';
import type { NextRequest } from 'next/server';
import { connect } from '@tidbcloud/serverless'
export const runtime = 'edge'
export async function GET(request: NextRequest) {
const conn = connect({url: process.env.DATABASE_URL})
const result = await conn.execute('show tables')
return NextResponse.json({result});
}
See TiDB Cloud Serverless Driver documentation to learn more.
MCP for AI Agent
Do not use it in production.
{
"mcpServers": {
"tidbcloud-serverless-mcp": {
"command": "npx",
"args": ["-y", "@tidbcloud/serverless-mcp"],
"env": {
"TIDB_DATABASE_URL": "mysql://<user>:<password>@<host>/<database>"
}
}
}
}
Configuration
See Configure TiDB Cloud Serverless Driver.
License
Apache 2.0, see LICENSE.
Quick facts
npm install @tidbcloud/serverlessHow Sourcemap Explorer detects @tidbcloud/serverless
We catch @tidbcloud/serverless from two complementary signals: bundled source paths and the embedded package.json. Modern bundlers (webpack, Vite, esbuild, Rollup, Turbopack) preserve the original node_modules/@tidbcloud/serverless/ paths inside the JavaScript sourcemap's sources[] array — that's the canonical signal. When the matching package.json is also captured in sourcesContent[], we read the exact version field — patch number included. No regex guessing, no version inference.
- 1
Confirm the site exposes sourcemaps
In DevTools Network, check the response headers of any application script for `SourceMap` or `X-SourceMap`. Failing that, fetch the script's last 4 KB and look for a `//# sourceMappingURL=` comment.
- 2
Find the package in the bundle
Open DevTools → Network → reload. Click any application script and look at its sourcemap. Inside, search `sources[]` for entries matching `node_modules/@tidbcloud/serverless/` — every match confirms the package is bundled. The matching `sourcesContent[i]` for `node_modules/@tidbcloud/serverless/package.json` gives you the exact installed version.
- 3
Read the version directly from package.json
Run `jq -r '. as $m | $m.sources | to_entries[] | select(.value | endswith("node_modules/@tidbcloud/serverless/package.json")) | $m.sourcesContent[.key] | fromjson | .version' bundle.js.map`. Sourcemap Explorer automates the same query in the popup.
Recent versions
FAQ
What is @tidbcloud/serverless used for?
TiDB Cloud Serverless Driver
How can I tell if a website is using @tidbcloud/serverless?
Open the page in Chrome with the Sourcemap Explorer extension installed and read the Stack tab. We catch `@tidbcloud/serverless` from two complementary signals: `node_modules/@tidbcloud/serverless/` paths inside the JavaScript sourcemap, and the embedded `package.json` we read for exact-version detection. Without the extension you can do the same lookup manually in DevTools — the steps are listed in the "How Sourcemap Explorer detects" section above.
What is the latest version of @tidbcloud/serverless?
0.3.0, as published on the npm registry. The "Recent versions" table on this page lists the most recent 8 releases with their release dates. Sourcemap Explorer reports the version actually bundled into a site, which can lag the latest release by months on real-world deployments.
Where can I read more?
Project homepage: https://github.com/tidbcloud/serverless-js#readme. Source code: https://github.com/tidbcloud/serverless-js. Published on npm: https://www.npmjs.com/package/@tidbcloud/serverless. Licensed as Apache-2.0.
Keep reading on Sourcemap Explorer
Practical guides
Detected by Sourcemap Explorer
When a bundle ships sourcemaps, we read the embedded package.json for @tidbcloud/serverless and report the precise version. Without sourcemaps, an import / require in the page's scripts is enough to flag it.