# defrost-db

A database client package for Defrost protocol that provides access to the
database schema and models.

## Installation

```bash
npm install defrost-db
# or
yarn add defrost-db
# or
pnpm add defrost-db
```

## Usage

### Basic Usage

```typescript
import { getPrismaClient } from "defrost-db";

// Connect to the database
const prisma = getPrismaClient(
	"postgresql://username:password@localhost:5432/defrost"
);

// Example: Query all projects
async function getAllProjects() {
	const projects = await prisma.project.findMany();
	return projects;
}

// Always close the connection when done
async function cleanup() {
	await prisma.$disconnect();
}
```

### With TypeScript Type Safety

```typescript
import { getPrismaClient, Project, Pool, User } from "defrost-db";

// Connect with full type information
const prisma = getPrismaClient(
	"postgresql://username:password@localhost:5432/defrost"
);

// All return values are properly typed
async function getProjectById(id: string): Promise<Project | null> {
	return await prisma.project.findUnique({
		where: { id },
	});
}

// Using included relationship types
async function getPoolsWithProjects(): Promise<
	(Pool & { project: Project })[]
> {
	return await prisma.pool.findMany({
		include: {
			project: true,
		},
	});
}

// Type-safe user data
async function getUserStakes(userAddress: string) {
	const user = (await prisma.user.findFirst({
		where: { id: userAddress },
		include: {
			stake: true,
			unstake: true,
		},
	})) as User & { stake: Stake[]; unstake: Unstake[] };

	return user;
}
```

## Database Schema

The package includes the Prisma schema for the Defrost protocol database. The
main entities include:

- `User`: User information and statistics
- `Project`: Project details
- `Pool`: Staking pools with their configurations
- `Stake`: User staking records
- `Unstake`: User unstaking records
- `InterestClaim`: Interest claim records
- `PlatformStatistics`: Overall platform statistics
- `EmissionRate`: Emission rate records for pools
- `NativeExchangeRateSnapshot`: Native token exchange rate snapshots
- `ProjectExchangeRateSnapshot`: Project token exchange rate snapshots

## Environment Variables

When using this package, you can either:

1. Pass the database URL directly to the `getPrismaClient` function
2. Set the `DB_URL` environment variable

## Type Safety

This package exports TypeScript definitions for all database models, making it
fully type-safe. You can import the types directly:

```typescript
import { User, Project, Pool, Stake } from "defrost-db";
```

## License

MIT
