Database connection with Waddler

Waddler runs SQL queries on your database via database drivers.

index.ts
import { waddler } from "waddler/better-sqlite3"

const sql = waddler(process.env.DATABASE_URL);
const usersCount = await sql`select count(*) from ${sql.identifier('users')};`;
          β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
          β”‚  sql`select count(*) from ${sql.identifier('users')};`  β”‚ <--- waddler query
          β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜     
                               β”‚               ʌ
select count(*) from "users"; -β”‚               β”‚
                               β”‚               β”‚- [{ count: 0 }]
                               v               β”‚
                            β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                            β”‚   better-sqlite3    β”‚ <--- database driver
                            β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                               β”‚               ʌ
01101000 01100101 01111001    -β”‚               β”‚
                               β”‚               β”‚- 01110011 01110101 01110000
                               v               β”‚
                            β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                            β”‚      Database      β”‚ 
                            β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Under the hood Waddler will create a better-sqlite3 driver instance

// above is equivalent to
import { waddler } from "waddler/better-sqlite3";
import Client from 'better-sqlite3';

const client = new Client(process.env.DATABASE_URL);
const sql = waddler({ client });

Waddler is by design natively compatible with every edge or serverless runtime, whenever you’d need access to a serverless database - we’ve got you covered

Neon HTTP
Neon with websockets
Vercel Postgres
PlanetScale HTTP
Cloudflare d1
import { waddler } from "waddler/neon-http";

const sql = waddler(process.env.DATABASE_URL);

And yes, we do support runtime specific drivers like Bun SQLite or Expo SQLite:

import { waddler } from "waddler/bun-sqlite"

const sql = waddler(); // <--- will create an in-memory db
const sql = waddler("./sqlite.db");
import { waddler } from "waddler/expo-sqlite";
import { openDatabaseSync } from "expo-sqlite";

const expo = openDatabaseSync("db.db");
const sql = waddler({ client: expo });

Database connection URL

Just in case if you’re not familiar with database connection URL concept

Local database file:

./<path-to-your-local-database-file>

Turso database url (for waddler/libsql):

libsql://<database>-<username>.aws-eu-west-1.turso.io

You should obtain your Turso database URL from the Turso website. The example above is provided only to illustrate the URL format.

Next steps

Feel free to check out per-driver documentations