import { relations } from "drizzle-orm"; import { boolean, index, pgTable, timestamp, uuid } from "drizzle-orm/pg-core"; import { notificationTypeEnum } from "./enums"; import { comments } from "./comments"; import { events } from "./events"; import { profiles } from "./profiles"; export const notifications = pgTable( "notifications", { id: uuid("id").primaryKey().defaultRandom(), profileId: uuid("profile_id") .notNull() .references(() => profiles.id), type: notificationTypeEnum("type").notNull(), actorId: uuid("actor_id") .notNull() .references(() => profiles.id), commentId: uuid("comment_id") .notNull() .references(() => comments.id), eventId: uuid("event_id") .notNull() .references(() => events.id), read: boolean("read").notNull().default(false), createdAt: timestamp("created_at", { withTimezone: true }).notNull().defaultNow(), }, (t) => [ index("notifications_profile_id_idx").on(t.profileId), index("notifications_profile_read_idx").on(t.profileId, t.read), ], ); export const notificationsRelations = relations(notifications, ({ one }) => ({ profile: one(profiles, { fields: [notifications.profileId], references: [profiles.id], relationName: "receivedNotifications" }), actor: one(profiles, { fields: [notifications.actorId], references: [profiles.id], relationName: "sentNotifications" }), comment: one(comments, { fields: [notifications.commentId], references: [comments.id] }), event: one(events, { fields: [notifications.eventId], references: [events.id] }), }));