Drizzle ORM 入门:在 Nuxt 4 中使用类型安全的数据库

# Drizzle ORM 入门:在 Nuxt 4 中使用类型安全的数据库 ## 为什么选择 Drizzle? Drizzle 是一个轻量级的 TypeScript ORM,相比 Prisma 有着更小的包体积和更高的性能。 ## 定义 Schema ```typescript import { pgTable, uuid, varchar, text, timestamp } from 'drizzle-orm/pg-core' export const posts = pgTable('posts', { id: uuid('id').defaultRandom().primaryKey(), title: varchar('title', { length: 255 }).notNull(), slug: varchar('slug', { length: 255 }).unique().notNull(), content: text('content').notNull(), createdAt: timestamp('created_at').defaultNow(), }) ``` ## 查询数据 ```typescript import { db } from '~~/server/utils/db' import { posts } from '~~/drizzle/schema' import { eq } from 'drizzle-orm' const allPosts = await db.select().from(posts) const post = await db.select().from(posts).where(eq(posts.slug, 'my-post')).limit(1) ``` Drizzle 的类型安全让开发体验非常丝滑!

评论(0)

暂无评论,来写第一条吧~

发表评论