Skip to content

lukas-reining/nestjs-azure-func-trigger

Repository files navigation

Nest Logo

A progressive Node.js framework for building efficient and scalable server-side applications.

NPM Version Package License NPM Downloads Travis Linux Coverage Discord Backers on Open Collective Sponsors on Open Collective

Description

Azure Functions Trigger module for Nest.

This package works well together with @nestjs/azure-func-http which provides azure function http triggers for Nest.

This package provides a more general way to use any kind of azure function trigger ( timed, event bus, ...) for triggering methods of your Nest services.

Installation

Using npm:

$ npm install nestjs-azure-func-trigger

Tutorial

Adding a trigger is done by creating a new Azure Function in the app folder and then providing the createApp to the AzureFunctionTriggerAdapter.

import { InvocationContext } from '@azure/functions';
import { AzureFunctionTriggerAdapter } from 'nestjs-azure-func-trigger';
import { createApp } from '../src/main';

export default async function(context: InvocationContext): Promise<void> {
  return AzureFunctionTriggerAdapter.handle(createApp, context);
}

When the function is triggered it starts up and calls every service method that is annotated with AzureFunctionTrigger decorator that specified this function by the name parameter.

import { Injectable } from '@nestjs/common';

@Injectable()
class ClassWithTrigger {
  @AzureFunctionTrigger('FunctionName')
  public timerWithContext (
    @AzureFunctionContext() context: InvocationContext,
  ) {
    context.log('TimerWithContext', context);
  }
}

A function.json for this case looks like the following. Important is only the name of the function folder which in this case is "FunctionName"

{
  "bindings": [
    {
      "name": "trigger",
      "type": "timerTrigger",
      "direction": "in",
      "schedule": "0 0,30 7-23 * * *"
    }
  ],
  "scriptFile": "../dist/FunctionName/index.js"
}