Become a Gold Sponsor

Drizzle <> Turso

This guide assumes familiarity with:

根据**官方网站**, Turso 是一个基于 libSQL 的边缘 SQLite 数据库即服务。

Drizzle ORM 原生支持 libSQL 驱动, 我们支持 SQL 方言和特定方言的驱动和语法,并镜像最流行的 类似 SQLite 的 allgetvaluesrun 查询方法语法。

第 1 步 - 安装包

npm
yarn
pnpm
bun
npm i drizzle-orm @libsql/client
npm i -D drizzle-kit

第 2 步 - 初始化驱动

Drizzle 原生支持所有 @libsql/client 驱动变体:

@libsql/clientdefaults to node import, automatically changes to web if target or platofrm is set for bundler, e.g. esbuild --platform=browser
@libsql/client/nodenode compatible module, supports :memory:, file, wss, http and turso conneciton protocols
@libsql/client/webmodule for fullstack web frameworks like next, nuxt, astro, etc.
@libsql/client/httpmodule for http and https connection protocols
@libsql/client/wsmodule for ws and wss conneciton protocols
@libsql/client/sqlite3module for :memory: and file conneciton protocols
@libsql/client-wasmSeparate experimental package for WASM

default
node
web
http
web sockets
wasm
import { drizzle } from 'drizzle-orm/libsql';

const db = drizzle({ connection: {
  url: process.env.DATABASE_URL, 
  authToken: process.env.DATABASE_AUTH_TOKEN 
}});

如果您需要提供您现有的驱动:

default
web
import { drizzle } from 'drizzle-orm/libsql';
import { createClient } from '@libsql/client';

const client = createClient({ 
  url: process.env.DATABASE_URL,
  authToken: process.env.DATABASE_AUTH_TOKEN
});

const db = drizzle({ client });

const result = await db.select().from(users).all()

第 3 步 - 执行查询

import { drizzle } from 'drizzle-orm/libsql';
import * as s from 'drizzle-orm/sqlite-core';

const db = drizzle({ connection: {
  url: process.env.DATABASE_URL, 
  authToken: process.env.DATABASE_AUTH_TOKEN 
}});

const users = s.sqliteTable("users", {
  id: s.integer(),
  name: s.text(),
})

const result = await db.select().from(users);

接下来是什么?