import { integer, sqliteTable, text, uniqueIndex, type AnySQLiteColumn } from "drizzle-orm/sqlite-core"; export const users = sqliteTable("users", { id: integer("id").primaryKey({ autoIncrement: true }), // Nullable: a user signs in with GitHub, AT Protocol, or both — not // required to have every identity. githubId: integer("github_id").unique(), atprotoDid: text("atproto_did").unique(), username: text("username").notNull(), avatarUrl: text("avatar_url"), createdAt: integer("created_at", { mode: "timestamp" }) .notNull() .$defaultFn(() => new Date()), }); export const sessions = sqliteTable("sessions", { id: text("id").primaryKey(), userId: integer("user_id") .notNull() .references(() => users.id, { onDelete: "cascade" }), expiresAt: integer("expires_at", { mode: "timestamp" }).notNull(), }); // One instance hosts many spools — each owned by a user, like GitHub repos. // A spool is the unit of community: it has its own threads and its own issues. export const spools = sqliteTable("spools", { id: integer("id").primaryKey({ autoIncrement: true }), slug: text("slug").notNull().unique(), name: text("name").notNull(), description: text("description"), repoUrl: text("repo_url"), // Stable AT-URI of the sh.tangled.repo record this spool was synced from // (at://did/sh.tangled.repo/rkey). Null for manually-created spools. tangledUri: text("tangled_uri").unique(), ownerId: integer("owner_id") .notNull() .references(() => users.id), // When true, only the owner and past contributors (anyone who has already // authored a thread message or issue here) may start new threads. threadsLocked: integer("threads_locked", { mode: "boolean" }).notNull().default(false), createdAt: integer("created_at", { mode: "timestamp" }) .notNull() .$defaultFn(() => new Date()), }); export const ISSUE_STATUSES = ["open", "in_progress", "resolved", "closed"] as const; export type IssueStatus = (typeof ISSUE_STATUSES)[number]; export const issues = sqliteTable( "issues", { id: integer("id").primaryKey({ autoIncrement: true }), spoolId: integer("spool_id") .notNull() .references(() => spools.id, { onDelete: "cascade" }), // Per-spool issue number (#1, #2… within each spool), GitHub-style. // `id` stays globally unique and is the API handle for mutations; // `number` is what people see and what URLs use. number: integer("number").notNull(), authorId: integer("author_id") .notNull() .references(() => users.id), title: text("title").notNull(), body: text("body").notNull().default(""), status: text("status", { enum: ISSUE_STATUSES }).notNull().default("open"), // Set when the issue was cut from a thread — the bridge between // the chat layer and the tracked layer. sourceMessageId: integer("source_message_id").references( (): AnySQLiteColumn => messages.id, { onDelete: "set null" } ), createdAt: integer("created_at", { mode: "timestamp" }) .notNull() .$defaultFn(() => new Date()), updatedAt: integer("updated_at", { mode: "timestamp" }) .notNull() .$defaultFn(() => new Date()), }, (t) => [uniqueIndex("issues_spool_number").on(t.spoolId, t.number)] ); export const comments = sqliteTable("comments", { id: integer("id").primaryKey({ autoIncrement: true }), issueId: integer("issue_id") .notNull() .references(() => issues.id, { onDelete: "cascade" }), authorId: integer("author_id") .notNull() .references(() => users.id), body: text("body").notNull(), createdAt: integer("created_at", { mode: "timestamp" }) .notNull() .$defaultFn(() => new Date()), }); // Threads: start one now, anyone can reply whenever. Replies reference // their root message (one level deep — a chat, not a forum tree). export const messages = sqliteTable("messages", { id: integer("id").primaryKey({ autoIncrement: true }), spoolId: integer("spool_id") .notNull() .references(() => spools.id, { onDelete: "cascade" }), authorId: integer("author_id") .notNull() .references(() => users.id), parentId: integer("parent_id").references((): AnySQLiteColumn => messages.id, { onDelete: "cascade", }), body: text("body").notNull(), createdAt: integer("created_at", { mode: "timestamp" }) .notNull() .$defaultFn(() => new Date()), }); // Repos seen on this owner's Tangled account that haven't been accepted or // declined for syncing yet. Repopulated from the live repo list on every // login; a row disappearing here (without becoming a spool) just means the // repo was deleted or renamed upstream. export const tangledRepoPending = sqliteTable("tangled_repo_pending", { id: integer("id").primaryKey({ autoIncrement: true }), ownerId: integer("owner_id") .notNull() .references(() => users.id, { onDelete: "cascade" }), tangledUri: text("tangled_uri").notNull().unique(), name: text("name").notNull(), description: text("description"), repoUrl: text("repo_url").notNull(), }); // Repos the owner explicitly chose not to sync. Kept so we don't re-ask // about the same repo on every future login. export const tangledRepoDeclined = sqliteTable("tangled_repo_declined", { id: integer("id").primaryKey({ autoIncrement: true }), ownerId: integer("owner_id") .notNull() .references(() => users.id, { onDelete: "cascade" }), tangledUri: text("tangled_uri").notNull().unique(), declinedAt: integer("declined_at", { mode: "timestamp" }) .notNull() .$defaultFn(() => new Date()), }); // Storage for @atproto/oauth-client-node's required state/session stores. // Backed by SQLite (not an in-memory Map) so an in-flight login survives a // `tsx watch` restart. export const atprotoOauthState = sqliteTable("atproto_oauth_state", { key: text("key").primaryKey(), value: text("value").notNull(), // JSON-serialized NodeSavedState }); export const atprotoOauthSession = sqliteTable("atproto_oauth_session", { did: text("did").primaryKey(), value: text("value").notNull(), // JSON-serialized NodeSavedSession }); export type User = typeof users.$inferSelect; export type Spool = typeof spools.$inferSelect; export type Issue = typeof issues.$inferSelect; export type Comment = typeof comments.$inferSelect; export type Message = typeof messages.$inferSelect;