Skip to main content
nestrs gives you two SQL integration paths depending on how much structure you want. SqlxDatabaseModule (feature database-sqlx) wraps an SQLx AnyPool and is the right choice when you want direct control over SQL queries with minimal abstraction. nestrs-prisma adds a PrismaModule, a PrismaService, and the prisma_model! macro for a Prisma-style developer experience without requiring a separate codegen step. MongoDB lives under a third path via MongoModule.

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.

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:

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 with a connection URI before NestFactory::create, then inject MongoService to access databases and collections.

Cargo.toml

Bootstrap and inject

Service with typed collections

Troubleshooting