import { relations } from "drizzle-orm"; import { index, numeric, pgTable, timestamp, uuid } from "drizzle-orm/pg-core"; import { positionSideEnum, tradeActionEnum } from "./enums"; import { markets } from "./markets"; import { profiles } from "./profiles"; export const trades = pgTable( "trades", { id: uuid("id").primaryKey().defaultRandom(), profileId: uuid("profile_id") .notNull() .references(() => profiles.id), marketId: uuid("market_id") .notNull() .references(() => markets.id), action: tradeActionEnum("action").notNull(), side: positionSideEnum("side").notNull(), shares: numeric("shares", { precision: 15, scale: 4 }).notNull(), price: numeric("price", { precision: 7, scale: 4 }).notNull(), amount: numeric("amount", { precision: 15, scale: 4 }).notNull(), fee: numeric("fee", { precision: 15, scale: 4 }).notNull().default("0.0000"), poolYesAfter: numeric("pool_yes_after", { precision: 15, scale: 4 }).notNull(), poolNoAfter: numeric("pool_no_after", { precision: 15, scale: 4 }).notNull(), createdAt: timestamp("created_at", { withTimezone: true }).notNull().defaultNow(), }, (t) => [ index("trades_profile_id_idx").on(t.profileId), index("trades_market_id_idx").on(t.marketId), index("trades_created_at_idx").on(t.createdAt), ], ); export const tradesRelations = relations(trades, ({ one }) => ({ profile: one(profiles, { fields: [trades.profileId], references: [profiles.id] }), market: one(markets, { fields: [trades.marketId], references: [markets.id] }), }));