driftsql

1.0.8 • Public • Published

DriftSQL

npm version npm downloads

A lightweight SQL client for TypeScript, supporting multiple databases like PostgreSQL, LibSQL, and HTTP-based database services. DriftSQL provides a unified, type-safe interface for database operations across different database drivers.

Features

  • 🔐 Type-safe database operations with TypeScript generics
  • 🛡️ SQL injection protection with parameterized queries
  • 🚀 Multiple driver support (PostgreSQL, LibSQL, HTTP)
  • 📝 Auto-completion support for database schema types
  • ⚡ Built-in error handling and connection management
  • 🔄 Unified API across different database types

Supported Drivers

  • PostgreSQL - Native PostgreSQL driver via pg
  • LibSQL - SQLite-compatible databases via @libsql/client
  • HTTP - HTTP-based database services

Usage

Install the package:

# ✨ Auto-detect (supports npm, yarn, pnpm, deno and bun)
npx nypm install driftsql

Import and use:

ESM (Node.js, Bun, Deno)

import { DriftSQLClient } from 'driftsql'

Quick Start

Define Your Database Schema

import { DriftSQLClient } from 'driftsql'

// Define your database schema types
interface User {
  id: number
  name: string
  email: string
  created_at: string
}

interface Post {
  id: number
  title: string
  content: string | null
  user_id: number | null
  published: boolean
  created_at: Date
  updated_at: Date
}

// Define your database schema
interface MyDatabase {
  users: User
  posts: Post
}

Initialize with PostgreSQL

const db = new DriftSQLClient<MyDatabase>({
  drivers: {
    postgres: {
      connectionString: 'postgresql://user:password@localhost:5432/mydb',
      // or individual options:
      // host: 'localhost',
      // port: 5432,
      // database: 'mydb',
      // user: 'user',
      // password: 'password'
    },
  },
})

Initialize with LibSQL

const db = new DriftSQLClient<MyDatabase>({
  drivers: {
    libsql: {
      url: 'file:local.db',
      // or for remote:
      // url: 'libsql://your-database.turso.io',
      // authToken: 'your-auth-token'
    },
  },
})

Initialize with HTTP

const db = new DriftSQLClient<MyDatabase>({
  url: 'https://your-database-api.com',
  password: 'your-bearer-token',
  options: {
    defaultTimeout: 5000, // optional, defaults to 5000ms
  },
})

Database Operations

// Raw SQL queries
const users = await db.query<User>('SELECT * FROM users WHERE active = $1', [true])
console.log(users.rows)

// Find operations
const user = await db.findFirst('users', { email: 'user@example.com' })
const activeUsers = await db.findMany('users', { active: true })

// Insert operations
const newUser = await db.insert('users', {
  name: 'John Doe',
  email: 'john@example.com',
})

// Update operations
const updatedUser = await db.update('users', { name: 'Jane Doe' }, { id: 1 })

// Delete operations
const deleted = await db.delete('users', { id: 1 })

// Check server status (HTTP only)
const status = await db.status()
console.log(`Database OK: ${status.ok}, Ping: ${status.ping}ms`)

// Clean up connections
await db.close()

Constructor Options

interface ClientOptions {
  url?: string // HTTP server URL (for HTTP driver)
  password?: string // Bearer token for HTTP authentication
  drivers?: {
    libsql?: LibsqlClientConfig // LibSQL configuration
    postgres?: PoolConfig // PostgreSQL configuration
    postgresNeonHTTP?: {
      // Neon configuration (experimental)
      connectionString: string
    }
  }
  options?: {
    defaultTimeout?: number // Request timeout in milliseconds
  }
}

License

Published under the MIT license. Made by community 💛

Readme

Keywords

none

Package Sidebar

Install

npm i driftsql

Weekly Downloads

237

Version

1.0.8

License

MIT

Unpacked Size

26.6 kB

Total Files

6

Last publish

Collaborators

  • themrdev