mirror of
https://github.com/whekin/household-bot.git
synced 2026-03-31 17:34:03 +00:00
feat(WHE-22): ingest configured topic messages with idempotent persistence
This commit is contained in:
@@ -1,12 +1,21 @@
|
||||
import postgres from 'postgres'
|
||||
import { drizzle } from 'drizzle-orm/postgres-js'
|
||||
|
||||
import { env } from '@household/config'
|
||||
export interface DbClientOptions {
|
||||
max?: number
|
||||
prepare?: boolean
|
||||
}
|
||||
|
||||
const queryClient = postgres(env.DATABASE_URL, {
|
||||
prepare: false,
|
||||
max: 5
|
||||
})
|
||||
export function createDbClient(databaseUrl: string, options: DbClientOptions = {}) {
|
||||
const queryClient = postgres(databaseUrl, {
|
||||
max: options.max ?? 5,
|
||||
prepare: options.prepare ?? false
|
||||
})
|
||||
|
||||
export const db = drizzle(queryClient)
|
||||
export { queryClient }
|
||||
const db = drizzle(queryClient)
|
||||
|
||||
return {
|
||||
db,
|
||||
queryClient
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,2 +1,2 @@
|
||||
export { db, queryClient } from './client'
|
||||
export { createDbClient } from './client'
|
||||
export * as schema from './schema'
|
||||
|
||||
@@ -180,6 +180,45 @@ export const purchaseEntries = pgTable(
|
||||
})
|
||||
)
|
||||
|
||||
export const purchaseMessages = pgTable(
|
||||
'purchase_messages',
|
||||
{
|
||||
id: uuid('id').defaultRandom().primaryKey(),
|
||||
householdId: uuid('household_id')
|
||||
.notNull()
|
||||
.references(() => households.id, { onDelete: 'cascade' }),
|
||||
senderMemberId: uuid('sender_member_id').references(() => members.id, {
|
||||
onDelete: 'set null'
|
||||
}),
|
||||
senderTelegramUserId: text('sender_telegram_user_id').notNull(),
|
||||
senderDisplayName: text('sender_display_name'),
|
||||
rawText: text('raw_text').notNull(),
|
||||
telegramChatId: text('telegram_chat_id').notNull(),
|
||||
telegramMessageId: text('telegram_message_id').notNull(),
|
||||
telegramThreadId: text('telegram_thread_id').notNull(),
|
||||
telegramUpdateId: text('telegram_update_id').notNull(),
|
||||
messageSentAt: timestamp('message_sent_at', { withTimezone: true }),
|
||||
processingStatus: text('processing_status').default('pending').notNull(),
|
||||
ingestedAt: timestamp('ingested_at', { withTimezone: true }).defaultNow().notNull()
|
||||
},
|
||||
(table) => ({
|
||||
householdThreadIdx: index('purchase_messages_household_thread_idx').on(
|
||||
table.householdId,
|
||||
table.telegramThreadId
|
||||
),
|
||||
senderIdx: index('purchase_messages_sender_idx').on(table.senderTelegramUserId),
|
||||
tgMessageUnique: uniqueIndex('purchase_messages_household_tg_message_unique').on(
|
||||
table.householdId,
|
||||
table.telegramChatId,
|
||||
table.telegramMessageId
|
||||
),
|
||||
tgUpdateUnique: uniqueIndex('purchase_messages_household_tg_update_unique').on(
|
||||
table.householdId,
|
||||
table.telegramUpdateId
|
||||
)
|
||||
})
|
||||
)
|
||||
|
||||
export const processedBotMessages = pgTable(
|
||||
'processed_bot_messages',
|
||||
{
|
||||
@@ -261,4 +300,5 @@ export type Member = typeof members.$inferSelect
|
||||
export type BillingCycle = typeof billingCycles.$inferSelect
|
||||
export type UtilityBill = typeof utilityBills.$inferSelect
|
||||
export type PurchaseEntry = typeof purchaseEntries.$inferSelect
|
||||
export type PurchaseMessage = typeof purchaseMessages.$inferSelect
|
||||
export type Settlement = typeof settlements.$inferSelect
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
import { and, eq } from 'drizzle-orm'
|
||||
import { drizzle } from 'drizzle-orm/postgres-js'
|
||||
import postgres from 'postgres'
|
||||
|
||||
import { createDbClient } from './client'
|
||||
import {
|
||||
billingCycles,
|
||||
households,
|
||||
@@ -20,13 +18,11 @@ if (!databaseUrl) {
|
||||
throw new Error('DATABASE_URL is required for db seed')
|
||||
}
|
||||
|
||||
const queryClient = postgres(databaseUrl, {
|
||||
prepare: false,
|
||||
max: 2
|
||||
const { db, queryClient } = createDbClient(databaseUrl, {
|
||||
max: 2,
|
||||
prepare: false
|
||||
})
|
||||
|
||||
const db = drizzle(queryClient)
|
||||
|
||||
const FIXTURE_IDS = {
|
||||
household: '11111111-1111-4111-8111-111111111111',
|
||||
cycle: '22222222-2222-4222-8222-222222222222',
|
||||
|
||||
Reference in New Issue
Block a user