first commit 🎉
This commit is contained in:
@@ -0,0 +1 @@
|
||||
export { noDeleteTimestamps, timestamps } from './timestamps'
|
||||
@@ -0,0 +1,11 @@
|
||||
import { timestamp } from 'drizzle-orm/pg-core'
|
||||
|
||||
export const noDeleteTimestamps = {
|
||||
createdAt: timestamp('created_at', { withTimezone: true }).notNull().defaultNow(),
|
||||
updatedAt: timestamp('updated_at', { withTimezone: true }).notNull().$onUpdate(() => new Date()),
|
||||
}
|
||||
|
||||
export const timestamps = {
|
||||
...noDeleteTimestamps,
|
||||
deletedAt: timestamp('deleted_at', { withTimezone: true }),
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
import { drizzle } from 'drizzle-orm/node-postgres'
|
||||
import { Pool } from 'pg'
|
||||
import { dbDsn } from '~/config'
|
||||
import { schema } from './model'
|
||||
|
||||
export const client = new Pool({
|
||||
connectionString: dbDsn,
|
||||
})
|
||||
|
||||
export const db = drizzle({ client, schema: schema })
|
||||
|
||||
export { table, type Table } from './model'
|
||||
export { spread, spreads } from './utils'
|
||||
@@ -0,0 +1,15 @@
|
||||
import users, * as userSchema from './schema/users'
|
||||
|
||||
// relation
|
||||
// import * as relations from './relations'
|
||||
|
||||
export const schema = {
|
||||
// ...relations,
|
||||
...userSchema,
|
||||
}
|
||||
|
||||
export const table = {
|
||||
users,
|
||||
} as const
|
||||
|
||||
export type Table = typeof table
|
||||
@@ -0,0 +1,2 @@
|
||||
// import { relations } from 'drizzle-orm'
|
||||
// import users from './schema/users'
|
||||
@@ -0,0 +1,26 @@
|
||||
import { boolean, pgEnum, pgTable, serial, text, varchar } from 'drizzle-orm/pg-core'
|
||||
import { timestamps } from '../customTypes'
|
||||
|
||||
export const userRoles = [
|
||||
'admin',
|
||||
'visitor',
|
||||
'user',
|
||||
'volunteer',
|
||||
] as const
|
||||
|
||||
export const roleEnum = pgEnum('role', userRoles)
|
||||
|
||||
export const users = pgTable('users', {
|
||||
id: serial('id').primaryKey(),
|
||||
name: varchar({ length: 255 }).notNull(),
|
||||
email: varchar({ length: 255 }).unique(),
|
||||
phone: varchar({ length: 255 }),
|
||||
password: varchar({ length: 127 }),
|
||||
image: text(),
|
||||
googleId: varchar('google_id', { length: 255 }),
|
||||
role: roleEnum().notNull().default('user'),
|
||||
status: boolean().notNull().default(true),
|
||||
...timestamps,
|
||||
})
|
||||
|
||||
export default users
|
||||
+102
@@ -0,0 +1,102 @@
|
||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||
/* eslint-disable @typescript-eslint/no-empty-object-type */
|
||||
|
||||
/**
|
||||
* @lastModified 2025-02-04
|
||||
* @see https://elysiajs.com/integrations/drizzle.html#utility
|
||||
*/
|
||||
|
||||
import { Kind, type TObject } from '@sinclair/typebox'
|
||||
import {
|
||||
createInsertSchema,
|
||||
createSelectSchema,
|
||||
BuildSchema,
|
||||
} from 'drizzle-typebox'
|
||||
|
||||
// import { table } from './schema'
|
||||
import type { Table } from 'drizzle-orm'
|
||||
|
||||
type Spread<
|
||||
T extends TObject | Table,
|
||||
Mode extends 'select' | 'insert' | undefined,
|
||||
> = T extends TObject<infer Fields>
|
||||
? {
|
||||
[K in keyof Fields]: Fields[K]
|
||||
}
|
||||
: T extends Table
|
||||
? Mode extends 'select'
|
||||
? BuildSchema<
|
||||
'select',
|
||||
T['_']['columns'],
|
||||
undefined
|
||||
>['properties']
|
||||
: Mode extends 'insert'
|
||||
? BuildSchema<
|
||||
'insert',
|
||||
T['_']['columns'],
|
||||
undefined
|
||||
>['properties']
|
||||
: {}
|
||||
: {}
|
||||
|
||||
/**
|
||||
* Spread a Drizzle schema into a plain object
|
||||
*/
|
||||
export const spread = <
|
||||
T extends TObject | Table,
|
||||
Mode extends 'select' | 'insert' | undefined,
|
||||
>(
|
||||
schema: T,
|
||||
mode?: Mode,
|
||||
): Spread<T, Mode> => {
|
||||
const newSchema: Record<string, unknown> = {}
|
||||
let table
|
||||
|
||||
switch (mode) {
|
||||
case 'insert':
|
||||
case 'select':
|
||||
if (Kind in schema) {
|
||||
table = schema
|
||||
break
|
||||
}
|
||||
|
||||
table = mode === 'insert'
|
||||
? createInsertSchema(schema)
|
||||
: createSelectSchema(schema)
|
||||
|
||||
break
|
||||
|
||||
default:
|
||||
if (!(Kind in schema)) throw new Error('Expect a schema')
|
||||
table = schema
|
||||
}
|
||||
|
||||
for (const key of Object.keys(table.properties))
|
||||
newSchema[key] = table.properties[key]
|
||||
|
||||
return newSchema as any
|
||||
}
|
||||
|
||||
/**
|
||||
* Spread a Drizzle Table into a plain object
|
||||
*
|
||||
* If `mode` is 'insert', the schema will be refined for insert
|
||||
* If `mode` is 'select', the schema will be refined for select
|
||||
* If `mode` is undefined, the schema will be spread as is, models will need to be refined manually
|
||||
*/
|
||||
export const spreads = <
|
||||
T extends Record<string, TObject | Table>,
|
||||
Mode extends 'select' | 'insert' | undefined,
|
||||
>(
|
||||
models: T,
|
||||
mode?: Mode,
|
||||
): {
|
||||
[K in keyof T]: Spread<T[K], Mode>
|
||||
} => {
|
||||
const newSchema: Record<string, unknown> = {}
|
||||
const keys = Object.keys(models)
|
||||
|
||||
for (const key of keys) newSchema[key] = spread(models[key], mode)
|
||||
|
||||
return newSchema as any
|
||||
}
|
||||
Reference in New Issue
Block a user