Prisma
Navios has no Prisma package and needs none. A PrismaClient is one long-lived object with a connect step and a disconnect step, which is exactly what a singleton service with lifecycle hooks is. This page assumes a working Prisma setup with a User model that has id, name and a unique email.
The client as a service
Extend the generated client and connect it to the service lifecycle:
onServiceInitruns once, when the service is first resolved. Everything that injectsPrismaServiceis built duringapp.init(), so a database that cannot be reached fails start-up instead of the first request.onServiceDestroyruns duringapp.close(), after in-flight requests have finished.- When your Prisma version needs constructor options, such as a driver adapter, add a constructor that calls
super({ ... }). It must take no parameters of its own; read configuration withinject()or from the environment.
The hooks are described in Lifecycle.
Using it in a service
PrismaService is injected like any other service. This is the Quick start's UsersService backed by the database:
The methods are now asynchronous, so the controller awaits them: (await this.users.findById(urlParams.userId)) ?? reply(404, ...). The optional db parameter is used by the transaction section below.
Response validation strips fields the declaration's schema does not list, so a column added to the model later, such as a password hash, does not leak through getUser.
Replacing it in tests
Give the application another provider for PrismaService. It wins over the class itself, and the real client is never constructed or connected.
A fake only needs the model methods the code under test calls. provide() wants every public member of what it replaces, hence the cast.
For tests that should run real queries, keep PrismaService and point it at a test database through its usual configuration. See Testing for the rest of the setup.
A transaction per request
Prisma's interactive transaction is a callback: $transaction(fn) commits when fn resolves and rolls back when it rejects. An interceptor has the same shape around a request, so one can wrap the other. The transaction client goes into a context key for the handler to pick up.
Attach it to the handlers that write, and pass the client on:
What decides the outcome:
An error rolls back because next() rejects with it, and it is turned into a response only after the interceptor has exited. A handler that returns reply(404, ...) or reply(409, ...) commits; throw an HttpException when an error status must also undo the work.
The transaction is committed before the response is written, so a client that receives a success can rely on it.
A transaction holds a database connection for as long as it is open, and Prisma closes one that stays open past its timeout. Keep the interceptor off routes that only read, and off routes that stream their response or wait on slow external services.
Services are singletons, so they cannot inject the transaction; it has to reach them as an argument, as create(input, db) does. Code several calls deep that cannot take the argument can read Tx from currentContext(), at the cost of enabling asyncContext for the application.