@drizzle-adapter/libsql

中文文档
1.1.5发布于 2026/09/27MITTypeScriptES Module近 30 天下载 2.3万
原始使用文档
文档目录17 节
  1. Overview
  2. Why LibSQL/Turso?
  3. Perfect For:
  4. Installation
  5. Important: Adapter Registration
  6. Usage
  7. Configuration
  8. Schema Definition
  9. Basic CRUD Operations
  10. Full-Text Search
  11. Best Practices
  12. Local Development
  13. Production with Turso
  14. Replica Support
  15. Best Practices
  16. Contributing
  17. Related Packages

@drizzle-adapter/libsql

LibSQL adapter implementation for the Drizzle Adapter ecosystem, supporting both local SQLite databases and Turso.

Overview

The @drizzle-adapter/libsql package provides the LibSQL implementation for the Drizzle Adapter interface. While you don't interact with this package directly (you use @drizzle-adapter/core instead), it enables support for LibSQL and Turso in the Drizzle Adapter ecosystem.

Why LibSQL/Turso?

LibSQL and Turso offer unique advantages:

  • Embedded or Serverless: Use locally or as a serverless database with Turso
  • SQLite Compatible: Built on SQLite with additional features
  • WebSocket Protocol: Efficient binary protocol for lower latency
  • Automatic Replication: Built-in replication with Turso
  • Edge Computing: Global distribution with Turso's edge network
  • Local Development: Easy local development with file-based SQLite
  • Cost Effective: Free tier and usage-based pricing with Turso
  • Multi-Region: Automatic multi-region deployment with Turso

Perfect For:

  1. Edge Applications: Data served from the closest location
  2. Local Development: Embedded database for development
  3. Hybrid Deployments: Mix local and serverless as needed
  4. Cost-Sensitive Projects: Start free and scale as needed
  5. Global Applications: Automatic global distribution with Turso

Installation

# Install both the core package and the LibSQL adapter
pnpm install @drizzle-adapter/core @drizzle-adapter/libsql

Important: Adapter Registration

For the adapter to work correctly with the DrizzleAdapterFactory, you must import it for its self-registration side effects:

// Import for side effects - adapter will self-register
import '@drizzle-adapter/libsql';

// Now you can use the factory
import { DrizzleAdapterFactory } from '@drizzle-adapter/core';

Usage

Configuration

import { DrizzleAdapterFactory, TypeDrizzleDatabaseConfig } from '@drizzle-adapter/core';

// Local SQLite database
const localConfig: TypeDrizzleDatabaseConfig = {
  DATABASE_DRIVER: 'libsql',
  DATABASE_URL: 'file:local.db'
};

// Turso database
const tursoConfig: TypeDrizzleDatabaseConfig = {
  DATABASE_DRIVER: 'libsql',
  DATABASE_URL: 'libsql://your-database-url.turso.io',
  DATABASE_AUTH_TOKEN: 'your-auth-token',
  DATABASE_SYNC_URL: 'file:local-replica.db' // Optional sync URL for replica
};

const factory = new DrizzleAdapterFactory();
const adapter = factory.create(config);

Schema Definition

const dataTypes = adapter.getDataTypes();

const users = dataTypes.dbTable('users', {
  id: dataTypes.dbInteger('id').primaryKey().autoincrement(),
  name: dataTypes.dbText('name').notNull(),
  email: dataTypes.dbText('email').notNull().unique(),
  metadata: dataTypes.dbText('metadata'), // JSON stored as text
  createdAt: dataTypes.dbInteger('created_at')
    .default(sql`(strftime('%s', 'now'))`)
});

const posts = dataTypes.dbTable('posts', {
  id: dataTypes.dbInteger('id').primaryKey().autoincrement(),
  userId: dataTypes.dbInteger('user_id')
    .references(() => users.id),
  title: dataTypes.dbText('title').notNull(),
  content: dataTypes.dbText('content').notNull(),
  published: dataTypes.dbInteger('published').default(0),
  createdAt: dataTypes.dbInteger('created_at')
    .default(sql`(strftime('%s', 'now'))`)
});

Basic CRUD Operations

import { eq, and, or, desc, sql } from 'drizzle-orm';

const client = await adapter.getConnection().getClient();

// INSERT
// Single insert with returning
const [newUser] = await client
  .insert(users)
  .values({
    name: 'John Doe',
    email: '[email protected]',
    metadata: JSON.stringify({ role: 'user' })
  })
  .returning();

