Files
triggerdotdev--trigger.dev/.changeset/mighty-flowers-train.md
T
Eric Allam 9491a1649c v3: Support decorators (#1060)
* Adds support for `emitDecoratorMetadata: true` and `experimentalDecorators: true` in your tsconfig

* Implement task.onSuccess/onFailure and config.onSuccess/onFailure

* Added onStart and more docs for lifecycle functions

* Use onStart instead of init for TypeORM
2024-04-25 11:17:13 +01:00

1.9 KiB

trigger.dev, @trigger.dev/core
trigger.dev @trigger.dev/core
patch patch

Adds support for emitDecoratorMetadata: true and experimentalDecorators: true in your tsconfig using the @anatine/esbuild-decorators package. This allows you to use libraries like TypeORM:

import "reflect-metadata";
import { DataSource } from "typeorm";
import { Entity, Column, PrimaryColumn } from "typeorm";

@Entity()
export class Photo {
  @PrimaryColumn()
  id!: number;

  @Column()
  name!: string;

  @Column()
  description!: string;

  @Column()
  filename!: string;

  @Column()
  views!: number;

  @Column()
  isPublished!: boolean;
}

export const AppDataSource = new DataSource({
  type: "postgres",
  host: "localhost",
  port: 5432,
  username: "postgres",
  password: "postgres",
  database: "v3-catalog",
  entities: [Photo],
  synchronize: true,
  logging: false,
});

And then in your trigger.config.ts file you can initialize the datasource using the new init option:

import type { TriggerConfig } from "@trigger.dev/sdk/v3";
import { AppDataSource } from "@/trigger/orm";

export const config: TriggerConfig = {
  // ... other options here
  init: async (payload, { ctx }) => {
    await AppDataSource.initialize();
  },
};

Now you are ready to use this in your tasks:

import { task } from "@trigger.dev/sdk/v3";
import { AppDataSource, Photo } from "./orm";

export const taskThatUsesDecorators = task({
  id: "taskThatUsesDecorators",
  run: async (payload: { message: string }) => {
    console.log("Creating a photo...");

    const photo = new Photo();
    photo.id = 2;
    photo.name = "Me and Bears";
    photo.description = "I am near polar bears";
    photo.filename = "photo-with-bears.jpg";
    photo.views = 1;
    photo.isPublished = true;

    await AppDataSource.manager.save(photo);
  },
});