# Datasources & Generators

Configure your database connection and code generation settings in your Prax schema.

## Datasources

### What is a Datasource?

The datasource block configures your database connection. It specifies the database provider, connection URL, and optional connection settings. Every schema must have at least one datasource.

### Basic Configuration

### Advanced Configuration

For production deployments, you may need additional settings like shadow databases for migrations or direct connections for specific operations.

### Datasource Properties

Property | Required | Description
--- | --- | ---
`provider` | Yes | Database provider: `postgresql`, `mysql`, or `sqlite`
`url` | Yes | Connection URL (use `env("VAR")` for environment variables)
`directUrl` | No | Direct connection URL bypassing connection pooler
`shadowDatabaseUrl` | No | Shadow database URL for safe migration testing
`connectionLimit` | No | Maximum number of connections in the pool
`poolTimeout` | No | Timeout in seconds for acquiring a connection from the pool
`relationMode` | No | How relations are handled: `foreignKeys` (default) or `prisma`

### Multiple Datasources

You can define multiple datasources for connecting to different databases. Use the `@@datasource()` attribute to specify which datasource a model belongs to.

### Connection URL Formats

Provider | URL Format
--- | ---
`postgresql` | postgresql://USER:PASSWORD@HOST:PORT/DATABASE?schema=SCHEMA
`mysql` | mysql://USER:PASSWORD@HOST:PORT/DATABASE
`sqlite` | file:./path/to/dev.db or sqlite::memory:

## Generators

### What is a Generator?

Generators transform your schema into code. The primary generator creates the Prax client, but you can add additional generators for documentation, API schemas, and more.

### Basic Generator

### Advanced Configuration

### Generator Properties

Property | Required | Description
--- | --- | ---
`provider` | Yes | Generator provider name (e.g., `prax-client-rust`)
`output` | Yes | Output directory for generated files
`previewFeatures` | No | Array of preview features to enable
`binaryTargets` | No | Target platforms for binary engines
`plugins` | No | Array of plugins to enable
`engineType` | No | Engine type: `library` (default) or `binary`

### Plugins

Plugins extend code generation with additional features. Enable them in your generator configuration.

#### serde

Adds `#[derive(Serialize, Deserialize)]` to all generated types for JSON serialization.

#### graphql

Generates async-graphql compatible types with `SimpleObject`, `InputObject`, and `Enum` derives.

#### validator

Adds validation attributes from your schema to generated types using the `validator` crate.

#### debug

Adds `#[derive(Debug)]` to all generated types for debugging output.

#### json_schema

Generates JSON Schema definitions for all models, useful for API documentation and frontend type generation.

### Custom Generators

You can use additional generators to create documentation, TypeScript clients, OpenAPI specifications, and more.

## Environment Variables

Use the `env("VAR_NAME")` function to reference environment variables. This keeps sensitive data out of your schema file.

Security: Never commit your `.env` file to version control. Add it to `.gitignore`. Use `.env.example` to document required variables.

## Complete Schema Example

Here's a well-organized schema file showing the recommended structure: