forked from imbytecat/fullstack-starter
feat: 添加数据库支持与Drizzle ORM配置
- 添加 postgres 依赖版本 3.4.8 以支持数据库连接功能 - 添加 Drizzle ORM 配置文件以指定数据库模式、迁移目录、数据库方言及连接凭证。 - 添加 postgres 依赖以支持数据库连接功能 - 初始化数据库连接,创建使用 Drizzle ORM 的数据库实例并导出连接函数。 - 添加空文件以保留数据库迁移目录的结构 - 导出用户数据模型的定义 - 创建用户表结构,包含唯一ID和姓名字段,并设置ID默认使用uuidv7生成。 - 添加环境变量验证,确保数据库连接URL格式正确。
This commit is contained in:
10
src/db/index.ts
Normal file
10
src/db/index.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
import { drizzle } from 'drizzle-orm/postgres-js'
|
||||
import * as schema from '@/db/schema'
|
||||
import { env } from '@/env'
|
||||
|
||||
export function createDb() {
|
||||
return drizzle({
|
||||
connection: env.DATABASE_URL,
|
||||
schema,
|
||||
})
|
||||
}
|
||||
0
src/db/migrations/.gitkeep
Normal file
0
src/db/migrations/.gitkeep
Normal file
1
src/db/schema/index.ts
Normal file
1
src/db/schema/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from './user'
|
||||
7
src/db/schema/user.ts
Normal file
7
src/db/schema/user.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
import { sql } from 'drizzle-orm'
|
||||
import { pgTable, text, uuid } from 'drizzle-orm/pg-core'
|
||||
|
||||
export const userTable = pgTable('user', {
|
||||
id: uuid('id').primaryKey().default(sql`uuidv7()`),
|
||||
name: text('name').notNull(),
|
||||
})
|
||||
7
src/env.ts
Normal file
7
src/env.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
import { z } from 'zod'
|
||||
|
||||
const envSchema = z.object({
|
||||
DATABASE_URL: z.url(),
|
||||
})
|
||||
|
||||
export const env = envSchema.parse(process.env)
|
||||
Reference in New Issue
Block a user