This guide describes how to migrate from fastify-decorators
v3 to v4.
Note: migration guide from v2.x to v3.x is here.
Update dependencies:
Dependency | Minimal supported version |
---|---|
Node.js | 20.12.0 |
TypeScript | 5.0.0 |
Fastify | 4.0.0 |
Replace usages of ControllerType
with Scope
:
before:
import { Controller, ControllerType } from 'fastify-decorators';
@Controller({
route: '/',
type: ControllerType.SINGLETON,
})
after:
import { Controller, Scope } from 'fastify-decorators';
@Controller({
route: '/',
scope: Scope.SINGLETON,
})
v3 | v4 |
---|---|
Built-in DI | DI moved to @fastify-decorators/simple-di |
In order to keep previous behavior there are 3 steps:
-
Add
@fastify-decorators/simple-di
to your application with your favorite package manager -
Remove
reflect-metadata
and explicit import of this library, simple-di package will import it itself -
Update imports in your application, for example:
Before:
import { Controller, GET, Initializer, Inject, Service } from 'fastify-decorators'; @Service() class SampleService { @Initializer() async initializeService() { // some async stuff here } } @Controller('/') export class SampleController { @Inject(SampleService) private service!: SampleService; @GET() async handleRequest() { // some stuff with service } }
After:
import { Controller, GET } from 'fastify-decorators'; import { Initializer, Inject, Service } from '@fastify-decorators/simple-di'; @Service() class SampleService { @Initializer() async initializeService() { // some async stuff here } } @Controller('/') export class SampleController { @Inject(SampleService) private service!: SampleService; @GET() async handleRequest() { // some stuff with service } }