Skip to content

tRPC API to DynamoDB

The connection generator wires a tRPC API to a TypeScript DynamoDB project, configuring local development so both start together automatically.

Before using this generator, ensure you have:

  1. A tRPC API project (generated with ts#api)
  2. A ts#dynamodb project
  1. Install the Nx Console VSCode Plugin if you haven't already
  2. Open the Nx Console in VSCode
  3. Click Generate (UI) in the "Common Nx Commands" section
  4. Search for @aws/nx-plugin - connection
  5. Fill in the required parameters
    • Click Generate

    Select your tRPC API project as the source and your DynamoDB project as the target.

    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.
    preferInstallDependencies boolean true Whether to prefer installing dependencies after the generator runs. Set to false to defer installing when batching multiple generators (an install still runs if needed so subsequent generators can compute the Nx project graph); install once at the end.

    The generator updates your tRPC API’s project.json to add a dependency from its dev target to the DynamoDB project’s dev target. No source files are modified.

    Import entity factories from the DynamoDB package and use them inside your tRPC procedures:

    packages/api/src/procedures/example.ts
    import { createExampleEntity } from ':my-scope/my-table';
    import { publicProcedure } from '../init.js';
    export const listExamples = publicProcedure
    .query(async () => {
    const entity = await createExampleEntity();
    const result = await entity.scan.go();
    return result.data;
    });

    To allow Lambda functions to access the DynamoDB table, grant the necessary permissions in your infrastructure.

    Call grantReadWriteData on the table construct. This grants both the DynamoDB and KMS permissions required by the Lambda execution role:

    packages/infra/src/stacks/application-stack.ts
    import { MyTable } from ':my-scope/common-constructs';
    const table = new MyTable(this, 'Table');
    const api = new Api(this, 'Api', {
    integrations: Api.defaultIntegrations(this).build(),
    });
    Object.entries(api.integrations).forEach(([, integration]) => {
    table.grantReadWriteData(integration.handler);
    });

    The connection generator configures your project’s dev target to depend on the DynamoDB project’s dev target. DynamoDB Local will start automatically alongside your project when running dev.

    The LOCAL_DEV=true environment variable is set automatically, so getDynamoDBClient() and resolveTableName() connect to the local DynamoDB Local instance instead of AWS.