Guides
Using Sidetrack With Effect

Using Effect-TS?

If you are using Effect-TS (opens in a new tab), 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.

ts
import * as Effect from "effect/Effect";
import { createSidetrackServiceTag, makeLayer } from "sidetrack/effect";
 
type Queues = {
userOnboarding: { email: string };
};
 
const SidetrackService = createSidetrackServiceTag<Queues>();
 
const sidetrackLayer = makeLayer<Queues>({
databaseOptions: {
connectionString: process.env["DATABASE_URL"]!,
},
queues: {
userOnboarding: {
handler: async (job) => {
console.log(`Welcome ${job.payload.email}`);
},
},
},
});
 
const program = Effect.runPromise(
Effect.flatMap(SidetrackService, (sidetrack) =>
sidetrack.insertJob("userOnboarding", { email: "hello@example.com" }),
).pipe(Effect.provide(sidetrackLayer)),
);
ts
import * as Effect from "effect/Effect";
import { createSidetrackServiceTag, makeLayer } from "sidetrack/effect";
 
type Queues = {
userOnboarding: { email: string };
};
 
const SidetrackService = createSidetrackServiceTag<Queues>();
 
const sidetrackLayer = makeLayer<Queues>({
databaseOptions: {
connectionString: process.env["DATABASE_URL"]!,
},
queues: {
userOnboarding: {
handler: async (job) => {
console.log(`Welcome ${job.payload.email}`);
},
},
},
});
 
const program = Effect.runPromise(
Effect.flatMap(SidetrackService, (sidetrack) =>
sidetrack.insertJob("userOnboarding", { email: "hello@example.com" }),
).pipe(Effect.provide(sidetrackLayer)),
);
⚠️

If the sidetrack/effect import doesn't work, you will need to use

ts
import { SidetrackEffect } from "sidetrack";
ts
import { SidetrackEffect } from "sidetrack";

and access the methods on that export (e.g. SidetrackEffect.makeLayer). Certain TypeScript configurations don't allow for that import to work correctly.

You can find the remaining reference documentation for the Effect docs here (opens in a new tab). 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.