Skip to content

Instantly share code, notes, and snippets.

@JanMalch
Created April 23, 2023 16:27
Show Gist options
  • Save JanMalch/7393c35217e3c42c17473e79a3ffc267 to your computer and use it in GitHub Desktop.
Save JanMalch/7393c35217e3c42c17473e79a3ffc267 to your computer and use it in GitHub Desktop.
BullMQ FlowJobBuilder
import { FlowJob } from "bullmq";
export class FlowJobBuilder {
private job: FlowJob
constructor(
firstJob: FlowJob,
) {
this.job = { ...firstJob }
}
private assertNoChildren(parent: FlowJob): asserts parent is Omit<FlowJob, 'children'> {
if (Array.isArray(parent.children) && parent.children.length > 0) {
throw new Error('May not pass job with children to FlowJobBuilder.')
}
}
andThen(parent: Omit<FlowJob, 'children'>): this {
this.assertNoChildren(parent as FlowJob);
this.job = {
...parent,
children: [
{ ...this.job }
]
}
return this;
}
build(): FlowJob {
return this.job;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment