Created
November 10, 2022 21:02
-
-
Save Shair17/e6fa21f1b37bb6ea6a33ad1b6a45c3ba to your computer and use it in GitHub Desktop.
An example for fastify + fastify decorators and plugins (db connector, and so on)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// server.ts | |
import fastify from 'fastify'; | |
import { bootstrap } from 'fastify-decorators'; | |
const app = fastify() | |
// Note that this plugin adds a decorator to the fastify instance like: fastify.awesomePlugin(...) | |
app.register(require('some-awesome-plugin')) | |
app.register(bootstrap, { | |
controllers: [...], | |
}); | |
// app.service.ts | |
import type { FastifyInstance } from 'fastify' | |
import { Service, getInstanceByToken, FastifyInstanceToken } from 'fastify-decorators' | |
@Service() | |
export class AppService { | |
private readonly fastify = getInstanceByToken<FastifyInstance>(FastifyInstanceToken); | |
async getApp() { | |
// Now you can access to fastify instance by: | |
this.fastify.awesomePlugin(...) | |
return { | |
ok: true | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment