A Prediction Market on the AT Protocol

feat(db/index.ts): move schema + db export here

Ciaran 80fa6055 35cf27a1

+26 -18
+14 -18
src/core/api.ts
··· 1 1 import 'dotenv/config'; 2 - import { drizzle } from "drizzle-orm/node-postgres"; 3 2 import { ZaCoCiaranCumulusBet, ZaCoCiaranCumulusMarket, ZaCoCiaranCumulusResolution } from '../../generated/typescript'; 4 3 import { is, type ActorIdentifier } from '@atcute/lexicons'; 5 4 import type { CreateCommit, DeleteCommit } from '@atcute/jetstream'; 6 - import * as schema from "../db/schema" 7 - import { DEFAULT_MARKET_COLS, DEFAULT_BET_COLS, DEFAULT_RESOLUTION_COLS } from '../db/schema'; 5 + import { DEFAULT_MARKET_COLS, DEFAULT_BET_COLS, DEFAULT_RESOLUTION_COLS, marketsTable, betsTable, resolutionsTable } from '@/db' 8 6 import { eq } from 'drizzle-orm'; 9 - 10 - 11 - export const db = drizzle(process.env.DATABASE_URL!, { schema }); 7 + import { db } from '@/db'; 12 8 13 9 export async function tryListMarkets() { 14 10 return await db.query.marketsTable.findMany({ ··· 24 20 export async function tryFindMarket(uri: string) { 25 21 return await db.query.marketsTable.findFirst({ 26 22 columns: DEFAULT_MARKET_COLS, 27 - where: eq(schema.marketsTable.uri, uri), 23 + where: eq(marketsTable.uri, uri), 28 24 with: { 29 25 bets: { columns: DEFAULT_BET_COLS }, 30 26 resolution: { columns: DEFAULT_RESOLUTION_COLS } ··· 35 31 export async function tryFindMarketBets(uri: string) { 36 32 return await db.query.betsTable.findMany({ 37 33 columns: DEFAULT_BET_COLS, 38 - where: eq(schema.betsTable.marketUri, uri), 34 + where: eq(betsTable.marketUri, uri), 39 35 orderBy: (bets, { desc }) => [desc(bets.createdAt)], 40 36 with: { 41 37 market: { ··· 49 45 export async function tryFindMarketResolutions(uri: string) { 50 46 return await db.query.resolutionsTable.findFirst({ 51 47 columns: DEFAULT_RESOLUTION_COLS, 52 - where: eq(schema.resolutionsTable.marketUri, uri), 48 + where: eq(resolutionsTable.marketUri, uri), 53 49 orderBy: (resolutions, { desc }) => [desc(resolutions.createdAt)], 54 50 with: { 55 51 market: { ··· 68 64 const { question, liquidity } = record; 69 65 const [closesAt, createdAt] = [new Date(record.closesAt), new Date(record.createdAt)]; 70 66 71 - const marketData: typeof schema.marketsTable.$inferInsert = { 67 + const marketData: typeof marketsTable.$inferInsert = { 72 68 uri, did, rev, rkey, cid, question, liquidity, closesAt, record, createdAt, 73 69 }; 74 70 75 - await db.insert(schema.marketsTable).values(marketData); 71 + await db.insert(marketsTable).values(marketData); 76 72 console.log("> Created Market!"); 77 73 } 78 74 } ··· 80 76 export async function tryDeleteMarket(did: ActorIdentifier, commit: DeleteCommit) { 81 77 const uri = `at://${did}/${commit.collection}/${commit.rkey}`; 82 78 console.log("> Deleting Market:", uri); 83 - await db.delete(schema.marketsTable).where(eq(schema.marketsTable.uri, uri)) 79 + await db.delete(marketsTable).where(eq(marketsTable.uri, uri)) 84 80 } 85 81 86 82 export async function tryCreateBet(did: ActorIdentifier, { record, rev, rkey, cid }: CreateCommit) { ··· 90 86 const { position, market } = record; 91 87 const createdAt = new Date(record.createdAt); 92 88 93 - const betData: typeof schema.betsTable.$inferInsert = { 89 + const betData: typeof betsTable.$inferInsert = { 94 90 uri, did, rev, rkey, cid, position, marketUri: market.uri, record, createdAt 95 91 } 96 92 97 - await db.insert(schema.betsTable).values(betData); 93 + await db.insert(betsTable).values(betData); 98 94 console.log("> Created Bet!"); 99 95 } 100 96 } ··· 102 98 export async function tryDeleteBet(did: ActorIdentifier, commit: DeleteCommit) { 103 99 const uri = `at://${did}/${commit.collection}/${commit.rkey}`; 104 100 console.log("> Deleting Bet:", uri); 105 - await db.delete(schema.betsTable).where(eq(schema.betsTable.uri, uri)) 101 + await db.delete(betsTable).where(eq(betsTable.uri, uri)) 106 102 } 107 103 108 104 export async function tryCreateResolution(did: ActorIdentifier, { record, rev, rkey, cid }: CreateCommit) { ··· 112 108 const { answer, market } = record; 113 109 const createdAt = new Date(record.createdAt); 114 110 115 - const resolutionData: typeof schema.resolutionsTable.$inferInsert = { 111 + const resolutionData: typeof resolutionsTable.$inferInsert = { 116 112 uri, did, rev, rkey, cid, answer, marketUri: market.uri, record, createdAt 117 113 } 118 114 119 - await db.insert(schema.resolutionsTable).values(resolutionData); 115 + await db.insert(resolutionsTable).values(resolutionData); 120 116 console.log("> Created Resolution!"); 121 117 } 122 118 } ··· 124 120 export async function tryDeleteResolution(did: ActorIdentifier, commit: DeleteCommit) { 125 121 const uri = `at://${did}/${commit.collection}/${commit.rkey}`; 126 122 console.log("> Deleting Resolution:", uri); 127 - await db.delete(schema.resolutionsTable).where(eq(schema.resolutionsTable.uri, uri)) 123 + await db.delete(resolutionsTable).where(eq(resolutionsTable.uri, uri)) 128 124 }
+12
src/db/schema.ts src/db/index.ts
··· 1 1 import { text, integer, json, pgTable, timestamp, pgEnum } from "drizzle-orm/pg-core"; 2 2 import { relations } from "drizzle-orm"; 3 + import { drizzle } from "drizzle-orm/node-postgres"; 3 4 4 5 const SHARED_SCHEMA = { 5 6 uri: text().primaryKey().notNull(), ··· 77 78 references: [marketsTable.uri], 78 79 }), 79 80 })); 81 + 82 + export const db = drizzle(process.env.DATABASE_URL!, { 83 + schema: { 84 + marketsTable, 85 + betsTable, 86 + resolutionsTable, 87 + marketsRelations, 88 + betsRelations, 89 + resolutionsRelations 90 + } 91 + });