Services

Use services for messages & working with db

What is service?

You can use a service to create messages and work with the db, and your code will be cleaner

Export message to service

We recommend that you describe the message you will use in the controller in the service and retrieve it with this.service.message

app.service.ts
import { Service } from 'nestgram';

@Service()
export class AppService {
  get helloMessage(): string {
    return 'Hello, world!';
  }
}
app.controller.ts
import { OnCommand, Controller } from 'nestgram';
import { AppService } from './app.service';

@Controller()
export class AppController {
  constructor(private readonly appService?: AppService) {}

  @OnCommand('start')
  async start(): Promise<string> {
    return this.appService.helloMessage;
  }
}

Working with db

We recommend to work with db in controllers. It will be more modular. If you'll use database work in services, we will provide you with a good work with it. You can read more about it here

Last updated