shit show but data works

This commit is contained in:
2025-07-03 10:43:58 -04:00
parent bdb5e0fe55
commit 8e756b6553
19 changed files with 1641 additions and 636 deletions

View File

@@ -0,0 +1,77 @@
-- Current sql file was generated after introspecting the database
-- If you want to run this migration please uncomment this code before executing migrations
/*
CREATE TABLE "product_category_mappings" (
"id" serial PRIMARY KEY NOT NULL,
"feed_name" varchar(255),
"feed_category_value" varchar(255),
"canonical_category_id" integer,
"confidence_score" double precision,
"last_reviewed_by" varchar(255),
"last_reviewed_at" timestamp
);
--> statement-breakpoint
CREATE TABLE "categories" (
"id" serial PRIMARY KEY NOT NULL,
"name" varchar(255) NOT NULL,
"parent_id" integer,
"slug" varchar(255) NOT NULL,
CONSTRAINT "categories_slug_key" UNIQUE("slug")
);
--> statement-breakpoint
CREATE TABLE "products" (
"id" serial PRIMARY KEY NOT NULL,
"name" varchar(255) NOT NULL,
"brand" varchar(255),
"description" text,
"upc" varchar(32),
"mpn" varchar(64),
"canonical_category_id" integer,
"created_at" timestamp DEFAULT now(),
"updated_at" timestamp DEFAULT now()
);
--> statement-breakpoint
CREATE TABLE "offers" (
"id" serial PRIMARY KEY NOT NULL,
"product_id" integer,
"feed_name" varchar(255) NOT NULL,
"feed_sku" varchar(255),
"price" numeric(10, 2),
"url" text,
"in_stock" boolean,
"vendor" varchar(255),
"last_seen_at" timestamp DEFAULT now(),
"raw_data" jsonb,
CONSTRAINT "offers_product_id_feed_name_feed_sku_key" UNIQUE("product_id","feed_name","feed_sku"),
CONSTRAINT "offers_feed_unique" UNIQUE("feed_name","feed_sku")
);
--> statement-breakpoint
CREATE TABLE "offer_price_history" (
"id" serial PRIMARY KEY NOT NULL,
"offer_id" integer,
"price" numeric(10, 2) NOT NULL,
"seen_at" timestamp DEFAULT now()
);
--> statement-breakpoint
CREATE TABLE "feeds" (
"id" serial PRIMARY KEY NOT NULL,
"name" varchar(255) NOT NULL,
"url" text,
"last_imported_at" timestamp,
CONSTRAINT "feeds_name_key" UNIQUE("name")
);
--> statement-breakpoint
CREATE TABLE "product_attributes" (
"id" serial PRIMARY KEY NOT NULL,
"product_id" integer,
"name" varchar(255) NOT NULL,
"value" varchar(255) NOT NULL
);
--> statement-breakpoint
ALTER TABLE "product_category_mappings" ADD CONSTRAINT "product_category_mappings_canonical_category_id_fkey" FOREIGN KEY ("canonical_category_id") REFERENCES "public"."categories"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "categories" ADD CONSTRAINT "categories_parent_id_fkey" FOREIGN KEY ("parent_id") REFERENCES "public"."categories"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "products" ADD CONSTRAINT "products_canonical_category_id_fkey" FOREIGN KEY ("canonical_category_id") REFERENCES "public"."categories"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "offers" ADD CONSTRAINT "offers_product_id_fkey" FOREIGN KEY ("product_id") REFERENCES "public"."products"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "offer_price_history" ADD CONSTRAINT "offer_price_history_offer_id_fkey" FOREIGN KEY ("offer_id") REFERENCES "public"."offers"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "product_attributes" ADD CONSTRAINT "product_attributes_product_id_fkey" FOREIGN KEY ("product_id") REFERENCES "public"."products"("id") ON DELETE cascade ON UPDATE no action;
*/

View File

@@ -0,0 +1,7 @@
DROP TABLE "product_category_mappings" CASCADE;--> statement-breakpoint
DROP TABLE "categories" CASCADE;--> statement-breakpoint
DROP TABLE "products" CASCADE;--> statement-breakpoint
DROP TABLE "offers" CASCADE;--> statement-breakpoint
DROP TABLE "offer_price_history" CASCADE;--> statement-breakpoint
DROP TABLE "feeds" CASCADE;--> statement-breakpoint
DROP TABLE "product_attributes" CASCADE;

