Using Effect-TS?
If you are using Effect-TS , Sidetrack exports an Effect
-based API as well.
Sidetrack is written with Effect, which means that you can use the Effect API directly if you prefer.
import * as Effect from "effect/Effect";
// import { createSidetrackServiceTag, layer } from "sidetrack/effect";
import { SidetrackEffect } from "sidetrack";
type Queues = {
userOnboarding: { email: string };
};
const SidetrackService = SidetrackEffect.getSidetrackService<Queues>();
const SidetrackLayer = SidetrackEffect.layer<Queues>({
databaseOptions: {
databaseUrl: process.env["DATABASE_URL"]!,
},
queues: {
userOnboarding: {
run: async (payload) => {
console.log(`Welcome ${payload.email}`);
},
},
},
});
Effect.runPromise(
Effect.flatMap(SidetrackService, (sidetrack) =>
sidetrack.insertJob("userOnboarding", { email: "hello@example.com" }),
).pipe(Effect.provide(SidetrackLayer)),
);
To run it independently as a long running process without calling start
for polling, you can do something like the following:
import * as Effect from "effect/Effect";
import { SidetrackEffect } from "sidetrack";
import { NodeRuntime } from "@effect/platform-node";
import * as Layer from "effect/Layer";
// import { createSidetrackServiceTag, layer } from "sidetrack/effect";
type Queues = {
userOnboarding: { email: string };
};
const SidetrackService = SidetrackEffect.getSidetrackService<Queues>();
const SidetrackLayer = SidetrackEffect.layer<Queues>({
databaseOptions: {
databaseUrl: process.env["DATABASE_URL"]!,
},
queues: {
userOnboarding: {
run: async (payload) => {
console.log(`Welcome ${payload.email}`);
},
},
},
// This is important so that sidetrack starts when the layer is constructed
startOnInitialization: true,
// ^?
});
// You may launch the layer independently or just construct it along with your other layers
NodeRuntime.runMain(Layer.launch(SidetrackLayer));
If you use ESM, you can use the sidetrack/effect
import if you want.
You can find the remaining reference documentation for the Effect docs here . The functions are similar to the ones in the class-based API, but they return Effects instead of Promises, with some error handling. Over time, the Effect API might be expanded to include more features. Please open an issue if you have any requests for how we can make the API more ergonomic or “Effectful.”
The test utils mentioned in the Testing Sidetrack section can also be found under the testUtils
key in the sidetrack service. For example, you can do sidetrack.testUtils.runJob
in the example above.