Feedback on Using `@effect/sql` in Your Project
Hey guys, I'm really new to Effect and trying to deep-dive into it by using it on my latest project. The thing I'm really struggling with is the fact that it seems that there are multiple ways to achieve the same goal, and not all methods are as clearly documented. So I would like to hear you guy's opinion on this but of code I wrote that uses
@effect/sql@effect/sql, is this the way to go? // Imports
export const InsertChatSchema = Chat.omit("createdAt");
export class ChatNotFoundError extends Schema.TaggedError<ChatNotFoundError>(
"ChatNotFoundError",
)("ChatNotFoundError", {
id: ChatId,
}) {
get message() {
return `Chat with id ${this.id} not found`;
}
}
export class ChatRepo extends Effect.Service<ChatRepo>()("ChatRepo", {
dependencies: [PgLive],
effect: Effect.gen(function*() {
const sql = yield* SqlClient.SqlClient;
const findAll = SqlSchema.findAll({
Result: Chat,
Request: Schema.Void,
execute: () => sql`
SELECT
*
FROM
chats
`,
});
const create = SqlSchema.single({
Request: InsertChatSchema,
Result: Chat,
execute: (requests) => sql`
INSERT INTO
chats ${sql.insert(requests)}
RETURNING
*
`,
});
const del = SqlSchema.single({
Request: ChatId,
Result: Schema.Unknown,
execute: (id) => sql`
DELETE FROM chats
WHERE
id = ${id}
RETURNING
id
`,
});
return {
findAll: flow(findAll, Effect.orDie),
del: (id: ChatId) =>
del(id).pipe(
Effect.asVoid,
Effect.catchTags({
NoSuchElementException: () => new ChatNotFoundError({ id }),
ParseError: Effect.die,
SqlError: Effect.die,
}),
),
create: flow(create, Effect.orDie),
} as const;
}),
}) { }// Imports
export const InsertChatSchema = Chat.omit("createdAt");
export class ChatNotFoundError extends Schema.TaggedError<ChatNotFoundError>(
"ChatNotFoundError",
)("ChatNotFoundError", {
id: ChatId,
}) {
get message() {
return `Chat with id ${this.id} not found`;
}
}
export class ChatRepo extends Effect.Service<ChatRepo>()("ChatRepo", {
dependencies: [PgLive],
effect: Effect.gen(function*() {
const sql = yield* SqlClient.SqlClient;
const findAll = SqlSchema.findAll({
Result: Chat,
Request: Schema.Void,
execute: () => sql`
SELECT
*
FROM
chats
`,
});
const create = SqlSchema.single({
Request: InsertChatSchema,
Result: Chat,
execute: (requests) => sql`
INSERT INTO
chats ${sql.insert(requests)}
RETURNING
*
`,
});
const del = SqlSchema.single({
Request: ChatId,
Result: Schema.Unknown,
execute: (id) => sql`
DELETE FROM chats
WHERE
id = ${id}
RETURNING
id
`,
});
return {
findAll: flow(findAll, Effect.orDie),
del: (id: ChatId) =>
del(id).pipe(
Effect.asVoid,
Effect.catchTags({
NoSuchElementException: () => new ChatNotFoundError({ id }),
ParseError: Effect.die,
SqlError: Effect.die,
}),
),
create: flow(create, Effect.orDie),
} as const;
}),
}) { }