Skip to content

Instantly share code, notes, and snippets.

@Shair17
Created November 10, 2022 21:02
Show Gist options
  • Save Shair17/e6fa21f1b37bb6ea6a33ad1b6a45c3ba to your computer and use it in GitHub Desktop.
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)
// 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