@sketchmonk/temporal-nest
This package contains two modules: TemporalWorkerModule
used to initiate a worker and TemporalClientModule
to use the Temporal client.
This is not field tested yet. So, I do not recommend using in a production system.
Installation
You can install @sketchmonk/temporal-nest
using any of the following package managers:
npm
npm install @sketchmonk/temporal-nest
yarn
yarn add @sketchmonk/temporal-nest
pnpm
pnpm add @sketchmonk/temporal-nest
Getting Started
To get started with @sketchmonk/temporal-nest
, follow the steps below:
Setting up the worker
1. Writing activities
- activities.service.ts
@Injectable()
export class ActivitiesService {
constructor(private readonly nameService: NameService) {}
async sayHello(name: string) {
return `Hello, ${nameService.capitalize(name)}`;
}
}
- activities.module.ts
@Global()
@Module({
providers: [ActivitiesService, NameService],
exports: [ActivitiesService]
})
export class ActivitiesModule {}
2. Writing the workflows
- workflows.ts
import * as wf from "@temporalio/workflow";
import type { ActivitiesService } from "src/activities/activities.service";
export const {
sayHello,
} = wf.proxyActivities<ActivitiesService>({
startToCloseTimeout: '10m'
})
export async function sayHelloFlow(names: string[]) {
for (let name of names) {
await sayHello(name);
}
}
3. Configure the worker
- app.module.ts
import { Module } from '@nestjs/common';
import { TemporalWorkerModule } from '@sketchmonk/temporal-nest';
import { ActivitiesModule } from './activities/activities.module';
import { ActivitiesService } from './activities/activities.service';
@Module({
imports: [
ActivitiesModule,
TemporalWorkerModule.registerAsync({
inject: [ActivitiesService],
useFactory: async (activities: ActivitiesService) => {
return {
taskQueue: 'hello',
activities: {
sayHello: activities.sayHello.bind(activities),
},
workflowsPath: require.resolve('./workflows'),
}
}
})
],
controllers: [],
providers: [],
})
export class AppModule {}
Setting up the Client
TemporalClientModule
1. Import the - app.module.ts
import { Module } from '@nestjs/common';
import { TemporalClientModule } from '@sketchmonk/temporal-nest';
import { AppController } from './app.controller';
@Module({
imports: [
TemporalClientModule.register({}),
],
controllers: [AppController],
providers: [],
})
export class AppModule {}
2. Inject the client instance
- app.controller.ts
import { Controller, Post, Body } from '@nestjs/common';
import { InjectClient } from '@sketchmonk/temporal-nest';
import { Client } from '@temporalio/client';
import * as crypto from 'crypto';
@Controller({
version: '1'
})
export class AppController {
constructor(
@InjectClient() private readonly client: Client,
) {}
@Post()
async sayHello(@Body('name') name: string) {
// generate random UUID
const workflowId = crypto.randomUUID();
// call workflow
return this.client.workflow.execute('login', {
taskQueue: 'hello',
args: [name],
workflowId,
});
}
}
That's it! You can now start using Temporal functionality inside your Nest JS project.