Openstatus
www.openstatus.dev
1import { relations, sql } from "drizzle-orm";
2import {
3 integer,
4 primaryKey,
5 sqliteTable,
6 text,
7 uniqueIndex,
8} from "drizzle-orm/sqlite-core";
9
10import { monitor } from "../monitors";
11import { workspace } from "../workspaces";
12import { notificationProvider } from "./constants";
13
14export const notification = sqliteTable("notification", {
15 id: integer("id").primaryKey(),
16 name: text("name").notNull(),
17 provider: text("provider", { enum: notificationProvider }).notNull(),
18 data: text("data").default("{}"),
19 workspaceId: integer("workspace_id").references(() => workspace.id),
20 createdAt: integer("created_at", { mode: "timestamp" }).default(
21 sql`(strftime('%s', 'now'))`,
22 ),
23 updatedAt: integer("updated_at", { mode: "timestamp" }).default(
24 sql`(strftime('%s', 'now'))`,
25 ),
26});
27
28export const notificationTrigger = sqliteTable(
29 "notification_trigger",
30 {
31 id: integer("id").primaryKey(),
32 monitorId: integer("monitor_id").references(() => monitor.id, {
33 onDelete: "cascade",
34 }),
35 notificationId: integer("notification_id").references(
36 () => notification.id,
37 { onDelete: "cascade" },
38 ),
39 cronTimestamp: integer("cron_timestamp").notNull(),
40 },
41 (table) => ({
42 unique: uniqueIndex("notification_id_monitor_id_crontimestampe").on(
43 table.notificationId,
44 table.monitorId,
45 table.cronTimestamp,
46 ),
47 }),
48);
49
50export const notificationsToMonitors = sqliteTable(
51 "notifications_to_monitors",
52 {
53 monitorId: integer("monitor_id")
54 .notNull()
55 .references(() => monitor.id, { onDelete: "cascade" }),
56 notificationId: integer("notification_id")
57 .notNull()
58 .references(() => notification.id, { onDelete: "cascade" }),
59 createdAt: integer("created_at", { mode: "timestamp" }).default(
60 sql`(strftime('%s', 'now'))`,
61 ),
62 },
63 (t) => ({
64 pk: primaryKey({ columns: [t.monitorId, t.notificationId] }),
65 }),
66);
67
68export const notificationsToMonitorsRelation = relations(
69 notificationsToMonitors,
70 ({ one }) => ({
71 monitor: one(monitor, {
72 fields: [notificationsToMonitors.monitorId],
73 references: [monitor.id],
74 }),
75 notification: one(notification, {
76 fields: [notificationsToMonitors.notificationId],
77 references: [notification.id],
78 }),
79 }),
80);
81
82export const notificationRelations = relations(
83 notification,
84 ({ one, many }) => ({
85 workspace: one(workspace, {
86 fields: [notification.workspaceId],
87 references: [workspace.id],
88 }),
89 monitor: many(notificationsToMonitors),
90 }),
91);