This document outlines the Prisma ORM implementation for PostgreSQL database integration in the TradeFlow API project.
The following dependencies have been added to package.json:
@prisma/client: Prisma client for database queriesprisma: Prisma CLI for database management
The Prisma schema is defined in prisma/schema.prisma with the following models:
model Trade {
id String @id @default(cuid())
poolId String
userAddress String
amountIn String // Using String to handle decimal values precisely
amountOut String // Using String to handle decimal values precisely
timestamp DateTime @default(now())
@@map("trades")
}model Pool {
id String @id @default(cuid())
address String @unique
tokenA String
tokenB String
fee String
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
trades Trade[]
@@map("pools")
}model Token {
id String @id @default(cuid())
address String @unique
symbol String
name String
decimals Int
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
@@map("tokens")
}Add the following to your .env file:
DATABASE_URL="postgresql://postgres:password@localhost:5432/tradeflow?schema=public"The .env.example file has been updated with the required database configuration.
The following npm scripts have been added for Prisma management:
npm run prisma:generate- Generate Prisma clientnpm run prisma:push- Push schema to databasenpm run prisma:migrate- Run database migrationsnpm run prisma:studio- Open Prisma Studio
A global PrismaService is available throughout the application:
import { PrismaService } from '../prisma/prisma.service';
@Injectable()
export class YourService {
constructor(private prisma: PrismaService) {}
async createTrade(data: CreateTradeDto) {
return this.prisma.trade.create({
data,
});
}
}A TradeService has been created with common operations:
createTrade()- Create a new trade recordgetTradesByUser()- Get all trades for a specific usergetTradesByPool()- Get all trades for a specific poolgetAllTrades()- Get all trades with paginationcreatePool()- Create a new poolgetPoolByAddress()- Get pool by address with tradescreateToken()- Create a new tokengetTokenByAddress()- Get token by address
-
Install dependencies:
npm install
-
Generate Prisma client:
npm run prisma:generate
-
Push schema to database:
npm run prisma:push
-
(Optional) Create and run migrations:
npm run prisma:migrate
The Prisma implementation runs alongside the existing TypeORM setup. Both can be used simultaneously:
- Prisma uses
DATABASE_URLenvironment variable - TypeORM uses individual
DB_HOST,DB_PORT,DB_USERNAME,DB_PASSWORD,DB_DATABASEvariables
- String types are used for
amountInandamountOutto handle decimal values precisely - All models include appropriate timestamps for auditing
- The Prisma module is globally available throughout the application
- Database relationships are properly defined (Pool -> Trade)