libSQL driver for TypeScript and JavaScript with support for local files, remote databases, and embedded replicas
npx @tessl/cli install tessl/npm-libsql--client@0.15.0libSQL Client is a comprehensive TypeScript/JavaScript driver for libSQL databases, offering both local and remote database connectivity with advanced features like embedded replicas that can work offline and sync with remote Turso databases. It provides full SQLite compatibility with additional libSQL features like encryption at rest and AI & Vector Search capabilities for modern applications.
npm install @libsql/clientimport { createClient } from "@libsql/client";For CommonJS:
const { createClient } = require("@libsql/client");Platform-specific imports:
// Node.js optimized
import { createClient } from "@libsql/client/node";
// HTTP-only client
import { createClient } from "@libsql/client/http";
// WebSocket-only client
import { createClient } from "@libsql/client/ws";
// Local SQLite3 client
import { createClient } from "@libsql/client/sqlite3";
// Web/browser client
import { createClient } from "@libsql/client/web";import { createClient } from "@libsql/client";
// Create client for local database
const client = createClient({
url: "file:local.db"
});
// Create client for remote database
const client = createClient({
url: "libsql://your-database.turso.io",
authToken: "your-auth-token"
});
// Create embedded replica (works offline, syncs with remote)
const client = createClient({
url: "file:local.db",
syncUrl: "libsql://your-database.turso.io",
authToken: "your-auth-token",
syncInterval: 60000 // sync every minute
});
// Execute a simple query
const result = await client.execute("SELECT * FROM users");
console.log(result.rows);
// Execute with parameters
const user = await client.execute({
sql: "SELECT * FROM users WHERE id = ?",
args: [1]
});
// Execute a batch of statements in a transaction
const results = await client.batch([
"CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, name TEXT)",
{
sql: "INSERT INTO users (name) VALUES (?)",
args: ["Alice"]
}
], "write");
// Don't forget to close when done
client.close();libSQL Client is built around several key components:
HttpClient, WsClient, Sqlite3Client) behind unified interfaceCore client creation with comprehensive configuration options for different deployment scenarios and database connection types.
function createClient(config: Config): Client;
interface Config {
url: string;
authToken?: string;
encryptionKey?: string;
syncUrl?: string;
syncInterval?: number;
readYourWrites?: boolean;
offline?: boolean;
tls?: boolean;
intMode?: IntMode;
fetch?: Function;
concurrency?: number | undefined;
}
type IntMode = "number" | "bigint" | "string";Core database interaction methods for executing SQL statements, managing results, and handling different query patterns.
interface Client {
execute(stmt: InStatement): Promise<ResultSet>;
execute(sql: string, args?: InArgs): Promise<ResultSet>;
batch(stmts: Array<InStatement | [string, InArgs?]>, mode?: TransactionMode): Promise<Array<ResultSet>>;
migrate(stmts: Array<InStatement>): Promise<Array<ResultSet>>;
transaction(mode?: TransactionMode): Promise<Transaction>;
/** @deprecated Please specify the `mode` explicitly. The default `"write"` will be removed in the next major release. */
transaction(): Promise<Transaction>;
executeMultiple(sql: string): Promise<void>;
sync(): Promise<Replicated>;
close(): void;
reconnect(): void;
closed: boolean;
protocol: string;
}
interface ResultSet {
columns: Array<string>;
columnTypes: Array<string>;
rows: Array<Row>;
rowsAffected: number;
lastInsertRowid: bigint | undefined;
toJSON(): any;
}Interactive transaction support with commit/rollback control for complex multi-statement operations requiring atomicity.
interface Transaction {
execute(stmt: InStatement): Promise<ResultSet>;
batch(stmts: Array<InStatement>): Promise<Array<ResultSet>>;
executeMultiple(sql: string): Promise<void>;
rollback(): Promise<void>;
commit(): Promise<void>;
close(): void;
closed: boolean;
}
type TransactionMode = "write" | "read" | "deferred";type Value = null | string | number | bigint | ArrayBuffer;
type InValue = Value | boolean | Uint8Array | Date;
type InStatement = { sql: string; args?: InArgs } | string;
type InArgs = Array<InValue> | Record<string, InValue>;
interface Row {
length: number;
[index: number]: Value;
[name: string]: Value;
}
type Replicated = { frame_no: number; frames_synced: number } | undefined;
class LibsqlError extends Error {
code: string;
rawCode?: number;
constructor(message: string, code: string, rawCode?: number, cause?: Error);
}libSQL Client automatically selects the appropriate implementation based on URL scheme:
file: URLs → Sqlite3Client (Node.js only)http:/https: URLs → HttpClient (all platforms)ws:/wss: URLs → WsClient (where WebSockets supported)libsql: URLs → Auto-detection (prefers WebSocket, falls back to HTTP)Node.js Environment:
Browser/Web Environment:
Deno/Cloudflare Workers:
The sync() method is only available for embedded replica configurations:
url (local file) and syncUrl (remote database)Sqlite3Client (Node.js environment)undefined for non-replica clients"number" mode (default):
RangeError for larger values"bigint" mode:
BigInt() conversion in calculations"string" mode: