Repo of the Day
planetscale/database-js
Published: Mar 28, 2023
Open repository ↗A Fetch API-compatible PlanetScale database driver
Summary
planetscale/database-js is a TypeScript driver for PlanetScale's MySQL/Vitess databases, built around the Fetch API so it works in environments that only allow outbound HTTP. It targets serverless and edge runtimes like Cloudflare Workers and Vercel Edge Functions, where traditional TCP-based MySQL clients are not viable. The project is published under the Apache 2.0 license.
What it is useful for
- Talking to a PlanetScale Vitess/MySQL database from edge runtimes that block raw TCP, such as Cloudflare Workers or Vercel Edge Functions.
- Replacing a Node
mysql2-style client inside serverless functions, where TCP handshake and pool startup cost matter. - Applications that need a swappable HTTP transport, including
undicifor Node andfetch-h2for HTTP/2, or a custom proxy layer. - Code that wants a fresh, per-request connection built through a
Clientfactory instead of a long-lived pool.
How engineers can use it
The README documents this setup:
npm install @planetscale/database
import { connect } from '@planetscale/database'
const conn = connect({ url: process.env['DATABASE_URL'] })
const results = await conn.execute('select 1 from dual')
From there, engineers can:
- Build one
new Client(config).connection()per request handler or transaction to avoid sharing connection state. - Wrap multi-statement work in
conn.transaction(async tx => { ... }); the README states that any unhandled error will roll the transaction back. - Provide a
fetchin the config to useundici(the README's recommended option for Node) orfetch-h2to enable HTTP/2, callingdisconnectAll()from thefetch-h2context when finished. - Override value coercion with a
castfunction, for example returningBigIntforINT64andUINT64columns, either globally on the connection or per call. - Choose row layout per call with
{ as: 'object' }(default) or{ as: 'array' }.
Documented limitations to be aware of:
executedoes not interpolate parameters and will throw if called with an array or a named-parameter object. The README recommends building SQL with a separate escaper such assql-escaperrather than relying on the driver.- This driver targets MySQL/Vitess only; for PlanetScale Postgres, the README points users to the separate Neon serverless driver.