Skip to main content

libsql

tip
  • 支持 replica sync - read 访问本地 sqlite,write 访问远程
  • 支持 CS 结构
  • 支持协议
    • HTTP
    • WebSocket
    • SQLite API - embed
brew install libsql
sqld --http-listen-addr 0.0.0.0:8080 --grpc-listen-addr 0.0.0.0:5001 --db-path ./db/ --admin-listen-addr 0.0.0.0:8088 --enable-namespaces
# 服务端的 DB
sqlite3 db/dbs/default/data ".table"

# HTTP
curl -d '{"statements": ["SELECT * FROM users"]}' 127.0.0.1:8080

# Multitenancy
# AMIN
curl -X POST http://localhost:8088/v1/namespaces/wener/create -v --json '{}'
# wener.local 会从 host 获取 namespace - 因为 grpc 默认情况不支持 path
import { createClient } from '@libsql/client';

const client = createClient({
url: 'http://localhost:8080',
});

await client.execute('CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)');
await client.execute('INSERT INTO users (name) VALUES (?)', ['Alice']);
const result = await client.execute('SELECT * FROM users');
console.log(result.rows);

// 本地副本
const client = createClient({
url: 'file:path/to/db-file.db',
authToken: '...',
syncUrl: '...',
syncInterval: 60,
});

sqld

Docker

envfor
SQLD_HTTP_AUTHbasic:${base64('user:pass')}
SQLD_AUTH_JWT_KEY_FILE
SQLD_AUTH_JWT_KEY
SQLD_DB_PATHiku.db
SQLD_NODEprimary/replica/standalone
SQLD_PRIMARY_URLfor replica, gRPC URL, https://<host>:<port>
SQLD_HTTP_LISTEN_ADDR0.0.0.0:8080
SQLD_GRPC_LISTEN_ADDR0.0.0.0:5001
SQLD_HEARTBEAT_URL
# -e SQLD_NODE=replica
docker run --rm -it \
-v $PWD/data:/var/lib/sqld \
-p 8080:8080 \
-e SQLD_NODE=primary \
--name sqld ghcr.io/tursodatabase/libsql-server:latest

Notes