chore(18-03): apply 18-01 schema foundation as dependency baseline
This commit is contained in:
146
src/db/global-items-seed.json
Normal file
146
src/db/global-items-seed.json
Normal file
@@ -0,0 +1,146 @@
|
||||
[
|
||||
{
|
||||
"brand": "Revelate Designs",
|
||||
"model": "Terrapin System",
|
||||
"category": "bags",
|
||||
"weightGrams": 529,
|
||||
"priceCents": 18500,
|
||||
"description": "Waterproof saddle bag with 14L capacity, roll-top closure, and integrated Revelate seat bag mount."
|
||||
},
|
||||
{
|
||||
"brand": "Apidura",
|
||||
"model": "Expedition Handlebar Pack",
|
||||
"category": "bags",
|
||||
"weightGrams": 300,
|
||||
"priceCents": 16000,
|
||||
"description": "14L waterproof handlebar roll bag with internal dry bag and accessory pocket."
|
||||
},
|
||||
{
|
||||
"brand": "Ortlieb",
|
||||
"model": "Frame-Pack Toptube",
|
||||
"category": "bags",
|
||||
"weightGrams": 180,
|
||||
"priceCents": 7500,
|
||||
"description": "4L waterproof top-tube bag with magnetic closure and reflective details."
|
||||
},
|
||||
{
|
||||
"brand": "Revelate Designs",
|
||||
"model": "Tangle Frame Bag",
|
||||
"category": "bags",
|
||||
"weightGrams": 170,
|
||||
"priceCents": 13500,
|
||||
"description": "Full-frame bag with water-resistant construction and multiple internal pockets."
|
||||
},
|
||||
{
|
||||
"brand": "Big Agnes",
|
||||
"model": "Copper Spur HV UL1",
|
||||
"category": "shelters",
|
||||
"weightGrams": 879,
|
||||
"priceCents": 42000,
|
||||
"description": "Ultralight 1-person freestanding tent with high-volume hub design and DAC Featherlite poles."
|
||||
},
|
||||
{
|
||||
"brand": "Tarptent",
|
||||
"model": "Protrail Li",
|
||||
"category": "shelters",
|
||||
"weightGrams": 454,
|
||||
"priceCents": 35000,
|
||||
"description": "Ultralight single-wall trekking pole shelter in Dyneema composite fabric."
|
||||
},
|
||||
{
|
||||
"brand": "Outdoor Research",
|
||||
"model": "Helium Bivy",
|
||||
"category": "shelters",
|
||||
"weightGrams": 510,
|
||||
"priceCents": 24900,
|
||||
"description": "Waterproof breathable bivy sack with single-hoop pole and full-zip entry."
|
||||
},
|
||||
{
|
||||
"brand": "Sea to Summit",
|
||||
"model": "Spark SP1",
|
||||
"category": "sleep-systems",
|
||||
"weightGrams": 375,
|
||||
"priceCents": 28000,
|
||||
"description": "Ultralight 850+ fill down sleeping bag rated to 40F/4C with Ultra-Dry Down."
|
||||
},
|
||||
{
|
||||
"brand": "Nemo",
|
||||
"model": "Tensor Ultralight Insulated Regular",
|
||||
"category": "sleep-systems",
|
||||
"weightGrams": 425,
|
||||
"priceCents": 18000,
|
||||
"description": "3-inch thick insulated sleeping pad with R-value 4.2 and Spaceframe baffles."
|
||||
},
|
||||
{
|
||||
"brand": "Therm-a-Rest",
|
||||
"model": "NeoAir XLite NXT",
|
||||
"category": "sleep-systems",
|
||||
"weightGrams": 354,
|
||||
"priceCents": 22000,
|
||||
"description": "Ultralight insulated air pad with R-value 4.5, Triangular Core Matrix, and WingLock valve."
|
||||
},
|
||||
{
|
||||
"brand": "MSR",
|
||||
"model": "PocketRocket 2",
|
||||
"category": "cooking",
|
||||
"weightGrams": 73,
|
||||
"priceCents": 5500,
|
||||
"description": "Ultralight canister stove with adjustable flame control, boils 1L in 3.5 minutes."
|
||||
},
|
||||
{
|
||||
"brand": "Toaks",
|
||||
"model": "Titanium 750ml Pot",
|
||||
"category": "cooking",
|
||||
"weightGrams": 103,
|
||||
"priceCents": 3300,
|
||||
"description": "Ultralight titanium pot with lid and foldable handles, 750ml capacity."
|
||||
},
|
||||
{
|
||||
"brand": "Katadyn",
|
||||
"model": "BeFree 1.0L",
|
||||
"category": "hydration",
|
||||
"weightGrams": 59,
|
||||
"priceCents": 4500,
|
||||
"description": "Ultralight hollow fiber water filter with 0.1 micron filtration and 1L soft flask."
|
||||
},
|
||||
{
|
||||
"brand": "HydraPak",
|
||||
"model": "Seeker 2L",
|
||||
"category": "hydration",
|
||||
"weightGrams": 73,
|
||||
"priceCents": 1800,
|
||||
"description": "Collapsible 2L water storage with wide mouth and compatible with Katadyn BeFree filter."
|
||||
},
|
||||
{
|
||||
"brand": "Nitecore",
|
||||
"model": "NU25 UL",
|
||||
"category": "lighting",
|
||||
"weightGrams": 28,
|
||||
"priceCents": 3600,
|
||||
"description": "Ultralight USB-C rechargeable headlamp with 400 lumens max and red light mode."
|
||||
},
|
||||
{
|
||||
"brand": "Exposure Lights",
|
||||
"model": "Revo Dynamo",
|
||||
"category": "lighting",
|
||||
"weightGrams": 130,
|
||||
"priceCents": 22000,
|
||||
"description": "Dynamo-powered front light with 800 lumens, built-in standlight, and USB charging output."
|
||||
},
|
||||
{
|
||||
"brand": "Surly",
|
||||
"model": "24-Pack Rack",
|
||||
"category": "racks",
|
||||
"weightGrams": 750,
|
||||
"priceCents": 10000,
|
||||
"description": "Front rack for bikepacking with 24-pack platform, fits most forks with mid-blade eyelets."
|
||||
},
|
||||
{
|
||||
"brand": "Salsa",
|
||||
"model": "Anything Cage HD",
|
||||
"category": "accessories",
|
||||
"weightGrams": 80,
|
||||
"priceCents": 2500,
|
||||
"description": "Heavy-duty bottle cage for oversized loads like dry bags and fuel canisters."
|
||||
}
|
||||
]
|
||||
@@ -1,9 +1,9 @@
|
||||
import { Database } from "bun:sqlite";
|
||||
import { drizzle } from "drizzle-orm/bun-sqlite";
|
||||
import { drizzle } from "drizzle-orm/postgres-js";
|
||||
import postgres from "postgres";
|
||||
import * as schema from "./schema.ts";
|
||||
|
||||
const sqlite = new Database(process.env.DATABASE_PATH || "gearbox.db");
|
||||
sqlite.run("PRAGMA journal_mode = WAL");
|
||||
sqlite.run("PRAGMA foreign_keys = ON");
|
||||
|
||||
export const db = drizzle(sqlite, { schema });
|
||||
const connectionString =
|
||||
process.env.DATABASE_URL ||
|
||||
"postgresql://gearbox:gearbox@localhost:5432/gearbox";
|
||||
const queryClient = postgres(connectionString);
|
||||
export const db = drizzle(queryClient, { schema });
|
||||
|
||||
219
src/db/schema.ts
219
src/db/schema.ts
@@ -1,58 +1,90 @@
|
||||
import { integer, real, sqliteTable, text } from "drizzle-orm/sqlite-core";
|
||||
import {
|
||||
boolean,
|
||||
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(),
|
||||
displayName: text("display_name"),
|
||||
avatarUrl: text("avatar_url"),
|
||||
bio: text("bio"),
|
||||
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 +96,28 @@ 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),
|
||||
isPublic: boolean("is_public").notNull().default(false),
|
||||
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 +127,96 @@ 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(),
|
||||
// ── Global Items ────────────────────────────────────────────────────
|
||||
|
||||
export const globalItems = pgTable("global_items", {
|
||||
id: serial("id").primaryKey(),
|
||||
brand: text("brand").notNull(),
|
||||
model: text("model").notNull(),
|
||||
category: text("category"),
|
||||
weightGrams: doublePrecision("weight_grams"),
|
||||
priceCents: integer("price_cents"),
|
||||
imageUrl: text("image_url"),
|
||||
description: text("description"),
|
||||
createdAt: timestamp("created_at").defaultNow().notNull(),
|
||||
});
|
||||
|
||||
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" })
|
||||
// ── Item Global Links ───────────────────────────────────────────────
|
||||
|
||||
export const itemGlobalLinks = pgTable("item_global_links", {
|
||||
id: serial("id").primaryKey(),
|
||||
itemId: integer("item_id")
|
||||
.notNull()
|
||||
.$defaultFn(() => new Date()),
|
||||
});
|
||||
|
||||
export const sessions = sqliteTable("sessions", {
|
||||
id: text("id").primaryKey(),
|
||||
userId: integer("user_id")
|
||||
.references(() => items.id, { onDelete: "cascade" })
|
||||
.unique(),
|
||||
globalItemId: integer("global_item_id")
|
||||
.notNull()
|
||||
.references(() => users.id, { onDelete: "cascade" }),
|
||||
expiresAt: integer("expires_at", { mode: "timestamp" }).notNull(),
|
||||
.references(() => globalItems.id, { onDelete: "cascade" }),
|
||||
});
|
||||
|
||||
export const apiKeys = sqliteTable("api_keys", {
|
||||
id: integer("id").primaryKey({ autoIncrement: true }),
|
||||
// ── Settings ────────────────────────────────────────────────────────
|
||||
|
||||
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] })],
|
||||
);
|
||||
|
||||
// ── API Keys ────────────────────────────────────────────────────────
|
||||
|
||||
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