Files
TX/lib/db/src/schema/notes.ts
T

84 lines
4.5 KiB
TypeScript
Raw Normal View History

import { pgTable, text, serial, timestamp, integer, varchar, boolean, primaryKey, uniqueIndex } from "drizzle-orm/pg-core";
import { createInsertSchema } from "drizzle-zod";
import { z } from "zod/v4";
import { usersTable } from "./users";
export const notesTable = pgTable("notes", {
id: serial("id").primaryKey(),
userId: integer("user_id").notNull().references(() => usersTable.id, { onDelete: "cascade" }),
title: varchar("title", { length: 300 }).notNull().default(""),
content: text("content").notNull().default(""),
color: varchar("color", { length: 32 }).notNull().default("default"),
isPinned: boolean("is_pinned").notNull().default(false),
isArchived: boolean("is_archived").notNull().default(false),
createdAt: timestamp("created_at", { withTimezone: true }).notNull().defaultNow(),
updatedAt: timestamp("updated_at", { withTimezone: true }).notNull().defaultNow().$onUpdate(() => new Date()),
});
export const noteLabelsTable = pgTable("note_labels", {
id: serial("id").primaryKey(),
userId: integer("user_id").notNull().references(() => usersTable.id, { onDelete: "cascade" }),
name: varchar("name", { length: 80 }).notNull(),
createdAt: timestamp("created_at", { withTimezone: true }).notNull().defaultNow(),
}, (t) => ({
userNameUnique: uniqueIndex("note_labels_user_name_unique").on(t.userId, t.name),
}));
export const noteLabelAssignmentsTable = pgTable("note_label_assignments", {
noteId: integer("note_id").notNull().references(() => notesTable.id, { onDelete: "cascade" }),
labelId: integer("label_id").notNull().references(() => noteLabelsTable.id, { onDelete: "cascade" }),
}, (t) => ({
pk: primaryKey({ columns: [t.noteId, t.labelId] }),
}));
/**
* Per-recipient delivery row for a note that was sent from one user
* (`senderUserId` / the owning `notes.user_id`) to another
* (`recipientUserId`). One row is created per recipient at send time.
*
* Status lifecycle: `unread` -> `read` (recipient opens) -> `replied`
* (recipient sends a reply). `archived` is recipient-side only and does
* not affect the sender's view.
*/
export const noteRecipientsTable = pgTable("note_recipients", {
id: serial("id").primaryKey(),
// The original sender-side note id at send time. Stored as a plain integer
// (no FK) so the recipient row — and the thread it anchors — survives the
// sender deleting their own note. Together with the snapshot fields below
// this guarantees recipient copies are fully independent of sender edits.
noteId: integer("note_id").notNull(),
senderUserId: integer("sender_user_id").notNull().references(() => usersTable.id, { onDelete: "cascade" }),
recipientUserId: integer("recipient_user_id").notNull().references(() => usersTable.id, { onDelete: "cascade" }),
// Immutable snapshot of the note as it was at send time. Sender edits to the
// original note do NOT propagate to delivered copies.
title: varchar("title", { length: 300 }).notNull().default(""),
content: text("content").notNull().default(""),
color: varchar("color", { length: 32 }).notNull().default("default"),
status: varchar("status", { length: 16 }).notNull().default("unread"),
sentAt: timestamp("sent_at", { withTimezone: true }).notNull().defaultNow(),
readAt: timestamp("read_at", { withTimezone: true }),
archivedAt: timestamp("archived_at", { withTimezone: true }),
}, (t) => ({
noteRecipientUnique: uniqueIndex("note_recipients_note_recipient_unique").on(t.noteId, t.recipientUserId),
}));
export const noteRepliesTable = pgTable("note_replies", {
id: serial("id").primaryKey(),
noteId: integer("note_id").notNull(),
senderUserId: integer("sender_user_id").notNull().references(() => usersTable.id, { onDelete: "cascade" }),
recipientUserId: integer("recipient_user_id").notNull().references(() => usersTable.id, { onDelete: "cascade" }),
content: text("content").notNull(),
createdAt: timestamp("created_at", { withTimezone: true }).notNull().defaultNow(),
});
export const insertNoteSchema = createInsertSchema(notesTable).omit({ id: true, createdAt: true, updatedAt: true });
export type InsertNote = z.infer<typeof insertNoteSchema>;
export type Note = typeof notesTable.$inferSelect;
export const insertNoteLabelSchema = createInsertSchema(noteLabelsTable).omit({ id: true, createdAt: true });
export type InsertNoteLabel = z.infer<typeof insertNoteLabelSchema>;
export type NoteLabel = typeof noteLabelsTable.$inferSelect;
export type NoteRecipient = typeof noteRecipientsTable.$inferSelect;
export type NoteReply = typeof noteRepliesTable.$inferSelect;