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:
3
bun.lock
3
bun.lock
@@ -14,6 +14,7 @@
|
||||
"dayjs": "^1.11.19",
|
||||
"drizzle-orm": "^1.0.0-beta.9-e89174b",
|
||||
"drizzle-zod": "^1.0.0-beta.9-e89174b",
|
||||
"postgres": "^3.4.8",
|
||||
"react": "^19.2.3",
|
||||
"react-dom": "^19.2.3",
|
||||
"tailwindcss": "^4.1.18",
|
||||
@@ -788,6 +789,8 @@
|
||||
|
||||
"postcss": ["postcss@8.5.6", "", { "dependencies": { "nanoid": "^3.3.11", "picocolors": "^1.1.1", "source-map-js": "^1.2.1" } }, "sha512-3Ybi1tAuwAP9s0r1UQ2J4n5Y0G05bJkpUIO0/bI9MhwmD70S5aTWbXGBwxHrelT+XM1k6dM0pk+SwNkpTRN7Pg=="],
|
||||
|
||||
"postgres": ["postgres@3.4.8", "", {}, "sha512-d+JFcLM17njZaOLkv6SCev7uoLaBtfK86vMUXhW1Z4glPWh4jozno9APvW/XKFJ3CCxVoC7OL38BqRydtu5nGg=="],
|
||||
|
||||
"prettier": ["prettier@3.8.0", "", { "bin": { "prettier": "bin/prettier.cjs" } }, "sha512-yEPsovQfpxYfgWNhCfECjG5AQaO+K3dp6XERmOepyPDVqcJm+bjyCVO3pmU+nAPe0N5dDvekfGezt/EIiRe1TA=="],
|
||||
|
||||
"process": ["process@0.11.10", "", {}, "sha512-cdGef/drWFoydD1JsMzuFf8100nZl+GT+yacc2bEced5f9Rjk4z+WtFUTBu9PhOi9j/jfmBPu0mMEY4wIdAF8A=="],
|
||||
|
||||
11
drizzle.config.ts
Normal file
11
drizzle.config.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
import { defineConfig } from 'drizzle-kit'
|
||||
import { env } from '@/env'
|
||||
|
||||
export default defineConfig({
|
||||
schema: './src/db/schema/index.ts',
|
||||
out: './src/db/migrations',
|
||||
dialect: 'postgresql',
|
||||
dbCredentials: {
|
||||
url: env.DATABASE_URL,
|
||||
},
|
||||
})
|
||||
@@ -20,6 +20,7 @@
|
||||
"dayjs": "^1.11.19",
|
||||
"drizzle-orm": "^1.0.0-beta.9-e89174b",
|
||||
"drizzle-zod": "^1.0.0-beta.9-e89174b",
|
||||
"postgres": "^3.4.8",
|
||||
"react": "^19.2.3",
|
||||
"react-dom": "^19.2.3",
|
||||
"tailwindcss": "^4.1.18",
|
||||
|
||||
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