Drizzle <> Cloudflare Durable Objects SQLite
This guide assumes familiarity with:
- Drizzle๋ฅผ ์ฌ์ฉํ ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ฐ๊ฒฐ ๊ธฐ๋ณธ ์ฌํญ
- Cloudflare SQLite Durable Objects - Durable Object ๋ด์ ์๋ฒ ๋๋ SQLite ๋ฐ์ดํฐ๋ฒ ์ด์ค - ์ฌ๊ธฐ์ ์ฝ๊ธฐ
Drizzle ORM์ Cloudflare Durable Objects ๋ฐ์ดํฐ๋ฒ ์ด์ค์ Cloudflare Workers ํ๊ฒฝ์ ์์ ํ ์ง์ํฉ๋๋ค.
์ ํฌ๋ SQL ๋ฐฉ์ธ๊ณผ ๋ฐฉ์ธ๋ณ ๋๋ผ์ด๋ฒ ๋ฐ ๊ตฌ๋ฌธ์ ์ฑํํ๊ณ ๊ฐ์ฅ ์ธ๊ธฐ ์๋ SQLite์ ์ ์ฌํ all, get, values ๋ฐ run ์ฟผ๋ฆฌ ๋ฉ์๋ ๊ตฌ๋ฌธ์ ๋ฐ์ํฉ๋๋ค.
Cloudflare Durable Objects ํ๋ก์ ํธ๋ฅผ ์ค์ ํ๋ ค๋ฉด **๊ณต์ ๋ฌธ์**๋ฅผ ์ฐธ์กฐํ์ธ์.
1๋จ๊ณ - ํจํค์ง ์ค์น
npm
yarn
pnpm
bun
npm i drizzle-orm
npm i -D drizzle-kit
2๋จ๊ณ - ๋๋ผ์ด๋ฒ ์ด๊ธฐํ ๋ฐ ์ฟผ๋ฆฌ ์คํ
Durable Objects ๋ฐ์ดํฐ๋ฒ ์ด์ค๋ฅผ ์ํด wrangler.toml ํ์ผ์ด ํ์ํ๋ฉฐ ๋ค์๊ณผ ๊ฐ์ด ๋ณด์ผ ๊ฒ์
๋๋ค:
#:schema node_modules/wrangler/config-schema.json
name = "sqlite-durable-objects"
main = "src/index.ts"
compatibility_date = "2024-11-12"
compatibility_flags = [ "nodejs_compat" ]
# Bind a Durable Object. Durable objects are a scale-to-zero compute primitive based on the actor model.
# Durable Objects can live for as long as needed. Use these when you need a long-running "server", such as in realtime apps.
# Docs: https://developers.cloudflare.com/workers/wrangler/configuration/#durable-objects
[[durable_objects.bindings]]
name = "MY_DURABLE_OBJECT"
class_name = "MyDurableObject"
# Durable Object migrations.
# Docs: https://developers.cloudflare.com/workers/wrangler/configuration/#migrations
[[migrations]]
tag = "v1"
new_sqlite_classes = ["MyDurableObject"]
# We need rules so we can import migrations in the next steps
[[rules]]
type = "Text"
globs = ["**/*.sql"]
fallthrough = true์ฒซ Durable Objects SQLite ์ฟผ๋ฆฌ๋ฅผ ์คํํ์ธ์:
/// <reference types="@cloudflare/workers-types" />
import { drizzle, DrizzleSqliteDODatabase } from 'drizzle-orm/durable-sqlite';
import { DurableObject } from 'cloudflare:workers'
import { migrate } from 'drizzle-orm/durable-sqlite/migrator';
import migrations from '../drizzle/migrations';
import { usersTable } from './db/schema';
export class MyDurableObject extends DurableObject {
storage: DurableObjectStorage;
db: DrizzleSqliteDODatabase<any>;
constructor(ctx: DurableObjectState, env: Env) {
super(ctx, env);
this.storage = ctx.storage;
this.db = drizzle(this.storage, { logger: false });
// ์ฟผ๋ฆฌ๋ฅผ ์๋ฝํ๊ธฐ ์ ์ ๋ชจ๋ ๋ง์ด๊ทธ๋ ์ด์
์ด ์๋ฃ๋์๋์ง ํ์ธํฉ๋๋ค.
// ๊ทธ๋ ์ง ์์ผ๋ฉด Drizzle ๋ฐ์ดํฐ๋ฒ ์ด์ค `this.db`์ ์ก์ธ์คํ๋ ๋ชจ๋ ํจ์์์ `this.migrate()`๋ฅผ ์คํํด์ผ ํฉ๋๋ค.
ctx.blockConcurrencyWhile(async () => {
await this._migrate();
});
}
async insertAndList(user: typeof usersTable.$inferInsert) {
await this.insert(user);
return this.select();
}
async insert(user: typeof usersTable.$inferInsert) {
await this.db.insert(usersTable).values(user);
}
async select() {
return this.db.select().from(usersTable);
}
async _migrate() {
migrate(this.db, migrations);
}
}
export default {
/**
* ์ด๊ฒ์ Cloudflare Worker์ ํ์ค fetch ํธ๋ค๋ฌ์
๋๋ค
*
* @param request - ํด๋ผ์ด์ธํธ์์ Worker๋ก ์ ์ถ๋ ์์ฒญ
* @param env - wrangler.toml์ ์ ์ธ๋ ๋ฐ์ธ๋ฉ์ ์ฐธ์กฐํ๋ ์ธํฐํ์ด์ค
* @param ctx - Worker์ ์คํ ์ปจํ
์คํธ
* @returns ํด๋ผ์ด์ธํธ๋ก ๋ค์ ์ ์ก๋ ์๋ต
*/
async fetch(request: Request, env: Env): Promise<Response> {
const id: DurableObjectId = env.MY_DURABLE_OBJECT.idFromName('durable-object');
const stub = env.MY_DURABLE_OBJECT.get(id);
// ์ต์
A - ์ต๊ณ ์ ์ฑ๋ฅ.
// DO ๋ด์์ ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ก์ธ์ค๊ฐ ๋น ๋ฅด๋ฏ๋ก, ๋ชจ๋ ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ํธ ์์ฉ์ ๋จ์ผ Durable Object ํธ์ถ๋ก ๋ฒ๋ค๋งํ๋ ๊ฒ์ด ์ต๋ ์ฑ๋ฅ์ ์ํด ์ ํธ๋ฉ๋๋ค.
const usersAll = await stub.insertAndList({
name: 'John',
age: 30,
email: 'john@example.com',
});
console.log('New user created. Getting all users from the database: ', users);
// ์ต์
B - ๋๋ฆฌ์ง๋ง ๋๋๋ก ๋๋ฒ๊น
์ ์ ์ฉํ ์ ์์ต๋๋ค.
// ๋
ธ์ถ๋ ๊ฒฝ์ฐ ๊ฐ๋ณ Drizzle ์ฟผ๋ฆฌ๋ฅผ ์ง์ ํธ์ถํ ์๋ ์์ง๋ง, ๋ชจ๋ ์ฟผ๋ฆฌ๋ Durable Object ์ธ์คํด์ค๋ก์ ์๋ณต์ด๋ผ๋ ์ ์ ๋ช
์ฌํ์ธ์.
await stub.insert({
name: 'John',
age: 30,
email: 'john@example.com',
});
console.log('New user created!');
const users = await stub.select();
console.log('Getting all users from the database: ', users);
return Response.json(users);
}
}