Effect CommunityEC
Effect Community3y ago
2 replies
Sadra

Optimizing Code for Weird Returning Type

anyone can help to optimize this code ? is there better way ? im getting weird returning type
const test = Effect.tryPromise({
      try: () =>
        lite
          .select()
          .from(edits)
          .where(and(eq(edits.postId, postId), eq(edits.userId, userId))),
      catch: () => new CheckPostExistError(),
    }).pipe(
      Effect.flatMap(
        ReadonlyArray.match({
          onNonEmpty: (_) => Effect.succeed(_[0]),
          onEmpty: () => Effect.fail(new UserNotExistError()),
        }),
      ),
      Effect.catchTag("UserNotExistError", () =>
        Effect.tryPromise({
          try: () =>
            lite
              .insert(edits)
              .values({ postId, userId, json: JSON.stringify([]) })
              .returning(),
          catch: () => new InsertSqliteError(),
        }).pipe(Effect.map((x) => x[0])),
      ),
);


returning type
const test: Effect.Effect<never, CheckPostExistError | InsertSqliteError, {
    json: string;
    postId: number;
    userId: number;
    updatedAt: number;
} | {
    json: string;
    postId: number;
    userId: number;
    updatedAt: number;
}>
Was this page helpful?