Effect CommunityEC
Effect Community3y ago
26 replies
Sadra

Using Function Pipeline with Dependency Injection: Should I Turn It to Dependency Injection?

anyone can help ? is there need to use this example of function pipeline with dependency injection ?
should i turn it to dependency injection ?
what would you do insted of this ?
const getPostIfExist = (postId: number, authorId: number) => {
  return Effect.succeed(postId).pipe(
    Effect.filterOrFail(
      (x) => !Number.isNaN(x),
      () => new Err("PostIdIsNaNError")
    ),
    Effect.tryMapPromise({
      try: () => pg.select().from(posts).where(eq(posts.id, postId)),
      catch: () => new Err("DbQueryConnectionError"),
    }),
    Effect.filterOrFail(
      (x) => x.length === 1,
      () => new Err("PostNotFound")
    ),
    Effect.filterOrFail(
      (x) => x[0].authorId === authorId,
      () => new Err("PostNotBelongToUserError")
    ),
    Effect.map((item) => ({ ok: true as const, post: item[0] })),
    Effect.catchAll((err) => {
      return Effect.succeed({ ok: false as const, err: err._tag as string });
    }),
    Effect.runPromise
  );
};
Was this page helpful?