Skip to content

Tutorials

Deep dives into seven complete Typeix applications: REST with TypeORM and Postgres or MongoDB, code-first GraphQL, OpenAPI documentation, server-side rendering with file uploads, and WebSocket or Socket.IO controllers.

5 min read Source

The documentation explains the framework piece by piece. These tutorials do the opposite: each page takes one complete, runnable application from resty-starters and explains how the concepts fit together in it — the module graph, where dependency injection happens, how a request travels through the stack, and why the code is written the way it is.

Every starter is deliberately small. There is no generated CRUD layer and no hidden abstraction: you can read the whole application in a few minutes, then extend it.

The eight starters

Starter Surface Concepts covered
REST API on Postgres REST + OpenAPI Modules with exports, async providers, TypeORM DataSource lifecycle, a transactional request interceptor, migrations
REST API on MongoDB REST + OpenAPI MongoRepository, ObjectId as an identifier, documenting a type that has no JSON Schema equivalent
Code-first GraphQL REST + GraphQL + OpenAPI Schema from decorators, resolvers as providers, arguments, field resolvers, method interceptors on resolvers
GraphQL over TypeORM GraphQL + REST + OpenAPI One class as TypeORM entity and GraphQL object type, resolvers inside a feature module, database-free schema tests
Server-rendered app with uploads HTML + multipart Handlebar rendering, streaming file upload, custom route rules, response caching, controller-level error handling
WebSockets HTTP + WebSocket @WebSocketController, subscriptions, socket injection
Socket.IO HTTP + Socket.IO @SocketIOController, event subscriptions, middleware hooks
Securing a REST API REST + OpenAPI One rule list over every route, JWT and Basic login, custom checkers, tenant isolation, @Guard / @GuardResult, request-scoped identity

Common anatomy

All eight share one bootstrapping shape, which is the single most important pattern to internalise:

import {pipeServer} from "@typeix/resty";
import {createServer} from "http";

async function bootstrap() {
  const server = createServer();
  await pipeServer(server, AppModule);   // 1. builds the injector, registers routes
  await pipeGraphQL(server, AppModule);  // 2. optional: attach more protocols
  await pipeSwagger(server, AppModule);  //    to the same module graph
  server.listen(8080);
}
  1. pipeServer resolves the module graph, instantiates providers, attaches the router and registers every route a controller declares. It stores the module injector on the root module class.
  2. pipeGraphQL, pipeSwagger and pipeWebSocket read that stored injector. They must therefore run after pipeServer, and they reuse the same providers rather than constructing a second application.

That ordering is the reason a GraphQL resolver and a REST controller can share the same UserService instance, and why the OpenAPI document is generated from the routes the router actually registered.

Decorators are metadata

The decorators in these starters (@Controller, @RootModule, @Query, @ApiProperty, …) only write metadata. Nothing is registered or executed until pipeServer scans it. That is what makes build-time tools possible: the GraphQL and OpenAPI pages can build a schema or a document from a module class without opening a socket.

Running a starter

git clone https://github.com/typeix/resty-starters
cd resty-starters
docker compose up -d --wait      # postgres + mongo, already configured
cd api-typeorm-sql
npm install
npm run start                    # or: npm run start:dev

The repository ships a docker-compose.yml at its root that starts every service the starters need, with the credentials already committed in each src/ormconfig.json:

Service Image Credentials Used by
postgres postgres:16-alpine postgres / admin, database typeix api-typeorm-sql, graphql-typeorm-sql
mongo mongo:7 no auth, database typeix api-typeorm-mongo
docker compose up -d postgres    # only what the starter you run needs
docker compose up -d mongo
docker compose down              # keep volumes
docker compose down -v           # drop the schema and start clean

Only two starters need a database. The remaining six start with no external service:

cd resty-starters/ts-graphql && npm install && npm run start

Both Postgres starters share one database

api-typeorm-sql and graphql-typeorm-sql ship the same migration and both target the typeix database. The migration is recorded once in the migrations table, so the second application sees it as applied and both starters read the same rows — convenient when comparing the REST and GraphQL implementations against identical data.

Script Effect
npm run start typeix start, compiles and runs from source
npm run start:dev typeix start --watch
npm run build typeix build, emits dist/
npm run start:prod node dist/bootstrap.js
npm test Jest, unit and in-process integration tests
npm run test:e2e Jest with test/jest-e2e.json

The starters pin the framework at ^9.2.0 and require Node ^26.0.0; each tsconfig.json enables experimentalDecorators and emitDecoratorMetadata, which every decorator in this section depends on.

How the starters are tested

Two patterns appear throughout, and both are worth copying:

In-process requests. fakeHttpServer pipes the application with isMockServer: true and gives you typed helpers, so a test exercises the real router, the real injector and the real interceptors without binding a port:

const app = await fakeHttpServer(AppModule);
const response = await app.GET("/");
expect(response.getStatusCode()).toEqual(200);

Build-time artefacts. The OpenAPI and GraphQL starters assert the generated artefact (SwaggerModule.createDocument(...), GraphQLModule.printSchema(...)) instead of scraping HTTP responses. A schema regression is then caught by a fast unit test with no server and no database.

The GraphQL starters use a real listening server for request tests, because pipeGraphQL needs a server.Server instance and FakeServerApi keeps the one it created private. A throwaway listener on port 0 avoids fixed ports in CI.

Where to start

  • New to the framework: REST API on Postgres covers modules, providers, controllers, interceptors and tests in one application.
  • Coming from NestJS with a GraphQL codebase: Code-first GraphQL maps the type-graphql concepts one by one.
  • Building something user-facing: Server-rendered app with uploads shows the parts of the framework that are not JSON APIs.
  • Building something authenticated: Securing a REST API puts every security feature behind a real route — one rule list, JWT and Basic login, tenant isolation and method guards.