mirror of
https://github.com/whekin/household-bot.git
synced 2026-04-01 01:54:03 +00:00
feat(WHE-27): add drizzle db package and typed env config
This commit is contained in:
12
packages/db/src/client.ts
Normal file
12
packages/db/src/client.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import postgres from 'postgres'
|
||||
import { drizzle } from 'drizzle-orm/postgres-js'
|
||||
|
||||
import { env } from '@household/config'
|
||||
|
||||
const queryClient = postgres(env.DATABASE_URL, {
|
||||
prepare: false,
|
||||
max: 5
|
||||
})
|
||||
|
||||
export const db = drizzle(queryClient)
|
||||
export { queryClient }
|
||||
2
packages/db/src/index.ts
Normal file
2
packages/db/src/index.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
export { db, queryClient } from './client'
|
||||
export * as schema from './schema'
|
||||
28
packages/db/src/schema.ts
Normal file
28
packages/db/src/schema.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import { index, integer, pgTable, text, timestamp, uniqueIndex, uuid } from 'drizzle-orm/pg-core'
|
||||
|
||||
export const households = pgTable('households', {
|
||||
id: uuid('id').defaultRandom().primaryKey(),
|
||||
name: text('name').notNull(),
|
||||
createdAt: timestamp('created_at', { withTimezone: true }).defaultNow().notNull()
|
||||
})
|
||||
|
||||
export const members = pgTable(
|
||||
'members',
|
||||
{
|
||||
id: uuid('id').defaultRandom().primaryKey(),
|
||||
householdId: uuid('household_id')
|
||||
.notNull()
|
||||
.references(() => households.id, { onDelete: 'cascade' }),
|
||||
telegramUserId: text('telegram_user_id').notNull(),
|
||||
displayName: text('display_name').notNull(),
|
||||
isAdmin: integer('is_admin').default(0).notNull(),
|
||||
joinedAt: timestamp('joined_at', { withTimezone: true }).defaultNow().notNull()
|
||||
},
|
||||
(table) => ({
|
||||
householdIdx: index('members_household_idx').on(table.householdId),
|
||||
householdTgUserUnique: uniqueIndex('members_household_tg_user_unique').on(
|
||||
table.householdId,
|
||||
table.telegramUserId
|
||||
)
|
||||
})
|
||||
)
|
||||
Reference in New Issue
Block a user