View File

@@ -0,0 +1,493 @@
{
"id": "00000000-0000-0000-0000-000000000000",
"prevId": "",
"version": "7",
"dialect": "postgresql",
"tables": {
"public.product_category_mappings": {
"name": "product_category_mappings",
"schema": "",
"columns": {
"id": {
"name": "id",
"type": "serial",
"primaryKey": true,
"notNull": true
},
"feed_name": {
"name": "feed_name",
"type": "varchar(255)",
"primaryKey": false,
"notNull": false
},
"feed_category_value": {
"name": "feed_category_value",
"type": "varchar(255)",
"primaryKey": false,
"notNull": false
},
"canonical_category_id": {
"name": "canonical_category_id",
"type": "integer",
"primaryKey": false,
"notNull": false
},
"confidence_score": {
"name": "confidence_score",
"type": "double precision",
"primaryKey": false,
"notNull": false
},
"last_reviewed_by": {
"name": "last_reviewed_by",
"type": "varchar(255)",
"primaryKey": false,
"notNull": false
},
"last_reviewed_at": {
"name": "last_reviewed_at",
"type": "timestamp",
"primaryKey": false,
"notNull": false
}
},
"indexes": {},
"foreignKeys": {
"product_category_mappings_canonical_category_id_fkey": {
"name": "product_category_mappings_canonical_category_id_fkey",
"tableFrom": "product_category_mappings",
"tableTo": "categories",
"schemaTo": "public",
"columnsFrom": [
"canonical_category_id"
],
"columnsTo": [
"id"
],
"onDelete": "no action",
"onUpdate": "no action"
}
},
"compositePrimaryKeys": {},
"uniqueConstraints": {},
"checkConstraints": {},
"policies": {},
"isRLSEnabled": false
},
"public.categories": {
"name": "categories",
"schema": "",
"columns": {
"id": {
"name": "id",
"type": "serial",
"primaryKey": true,
"notNull": true
},
"name": {
"name": "name",
"type": "varchar(255)",
"primaryKey": false,
"notNull": true
},
"parent_id": {
"name": "parent_id",
"type": "integer",
"primaryKey": false,
"notNull": false
},
"slug": {
"name": "slug",
"type": "varchar(255)",
"primaryKey": false,
"notNull": true
}
},
"indexes": {},
"foreignKeys": {
"categories_parent_id_fkey": {
"name": "categories_parent_id_fkey",
"tableFrom": "categories",
"tableTo": "categories",
"schemaTo": "public",
"columnsFrom": [
"parent_id"
],
"columnsTo": [
"id"
],
"onDelete": "no action",
"onUpdate": "no action"
}
},
"compositePrimaryKeys": {},
"uniqueConstraints": {
"categories_slug_key": {
"columns": [
"slug"
],
"nullsNotDistinct": false,
"name": "categories_slug_key"
}
},
"checkConstraints": {},
"policies": {},
"isRLSEnabled": false
},
"public.products": {
"name": "products",
"schema": "",
"columns": {
"id": {
"name": "id",
"type": "serial",
"primaryKey": true,
"notNull": true
},
"name": {
"name": "name",
"type": "varchar(255)",
"primaryKey": false,
"notNull": true
},
"brand": {
"name": "brand",
"type": "varchar(255)",
"primaryKey": false,
"notNull": false
},
"description": {
"name": "description",
"type": "text",
"primaryKey": false,
"notNull": false
},
"upc": {
"name": "upc",
"type": "varchar(32)",
"primaryKey": false,
"notNull": false
},
"mpn": {
"name": "mpn",
"type": "varchar(64)",
"primaryKey": false,
"notNull": false
},
"canonical_category_id": {
"name": "canonical_category_id",
"type": "integer",
"primaryKey": false,
"notNull": false
},
"created_at": {
"name": "created_at",
"type": "timestamp",
"primaryKey": false,
"notNull": false,
"default": "now()"
},
"updated_at": {
"name": "updated_at",
"type": "timestamp",
"primaryKey": false,
"notNull": false,
"default": "now()"
}
},
"indexes": {},
"foreignKeys": {
"products_canonical_category_id_fkey": {
"name": "products_canonical_category_id_fkey",
"tableFrom": "products",
"tableTo": "categories",
"schemaTo": "public",
"columnsFrom": [
"canonical_category_id"
],
"columnsTo": [
"id"
],
"onDelete": "no action",
"onUpdate": "no action"
}
},
"compositePrimaryKeys": {},
"uniqueConstraints": {},
"checkConstraints": {},
"policies": {},
"isRLSEnabled": false
},
"public.offers": {
"name": "offers",
"schema": "",
"columns": {
"id": {
"name": "id",
"type": "serial",
"primaryKey": true,
"notNull": true
},
"product_id": {
"name": "product_id",
"type": "integer",
"primaryKey": false,
"notNull": false
},
"feed_name": {
"name": "feed_name",
"type": "varchar(255)",
"primaryKey": false,
"notNull": true
},
"feed_sku": {
"name": "feed_sku",
"type": "varchar(255)",
"primaryKey": false,
"notNull": false
},
"price": {
"name": "price",
"type": "numeric(10, 2)",
"primaryKey": false,
"notNull": false
},
"url": {
"name": "url",
"type": "text",
"primaryKey": false,
"notNull": false
},
"in_stock": {
"name": "in_stock",
"type": "boolean",
"primaryKey": false,
"notNull": false
},
"vendor": {
"name": "vendor",
"type": "varchar(255)",
"primaryKey": false,
"notNull": false
},
"last_seen_at": {
"name": "last_seen_at",
"type": "timestamp",
"primaryKey": false,
"notNull": false,
"default": "now()"
},
"raw_data": {
"name": "raw_data",
"type": "jsonb",
"primaryKey": false,
"notNull": false
}
},
"indexes": {},
"foreignKeys": {
"offers_product_id_fkey": {
"name": "offers_product_id_fkey",
"tableFrom": "offers",
"tableTo": "products",
"schemaTo": "public",
"columnsFrom": [
"product_id"
],
"columnsTo": [
"id"
],
"onDelete": "cascade",
"onUpdate": "no action"
}
},
"compositePrimaryKeys": {},
"uniqueConstraints": {
"offers_product_id_feed_name_feed_sku_key": {
"columns": [
"product_id",
"feed_name",
"feed_sku"
],
"nullsNotDistinct": false,
"name": "offers_product_id_feed_name_feed_sku_key"
},
"offers_feed_unique": {
"columns": [
"feed_name",
"feed_sku"
],
"nullsNotDistinct": false,
"name": "offers_feed_unique"
}
},
"checkConstraints": {},
"policies": {},
"isRLSEnabled": false
},
"public.offer_price_history": {
"name": "offer_price_history",
"schema": "",
"columns": {
"id": {
"name": "id",
"type": "serial",
"primaryKey": true,
"notNull": true
},
"offer_id": {
"name": "offer_id",
"type": "integer",
"primaryKey": false,
"notNull": false
},
"price": {
"name": "price",
"type": "numeric(10, 2)",
"primaryKey": false,
"notNull": true
},
"seen_at": {
"name": "seen_at",
"type": "timestamp",
"primaryKey": false,
"notNull": false,
"default": "now()"
}
},
"indexes": {},
"foreignKeys": {
"offer_price_history_offer_id_fkey": {
"name": "offer_price_history_offer_id_fkey",
"tableFrom": "offer_price_history",
"tableTo": "offers",
"schemaTo": "public",
"columnsFrom": [
"offer_id"
],
"columnsTo": [
"id"
],
"onDelete": "cascade",
"onUpdate": "no action"
}
},
"compositePrimaryKeys": {},
"uniqueConstraints": {},
"checkConstraints": {},
"policies": {},
"isRLSEnabled": false
},
"public.feeds": {
"name": "feeds",
"schema": "",
"columns": {
"id": {
"name": "id",
"type": "serial",
"primaryKey": true,
"notNull": true
},
"name": {
"name": "name",
"type": "varchar(255)",
"primaryKey": false,
"notNull": true
},
"url": {
"name": "url",
"type": "text",
"primaryKey": false,
"notNull": false
},
"last_imported_at": {
"name": "last_imported_at",
"type": "timestamp",
"primaryKey": false,
"notNull": false
}
},
"indexes": {},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {
"feeds_name_key": {
"columns": [
"name"
],
"nullsNotDistinct": false,
"name": "feeds_name_key"
}
},
"checkConstraints": {},
"policies": {},
"isRLSEnabled": false
},
"public.product_attributes": {
"name": "product_attributes",
"schema": "",
"columns": {
"id": {
"name": "id",
"type": "serial",
"primaryKey": true,
"notNull": true
},
"product_id": {
"name": "product_id",
"type": "integer",
"primaryKey": false,
"notNull": false
},
"name": {
"name": "name",
"type": "varchar(255)",
"primaryKey": false,
"notNull": true
},
"value": {
"name": "value",
"type": "varchar(255)",
"primaryKey": false,
"notNull": true
}
},
"indexes": {},
"foreignKeys": {
"product_attributes_product_id_fkey": {
"name": "product_attributes_product_id_fkey",
"tableFrom": "product_attributes",
"tableTo": "products",
"schemaTo": "public",
"columnsFrom": [
"product_id"
],
"columnsTo": [
"id"
],
"onDelete": "cascade",
"onUpdate": "no action"
}
},
"compositePrimaryKeys": {},
"uniqueConstraints": {},
"checkConstraints": {},
"policies": {},
"isRLSEnabled": false
}
},
"enums": {},
"schemas": {},
"sequences": {},
"roles": {},
"policies": {},
"views": {},
"_meta": {
"schemas": {},
"tables": {},
"columns": {}
},
"internal": {
"tables": {}
}
}

