[feat] update drizzle to v2 and add card-price relations
This commit is contained in:
@@ -1,11 +1,11 @@
|
||||
// src/db/index.ts
|
||||
import 'dotenv/config';
|
||||
import { relations } from './relations';
|
||||
import { drizzle } from 'drizzle-orm/mysql2';
|
||||
import mysql from 'mysql2/promise';
|
||||
import * as schema from './schema';
|
||||
//import mysql from 'mysql2/promise';
|
||||
//import * as schema from './schema';
|
||||
|
||||
const poolConnection = mysql.createPool({
|
||||
uri: process.env.DATABASE_URL,
|
||||
});
|
||||
//const poolConnection = mysql.createPool(process.env.DATABASE_URL!);
|
||||
|
||||
export const db = drizzle(process.env.DATABASE_URL!, { relations });
|
||||
|
||||
export const db = drizzle(poolConnection, { schema, mode: 'default' });
|
||||
|
||||
16
src/db/relations.ts
Normal file
16
src/db/relations.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import { defineRelations } from "drizzle-orm";
|
||||
import * as schema from "./schema";
|
||||
|
||||
export const relations = defineRelations(schema, (r) => ({
|
||||
skus: {
|
||||
card: r.one.cards({
|
||||
from: r.skus.productId,
|
||||
to: r.cards.productId,
|
||||
}),
|
||||
},
|
||||
cards: {
|
||||
prices: r.many.skus(),
|
||||
},
|
||||
|
||||
}));
|
||||
|
||||
116
src/db/schema.ts
116
src/db/schema.ts
@@ -1,63 +1,63 @@
|
||||
// src/db/schema.ts
|
||||
import { mysqlTable, varchar, int, boolean, decimal, datetime, index } from 'drizzle-orm/mysql-core';
|
||||
import { mysqlTable, int, varchar, boolean, decimal, datetime, index } from "drizzle-orm/mysql-core"
|
||||
|
||||
export const cards = mysqlTable('cards', {
|
||||
productId: int().notNull().primaryKey(),
|
||||
productName: varchar({ length: 255 }).notNull(),
|
||||
productLineName: varchar({ length: 255 }).notNull().default(''),
|
||||
productLineUrlName: varchar({ length: 255 }).notNull().default(''),
|
||||
productStatusId: int().notNull().default(0),
|
||||
productTypeId: int().notNull().default(0),
|
||||
productUrlName: varchar({ length: 255 }).notNull().default(''),
|
||||
rarityName: varchar({ length: 100 }).notNull().default(''),
|
||||
score: decimal({ precision: 10, scale: 2 }).notNull().default('0'),
|
||||
sealed: boolean().notNull().default(false),
|
||||
sellerListable: boolean().notNull().default(false),
|
||||
setId: int().notNull().default(0),
|
||||
shippingCategoryId: int().notNull().default(0),
|
||||
duplicate: boolean().notNull().default(false),
|
||||
foilOnly: boolean().notNull().default(false),
|
||||
attack1: varchar({ length: 1024 }),
|
||||
attack2: varchar({ length: 1024 }),
|
||||
attack3: varchar({ length: 1024 }),
|
||||
attack4: varchar({ length: 1024 }),
|
||||
cardType: varchar({ length: 100 }),
|
||||
cardTypeB: varchar({ length: 100 }),
|
||||
energyType: varchar({ length: 100 }),
|
||||
flavorText: varchar({ length: 1000 }),
|
||||
hp: int().notNull().default(0),
|
||||
number: varchar({ length: 50 }).notNull().default(''),
|
||||
releaseDate: datetime(),
|
||||
resistance: varchar({ length: 100 }),
|
||||
retreatCost: varchar({ length: 100 }),
|
||||
stage: varchar({ length: 100 }),
|
||||
weakness: varchar({ length: 100 }),
|
||||
lowestPrice: decimal({ precision: 10, scale: 2 }).notNull().default('0'),
|
||||
lowestPriceWithShipping: decimal({ precision: 10, scale: 2 }).notNull().default('0'),
|
||||
marketPrice: decimal({ precision: 10, scale: 2 }).notNull().default('0'),
|
||||
maxFulfillableQuantity: int().notNull().default(0),
|
||||
medianPrice: decimal({ precision: 10, scale: 2 }).notNull().default('0'),
|
||||
totalListings: int().notNull().default(0),
|
||||
export const cards = mysqlTable("cards", {
|
||||
productId: int().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(),
|
||||
productUrlName: varchar({ length: 255 }).default("").notNull(),
|
||||
rarityName: varchar({ length: 100 }).default("").notNull(),
|
||||
sealed: boolean().default(false).notNull(),
|
||||
sellerListable: boolean().default(false).notNull(),
|
||||
setId: int().default(0).notNull(),
|
||||
shippingCategoryId: int().default(0).notNull(),
|
||||
duplicate: boolean().default(false).notNull(),
|
||||
foilOnly: boolean().default(false).notNull(),
|
||||
maxFulfillableQuantity: int().default(0).notNull(),
|
||||
totalListings: int().default(0).notNull(),
|
||||
score: decimal({ precision: 10, scale: 2, mode: 'number' }).default(0).notNull(),
|
||||
lowestPrice: decimal({ precision: 10, scale: 2, mode: 'number' }).default(0).notNull(),
|
||||
lowestPriceWithShipping: decimal({ precision: 10, scale: 2, mode: 'number' }).default(0).notNull(),
|
||||
marketPrice: decimal({ precision: 10, scale: 2, mode: 'number' }).default(0).notNull(),
|
||||
medianPrice: decimal({ precision: 10, scale: 2, mode: 'number' }).default(0).notNull(),
|
||||
attack1: varchar({ length: 1024 }),
|
||||
attack2: varchar({ length: 1024 }),
|
||||
attack3: varchar({ length: 1024 }),
|
||||
attack4: varchar({ length: 1024 }),
|
||||
cardType: varchar({ length: 100 }),
|
||||
cardTypeB: varchar({ length: 100 }),
|
||||
energyType: varchar({ length: 100 }),
|
||||
flavorText: varchar({ length: 1000 }),
|
||||
hp: int().default(0).notNull(),
|
||||
number: varchar({ length: 50 }).default("").notNull(),
|
||||
releaseDate: datetime(),
|
||||
resistance: varchar({ length: 100 }),
|
||||
retreatCost: varchar({ length: 100 }),
|
||||
stage: varchar({ length: 100 }),
|
||||
weakness: varchar({ length: 100 }),
|
||||
});
|
||||
|
||||
export const sets = mysqlTable('sets', {
|
||||
setId: int().notNull().primaryKey(),
|
||||
setCode: varchar({ length: 100 }).notNull(),
|
||||
setName: varchar({ length: 255 }).notNull(),
|
||||
setUrlName: varchar({ length: 255 }).notNull(),
|
||||
export const sets = mysqlTable("sets", {
|
||||
setId: int().primaryKey(),
|
||||
setName: varchar({ length: 255 }).notNull(),
|
||||
setUrlName: varchar({ length: 255 }).notNull(),
|
||||
setCode: varchar({ length: 100 }).notNull(),
|
||||
});
|
||||
|
||||
export const skus = mysqlTable('skus', {
|
||||
skuId: int().notNull().primaryKey(),
|
||||
productId: int().notNull(),
|
||||
condition: varchar({ length: 255 }).notNull(),
|
||||
language: varchar({ length: 100 }).notNull(),
|
||||
variant: varchar({ length: 100 }).notNull(),
|
||||
calculatedAt: datetime(),
|
||||
highestPrice: decimal({ precision: 10, scale: 2 }),
|
||||
lowestPrice: decimal({ precision: 10, scale: 2 }),
|
||||
marketPrice: decimal({ precision: 10, scale: 2 }),
|
||||
priceCount: int(),
|
||||
},(table) => ({
|
||||
productIdIdx: index('productIdIdx').on(table.productId),
|
||||
}));
|
||||
export const skus = mysqlTable("skus", {
|
||||
skuId: int().primaryKey(),
|
||||
productId: int().notNull(),
|
||||
condition: varchar({ length: 255 }).notNull(),
|
||||
language: varchar({ length: 100 }).notNull(),
|
||||
variant: varchar({ length: 100 }).notNull(),
|
||||
calculatedAt: datetime(),
|
||||
highestPrice: decimal({ precision: 10, scale: 2 }),
|
||||
lowestPrice: decimal({ precision: 10, scale: 2 }),
|
||||
marketPrice: decimal({ precision: 10, scale: 2 }),
|
||||
priceCount: int(),
|
||||
},
|
||||
(table) => [
|
||||
index("productIdIdx").on(table.productId),
|
||||
]);
|
||||
|
||||
Reference in New Issue
Block a user