DriftSQL

DriftSQL

A lightweight, type-safe SQL client for TypeScript with support for PostgreSQL, MySQL, LibSQL/SQLite, and Neon.

DriftSQL

A lightweight, type-safe SQL client for TypeScript with support for PostgreSQL, MySQL, LibSQL/SQLite, and Neon.

DriftSQL provides a unified API across different database drivers while maintaining full type safety and modern TypeScript features.

Features

  • 🔐 Type-safe - Full TypeScript support with generated interfaces
  • 🚀 Modular - Import only what you need
  • 🛡️ SQL injection protection - Parameterized queries by default
  • 🔄 Unified API - Same interface across all database drivers
  • Transactions - When supported by the driver
  • 🔍 Schema inspection - Generate TypeScript types from your database
  • 🎯 Helper methods - Convenient CRUD operations
  • 🔀 Fallback drivers - Automatic failover support

Quick Start

import { PostgresDriver, SQLClient } from 'driftsql'

const driver = new PostgresDriver({
  connectionString: 'postgresql://user:password@localhost:5432/mydb'
})
const client = new SQLClient({ driver })

// Raw queries with parameters
const users = await client.query('SELECT * FROM users WHERE age > $1', [18])

// Helper methods
const user = await client.findFirst('users', { email: 'user@example.com' })
const newUser = await client.insert('users', {
  name: 'John Doe',
  email: 'john@example.com'
})
import { MySQLDriver, SQLClient } from 'driftsql'

const driver = new MySQLDriver({
  connectionString: 'mysql://user:password@localhost:3306/mydb'
})
const client = new SQLClient({ driver })

// Raw queries with parameters
const users = await client.query('SELECT * FROM users WHERE age > ?', [18])

// Helper methods work the same across all drivers
const user = await client.findFirst('users', { email: 'user@example.com' })
import { LibSQLDriver, SQLClient } from 'driftsql'

// For Turso/LibSQL
const driver = new LibSQLDriver({
  url: 'libsql://your-database.turso.io',
  authToken: 'your-auth-token'
})

// For local SQLite
// const driver = new LibSQLDriver({ url: 'file:./database.db' })

const client = new SQLClient({ driver })

const users = await client.query('SELECT * FROM users WHERE age > ?', [18])
import { NeonDriver, SQLClient } from 'driftsql'

const driver = new NeonDriver({
  connectionString: 'postgresql://user:password@localhost:5432/mydb'
})
const client = new SQLClient({ driver })

const users = await client.query('SELECT * FROM users WHERE age > $1', [18])

Installation

bun add driftsql

Type Safety with Schema Inspection

Generate TypeScript interfaces from your database schema:

import { inspectDB, PostgresDriver } from 'driftsql'

const driver = new PostgresDriver({
  connectionString: 'postgresql://user:password@localhost:5432/mydb',
})

// Generate types for all tables
await inspectDB({
  driver,
  outputFile: 'db-types.ts', // optional, defaults to 'db-types.ts'
})

This creates a file with TypeScript interfaces:

// Generated db-types.ts
export interface Users {
  id: number
  name: string
  email: string
  created_at: Date
  active: boolean | null
}

export interface Posts {
  id: number
  title: string
  content: string
  user_id: number
  published: boolean
}

export interface Database {
  users: Users
  posts: Posts
}

Use the generated types with your client:

import type { Database } from './db-types'
import { PostgresDriver, SQLClient } from 'driftsql'

const client = new SQLClient<Database>({ driver })

// Now you get full type safety
const user = await client.findFirst('users', { email: 'test@example.com' }) // Returns Users | null
const posts = await client.findMany('posts', { where: { published: true } }) // Returns Posts[]

Next Steps