View File

@@ -0,0 +1,18 @@
{
"id": "919e511e-3cff-4bd0-b4ec-20865db2d1d3",
"prevId": "00000000-0000-0000-0000-000000000000",
"version": "7",
"dialect": "postgresql",
"tables": {},
"enums": {},
"schemas": {},
"sequences": {},
"roles": {},
"policies": {},
"views": {},
"_meta": {
"columns": {},
"schemas": {},
"tables": {}
}
}

View File

@@ -0,0 +1,20 @@
{
"version": "7",
"dialect": "postgresql",
"entries": [
{
"idx": 0,
"version": "7",
"when": 1751488452074,
"tag": "0000_luxuriant_albert_cleary",
"breakpoints": true
},
{
"idx": 1,
"version": "7",
"when": 1751488491929,
"tag": "0001_superb_umar",
"breakpoints": true
}
]
}

View File

@@ -0,0 +1,53 @@
import { relations } from "drizzle-orm/relations";
import { categories, productCategoryMappings, products, offers, offerPriceHistory, productAttributes } from "./schema";
export const productCategoryMappingsRelations = relations(productCategoryMappings, ({one}) => ({
category: one(categories, {
fields: [productCategoryMappings.canonicalCategoryId],
references: [categories.id]
}),
}));
export const categoriesRelations = relations(categories, ({one, many}) => ({
productCategoryMappings: many(productCategoryMappings),
category: one(categories, {
fields: [categories.parentId],
references: [categories.id],
relationName: "categories_parentId_categories_id"
}),
categories: many(categories, {
relationName: "categories_parentId_categories_id"
}),
products: many(products),
}));
export const productsRelations = relations(products, ({one, many}) => ({
category: one(categories, {
fields: [products.canonicalCategoryId],
references: [categories.id]
}),
offers: many(offers),
productAttributes: many(productAttributes),
}));
export const offersRelations = relations(offers, ({one, many}) => ({
product: one(products, {
fields: [offers.productId],
references: [products.id]
}),
offerPriceHistories: many(offerPriceHistory),
}));
export const offerPriceHistoryRelations = relations(offerPriceHistory, ({one}) => ({
offer: one(offers, {
fields: [offerPriceHistory.offerId],
references: [offers.id]
}),
}));
export const productAttributesRelations = relations(productAttributes, ({one}) => ({
product: one(products, {
fields: [productAttributes.productId],
references: [products.id]
}),
}));

View File

@@ -0,0 +1,108 @@
import { pgTable, foreignKey, serial, varchar, integer, doublePrecision, timestamp, unique, text, numeric, boolean, jsonb } from "drizzle-orm/pg-core"
import { sql } from "drizzle-orm"
export const productCategoryMappings = pgTable("product_category_mappings", {
id: serial().primaryKey().notNull(),
feedName: varchar("feed_name", { length: 255 }),
feedCategoryValue: varchar("feed_category_value", { length: 255 }),
canonicalCategoryId: integer("canonical_category_id"),
confidenceScore: doublePrecision("confidence_score"),
lastReviewedBy: varchar("last_reviewed_by", { length: 255 }),
lastReviewedAt: timestamp("last_reviewed_at", { mode: 'string' }),
}, (table) => [
foreignKey({
columns: [table.canonicalCategoryId],
foreignColumns: [categories.id],
name: "product_category_mappings_canonical_category_id_fkey"
}),
]);
export const categories = pgTable("categories", {
id: serial().primaryKey().notNull(),
name: varchar({ length: 255 }).notNull(),
parentId: integer("parent_id"),
slug: varchar({ length: 255 }).notNull(),
}, (table) => [
foreignKey({
columns: [table.parentId],
foreignColumns: [table.id],
name: "categories_parent_id_fkey"
}),
unique("categories_slug_key").on(table.slug),
]);
export const products = pgTable("products", {
id: serial().primaryKey().notNull(),
name: varchar({ length: 255 }).notNull(),
brand: varchar({ length: 255 }),
description: text(),
upc: varchar({ length: 32 }),
mpn: varchar({ length: 64 }),
canonicalCategoryId: integer("canonical_category_id"),
createdAt: timestamp("created_at", { mode: 'string' }).defaultNow(),
updatedAt: timestamp("updated_at", { mode: 'string' }).defaultNow(),
}, (table) => [
foreignKey({
columns: [table.canonicalCategoryId],
foreignColumns: [categories.id],
name: "products_canonical_category_id_fkey"
}),
]);
export const offers = pgTable("offers", {
id: serial().primaryKey().notNull(),
productId: integer("product_id"),
feedName: varchar("feed_name", { length: 255 }).notNull(),
feedSku: varchar("feed_sku", { length: 255 }),
price: numeric({ precision: 10, scale: 2 }),
url: text(),
inStock: boolean("in_stock"),
vendor: varchar({ length: 255 }),
lastSeenAt: timestamp("last_seen_at", { mode: 'string' }).defaultNow(),
rawData: jsonb("raw_data"),
}, (table) => [
foreignKey({
columns: [table.productId],
foreignColumns: [products.id],
name: "offers_product_id_fkey"
}).onDelete("cascade"),
unique("offers_product_id_feed_name_feed_sku_key").on(table.productId, table.feedName, table.feedSku),
unique("offers_feed_unique").on(table.feedName, table.feedSku),
]);
export const offerPriceHistory = pgTable("offer_price_history", {
id: serial().primaryKey().notNull(),
offerId: integer("offer_id"),
price: numeric({ precision: 10, scale: 2 }).notNull(),
seenAt: timestamp("seen_at", { mode: 'string' }).defaultNow(),
}, (table) => [
foreignKey({
columns: [table.offerId],
foreignColumns: [offers.id],
name: "offer_price_history_offer_id_fkey"
}).onDelete("cascade"),
]);
export const feeds = pgTable("feeds", {
id: serial().primaryKey().notNull(),
name: varchar({ length: 255 }).notNull(),
url: text(),
lastImportedAt: timestamp("last_imported_at", { mode: 'string' }),
}, (table) => [
unique("feeds_name_key").on(table.name),
]);
export const productAttributes = pgTable("product_attributes", {
id: serial().primaryKey().notNull(),
productId: integer("product_id"),
name: varchar({ length: 255 }).notNull(),
value: varchar({ length: 255 }).notNull(),
}, (table) => [
foreignKey({
columns: [table.productId],
foreignColumns: [products.id],
name: "product_attributes_product_id_fkey"
}).onDelete("cascade"),
]);