A Fetch API-compatible PlanetScale database driver
The @planetscale/database package is a JavaScript database driver specifically built for serverless and edge computing environments. Unlike traditional MySQL drivers that rely on TCP connections, it uses HTTP and the Fetch API to communicate with PlanetScale's MySQL-compatible databases (powered by Vitess). This architecture solves the fundamental problem of connection pooling and cold starts in serverless functions.
PlanetScale is a horizontally scalable database platform built on Vitess, the same technology that powers YouTube's database infrastructure. The driver enables developers to query MySQL databases from environments where TCP sockets are unavailable or impractical, such as Cloudflare Workers, Vercel Edge Functions, Deno Deploy, and Netlify Edge Functions. It handles authentication via connection strings and returns query results as JavaScript objects.
With over 179,000 weekly downloads, the package has become the standard way to access PlanetScale databases from JavaScript applications. It's particularly popular among developers building globally distributed applications who need low-latency database access from edge locations. The driver works standalone for raw SQL queries or integrates with ORMs like Prisma and Drizzle for type-safe database operations.
The package is maintained by PlanetScale and released under the Apache-2.0 license. It requires Node.js 18+ or any runtime supporting the Fetch API standard, making it highly portable across modern JavaScript environments.
import { connect } from '@planetscale/database';
// Initialize connection with PlanetScale credentials
const config = {
host: process.env.DATABASE_HOST,
username: process.env.DATABASE_USERNAME,
password: process.env.DATABASE_PASSWORD
};
const conn = connect(config);
// Execute a simple query
const results = await conn.execute('SELECT id, email, created_at FROM users WHERE status = ? LIMIT 10', ['active']);
console.log(results.rows);
// Insert with parameters to prevent SQL injection
const insertResult = await conn.execute(
'INSERT INTO users (email, name, status) VALUES (?, ?, ?)',
['user@example.com', 'John Doe', 'active']
);
console.log('Inserted ID:', insertResult.insertId);
// Use transactions for atomic operations
const txn = await conn.transaction();
try {
await txn.execute('UPDATE accounts SET balance = balance - ? WHERE user_id = ?', [100, 42]);
await txn.execute('UPDATE accounts SET balance = balance + ? WHERE user_id = ?', [100, 73]);
await txn.commit();
} catch (error) {
await txn.rollback();
throw error;
}
// Alternatively, use connection URL for simpler config
const simpleConn = connect({ url: process.env.DATABASE_URL });
const { rows } = await simpleConn.execute('SELECT COUNT(*) as total FROM orders');
console.log('Total orders:', rows[0].total);Edge API Routes: Build API endpoints in Vercel Edge Functions or Cloudflare Workers that query MySQL databases with minimal latency. The HTTP-based connection model avoids TCP socket limitations and leverages PlanetScale's global routing to connect users to the nearest database replica.
Serverless Backend Functions: Use in AWS Lambda, Google Cloud Functions, or Azure Functions to execute database queries without managing connection pools. The stateless nature of HTTP requests aligns perfectly with the ephemeral lifecycle of serverless functions, eliminating connection exhaustion issues.
Jamstack Applications: Power Next.js, Remix, or Astro applications with dynamic data fetching at the edge. Combine with PlanetScale's branching workflow to test schema changes in isolated database branches before deploying to production.
Real-time Data APIs: Create lightweight microservices that expose database queries over HTTP endpoints. The driver's minimal overhead makes it suitable for high-frequency read operations in dashboards, admin panels, or public APIs.
Multi-region Applications: Deploy the same codebase across multiple geographic regions while automatically routing database requests through PlanetScale's distributed infrastructure. Particularly useful for compliance requirements where data residency matters.
Drizzle ORM package for SQL databases
node-postgres for serverless environments from neon.com
Prisma is an open-source database toolkit. It includes a JavaScript/TypeScript ORM for Node.js, migrations and a modern GUI to view and edit the data in your database. You can use Prisma in new projects or add it to an existing one.
Connect to Vercel Postgres databases on the Edge
npm install @planetscale/databasepnpm add @planetscale/databasebun add @planetscale/database