Skip to content

Neon 是一個完全托管的無服務器 Postgres,將計算和存儲分離,提供自動擴展、分支和無限存儲等功能。可以直接使用 @neondatabase/serverless 驅動或通過像 Drizzle 這樣的 ORM 在 Bun 中使用 Neon。

Drizzle ORM 支持類似 SQL 的"查詢構建器"API 和類似 ORM 的 Queries API。首先創建一個項目目錄,使用 bun init 初始化目錄,然後安裝 Drizzle 和 Neon 無服務器驅動

sh
mkdir bun-drizzle-neon
cd bun-drizzle-neon
bun init -y
bun add drizzle-orm @neondatabase/serverless
bun add -D drizzle-kit

創建一個 .env.local 文件並添加你的 Neon Postgres 連接字符串

ini
DATABASE_URL=postgresql://usertitle:password@ep-adj-noun-guid.us-east-1.aws.neon.tech/neondb?sslmode=require

我們將使用 Neon 無服務器驅動連接到 Neon 數據庫,並封裝在 Drizzle 數據庫實例中。

ts
import { neon } from "@neondatabase/serverless";
import { drizzle } from "drizzle-orm/neon-http";

// Bun 會自動從 .env.local 加載 DATABASE_URL
// 請參閱:https://bun.com/docs/runtime/environment-variables 獲取更多信息
const sql = neon(process.env.DATABASE_URL!);

export const db = drizzle(sql);

要查看數據庫的實際效果,將這些行添加到 index.ts

ts
import { db } from "./db";
import { sql } from "drizzle-orm";

const query = sql`select 'hello world' as text`;
const result = await db.execute(query);
console.log(result.rows);

然後使用 Bun 運行 index.ts

sh
bun run index.ts
txt
[
  {
    text: "hello world",
  }
]

我們可以使用 Drizzle ORM 原語為數據庫定義一個模式。創建一個 schema.ts 文件並添加此代碼。

ts
import { pgTable, integer, serial, text, timestamp } from "drizzle-orm/pg-core";

export const authors = pgTable("authors", {
  id: serial("id").primaryKey(),
  title: text("name").notNull(),
  bio: text("bio"),
  createdAt: timestamp("created_at").notNull().defaultNow(),
});

然後我們使用 drizzle-kit CLI 生成初始 SQL 遷移。

sh
bunx drizzle-kit generate --dialect postgresql --schema ./schema.ts --out ./drizzle

這將創建一個新的 drizzle 目錄,其中包含一個 .sql 遷移文件和 meta 目錄。

txt
drizzle
├── 0000_aspiring_post.sql
└── meta
    ├── 0000_snapshot.json
    └── _journal.json

我們可以使用一個簡單的 migrate.ts 腳本來執行這些遷移。此腳本創建一個到 Neon 數據庫的新連接,並執行 drizzle 目錄中所有未執行的遷移。

ts
import { db } from "./db";
import { migrate } from "drizzle-orm/neon-http/migrator";

const main = async () => {
  try {
    await migrate(db, { migrationsFolder: "drizzle" });
    console.log("Migration completed");
  } catch (error) {
    console.error("Error during migration:", error);
    process.exit(1);
  }
};

main();

我們可以使用 bun 運行此腳本來執行遷移。

sh
bun run migrate.ts
txt
Migration completed

現在我們可以向數據庫添加一些數據。創建一個 seed.ts 文件,內容如下。

ts
import { db } from "./db";
import * as schema from "./schema";

async function seed() {
  await db.insert(schema.authors).values([
    {
      title: "J.R.R. Tolkien",
      bio: "The creator of Middle-earth and author of The Lord of the Rings.",
    },
    {
      title: "George R.R. Martin",
      bio: "The author of the epic fantasy series A Song of Ice and Fire.",
    },
    {
      title: "J.K. Rowling",
      bio: "The creator of the Harry Potter series.",
    },
  ]);
}

async function main() {
  try {
    await seed();
    console.log("Seeding completed");
  } catch (error) {
    console.error("Error during seeding:", error);
    process.exit(1);
  }
}

main();

然後運行此文件。

sh
bun run seed.ts
txt
Seeding completed

現在我們有了一個帶有模式和示例數據的數據庫。我們可以使用 Drizzle 查詢它。將 index.ts 的內容替換為以下內容。

ts
import * as schema from "./schema";
import { db } from "./db";

const result = await db.select().from(schema.authors);
console.log(result);

然後運行文件。你應該會看到我們插入的三位作者。

sh
bun run index.ts
txt
[
  {
    id: 1,
    title: "J.R.R. Tolkien",
    bio: "The creator of Middle-earth and author of The Lord of the Rings.",
    createdAt: 2024-05-11T10:28:46.029Z,
  }, {
    id: 2,
    title: "George R.R. Martin",
    bio: "The author of the epic fantasy series A Song of Ice and Fire.",
    createdAt: 2024-05-11T10:28:46.029Z,
  }, {
    id: 3,
    title: "J.K. Rowling",
    bio: "The creator of the Harry Potter series.",
    createdAt: 2024-05-11T10:28:46.029Z,
  }
]

本示例使用了 Neon 無服務器驅動的 SQL-over-HTTP 功能。Neon 的無服務器驅動還提供了 ClientPool 構造函數,以支持會話、交互式事務和 node-postgres 兼容性。請參閱 Neon 文檔 獲取完整概述。

請參閱 Drizzle 網站 獲取有關使用 Drizzle ORM 的更多文檔。

Bun學習網由www.bunjs.com.cn整理維護