|
| 1 | +# Prisma ORM Setup for TradeFlow API |
| 2 | + |
| 3 | +This document outlines the Prisma ORM implementation for PostgreSQL database integration in the TradeFlow API project. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +The following dependencies have been added to `package.json`: |
| 8 | +- `@prisma/client`: Prisma client for database queries |
| 9 | +- `prisma`: Prisma CLI for database management |
| 10 | + |
| 11 | +## Database Schema |
| 12 | + |
| 13 | +The Prisma schema is defined in `prisma/schema.prisma` with the following models: |
| 14 | + |
| 15 | +### Trade Model |
| 16 | +```prisma |
| 17 | +model Trade { |
| 18 | + id String @id @default(cuid()) |
| 19 | + poolId String |
| 20 | + userAddress String |
| 21 | + amountIn String // Using String to handle decimal values precisely |
| 22 | + amountOut String // Using String to handle decimal values precisely |
| 23 | + timestamp DateTime @default(now()) |
| 24 | + |
| 25 | + @@map("trades") |
| 26 | +} |
| 27 | +``` |
| 28 | + |
| 29 | +### Pool Model |
| 30 | +```prisma |
| 31 | +model Pool { |
| 32 | + id String @id @default(cuid()) |
| 33 | + address String @unique |
| 34 | + tokenA String |
| 35 | + tokenB String |
| 36 | + fee String |
| 37 | + createdAt DateTime @default(now()) |
| 38 | + updatedAt DateTime @updatedAt |
| 39 | + |
| 40 | + trades Trade[] |
| 41 | + |
| 42 | + @@map("pools") |
| 43 | +} |
| 44 | +``` |
| 45 | + |
| 46 | +### Token Model |
| 47 | +```prisma |
| 48 | +model Token { |
| 49 | + id String @id @default(cuid()) |
| 50 | + address String @unique |
| 51 | + symbol String |
| 52 | + name String |
| 53 | + decimals Int |
| 54 | + createdAt DateTime @default(now()) |
| 55 | + updatedAt DateTime @updatedAt |
| 56 | + |
| 57 | + @@map("tokens") |
| 58 | +} |
| 59 | +``` |
| 60 | + |
| 61 | +## Environment Configuration |
| 62 | + |
| 63 | +Add the following to your `.env` file: |
| 64 | + |
| 65 | +```env |
| 66 | +DATABASE_URL="postgresql://postgres:password@localhost:5432/tradeflow?schema=public" |
| 67 | +``` |
| 68 | + |
| 69 | +The `.env.example` file has been updated with the required database configuration. |
| 70 | + |
| 71 | +## Available Scripts |
| 72 | + |
| 73 | +The following npm scripts have been added for Prisma management: |
| 74 | + |
| 75 | +- `npm run prisma:generate` - Generate Prisma client |
| 76 | +- `npm run prisma:push` - Push schema to database |
| 77 | +- `npm run prisma:migrate` - Run database migrations |
| 78 | +- `npm run prisma:studio` - Open Prisma Studio |
| 79 | + |
| 80 | +## Usage |
| 81 | + |
| 82 | +### Prisma Service |
| 83 | + |
| 84 | +A global `PrismaService` is available throughout the application: |
| 85 | + |
| 86 | +```typescript |
| 87 | +import { PrismaService } from '../prisma/prisma.service'; |
| 88 | + |
| 89 | +@Injectable() |
| 90 | +export class YourService { |
| 91 | + constructor(private prisma: PrismaService) {} |
| 92 | + |
| 93 | + async createTrade(data: CreateTradeDto) { |
| 94 | + return this.prisma.trade.create({ |
| 95 | + data, |
| 96 | + }); |
| 97 | + } |
| 98 | +} |
| 99 | +``` |
| 100 | + |
| 101 | +### Trade Service |
| 102 | + |
| 103 | +A `TradeService` has been created with common operations: |
| 104 | + |
| 105 | +- `createTrade()` - Create a new trade record |
| 106 | +- `getTradesByUser()` - Get all trades for a specific user |
| 107 | +- `getTradesByPool()` - Get all trades for a specific pool |
| 108 | +- `getAllTrades()` - Get all trades with pagination |
| 109 | +- `createPool()` - Create a new pool |
| 110 | +- `getPoolByAddress()` - Get pool by address with trades |
| 111 | +- `createToken()` - Create a new token |
| 112 | +- `getTokenByAddress()` - Get token by address |
| 113 | + |
| 114 | +## Database Setup |
| 115 | + |
| 116 | +1. Install dependencies: |
| 117 | + ```bash |
| 118 | + npm install |
| 119 | + ``` |
| 120 | + |
| 121 | +2. Generate Prisma client: |
| 122 | + ```bash |
| 123 | + npm run prisma:generate |
| 124 | + ``` |
| 125 | + |
| 126 | +3. Push schema to database: |
| 127 | + ```bash |
| 128 | + npm run prisma:push |
| 129 | + ``` |
| 130 | + |
| 131 | +4. (Optional) Create and run migrations: |
| 132 | + ```bash |
| 133 | + npm run prisma:migrate |
| 134 | + ``` |
| 135 | + |
| 136 | +## Integration with Existing TypeORM |
| 137 | + |
| 138 | +The Prisma implementation runs alongside the existing TypeORM setup. Both can be used simultaneously: |
| 139 | + |
| 140 | +- Prisma uses `DATABASE_URL` environment variable |
| 141 | +- TypeORM uses individual `DB_HOST`, `DB_PORT`, `DB_USERNAME`, `DB_PASSWORD`, `DB_DATABASE` variables |
| 142 | + |
| 143 | +## Notes |
| 144 | + |
| 145 | +- String types are used for `amountIn` and `amountOut` to handle decimal values precisely |
| 146 | +- All models include appropriate timestamps for auditing |
| 147 | +- The Prisma module is globally available throughout the application |
| 148 | +- Database relationships are properly defined (Pool -> Trade) |
0 commit comments