# ISPM ERP Database Documentation

ISPM ERP uses Prisma as its ORM and SQLite as the default database engine for local development and simple standalone deployments.

## Prisma schema overview

The Prisma schema is located in `server/prisma/schema.prisma`. It defines the database connection, Prisma client generator, and the application models used by the backend.

By default:

- Provider: `sqlite`
- Database URL: `file:./dev.db`

## Models

### User

Represents application users who can authenticate and access the ERP.

Typical responsibilities:

- Login and authentication
- Role-based access to the application
- Administrative management

### AnneeScolaire

Represents an academic year.

Typical responsibilities:

- Define school year periods
- Organize data by academic cycle
- Link departments, students, and financial records to a specific year

### Filiere

Represents a department, program, or field of study.

Typical responsibilities:

- Organize students by academic program
- Associate programs with academic years

### Etudiant

Represents a student record.

Typical responsibilities:

- Store student identity and enrollment information
- Associate students with departments and academic years
- Track administrative and financial status

### Echeancier

Represents a payment schedule or installment plan.

Typical responsibilities:

- Define expected payment deadlines
- Track planned tuition or fee installments

### Paiement

Represents a payment made by a student.

Typical responsibilities:

- Record paid amounts
- Track payment dates and references
- Link payments to students and schedules

### Document

Represents uploaded or generated documents related to the ERP.

Typical responsibilities:

- Store administrative files
- Associate documents with students or other entities

### Settings

Represents application-wide configuration values.

Typical responsibilities:

- Store configurable ERP settings
- Centralize system preferences

## Resetting the database

To reset the local database and reapply migrations, run the following from `server/`:

```bash
npx prisma migrate reset
```

This command will:

- Drop the database
- Recreate it
- Reapply all migrations
- Optionally run the seed script

Use it carefully because it deletes existing data.

## Seeding the database

To populate the database with initial data, run:

```bash
npx prisma db seed
```

This is typically used to create the initial administrator account and baseline application data.

Default seeded credentials:

- Email: `admin@ispm.ma`
- Password: `admin123`

## Backup strategy

### SQLite backup

For SQLite, the simplest backup strategy is to copy the database file:

```bash
cp server/dev.db backups/dev-$(date +%F).db
```

You should also back up:

- `server/.env`
- Uploaded documents, if stored on disk

### Production recommendation

SQLite is easy to manage, but for production workloads you may prefer PostgreSQL for:

- Better concurrency
- Improved reliability under load
- Easier managed hosting options
- Better operational tooling

## Migrating to PostgreSQL for production

To migrate Prisma from SQLite to PostgreSQL:

1. Update `server/prisma/schema.prisma`
2. Change the datasource provider from `sqlite` to `postgresql`
3. Update `DATABASE_URL` in `server/.env`

Example:

```env
DATABASE_URL=postgresql://user:password@localhost:5432/ispm_erp
```

Then generate and apply migrations again for the new database target.

Before switching in production, review:

- Connection pooling
- Backup and restore procedures
- Hosting and monitoring strategy