Skip to content

Open-source status page, incident response and uptime monitoring

2.7 KB
Raw Blame
import { relations, sql } from "drizzle-orm";
import {
  index,
  integer,
  primaryKey,
  sqliteTable,
  text,
  uniqueIndex,
} from "drizzle-orm/sqlite-core";

import { monitor } from "../monitors";
import { workspace } from "../workspaces";
import { notificationProvider } from "./constants";

export const notification = sqliteTable(
  "notification",
  {
    id: integer("id").primaryKey(),
    name: text("name").notNull(),
    provider: text("provider", { enum: notificationProvider }).notNull(),
    data: text("data").default("{}"),
    workspaceId: integer("workspace_id").references(() => workspace.id),
    createdAt: integer("created_at", { mode: "timestamp" }).default(
      sql`(strftime('%s', 'now'))`,
    ),
    updatedAt: integer("updated_at", { mode: "timestamp" }).default(
      sql`(strftime('%s', 'now'))`,
    ),
  },
  (t) => [index("notification_workspace_id_idx").on(t.workspaceId)],
);

export const notificationTrigger = sqliteTable(
  "notification_trigger",
  {
    id: integer("id").primaryKey(),
    monitorId: integer("monitor_id").references(() => monitor.id, {
      onDelete: "cascade",
    }),
    notificationId: integer("notification_id").references(
      () => notification.id,
      { onDelete: "cascade" },
    ),
    cronTimestamp: integer("cron_timestamp").notNull(),
  },
  (table) => [
    uniqueIndex("notification_id_monitor_id_crontimestampe").on(
      table.notificationId,
      table.monitorId,
      table.cronTimestamp,
    ),
  ],
);

export const notificationsToMonitors = sqliteTable(
  "notifications_to_monitors",
  {
    monitorId: integer("monitor_id")
      .notNull()
      .references(() => monitor.id, { onDelete: "cascade" }),
    notificationId: integer("notification_id")
      .notNull()
      .references(() => notification.id, { onDelete: "cascade" }),
    createdAt: integer("created_at", { mode: "timestamp" }).default(
      sql`(strftime('%s', 'now'))`,
    ),
  },
  (t) => [
    primaryKey({ columns: [t.monitorId, t.notificationId] }),
    index("notifications_to_monitors_notification_id_idx").on(t.notificationId),
  ],
);

export const notificationsToMonitorsRelation = relations(
  notificationsToMonitors,
  ({ one }) => ({
    monitor: one(monitor, {
      fields: [notificationsToMonitors.monitorId],
      references: [monitor.id],
    }),
    notification: one(notification, {
      fields: [notificationsToMonitors.notificationId],
      references: [notification.id],
    }),
  }),
);

export const notificationRelations = relations(
  notification,
  ({ one, many }) => ({
    workspace: one(workspace, {
      fields: [notification.workspaceId],
      references: [workspace.id],
    }),
    monitor: many(notificationsToMonitors),
  }),
);