Skip to content

v3: Support decorators #1060

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 25, 2024
Merged

v3: Support decorators #1060

merged 4 commits into from
Apr 25, 2024

Conversation

ericallam
Copy link
Member

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);
  },
});

Copy link

changeset-bot bot commented Apr 24, 2024

🦋 Changeset detected

Latest commit: c551150

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@ericallam ericallam force-pushed the v3/decorator-support branch from f26049f to c551150 Compare April 25, 2024 10:03
@ericallam ericallam enabled auto-merge (squash) April 25, 2024 10:17
@ericallam ericallam disabled auto-merge April 25, 2024 10:17
@ericallam ericallam merged commit 9491a16 into main Apr 25, 2024
@ericallam ericallam deleted the v3/decorator-support branch April 25, 2024 10:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant