[feat] switched from mysql to postgresql

This commit is contained in:
2026-03-11 19:18:45 -04:00
parent 1089bcdc20
commit a68ed7f7b8
10 changed files with 1801 additions and 1175 deletions

View File

@@ -1,11 +1,23 @@
// src/db/index.ts
import 'dotenv/config';
import { relations } from './relations.ts';
import { drizzle } from 'drizzle-orm/mysql2';
import mysql from 'mysql2/promise';
import { drizzle } from "drizzle-orm/node-postgres";
import { Pool } from "pg";
//export const poolConnection = mysql.createPool({ uri: process.env.DATABASE_URL, client_found_rows: false });
export const poolConnection = mysql.createPool({ uri: process.env.DATABASE_URL, flags: ["-FOUND_ROWS"] });
const pool = new Pool({
connectionString: process.env.DATABASE_URL,
max: 10,
idleTimeoutMillis: 30000,
connectionTimeoutMillis: 2000,
});
export const db = drizzle({ client: poolConnection, relations: relations});
// Handle pool errors to prevent connection corruption
pool.on('error', (err) => {
console.error('Unexpected error on idle client', err);
});
export const db = drizzle({ client: pool, relations: relations, casing: 'snake_case' });
export const ClosePool = () => {
pool.end();
}

View File

@@ -1,22 +1,25 @@
import { mysqlTable, int, varchar, boolean, decimal, datetime, index } from "drizzle-orm/mysql-core"
//import { mysqlTable, int, varchar, boolean, decimal, datetime, index } from "drizzle-orm/mysql-core"
import { integer, varchar, boolean, decimal, timestamp, index, pgSchema } from "drizzle-orm/pg-core";
export const tcgcards = mysqlTable("tcgcards", {
productId: int().primaryKey(),
export const pokeSchema = pgSchema("pokemon");
export const tcgcards = pokeSchema.table('tcg_cards', {
productId: integer().primaryKey(),
productName: varchar({ length: 255 }).notNull(),
productLineName: varchar({ length: 255 }).default("").notNull(),
productLineUrlName: varchar({ length: 255 }).default("").notNull(),
productStatusId: int().default(0).notNull(),
productTypeId: int().default(0).notNull(),
productStatusId: integer().default(0).notNull(),
productTypeId: integer().default(0).notNull(),
productUrlName: varchar({ length: 255 }).default("").notNull(),
rarityName: varchar({ length: 100 }).default("").notNull(),
sealed: boolean().default(false).notNull(),
sellerListable: boolean().default(false).notNull(),
setId: int(),
shippingCategoryId: int(),
setId: integer(),
shippingCategoryId: integer(),
duplicate: boolean().default(false).notNull(),
foilOnly: boolean().default(false).notNull(),
maxFulfillableQuantity: int(),
totalListings: int(),
maxFulfillableQuantity: integer(),
totalListings: integer(),
score: decimal({ precision: 10, scale: 2, mode: 'number' }),
lowestPrice: decimal({ precision: 10, scale: 2, mode: 'number' }),
lowestPriceWithShipping: decimal({ precision: 10, scale: 2, mode: 'number' }),
@@ -30,82 +33,82 @@ export const tcgcards = mysqlTable("tcgcards", {
cardTypeB: varchar({ length: 100 }),
energyType: varchar({ length: 100 }),
flavorText: varchar({ length: 1000 }),
hp: int(),
hp: integer(),
number: varchar({ length: 50 }).default("").notNull(),
releaseDate: datetime(),
releaseDate: timestamp(),
resistance: varchar({ length: 100 }),
retreatCost: varchar({ length: 100 }),
stage: varchar({ length: 100 }),
weakness: varchar({ length: 100 }),
Artist: varchar({ length: 255 }),
artist: varchar({ length: 255 }),
});
export const cards = mysqlTable("cards", {
cardId: int().notNull().primaryKey().autoincrement(),
productId: int().notNull(),
export const cards = pokeSchema.table('cards', {
cardId: integer().notNull().primaryKey().generatedAlwaysAsIdentity(),
productId: integer().notNull(),
variant: varchar({ length: 100 }).notNull(),
productName: varchar({ length: 255 }),
productLineName: varchar({ length: 255 }),
productUrlName: varchar({ length: 255 }).default("").notNull(),
rarityName: varchar({ length: 100 }),
sealed: boolean().default(false).notNull(),
setId: int(),
setId: integer(),
cardType: varchar({ length: 100 }),
energyType: varchar({ length: 100 }),
number: varchar({ length: 50 }),
Artist: varchar({ length: 255 }),
artist: varchar({ length: 255 }),
},
(table) => [
index("card_productIdIdx").on(table.productId, table.variant),
index('idx_card_product_id').on(table.productId, table.variant),
]);
export const tcg_overrides = mysqlTable("tcg_overrides", {
productId: int().primaryKey(),
export const tcg_overrides = pokeSchema.table('tcg_overrides', {
productId: integer().primaryKey(),
productName: varchar({ length: 255 }),
productLineName: varchar({ length: 255 }),
productUrlName: varchar({ length: 255 }).default("").notNull(),
productUrlName: varchar({ length: 255 }).default('').notNull(),
rarityName: varchar({ length: 100 }),
sealed: boolean().default(false).notNull(),
setId: int(),
setId: integer(),
cardType: varchar({ length: 100 }),
energyType: varchar({ length: 100 }),
number: varchar({ length: 50 }),
Artist: varchar({ length: 255 }),
artist: varchar({ length: 255 }),
});
export const sets = mysqlTable("sets", {
setId: int().primaryKey(),
export const sets = pokeSchema.table('sets', {
setId: integer().primaryKey(),
setName: varchar({ length: 255 }).notNull(),
setUrlName: varchar({ length: 255 }).notNull(),
setCode: varchar({ length: 100 }).notNull(),
});
export const skus = mysqlTable("skus", {
skuId: int().primaryKey(),
cardId: int().default(0).notNull(),
productId: int().notNull(),
export const skus = pokeSchema.table('skus', {
skuId: integer().primaryKey(),
cardId: integer().default(0).notNull(),
productId: integer().notNull(),
condition: varchar({ length: 255 }).notNull(),
language: varchar({ length: 100 }).notNull(),
variant: varchar({ length: 100 }).notNull(),
calculatedAt: datetime(),
calculatedAt: timestamp(),
highestPrice: decimal({ precision: 10, scale: 2 }),
lowestPrice: decimal({ precision: 10, scale: 2 }),
marketPrice: decimal({ precision: 10, scale: 2 }),
priceCount: int(),
priceCount: integer(),
},
(table) => [
index("productIdIdx").on(table.productId, table.variant),
index('idx_product_id_condition').on(table.productId, table.variant),
]);
export const priceHistory = mysqlTable("price_history", {
skuId: int().default(0).notNull(),
calculatedAt: datetime(),
export const priceHistory = pokeSchema.table('price_history', {
skuId: integer().default(0).notNull(),
calculatedAt: timestamp(),
marketPrice: decimal({ precision: 10, scale: 2 }),
},
(table) => [
index("idx_price_history").on(table.skuId, table.calculatedAt),
index('idx_price_history').on(table.skuId, table.calculatedAt),
]);
export const processingSkus = mysqlTable("processingSkus", {
skuId: int().primaryKey(),
export const processingSkus = pokeSchema.table('processing_skus', {
skuId: integer().primaryKey(),
});