Bun provides a unified Promise-based API for PostgreSQL, MySQL, and SQLite using tagged template literals. This approach protects against SQL injection by automatically handling parameter escaping. The API supports connection pooling, transactions, prepared statements, and BigInt support.
import { sql, SQL } from "bun";
// PostgreSQL (default)
const users = await sql`SELECT * FROM users WHERE active = ${true} LIMIT ${10}`;
// MySQL
const mysql = new SQL("mysql://user:pass@localhost:3306/mydb");
const mysqlResults = await mysql`SELECT * FROM users WHERE active = ${true}`;
// SQLite
const sqlite = new SQL("sqlite://myapp.db");
const sqliteResults = await sqlite`SELECT * FROM users WHERE active = ${1}`;