← Back to archive

Repo of the Day

Pyxlab/adonis-trpc

Published: Apr 6, 2023

Open repository ↗

@pyxlab/adonis-trpc provides an easy way to start using tRPC.

Summary

@pyxlab/adonis-trpc is a TypeScript package that wires tRPC into an AdonisJS application, exposing typed procedures through the framework's IoC container. The project is published under the MIT license and targets Node.js backends already running AdonisJS.

What it is useful for

The package is useful when an AdonisJS backend needs to expose end-to-end typed APIs to a TypeScript client without writing separate schema files or HTTP route layers. It supports Zod-based input validation, integration with AdonisJS auth through .protected procedures that surface ctx.auth.user, and works with Lucid ORM models on the inside. The procedural style fits internal services, BFFs, or full-stack TypeScript apps where the same types should flow from the database through the API to the client.

How engineers can use it

Install the package into an existing AdonisJS project with yarn add @pyxlab/adonis-trpc or npm install @pyxlab/adonis-trpc, then run node ace configure @pyxlab/adonis-trpc to generate the configuration file. Procedures live under app/Routes, grouped by folder, with an index.ts that merges them into a single appRouter. A procedure looks like this:

import procedure from '@ioc:Pyxlab/Adonis/Trpc/Procedure'
import { z } from 'zod'

export const createPost = procedure
  .protected
  .input(z.object({ title: z.string(), body: z.string() }))
  .mutation(async ({ ctx: { auth }, input }) => {
    return (await auth.user!.related('posts').create(input)).serialize()
  })

A built-in generator speeds up scaffolding. node ace make:procedure Post --model --table --resource creates the route folder, a Lucid model, a migration, and resource procedures (getById, create, list, update, delete) for the Post entity. Add --exact to skip the Procedure suffix, as in node ace make:procedure Auth createSession --exact. Use .query for reads and .mutation for writes; the protected modifier gates access behind AdonisJS auth.

One documented caveat: when returning Lucid models, the README notes that serialize is inherited from BaseModel, so engineers need to override serialize on their model and cast the result to InferTypeModel to keep end-to-end type inference intact. See the README for the full model-typing snippet and the contribution guidelines before sending changes upstream.