import { relations } from "drizzle-orm"; import { index, numeric, pgTable, timestamp, uniqueIndex, uuid } from "drizzle-orm/pg-core"; import { positionSideEnum } from "./enums"; import { markets } from "./markets"; import { profiles } from "./profiles"; export const positions = pgTable( "positions", { id: uuid("id").primaryKey().defaultRandom(), profileId: uuid("profile_id") .notNull() .references(() => profiles.id), marketId: uuid("market_id") .notNull() .references(() => markets.id), side: positionSideEnum("side").notNull(), shares: numeric("shares", { precision: 15, scale: 4 }).notNull().default("0.0000"), avgPrice: numeric("avg_price", { precision: 7, scale: 4 }).notNull().default("0.0000"), realizedPnl: numeric("realized_pnl", { precision: 15, scale: 4 }).notNull().default("0.0000"), createdAt: timestamp("created_at", { withTimezone: true }).notNull().defaultNow(), updatedAt: timestamp("updated_at", { withTimezone: true }).notNull().defaultNow(), }, (t) => [ uniqueIndex("positions_profile_market_side_idx").on(t.profileId, t.marketId, t.side), index("positions_profile_id_idx").on(t.profileId), index("positions_market_id_idx").on(t.marketId), ], ); export const positionsRelations = relations(positions, ({ one }) => ({ profile: one(profiles, { fields: [positions.profileId], references: [profiles.id] }), market: one(markets, { fields: [positions.marketId], references: [markets.id] }), }));