forked from imbytecat/fullstack-starter
- 将 typecheck 脚本改为使用 tsc --noEmit,并添加 @furtherverse/tsconfig 工作区依赖。 - 添加新的本地包 @furtherverse/database 并更新 uuid 依赖至 13.0.0 版本,同时修复相关依赖引用。 - 添加 uuid 依赖以支持生成唯一标识符功能 - 初始化数据库包的配置,设置模块导入导出路径并配置类型检查与格式化脚本。 - 导出 todo 模式定义文件中的所有内容 - 添加待办事项表结构,包含自动生成字段、标题和完成状态字段。 - 添加用于生成主键、创建和更新时间戳字段的实用工具函数,并提供自动生成字段及其键的映射。 - 导出字段工具模块中的所有内容 - 添加 SQLite 数据库连接初始化功能 - 添加数据库包的 TypeScript 配置并继承基础配置文件
59 lines
1.4 KiB
TypeScript
59 lines
1.4 KiB
TypeScript
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)
|