Drizzle <> Turso 同步数据库

本指南假设你已熟悉:

根据 官方网站 的介绍, Turso 是一款小型数据库,助力你在 AI 时代实现宏大梦想。

第一步:安装依赖包

npm
yarn
pnpm
bun
npm i drizzle-orm@rc @tursodatabase/sync
npm i -D drizzle-kit@rc

第二步:初始化驱动并执行查询

import { drizzle } from 'drizzle-orm/tursodatabase-sync';

const db = drizzle({
	connection: {
    	path: 'local.db',                // path used as a prefix for local files created by sync-engine
    	url: 'https://<db>.turso.io',    // URL of the remote database: turso db show <db>
    	authToken: '...',                // auth token issued from the Turso Cloud: turso db tokens create <db>
    	clientName: 'turso-sync-example' // arbitrary client name
	}
});

const result = await db.all('select 1');

如果需要传入已有驱动:

import { connect } from '@tursodatabase/sync';
import { drizzle } from 'drizzle-orm/tursodatabase-sync';

const client = await connect({
    path: 'local.db',                // path used as a prefix for local files created by sync-engine
    url: 'https://<db>.turso.io',    // URL of the remote database: turso db show <db>
    authToken: '...',                // auth token issued from the Turso Cloud: turso db tokens create <db>
    clientName: 'turso-sync-example' // arbitrary client name
});
const db = drizzle({ client });

const result = await db.all('select 1');

下一步