# Schema Introspection

Generate Prax schemas from existing databases with the `prax db pull` command.

## Overview

Schema introspection analyzes your existing database and generates a Prax schema file. This is useful for migrating existing projects or keeping your schema in sync with database changes.

📊

#### Tables & Columns

Types, constraints, defaults

🔗

#### Relations

Foreign keys, references

📇

#### Indexes & Views

Performance optimizations

## Basic Usage

The simplest way to generate a schema from your database.

## Filtering Tables

Include or exclude specific tables using glob patterns.

## Including Views

Optionally include views and materialized views in the generated schema.

## Output Formats

Export the schema in different formats for various use cases.

## MongoDB Schema Inference

MongoDB is schemaless, so Prax infers the schema by sampling documents.

## Generated Schema Example

Here's what an introspected schema looks like:

## Programmatic API

Use the introspection API directly in your Rust code.

## MongoDB Type Inference

Prax analyzes document samples to infer types, including arrays and embedded documents.

## Type Mapping

How database types are mapped to Prax types:

## CLI Options Reference

Option | Description | Default
--- | --- | ---
`--output, -o` | Output file path | prax/schema.prax
`--force, -f` | Overwrite without prompting | false
`--schema, -s` | Database schema to introspect | public/dbo
`--tables` | Glob pattern for tables | * (all)
`--exclude` | Glob pattern to exclude | none
`--include-views` | Include views | false
`--include-materialized-views` | Include materialized views | false
`--comments` | Include column comments | false
`--format` | Output format (prax, json, sql) | prax
`--print` | Print to stdout | false
`--sample-size` | MongoDB sample size | 100

## Best Practices

#### Review Generated Schema

Always review the generated schema. Introspection infers relations from foreign keys, but you may want to add or adjust relation names for clarity.

#### Use Version Control

Commit your schema file to version control. Compare diffs after re-introspecting to catch unexpected database changes.

#### MongoDB Sample Size

For MongoDB, use a larger sample size if your documents vary significantly. This improves type inference accuracy but takes longer.

#### Exclude Internal Tables

Use `--exclude` to skip migration tables, Prisma internals (_prisma*), and other non-application tables.