feat(16-01): migrate schema to pgTable and add users table with userId columns
- Rewrite schema.ts from sqlite-core to pg-core (pgTable, serial, timestamp, doublePrecision) - Add users table with id, logtoSub (unique), createdAt - Add userId FK column to items, categories, threads, setups, apiKeys, oauthTokens - Add composite unique constraint on categories(userId, name) - Change settings PK to composite (userId, key) - Remove global Uncategorized seed from seed.ts (now per-user lazy) - Generate Drizzle pg migration
This commit is contained in:
193
src/db/schema.ts
193
src/db/schema.ts
@@ -1,58 +1,86 @@
|
||||
import { integer, real, sqliteTable, text } from "drizzle-orm/sqlite-core";
|
||||
import {
|
||||
doublePrecision,
|
||||
integer,
|
||||
pgTable,
|
||||
primaryKey,
|
||||
serial,
|
||||
text,
|
||||
timestamp,
|
||||
unique,
|
||||
} from "drizzle-orm/pg-core";
|
||||
|
||||
export const categories = sqliteTable("categories", {
|
||||
id: integer("id").primaryKey({ autoIncrement: true }),
|
||||
name: text("name").notNull().unique(),
|
||||
icon: text("icon").notNull().default("package"),
|
||||
createdAt: integer("created_at", { mode: "timestamp" })
|
||||
.notNull()
|
||||
.$defaultFn(() => new Date()),
|
||||
// ── Users ───────────────────────────────────────────────────────────
|
||||
|
||||
export const users = pgTable("users", {
|
||||
id: serial("id").primaryKey(),
|
||||
logtoSub: text("logto_sub").notNull().unique(),
|
||||
createdAt: timestamp("created_at").defaultNow().notNull(),
|
||||
});
|
||||
|
||||
export const items = sqliteTable("items", {
|
||||
id: integer("id").primaryKey({ autoIncrement: true }),
|
||||
// ── Categories ──────────────────────────────────────────────────────
|
||||
|
||||
export const categories = pgTable(
|
||||
"categories",
|
||||
{
|
||||
id: serial("id").primaryKey(),
|
||||
name: text("name").notNull(),
|
||||
icon: text("icon").notNull().default("package"),
|
||||
userId: integer("user_id")
|
||||
.notNull()
|
||||
.references(() => users.id),
|
||||
createdAt: timestamp("created_at").defaultNow().notNull(),
|
||||
},
|
||||
(table) => [unique().on(table.userId, table.name)],
|
||||
);
|
||||
|
||||
// ── Items ───────────────────────────────────────────────────────────
|
||||
|
||||
export const items = pgTable("items", {
|
||||
id: serial("id").primaryKey(),
|
||||
name: text("name").notNull(),
|
||||
weightGrams: real("weight_grams"),
|
||||
weightGrams: doublePrecision("weight_grams"),
|
||||
priceCents: integer("price_cents"),
|
||||
categoryId: integer("category_id")
|
||||
.notNull()
|
||||
.references(() => categories.id),
|
||||
userId: integer("user_id")
|
||||
.notNull()
|
||||
.references(() => users.id),
|
||||
notes: text("notes"),
|
||||
productUrl: text("product_url"),
|
||||
imageFilename: text("image_filename"),
|
||||
imageSourceUrl: text("image_source_url"),
|
||||
quantity: integer("quantity").notNull().default(1),
|
||||
createdAt: integer("created_at", { mode: "timestamp" })
|
||||
.notNull()
|
||||
.$defaultFn(() => new Date()),
|
||||
updatedAt: integer("updated_at", { mode: "timestamp" })
|
||||
.notNull()
|
||||
.$defaultFn(() => new Date()),
|
||||
createdAt: timestamp("created_at").defaultNow().notNull(),
|
||||
updatedAt: timestamp("updated_at").defaultNow().notNull(),
|
||||
});
|
||||
|
||||
export const threads = sqliteTable("threads", {
|
||||
id: integer("id").primaryKey({ autoIncrement: true }),
|
||||
// ── Threads ─────────────────────────────────────────────────────────
|
||||
|
||||
export const threads = pgTable("threads", {
|
||||
id: serial("id").primaryKey(),
|
||||
name: text("name").notNull(),
|
||||
status: text("status").notNull().default("active"),
|
||||
resolvedCandidateId: integer("resolved_candidate_id"),
|
||||
categoryId: integer("category_id")
|
||||
.notNull()
|
||||
.references(() => categories.id),
|
||||
createdAt: integer("created_at", { mode: "timestamp" })
|
||||
userId: integer("user_id")
|
||||
.notNull()
|
||||
.$defaultFn(() => new Date()),
|
||||
updatedAt: integer("updated_at", { mode: "timestamp" })
|
||||
.notNull()
|
||||
.$defaultFn(() => new Date()),
|
||||
.references(() => users.id),
|
||||
createdAt: timestamp("created_at").defaultNow().notNull(),
|
||||
updatedAt: timestamp("updated_at").defaultNow().notNull(),
|
||||
});
|
||||
|
||||
export const threadCandidates = sqliteTable("thread_candidates", {
|
||||
id: integer("id").primaryKey({ autoIncrement: true }),
|
||||
// ── Thread Candidates ───────────────────────────────────────────────
|
||||
|
||||
export const threadCandidates = pgTable("thread_candidates", {
|
||||
id: serial("id").primaryKey(),
|
||||
threadId: integer("thread_id")
|
||||
.notNull()
|
||||
.references(() => threads.id, { onDelete: "cascade" }),
|
||||
name: text("name").notNull(),
|
||||
weightGrams: real("weight_grams"),
|
||||
weightGrams: doublePrecision("weight_grams"),
|
||||
priceCents: integer("price_cents"),
|
||||
categoryId: integer("category_id")
|
||||
.notNull()
|
||||
@@ -64,28 +92,27 @@ export const threadCandidates = sqliteTable("thread_candidates", {
|
||||
status: text("status").notNull().default("researching"),
|
||||
pros: text("pros"),
|
||||
cons: text("cons"),
|
||||
sortOrder: real("sort_order").notNull().default(0),
|
||||
createdAt: integer("created_at", { mode: "timestamp" })
|
||||
.notNull()
|
||||
.$defaultFn(() => new Date()),
|
||||
updatedAt: integer("updated_at", { mode: "timestamp" })
|
||||
.notNull()
|
||||
.$defaultFn(() => new Date()),
|
||||
sortOrder: doublePrecision("sort_order").notNull().default(0),
|
||||
createdAt: timestamp("created_at").defaultNow().notNull(),
|
||||
updatedAt: timestamp("updated_at").defaultNow().notNull(),
|
||||
});
|
||||
|
||||
export const setups = sqliteTable("setups", {
|
||||
id: integer("id").primaryKey({ autoIncrement: true }),
|
||||
// ── Setups ──────────────────────────────────────────────────────────
|
||||
|
||||
export const setups = pgTable("setups", {
|
||||
id: serial("id").primaryKey(),
|
||||
name: text("name").notNull(),
|
||||
createdAt: integer("created_at", { mode: "timestamp" })
|
||||
userId: integer("user_id")
|
||||
.notNull()
|
||||
.$defaultFn(() => new Date()),
|
||||
updatedAt: integer("updated_at", { mode: "timestamp" })
|
||||
.notNull()
|
||||
.$defaultFn(() => new Date()),
|
||||
.references(() => users.id),
|
||||
createdAt: timestamp("created_at").defaultNow().notNull(),
|
||||
updatedAt: timestamp("updated_at").defaultNow().notNull(),
|
||||
});
|
||||
|
||||
export const setupItems = sqliteTable("setup_items", {
|
||||
id: integer("id").primaryKey({ autoIncrement: true }),
|
||||
// ── Setup Items ─────────────────────────────────────────────────────
|
||||
|
||||
export const setupItems = pgTable("setup_items", {
|
||||
id: serial("id").primaryKey(),
|
||||
setupId: integer("setup_id")
|
||||
.notNull()
|
||||
.references(() => setups.id, { onDelete: "cascade" }),
|
||||
@@ -95,69 +122,69 @@ export const setupItems = sqliteTable("setup_items", {
|
||||
classification: text("classification").notNull().default("base"),
|
||||
});
|
||||
|
||||
export const settings = sqliteTable("settings", {
|
||||
key: text("key").primaryKey(),
|
||||
value: text("value").notNull(),
|
||||
});
|
||||
// ── Settings ────────────────────────────────────────────────────────
|
||||
|
||||
export const users = sqliteTable("users", {
|
||||
id: integer("id").primaryKey({ autoIncrement: true }),
|
||||
username: text("username").notNull().unique(),
|
||||
passwordHash: text("password_hash").notNull(),
|
||||
createdAt: integer("created_at", { mode: "timestamp" })
|
||||
.notNull()
|
||||
.$defaultFn(() => new Date()),
|
||||
});
|
||||
export const settings = pgTable(
|
||||
"settings",
|
||||
{
|
||||
userId: integer("user_id")
|
||||
.notNull()
|
||||
.references(() => users.id),
|
||||
key: text("key").notNull(),
|
||||
value: text("value").notNull(),
|
||||
},
|
||||
(table) => [primaryKey({ columns: [table.userId, table.key] })],
|
||||
);
|
||||
|
||||
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(),
|
||||
});
|
||||
// ── API Keys ────────────────────────────────────────────────────────
|
||||
|
||||
export const apiKeys = sqliteTable("api_keys", {
|
||||
id: integer("id").primaryKey({ autoIncrement: true }),
|
||||
export const apiKeys = pgTable("api_keys", {
|
||||
id: serial("id").primaryKey(),
|
||||
name: text("name").notNull(),
|
||||
keyHash: text("key_hash").notNull(),
|
||||
keyPrefix: text("key_prefix").notNull(),
|
||||
createdAt: integer("created_at", { mode: "timestamp" })
|
||||
userId: integer("user_id")
|
||||
.notNull()
|
||||
.$defaultFn(() => new Date()),
|
||||
.references(() => users.id),
|
||||
createdAt: timestamp("created_at").defaultNow().notNull(),
|
||||
});
|
||||
|
||||
export const oauthClients = sqliteTable("oauth_clients", {
|
||||
id: integer("id").primaryKey({ autoIncrement: true }),
|
||||
// ── OAuth Clients ───────────────────────────────────────────────────
|
||||
|
||||
export const oauthClients = pgTable("oauth_clients", {
|
||||
id: serial("id").primaryKey(),
|
||||
clientId: text("client_id").notNull().unique(),
|
||||
clientName: text("client_name"),
|
||||
redirectUris: text("redirect_uris").notNull(), // JSON array
|
||||
createdAt: integer("created_at", { mode: "timestamp" })
|
||||
.notNull()
|
||||
.$defaultFn(() => new Date()),
|
||||
createdAt: timestamp("created_at").defaultNow().notNull(),
|
||||
});
|
||||
|
||||
export const oauthCodes = sqliteTable("oauth_codes", {
|
||||
id: integer("id").primaryKey({ autoIncrement: true }),
|
||||
// ── OAuth Authorization Codes ───────────────────────────────────────
|
||||
|
||||
export const oauthCodes = pgTable("oauth_codes", {
|
||||
id: serial("id").primaryKey(),
|
||||
code: text("code").notNull().unique(),
|
||||
clientId: text("client_id").notNull(),
|
||||
codeChallenge: text("code_challenge").notNull(),
|
||||
codeChallengeMethod: text("code_challenge_method").notNull().default("S256"),
|
||||
codeChallengeMethod: text("code_challenge_method")
|
||||
.notNull()
|
||||
.default("S256"),
|
||||
redirectUri: text("redirect_uri").notNull(),
|
||||
expiresAt: integer("expires_at", { mode: "timestamp" }).notNull(),
|
||||
expiresAt: timestamp("expires_at").notNull(),
|
||||
used: integer("used").notNull().default(0),
|
||||
});
|
||||
|
||||
export const oauthTokens = sqliteTable("oauth_tokens", {
|
||||
id: integer("id").primaryKey({ autoIncrement: true }),
|
||||
// ── OAuth Tokens ────────────────────────────────────────────────────
|
||||
|
||||
export const oauthTokens = pgTable("oauth_tokens", {
|
||||
id: serial("id").primaryKey(),
|
||||
accessTokenHash: text("access_token_hash").notNull().unique(),
|
||||
refreshTokenHash: text("refresh_token_hash").notNull().unique(),
|
||||
clientId: text("client_id").notNull(),
|
||||
expiresAt: integer("expires_at", { mode: "timestamp" }).notNull(), // access token expiry
|
||||
refreshExpiresAt: integer("refresh_expires_at", {
|
||||
mode: "timestamp",
|
||||
}).notNull(), // refresh token expiry
|
||||
createdAt: integer("created_at", { mode: "timestamp" })
|
||||
userId: integer("user_id")
|
||||
.notNull()
|
||||
.$defaultFn(() => new Date()),
|
||||
.references(() => users.id),
|
||||
expiresAt: timestamp("expires_at").notNull(),
|
||||
refreshExpiresAt: timestamp("refresh_expires_at").notNull(),
|
||||
createdAt: timestamp("created_at").defaultNow().notNull(),
|
||||
});
|
||||
|
||||
@@ -1,14 +1,4 @@
|
||||
import { db } from "./index.ts";
|
||||
import { categories } from "./schema.ts";
|
||||
|
||||
export function seedDefaults() {
|
||||
const existing = db.select().from(categories).all();
|
||||
if (existing.length === 0) {
|
||||
db.insert(categories)
|
||||
.values({
|
||||
name: "Uncategorized",
|
||||
icon: "package",
|
||||
})
|
||||
.run();
|
||||
}
|
||||
// Per-user default categories are created on first login (Phase 16)
|
||||
// The getOrCreateUncategorized helper in category.service.ts handles this lazily.
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user