first commit
This commit is contained in:
203
backend/prisma/migrations/20260119102154_init/migration.sql
Normal file
203
backend/prisma/migrations/20260119102154_init/migration.sql
Normal file
@@ -0,0 +1,203 @@
|
||||
-- CreateEnum
|
||||
CREATE TYPE "RecurrenceType" AS ENUM ('hourly', 'daily', 'weekly', 'monthly', 'yearly');
|
||||
|
||||
-- CreateEnum
|
||||
CREATE TYPE "TargetType" AS ENUM ('todo', 'reminder_task');
|
||||
|
||||
-- CreateEnum
|
||||
CREATE TYPE "ChannelType" AS ENUM ('inapp', 'bark');
|
||||
|
||||
-- CreateEnum
|
||||
CREATE TYPE "NotificationStatus" AS ENUM ('pending', 'queued', 'sent', 'failed');
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE "User" (
|
||||
"id" TEXT NOT NULL,
|
||||
"username" TEXT NOT NULL,
|
||||
"passwordHash" TEXT NOT NULL,
|
||||
"timezone" TEXT NOT NULL DEFAULT 'Asia/Shanghai',
|
||||
"barkUrl" TEXT,
|
||||
"inappEnabled" BOOLEAN NOT NULL DEFAULT true,
|
||||
"barkEnabled" BOOLEAN NOT NULL DEFAULT false,
|
||||
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
"updatedAt" TIMESTAMP(3) NOT NULL,
|
||||
|
||||
CONSTRAINT "User_pkey" PRIMARY KEY ("id")
|
||||
);
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE "Invite" (
|
||||
"id" TEXT NOT NULL,
|
||||
"code" TEXT NOT NULL,
|
||||
"creatorId" TEXT NOT NULL,
|
||||
"maxUses" INTEGER NOT NULL DEFAULT 5,
|
||||
"usedCount" INTEGER NOT NULL DEFAULT 0,
|
||||
"expiresAt" TIMESTAMP(3) NOT NULL,
|
||||
"revokedAt" TIMESTAMP(3),
|
||||
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
|
||||
CONSTRAINT "Invite_pkey" PRIMARY KEY ("id")
|
||||
);
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE "RecurrenceRule" (
|
||||
"id" TEXT NOT NULL,
|
||||
"type" "RecurrenceType" NOT NULL,
|
||||
"interval" INTEGER NOT NULL DEFAULT 1,
|
||||
"byWeekday" INTEGER,
|
||||
"byMonthday" INTEGER,
|
||||
"timezone" TEXT NOT NULL DEFAULT 'Asia/Shanghai',
|
||||
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
"updatedAt" TIMESTAMP(3) NOT NULL,
|
||||
|
||||
CONSTRAINT "RecurrenceRule_pkey" PRIMARY KEY ("id")
|
||||
);
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE "Todo" (
|
||||
"id" TEXT NOT NULL,
|
||||
"ownerId" TEXT NOT NULL,
|
||||
"title" TEXT NOT NULL,
|
||||
"description" TEXT,
|
||||
"dueAt" TIMESTAMP(3) NOT NULL,
|
||||
"recurrenceRuleId" TEXT,
|
||||
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
"updatedAt" TIMESTAMP(3) NOT NULL,
|
||||
|
||||
CONSTRAINT "Todo_pkey" PRIMARY KEY ("id")
|
||||
);
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE "ReminderTask" (
|
||||
"id" TEXT NOT NULL,
|
||||
"creatorId" TEXT NOT NULL,
|
||||
"title" TEXT NOT NULL,
|
||||
"description" TEXT,
|
||||
"dueAt" TIMESTAMP(3) NOT NULL,
|
||||
"recurrenceRuleId" TEXT,
|
||||
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
"updatedAt" TIMESTAMP(3) NOT NULL,
|
||||
|
||||
CONSTRAINT "ReminderTask_pkey" PRIMARY KEY ("id")
|
||||
);
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE "ReminderTaskRecipient" (
|
||||
"taskId" TEXT NOT NULL,
|
||||
"userId" TEXT NOT NULL,
|
||||
|
||||
CONSTRAINT "ReminderTaskRecipient_pkey" PRIMARY KEY ("taskId","userId")
|
||||
);
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE "ReminderOffset" (
|
||||
"id" TEXT NOT NULL,
|
||||
"targetType" "TargetType" NOT NULL,
|
||||
"targetId" TEXT NOT NULL,
|
||||
"offsetMinutes" INTEGER NOT NULL,
|
||||
"channelInapp" BOOLEAN NOT NULL DEFAULT true,
|
||||
"channelBark" BOOLEAN NOT NULL DEFAULT false,
|
||||
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
|
||||
CONSTRAINT "ReminderOffset_pkey" PRIMARY KEY ("id")
|
||||
);
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE "Notification" (
|
||||
"id" TEXT NOT NULL,
|
||||
"recipientId" TEXT NOT NULL,
|
||||
"targetType" "TargetType" NOT NULL,
|
||||
"targetId" TEXT NOT NULL,
|
||||
"triggerAt" TIMESTAMP(3) NOT NULL,
|
||||
"channel" "ChannelType" NOT NULL,
|
||||
"status" "NotificationStatus" NOT NULL DEFAULT 'pending',
|
||||
"lockedAt" TIMESTAMP(3),
|
||||
"sentAt" TIMESTAMP(3),
|
||||
"readAt" TIMESTAMP(3),
|
||||
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
"updatedAt" TIMESTAMP(3) NOT NULL,
|
||||
|
||||
CONSTRAINT "Notification_pkey" PRIMARY KEY ("id")
|
||||
);
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE "DeliveryLog" (
|
||||
"id" TEXT NOT NULL,
|
||||
"notificationId" TEXT NOT NULL,
|
||||
"attemptNo" INTEGER NOT NULL,
|
||||
"channel" "ChannelType" NOT NULL,
|
||||
"status" "NotificationStatus" NOT NULL,
|
||||
"responseMeta" JSONB,
|
||||
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
|
||||
CONSTRAINT "DeliveryLog_pkey" PRIMARY KEY ("id")
|
||||
);
|
||||
|
||||
-- CreateIndex
|
||||
CREATE UNIQUE INDEX "User_username_key" ON "User"("username");
|
||||
|
||||
-- CreateIndex
|
||||
CREATE UNIQUE INDEX "Invite_code_key" ON "Invite"("code");
|
||||
|
||||
-- CreateIndex
|
||||
CREATE INDEX "Invite_creatorId_idx" ON "Invite"("creatorId");
|
||||
|
||||
-- CreateIndex
|
||||
CREATE INDEX "Invite_expiresAt_idx" ON "Invite"("expiresAt");
|
||||
|
||||
-- CreateIndex
|
||||
CREATE INDEX "Todo_ownerId_dueAt_idx" ON "Todo"("ownerId", "dueAt");
|
||||
|
||||
-- CreateIndex
|
||||
CREATE INDEX "Todo_recurrenceRuleId_idx" ON "Todo"("recurrenceRuleId");
|
||||
|
||||
-- CreateIndex
|
||||
CREATE INDEX "ReminderTask_creatorId_dueAt_idx" ON "ReminderTask"("creatorId", "dueAt");
|
||||
|
||||
-- CreateIndex
|
||||
CREATE INDEX "ReminderTask_recurrenceRuleId_idx" ON "ReminderTask"("recurrenceRuleId");
|
||||
|
||||
-- CreateIndex
|
||||
CREATE INDEX "ReminderTaskRecipient_userId_idx" ON "ReminderTaskRecipient"("userId");
|
||||
|
||||
-- CreateIndex
|
||||
CREATE INDEX "ReminderOffset_targetType_targetId_idx" ON "ReminderOffset"("targetType", "targetId");
|
||||
|
||||
-- CreateIndex
|
||||
CREATE INDEX "Notification_status_triggerAt_idx" ON "Notification"("status", "triggerAt");
|
||||
|
||||
-- CreateIndex
|
||||
CREATE INDEX "Notification_recipientId_readAt_idx" ON "Notification"("recipientId", "readAt");
|
||||
|
||||
-- CreateIndex
|
||||
CREATE UNIQUE INDEX "Notification_recipientId_targetType_targetId_triggerAt_chan_key" ON "Notification"("recipientId", "targetType", "targetId", "triggerAt", "channel");
|
||||
|
||||
-- CreateIndex
|
||||
CREATE INDEX "DeliveryLog_notificationId_idx" ON "DeliveryLog"("notificationId");
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE "Invite" ADD CONSTRAINT "Invite_creatorId_fkey" FOREIGN KEY ("creatorId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE "Todo" ADD CONSTRAINT "Todo_ownerId_fkey" FOREIGN KEY ("ownerId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE "Todo" ADD CONSTRAINT "Todo_recurrenceRuleId_fkey" FOREIGN KEY ("recurrenceRuleId") REFERENCES "RecurrenceRule"("id") ON DELETE SET NULL ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE "ReminderTask" ADD CONSTRAINT "ReminderTask_creatorId_fkey" FOREIGN KEY ("creatorId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE "ReminderTask" ADD CONSTRAINT "ReminderTask_recurrenceRuleId_fkey" FOREIGN KEY ("recurrenceRuleId") REFERENCES "RecurrenceRule"("id") ON DELETE SET NULL ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE "ReminderTaskRecipient" ADD CONSTRAINT "ReminderTaskRecipient_taskId_fkey" FOREIGN KEY ("taskId") REFERENCES "ReminderTask"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE "ReminderTaskRecipient" ADD CONSTRAINT "ReminderTaskRecipient_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE "Notification" ADD CONSTRAINT "Notification_recipientId_fkey" FOREIGN KEY ("recipientId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE "DeliveryLog" ADD CONSTRAINT "DeliveryLog_notificationId_fkey" FOREIGN KEY ("notificationId") REFERENCES "Notification"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
|
||||
@@ -0,0 +1,2 @@
|
||||
-- AlterTable
|
||||
ALTER TABLE "User" ADD COLUMN "avatar" TEXT;
|
||||
3
backend/prisma/migrations/migration_lock.toml
Normal file
3
backend/prisma/migrations/migration_lock.toml
Normal file
@@ -0,0 +1,3 @@
|
||||
# Please do not edit this file manually
|
||||
# It should be added in your version-control system (i.e. Git)
|
||||
provider = "postgresql"
|
||||
176
backend/prisma/schema.prisma
Normal file
176
backend/prisma/schema.prisma
Normal file
@@ -0,0 +1,176 @@
|
||||
generator client {
|
||||
provider = "prisma-client-js"
|
||||
}
|
||||
|
||||
datasource db {
|
||||
provider = "postgresql"
|
||||
url = env("DATABASE_URL")
|
||||
}
|
||||
|
||||
enum RecurrenceType {
|
||||
hourly
|
||||
daily
|
||||
weekly
|
||||
monthly
|
||||
yearly
|
||||
}
|
||||
|
||||
enum TargetType {
|
||||
todo
|
||||
reminder_task
|
||||
}
|
||||
|
||||
enum ChannelType {
|
||||
inapp
|
||||
bark
|
||||
}
|
||||
|
||||
enum NotificationStatus {
|
||||
pending
|
||||
queued
|
||||
sent
|
||||
failed
|
||||
}
|
||||
|
||||
model User {
|
||||
id String @id @default(cuid())
|
||||
username String @unique
|
||||
passwordHash String
|
||||
avatar String?
|
||||
timezone String @default("Asia/Shanghai")
|
||||
barkUrl String?
|
||||
inappEnabled Boolean @default(true)
|
||||
barkEnabled Boolean @default(false)
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
|
||||
invites Invite[] @relation("InviteCreator")
|
||||
todos Todo[] @relation("TodoOwner")
|
||||
createdTasks ReminderTask[] @relation("TaskCreator")
|
||||
taskRecipients ReminderTaskRecipient[]
|
||||
notifications Notification[] @relation("NotificationRecipient")
|
||||
}
|
||||
|
||||
model Invite {
|
||||
id String @id @default(cuid())
|
||||
code String @unique
|
||||
creatorId String
|
||||
maxUses Int @default(5)
|
||||
usedCount Int @default(0)
|
||||
expiresAt DateTime
|
||||
revokedAt DateTime?
|
||||
createdAt DateTime @default(now())
|
||||
|
||||
creator User @relation("InviteCreator", fields: [creatorId], references: [id])
|
||||
|
||||
@@index([creatorId])
|
||||
@@index([expiresAt])
|
||||
}
|
||||
|
||||
model RecurrenceRule {
|
||||
id String @id @default(cuid())
|
||||
type RecurrenceType
|
||||
interval Int @default(1)
|
||||
byWeekday Int?
|
||||
byMonthday Int?
|
||||
timezone String @default("Asia/Shanghai")
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
|
||||
todos Todo[]
|
||||
tasks ReminderTask[]
|
||||
}
|
||||
|
||||
model Todo {
|
||||
id String @id @default(cuid())
|
||||
ownerId String
|
||||
title String
|
||||
description String?
|
||||
dueAt DateTime
|
||||
recurrenceRuleId String?
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
|
||||
owner User @relation("TodoOwner", fields: [ownerId], references: [id])
|
||||
recurrenceRule RecurrenceRule? @relation(fields: [recurrenceRuleId], references: [id])
|
||||
|
||||
@@index([ownerId, dueAt])
|
||||
@@index([recurrenceRuleId])
|
||||
}
|
||||
|
||||
model ReminderTask {
|
||||
id String @id @default(cuid())
|
||||
creatorId String
|
||||
title String
|
||||
description String?
|
||||
dueAt DateTime
|
||||
recurrenceRuleId String?
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
|
||||
creator User @relation("TaskCreator", fields: [creatorId], references: [id])
|
||||
recurrenceRule RecurrenceRule? @relation(fields: [recurrenceRuleId], references: [id])
|
||||
recipients ReminderTaskRecipient[]
|
||||
|
||||
@@index([creatorId, dueAt])
|
||||
@@index([recurrenceRuleId])
|
||||
}
|
||||
|
||||
model ReminderTaskRecipient {
|
||||
taskId String
|
||||
userId String
|
||||
|
||||
task ReminderTask @relation(fields: [taskId], references: [id])
|
||||
user User @relation(fields: [userId], references: [id])
|
||||
|
||||
@@id([taskId, userId])
|
||||
@@index([userId])
|
||||
}
|
||||
|
||||
model ReminderOffset {
|
||||
id String @id @default(cuid())
|
||||
targetType TargetType
|
||||
targetId String
|
||||
offsetMinutes Int
|
||||
channelInapp Boolean @default(true)
|
||||
channelBark Boolean @default(false)
|
||||
createdAt DateTime @default(now())
|
||||
|
||||
@@index([targetType, targetId])
|
||||
}
|
||||
|
||||
model Notification {
|
||||
id String @id @default(cuid())
|
||||
recipientId String
|
||||
targetType TargetType
|
||||
targetId String
|
||||
triggerAt DateTime
|
||||
channel ChannelType
|
||||
status NotificationStatus @default(pending)
|
||||
lockedAt DateTime?
|
||||
sentAt DateTime?
|
||||
readAt DateTime?
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
|
||||
recipient User @relation("NotificationRecipient", fields: [recipientId], references: [id])
|
||||
logs DeliveryLog[]
|
||||
|
||||
@@unique([recipientId, targetType, targetId, triggerAt, channel])
|
||||
@@index([status, triggerAt])
|
||||
@@index([recipientId, readAt])
|
||||
}
|
||||
|
||||
model DeliveryLog {
|
||||
id String @id @default(cuid())
|
||||
notificationId String
|
||||
attemptNo Int
|
||||
channel ChannelType
|
||||
status NotificationStatus
|
||||
responseMeta Json?
|
||||
createdAt DateTime @default(now())
|
||||
|
||||
notification Notification @relation(fields: [notificationId], references: [id])
|
||||
|
||||
@@index([notificationId])
|
||||
}
|
||||
Reference in New Issue
Block a user