forked from imbytecat/fullstack-starter
feat: 添加数据库包及 UUID 支持,初始化类型检查与字段工具
- 将 typecheck 脚本改为使用 tsc --noEmit,并添加 @furtherverse/tsconfig 工作区依赖。 - 添加新的本地包 @furtherverse/database 并更新 uuid 依赖至 13.0.0 版本,同时修复相关依赖引用。 - 添加 uuid 依赖以支持生成唯一标识符功能 - 初始化数据库包的配置,设置模块导入导出路径并配置类型检查与格式化脚本。 - 导出 todo 模式定义文件中的所有内容 - 添加待办事项表结构,包含自动生成字段、标题和完成状态字段。 - 添加用于生成主键、创建和更新时间戳字段的实用工具函数,并提供自动生成字段及其键的映射。 - 导出字段工具模块中的所有内容 - 添加 SQLite 数据库连接初始化功能 - 添加数据库包的 TypeScript 配置并继承基础配置文件
This commit is contained in:
1
packages/database/src/postgres/schema/index.ts
Normal file
1
packages/database/src/postgres/schema/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from './todo'
|
||||
8
packages/database/src/postgres/schema/todo.ts
Normal file
8
packages/database/src/postgres/schema/todo.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
import { boolean, pgTable, text } from 'drizzle-orm/pg-core'
|
||||
import { generatedFields } from '../utils'
|
||||
|
||||
export const todoTable = pgTable('todo', {
|
||||
...generatedFields,
|
||||
title: text('title').notNull(),
|
||||
completed: boolean('completed').notNull().default(false),
|
||||
})
|
||||
58
packages/database/src/postgres/utils/field.ts
Normal file
58
packages/database/src/postgres/utils/field.ts
Normal file
@@ -0,0 +1,58 @@
|
||||
import { sql } from 'drizzle-orm'
|
||||
import { timestamp, uuid } from 'drizzle-orm/pg-core'
|
||||
import { v7 as uuidv7 } from 'uuid'
|
||||
|
||||
// id
|
||||
|
||||
export const id = (name: string) => uuid(name)
|
||||
export const pk = (name: string, strategy?: 'native' | 'extension') => {
|
||||
switch (strategy) {
|
||||
// PG 18+
|
||||
case 'native':
|
||||
return id(name).primaryKey().default(sql`uuidv7()`)
|
||||
|
||||
// PG 13+ with extension
|
||||
case 'extension':
|
||||
return id(name).primaryKey().default(sql`uuid_generate_v7()`)
|
||||
|
||||
// Any PG version
|
||||
default:
|
||||
return id(name)
|
||||
.primaryKey()
|
||||
.$defaultFn(() => uuidv7())
|
||||
}
|
||||
}
|
||||
|
||||
// timestamp
|
||||
|
||||
export const createdAt = (name = 'created_at') =>
|
||||
timestamp(name, { withTimezone: true }).notNull().defaultNow()
|
||||
|
||||
export const updatedAt = (name = 'updated_at') =>
|
||||
timestamp(name, { withTimezone: true })
|
||||
.notNull()
|
||||
.defaultNow()
|
||||
.$onUpdateFn(() => new Date())
|
||||
|
||||
// generated fields
|
||||
|
||||
export const generatedFields = {
|
||||
id: pk('id'),
|
||||
createdAt: createdAt('created_at'),
|
||||
updatedAt: updatedAt('updated_at'),
|
||||
}
|
||||
|
||||
// Helper to create omit keys from generatedFields
|
||||
const createGeneratedFieldKeys = <T extends Record<string, unknown>>(
|
||||
fields: T,
|
||||
): Record<keyof T, true> => {
|
||||
return Object.keys(fields).reduce(
|
||||
(acc, key) => {
|
||||
acc[key as keyof T] = true
|
||||
return acc
|
||||
},
|
||||
{} as Record<keyof T, true>,
|
||||
)
|
||||
}
|
||||
|
||||
export const generatedFieldKeys = createGeneratedFieldKeys(generatedFields)
|
||||
1
packages/database/src/postgres/utils/index.ts
Normal file
1
packages/database/src/postgres/utils/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from './field'
|
||||
0
packages/database/src/sqlite/index.ts
Normal file
0
packages/database/src/sqlite/index.ts
Normal file
Reference in New Issue
Block a user