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"

# SQLD_DB_PATH
# SQLD_HTTP_AUTH=basic:${base64('user:pass')}
# SQLD_AUTH_JWT_KEY_FILE
# -e SQLD_NODE=replica -e SQLD_PRIMARY_URL=https://<host>:<port>
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

# 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,
});

Notes