Skip to content

Engelce/nestjs-temporal

 
 

Repository files navigation

Nest Logo

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

NPM Version Package License NPM Downloads CircleCI Coverage Discord Backers on Open Collective Sponsors on Open Collective Support us

nestjs-temporal

Temporal module for Nest.

Quick start

import { Module } from '@nestjs/common';
import { TemporalModule } from 'nestjs-temporal';

@Module({
  imports: [
    TemporalModule.forRoot({
      taskQueue: 'default',
      workflowsPath: require.resolve('./temporal/workflow'),
    }),
    
    TemporalModule.registerClient(),
  ],
})
export class AppModule {}
import { Injectable } from '@nestjs/common';
import { Activities, Activity } from 'nestjs-temporal';
import { ActivityInterface } from '@temporalio/activity';

@Injectable()
@Activities()
export class GreetingActivity {
  constructor() {}

  @Activity()
  async greeting(name: string): Promise<string> {
    return 'Hello ' + name;
  }
}

export interface IGreetingActivity extends ActivityInterface {
  greeting(name: string): Promise<string>;
}
import { proxyActivities } from '@temporalio/workflow';
// Only import the activity types
import { IGreetingActivity } from '../activities';

const { greeting, reverseGreeting } = proxyActivities<IGreetingActivity>({
  startToCloseTimeout: '1 minute',
});

export async function example(name: string): Promise<string> {
  return await greeting(name);
}
import { Controller, Post } from '@nestjs/common';
import { Connection, WorkflowClient } from '@temporalio/client';
import { InjectTemporalClient } from 'nestjs-temporal';

@Controller()
export class AppController {
  constructor(
    @InjectTemporalClient() private readonly temporalClient: WorkflowClient,
  ) {}

  @Post()
  async greeting() {
    const handle = await this.temporalClient.start('example', {
      args: ['Temporal'],
      taskQueue: 'default',
      workflowId: 'wf-id-' + Math.floor(Math.random() * 1000),
    });
    console.log(`Started workflow ${handle.workflowId}`);
  }
}

Advanced Options

  • Creating the Worker connection:
import { Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { TemporalModule } from 'nestjs-temporal';
import { bundleWorkflowCode, NativeConnection, Runtime } from '@temporalio/worker';
import * as path from 'path';

@Module({
  imports: [
    TempModule.forRootAsync({
      imports: [ConfigModule],
      inject: [ConfigService],
      useFactory: async (config: ConfigService) => {
        Runtime.install({});
        const temporalHost = config.get('app.temporalHost');
        const connection = await NativeConnection.connect({
          address: temporalHost,
        });
        const workflowBundle = await bundleWorkflowCode({
          workflowsPath: path.join(__dirname, './workflows'),
        });

        return {
          connection,
          taskQueue: 'default',
          workflowBundle,
        };
      },
    }),
    ClientModule,
  ],
})
export class AppModule {}
  • Creating the client connection:
import { Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { TemporalModule } from 'nestjs-temporal';
import { Connection } from '@temporalio/client';

@Module({
  imports: [
    TempModule.registerClientAsync({
      imports: [ConfigModule],
      inject: [ConfigService],
      useFactory: async (config: ConfigService) => {
        const temporalHost = config.get('app.temporalHost');
        const connection = await Connection.connect({
          address: temporalHost,
        });

        return {
          connection,
        };
      },
    }),
  ],
})
export class ClientModule {}

People

License

Nest is MIT licensed.

About

Temporal plugin for nestjs framework

Resources

License

Security policy

Stars

Watchers

Forks

Packages

No packages published

Languages

  • TypeScript 94.9%
  • JavaScript 4.8%
  • Shell 0.3%