// Bulk insert
await client
  .insert(posts)
  .values([
    {
      userId: newUser.id,
      title: 'First Post',
      content: 'Hello, world!'
    },
    {
      userId: newUser.id,
      title: 'Second Post',
      content: 'Another post'
    }
  ]);

// SELECT
// Select all
const allUsers = await client
  .select()
  .from(users);

// Select with conditions
const user = await client
  .select()
  .from(users)
  .where(eq(users.email, '[email protected]'));

// Select with join
const userPosts = await client
  .select({
    userName: users.name,
    postTitle: posts.title,
    content: posts.content,
    metadata: users.metadata,
    createdAt: sql`datetime(${posts.createdAt}, 'unixepoch')`
  })
  .from(posts)
  .leftJoin(users, eq(posts.userId, users.id))
  .where(eq(posts.published, 1))
  .orderBy(desc(posts.createdAt));

// UPDATE
await client
  .update(users)
  .set({
    name: 'John Smith',
    metadata: JSON.stringify({ role: 'admin' })
  })
  .where(eq(users.id, newUser.id));

// DELETE
await client
  .delete(posts)
  .where(
    and(
      eq(posts.userId, newUser.id),
      eq(posts.published, 0)
    )
  );
// Create FTS table
await client.execute(sql`
  CREATE VIRTUAL TABLE posts_fts USING fts5(
    title, content,
    content='posts',
    content_rowid='id'
  )
`);

// Search posts
const searchResults = await client
  .select({
    title: posts.title,
    content: posts.content,
    author: users.name,
    rank: sql`rank`
  })
  .from(posts)
  .leftJoin(users, eq(posts.userId, users.id))
  .where(sql`posts.id IN (
    SELECT rowid 
    FROM posts_fts 
    WHERE posts_fts MATCH ${searchTerm}
    ORDER BY rank
  )`);

Best Practices

Local Development

// Use file-based SQLite for development
const devAdapter = factory.create({
  DATABASE_DRIVER: 'libsql',
  DATABASE_URL: 'file:dev.db'
});

Production with Turso

// Use Turso for production
const prodAdapter = factory.create({
  DATABASE_DRIVER: 'libsql',
  DATABASE_URL: process.env.DATABASE_URL,
  DATABASE_AUTH_TOKEN: process.env.DATABASE_AUTH_TOKEN
});

Replica Support

// Use local replica for better performance
const replicaAdapter = factory.create({
  DATABASE_DRIVER: 'libsql',
  DATABASE_URL: process.env.DATABASE_URL,
  DATABASE_AUTH_TOKEN: process.env.DATABASE_AUTH_TOKEN,
  DATABASE_SYNC_URL: 'file:local-replica.db'
});

Best Practices

  1. Development Flow: Use file-based SQLite for development
  2. Production: Use Turso for production deployments
  3. Replicas: Leverage local replicas for better performance
  4. JSON Handling: Store JSON as TEXT and parse/stringify as needed
  5. Query Optimization: Use indexes and FTS for better performance

Contributing

We welcome contributions! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.

版本与发布记录

@drizzle-adapter/libsql 共发布 15 个版本,首次发布于 2024/12/03;过去 12 个月发布了 3 个版本。

版本发布日期距今
1.1.5latest2026/09/271 个月前
1.1.32026/09/261 个月前
1.1.12026/03/267 个月前
1.0.132025/07/171.3 年前
1.0.122025/07/121.3 年前
1.0.112025/07/121.3 年前

关于 @drizzle-adapter/libsql 的常见问题

如何安装 @drizzle-adapter/libsql?

在项目目录运行 npm install @drizzle-adapter/libsql,或使用 pnpm add / yarn add / bun add。

@drizzle-adapter/libsql 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@drizzle-adapter/libsql 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@drizzle-adapter/libsql 可以商用吗?

@drizzle-adapter/libsql 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@drizzle-adapter/libsql 的下载量有多少?

根据 npm 官方下载统计,@drizzle-adapter/libsql 在 2026/10/13 – 2026/11/11 这 30 天内被下载 22,581 次,日均约 753 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@drizzle-adapter/libsql 还在维护吗?

最新版本 1.1.5 发布于 2026/09/27(1 个月前);至今共发布 15 个版本,其中过去 12 个月发布了 3 个;首次发布于 2024/12/03。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。