import { pgTable, varchar, text, timestamp, index } from 'drizzle-orm/pg-core'; import { companies, users } from './tenancy'; import { notificationKindEnum, pk, fk, createdAt } from './_shared'; export const notifications = pgTable( 'notifications', { id: pk(), userId: fk('user_id') .notNull() .references(() => users.id, { onDelete: 'cascade' }), companyId: fk('company_id') .notNull() .references(() => companies.id, { onDelete: 'cascade' }), kind: notificationKindEnum('kind').notNull(), title: varchar('title', { length: 255 }).notNull(), body: text('body').notNull(), // Absolute or relative URL back into the app (e.g. /assets/). link: varchar('link', { length: 1024 }), readAt: timestamp('read_at', { withTimezone: true }), createdAt: createdAt() }, (t) => ({ // Unread-list query: user + read_at IS NULL + ordered by created_at desc. byUserUnread: index('notifications_by_user_unread').on(t.userId, t.readAt, t.createdAt), byUserCompany: index('notifications_by_user_company').on(t.userId, t.companyId, t.createdAt) }) ); export type Notification = typeof notifications.$inferSelect; export type NewNotification = typeof notifications.$inferInsert;