Skip to content

SqlModel.ts

Builds SQL repositories and request resolvers from Effect schema models.

Use this module when a schema Model represents rows in a SQL table and the usual insert, update, find-by-id, delete, and batching behavior should be derived from that model. The helpers encode insert and update input with the model’s input schemas and decode returned rows with the full model schema. Soft deletes are optional, and SQL dialect differences such as returning support are handled by the repository implementation.

Since v4.0.0



Creates a CRUD repository for a schema model backed by a SQL table, with insert, update, find-by-id, and delete operations. When softDeleteColumn is supplied, reads ignore soft-deleted rows and delete updates that column instead of removing the row.

Signature

declare const makeRepository: <
S extends Model.Any,
Id extends keyof S["Type"] & keyof S["update"]["Type"] & keyof S["fields"],
SoftDelete extends keyof S["fields"] = never
>(
Model: S,
options: {
readonly tableName: string
readonly spanPrefix: string
readonly idColumn: Id
readonly softDeleteColumn?: SoftDelete | undefined
}
) => Effect.Effect<
{
readonly insert: (
insert: S["insert"]["Type"]
) => Effect.Effect<
S["Type"],
Schema.SchemaError | SqlError,
S["DecodingServices"] | S["insert"]["EncodingServices"]
>
readonly insertVoid: (
insert: S["insert"]["Type"]
) => Effect.Effect<void, Schema.SchemaError | SqlError, S["insert"]["EncodingServices"]>
readonly update: (
update: S["update"]["Type"]
) => Effect.Effect<
S["Type"],
Schema.SchemaError | SqlError,
S["DecodingServices"] | S["update"]["EncodingServices"]
>
readonly updateVoid: (
update: S["update"]["Type"]
) => Effect.Effect<void, Schema.SchemaError | SqlError, S["update"]["EncodingServices"]>
readonly findById: (
id: S["fields"][Id]["Type"]
) => Effect.Effect<
S["Type"],
Cause.NoSuchElementError | Schema.SchemaError | SqlError,
S["DecodingServices"] | S["fields"][Id]["EncodingServices"]
>
readonly delete: (
id: S["fields"][Id]["Type"]
) => Effect.Effect<void, Schema.SchemaError | SqlError, S["fields"][Id]["EncodingServices"]>
},
never,
SqlClient
>

Source

Since v4.0.0

Creates batched request resolvers for a schema model’s insert, insert-void, find-by-id, and delete operations, honoring the optional soft-delete column.

Signature

declare const makeResolvers: <
S extends Model.Any,
Id extends keyof S["Type"] & keyof S["update"]["Type"] & keyof S["fields"],
SoftDelete extends keyof S["fields"] = never
>(
Model: S,
options: {
readonly tableName: string
readonly spanPrefix: string
readonly idColumn: Id
readonly softDeleteColumn?: SoftDelete | undefined
}
) => Effect.Effect<
{
readonly insert: RequestResolver.RequestResolver<
SqlResolver.SqlRequest<
S["insert"]["Type"],
S["Type"],
ResultLengthMismatch | SqlError,
S["insert"]["EncodingServices"]
>
>
readonly insertVoid: RequestResolver.RequestResolver<
SqlResolver.SqlRequest<S["insert"]["Type"], void, SqlError, S["insert"]["EncodingServices"]>
>
readonly findById: RequestResolver.RequestResolver<
SqlResolver.SqlRequest<
S["fields"][Id]["Type"],
S["Type"],
Cause.NoSuchElementError | SqlError,
S["DecodingServices"] | S["fields"][Id]["EncodingServices"]
>
>
readonly delete: RequestResolver.RequestResolver<
SqlResolver.SqlRequest<S["fields"][Id]["Type"], void, SqlError, S["fields"][Id]["EncodingServices"]>
>
},
never,
SqlClient | Scope
>

Source

Since v4.0.0