Skip to content

SqlModel

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.

2 exports Added in v4.0.0 Source

Constructors

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 function makeRepository<
  S extends Any,
  Id extends string | number | symbol,
  SoftDelete extends string | number | symbol = never,
>(
  Model: S,
  options: {
    readonly idColumn: Id;
    readonly softDeleteColumn?: SoftDelete;
    readonly spanPrefix: string;
    readonly tableName: string;
  },
): Effect<
  {
    readonly delete: (
      id: S["fields"][Id]["Type"],
    ) => Effect<void, SchemaError | SqlError, S["fields"][Id]["EncodingServices"]>;
    readonly findById: (
      id: S["fields"][Id]["Type"],
    ) => Effect<
      S["Type"],
      NoSuchElementError | SchemaError | SqlError,
      S["DecodingServices"] | S["fields"][Id]["EncodingServices"]
    >;
    readonly insert: (
      insert: S["insert"]["Type"],
    ) => Effect<
      S["Type"],
      SchemaError | SqlError,
      S["DecodingServices"] | S["insert"]["EncodingServices"]
    >;
    readonly insertVoid: (
      insert: S["insert"]["Type"],
    ) => Effect<void, SchemaError | SqlError, S["insert"]["EncodingServices"]>;
    readonly update: (
      update: S["update"]["Type"],
    ) => Effect<
      S["Type"],
      SchemaError | SqlError,
      S["DecodingServices"] | S["update"]["EncodingServices"]
    >;
    readonly updateVoid: (
      update: S["update"]["Type"],
    ) => Effect<void, SchemaError | SqlError, S["update"]["EncodingServices"]>;
  },
  never,
  SqlClient
>;

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 function makeResolvers<
  S extends Any,
  Id extends string | number | symbol,
  SoftDelete extends string | number | symbol = never,
>(
  Model: S,
  options: {
    readonly idColumn: Id;
    readonly softDeleteColumn?: SoftDelete;
    readonly spanPrefix: string;
    readonly tableName: string;
  },
): Effect<
  {
    readonly delete: RequestResolver<
      SqlRequest<S["fields"][Id]["Type"], void, SqlError, S["fields"][Id]["EncodingServices"]>
    >;
    readonly findById: RequestResolver<
      SqlRequest<
        S["fields"][Id]["Type"],
        S["Type"],
        NoSuchElementError | SqlError,
        S["DecodingServices"] | S["fields"][Id]["EncodingServices"]
      >
    >;
    readonly insert: RequestResolver<
      SqlRequest<
        S["insert"]["Type"],
        S["Type"],
        SqlError | ResultLengthMismatch,
        S["insert"]["EncodingServices"]
      >
    >;
    readonly insertVoid: RequestResolver<
      SqlRequest<S["insert"]["Type"], void, SqlError, S["insert"]["EncodingServices"]>
    >;
  },
  never,
  Scope | SqlClient
>;