tRPC API to Relational Database
The connection generator wires a tRPC API to a Relational Database project, generating a type-safe tRPC middleware plugin that makes a Prisma client available in your procedure context.
Prerequisites
Section titled “Prerequisites”Before using this generator, ensure you have:
- A
ts#trpc-apiproject - A
ts#rdbproject
Run the Generator
Section titled “Run the Generator”- Install the Nx Console VSCode Plugin if you haven't already
- Open the Nx Console in VSCode
- Click
Generate (UI)in the "Common Nx Commands" section - Search for
@aws/nx-plugin - connection - Fill in the required parameters
- Click
Generate
pnpm nx g @aws/nx-plugin:connectionyarn nx g @aws/nx-plugin:connectionnpx nx g @aws/nx-plugin:connectionbunx nx g @aws/nx-plugin:connectionYou can also perform a dry-run to see what files would be changed
pnpm nx g @aws/nx-plugin:connection --dry-runyarn nx g @aws/nx-plugin:connection --dry-runnpx nx g @aws/nx-plugin:connection --dry-runbunx nx g @aws/nx-plugin:connection --dry-runSelect your tRPC API project as the source and your relational database project as the target.
Options
Section titled “Options”| Parameter | Type | Default | Description |
|---|---|---|---|
| sourceProject Required | string | - | The source project |
| targetProject Required | string | - | The target project to connect to |
| sourceComponent | string | - | The source component to connect from (component name, path relative to source project root, or generator id). Use '.' to explicitly select the project as the source. |
| targetComponent | string | - | The target component to connect to (component name, path relative to target project root, or generator id). Use '.' to explicitly select the project as the target. |
Generator Output
Section titled “Generator Output”The generator creates a middleware file in your tRPC API project:
Directorypackages/api/src
Directorymiddleware
- <db-name>.ts tRPC plugin exposing the Prisma client in procedure context
Additionally, it updates your tRPC API’s serve-local target to start the database automatically when running locally.
Using the Middleware
Section titled “Using the Middleware”Register the Plugin
Section titled “Register the Plugin”Add the generated plugin to your tRPC router so all procedures using it gain access to the database:
import { t } from './init.js';import { createMyDbPlugin } from './middleware/my-db.js';
export const authenticatedProcedure = t.procedure .concat(createMyDbPlugin());Access the Database in Procedures
Section titled “Access the Database in Procedures”The plugin merges IMyDbContext into your procedure context, making myDb available as an optional property:
import { z } from 'zod';import { authenticatedProcedure } from '../router.js';
export const listUsers = authenticatedProcedure .output(z.array(z.object({ id: z.string(), name: z.string() }))) .query(async ({ ctx }) => { // ctx.myDb is the Prisma client — typed as Awaited<ReturnType<typeof getPrisma>> return await ctx.myDb!.user.findMany(); });MySQL: Disconnect After Each Request
Section titled “MySQL: Disconnect After Each Request”When the target database uses the MySQL engine, the generated middleware wraps opts.next() in a try/finally block that calls $disconnect():
return t.procedure.use(async (opts) => { const myDb = await getPrisma(); try { return await opts.next({ ctx: { ...opts.ctx, myDb } }); } finally { await myDb.$disconnect(); }});This addresses the MySQL adapter holding the Node.js event loop open after a query, which would otherwise prevent Lambda from flushing streaming responses. Disconnecting in finally releases the event loop so the response can complete. See MySQL: API Gateway Streaming Mode for details.
PostgreSQL does not require this — its adapter uses a connection pool configured with allowExitOnIdle: true.
Multiple Databases
Section titled “Multiple Databases”You can connect additional databases by running the generator again with a different target. Each database gets its own plugin and context interface:
export const dbProcedure = t.procedure .concat(createMyDbPlugin()) .concat(createOtherDbPlugin());Infrastructure
Section titled “Infrastructure”To allow your API to connect to the database at runtime, the API Lambda functions must be deployed into the same VPC as the database and granted network and IAM access.
In your application stack, deploy the API into the same VPC as the database, then call allowDefaultPortFrom and grantConnect to open the network path and grant IAM rds-db:connect permission to each Lambda handler:
import { MyDatabase } from ':my-scope/common-constructs';
const db = new MyDatabase(this, 'Db', { vpc, ... });
const api = new MyApi(this, 'Api', { integrations: MyApi.defaultIntegrations(this) .withDefaultOptions({ vpc, vpcSubnets: { subnetType: SubnetType.PRIVATE_WITH_EGRESS }, }) .build(),});
Object.entries(api.integrations).forEach(([operation, integration]) => { db.allowDefaultPortFrom(integration.handler, `Allow ${operation} to connect to the database`); db.grantConnect(integration.handler);});Deploy the API Lambda functions into a private subnet with egress, not a private isolated subnet. At runtime, getPrisma() retrieves database connection details from AWS AppConfig, which is a public AWS service endpoint that requires outbound internet access.
Pass the database module outputs into your API module so it can reach the database and read its runtime configuration:
module "my_database" { source = "../../common/terraform/src/app/dbs/my-database" vpc_id = module.vpc.vpc_id database_subnet_ids = module.vpc.private_isolated_subnet_ids lambda_subnet_ids = module.vpc.private_subnet_ids}
module "api" { source = "..." vpc_id = module.vpc.vpc_id private_subnet_ids = module.vpc.private_subnet_ids
appconfig_application_id = module.my_database.appconfig_application_id database_cluster_resource_id = module.my_database.cluster_resource_id database_runtime_user = module.my_database.database_runtime_user database_security_group_id = module.my_database.security_group_id database_port = module.my_database.cluster_port
environment_variables = { RUNTIME_CONFIG_APP_ID = module.my_database.appconfig_application_id }}Deploy the API Lambda functions into private subnets with egress, not private isolated subnets. Ensure the API Lambda role has rds-db:connect permission and that its security group can reach the database security group on the database port.
Local Development
Section titled “Local Development”The generator configures your tRPC API’s serve-local target to depend on the database’s serve-local target, so running:
pnpm nx serve-local <api-project-name>yarn nx serve-local <api-project-name>npx nx serve-local <api-project-name>bunx nx serve-local <api-project-name>will automatically start the local database alongside your API.