Skip to main content
nestrs gives you SQL paths depending on how much structure you want:
  • nestrs-sea-orm (recommended ORM) — SeaORM Repo, ambient transactions, RowAuthz / AbilityAuthz. See adapters/sea-orm.
  • SqlxDatabaseModule (feature database-sqlx) — SQLx AnyPool + CrudService when you want direct SQL.
  • nestrs-prisma — PrismaModule / prisma_model! for a Prisma-style DX.
  • MongoModule — document store path.
For a TypeORM/Sequelize-style Active Record layer, prefer nestrs-sea-orm.

Path 1: SqlxDatabaseModule (direct SQLx)

SqlxDatabaseModule manages a single AnyPool shared across all injected consumers. Call for_root before NestFactory::create to register the URL, then import the module.

Cargo.toml

Environment variable

Module registration

Injecting SqlxDatabaseService

SqlxDatabaseService exposes the pool via pool() and a ping() health check. Use pool() to run any SQLx query directly.

Migrations and seeding (nestrs-cli db)

Both SQL paths compose with the built-in migrations CLI (the db feature of nestrs-scaffold). It works on plain SQL migration files — there is no DDL-diff engine or entity auto-discovery.
Key details:
  • URL resolution — --database-url > DATABASE_URL > NESTRS_DB__URL.
  • File naming — sequence-numbered (<NNN>_<name>.sql, zero-padded 3 digits), not timestamped: deterministic, greppable, sortable, hand-writable.
  • Reversible migrations — --reversible writes paired .up.sql / .down.sql files; the sqlx backend records history in _sqlx_migrations via sqlx::migrate::Migrator.
  • Seeding — --bin passes through to a Cargo seed binary (the common case: a bin that composes with CrudService<T>); --seed-file runs a SQL file inside an explicit transaction so a mid-file failure rolls back.
  • Prisma pass-through — --backend prisma shells out to npx prisma …. Prisma records its own history and has no revert; use the sqlx backend with --reversible if you need downgrades.
See the CLI reference for the full flag table.

Path 2: nestrs-prisma

nestrs-prisma ships PrismaModule and PrismaService with higher-level helpers: query_all_as for typed row mapping, execute for DDL and DML, and prisma_model! for declarative repositories that generate find_unique, find_many, create, update, delete, and more.

Cargo.toml

Enable exactly one SQLx backend feature: sqlx-postgres, sqlx-mysql, or sqlx-sqlite. async-trait must be a direct dependency of any crate that uses prisma_model!.

Schema

Create a Prisma schema file at prisma/schema.prisma. nestrs-prisma reads the schema for optional sync but does not require the Prisma CLI at runtime.
Apply the schema to your database — nestrs-cli db --backend prisma migrate run is the nestrs-native pass-through, or use Prisma directly:

Bootstrap PrismaModule

Call PrismaModule::for_root_with_options before NestFactory::create so the pool is ready before DI builds providers.

Raw queries with PrismaService

query_all_as maps rows to any type that implements sqlx::FromRow. execute runs DDL or parameterless DML. query_scalar is useful for health checks.

Declarative repositories with prisma_model!

prisma_model! generates a full repository from a table declaration. The macro expands a struct, CreateInput, Where, Update, OrderBy, and a PrismaUserRepository trait — all accessible via prisma.user().
After the macro expands, you can use the full repository API:
PrismaError implements Into<HttpException>. Map errors with .map_err(HttpException::from) or ? when the return type is Result<_, HttpException>.

HTTP controller

Run the quickstart example

The nestrs-prisma crate ships a full end-to-end example with two related models, CRUD operations, and schema sync:

Path 3: MongoModule

MongoModule wraps the official mongodb driver. Call for_root (or for_root_async for vault / env factories) with a connection URI before NestFactory::create, then inject MongoService to access databases and collections. MongoService::model::<T>() is the NestJS @InjectModel analogue over MongoRepository::for_feature.

Cargo.toml

Bootstrap and inject

Service with typed collections

MongoService::model::<T>() is the NestJS @InjectModel analogue: it returns MongoRepository<T> for a type that implements Document (#[derive(Document)]) after MongoModule::for_feature("db_name"). See the MongoDB recipe.

Troubleshooting