// 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;
}),
}) { }