Skip to main content Link Search Menu Expand Document (external link)

Model overview

Added in v1.0.0


Table of contents


constructors

Class

A base class used for creating domain model schemas.

It supports common variants for database and JSON apis.

Signature

export declare const Class: any

Example

import { Schema } from "effect"
import { Model } from "@effect/sql"

export const GroupId = Schema.Number.pipe(Schema.brand("GroupId"))

export class Group extends Model.Class<Group>("Group")({
  id: Model.Generated(GroupId),
  name: Schema.NonEmptyTrimmedString,
  createdAt: Model.DateTimeInsertFromDate,
  updatedAt: Model.DateTimeUpdateFromDate
}) {}

// schema used for selects
Group

// schema used for inserts
Group.insert

// schema used for updates
Group.update

// schema used for json api
Group.json
Group.jsonCreate
Group.jsonUpdate

// you can also turn them into classes
class GroupJson extends Schema.Class<GroupJson>("GroupJson")(Group.json) {
  get upperName() {
    return this.name.toUpperCase()
  }
}

Added in v1.0.0

Struct

Signature

export declare const Struct: any

Added in v1.0.0

Union

Signature

export declare const Union: any

Added in v1.0.0

date & time

Date

A schema for a DateTime.Utc that is serialized as a date string in the format YYYY-MM-DD.

Signature

export declare const Date: Date

Added in v1.0.0

Date (interface)

Signature

export interface Date extends Schema.transformOrFail<typeof Schema.String, typeof Schema.DateTimeUtcFromSelf> {}

Added in v1.0.0

DateTimeFromDate

Signature

export declare const DateTimeFromDate: DateTimeFromDate

Added in v1.0.0

DateTimeFromDate (interface)

Signature

export interface DateTimeFromDate
  extends Schema.transform<typeof Schema.ValidDateFromSelf, typeof Schema.DateTimeUtcFromSelf> {}

Added in v1.0.0

DateTimeFromDateWithNow

Signature

export declare const DateTimeFromDateWithNow: any

Added in v1.0.0

DateTimeFromNumberWithNow

Signature

export declare const DateTimeFromNumberWithNow: any

Added in v1.0.0

DateTimeInsert

A field that represents a date-time value that is inserted as the current DateTime.Utc. It is serialized as a string for the database.

It is omitted from updates and is available for selection.

Signature

export declare const DateTimeInsert: DateTimeInsert

Added in v1.0.0

DateTimeInsert (interface)

Signature

export interface DateTimeInsert
  extends VariantSchema.Field<{
    readonly select: typeof Schema.DateTimeUtc
    readonly insert: VariantSchema.Overrideable<DateTime.Utc, string>
    readonly json: typeof Schema.DateTimeUtc
  }> {}

Added in v1.0.0

DateTimeInsertFromDate

A field that represents a date-time value that is inserted as the current DateTime.Utc. It is serialized as a Date for the database.

It is omitted from updates and is available for selection.

Signature

export declare const DateTimeInsertFromDate: DateTimeInsertFromDate

Added in v1.0.0

DateTimeInsertFromDate (interface)

Signature

export interface DateTimeInsertFromDate
  extends VariantSchema.Field<{
    readonly select: DateTimeFromDate
    readonly insert: VariantSchema.Overrideable<DateTime.Utc, globalThis.Date>
    readonly json: typeof Schema.DateTimeUtc
  }> {}

Added in v1.0.0

DateTimeInsertFromNumber

A field that represents a date-time value that is inserted as the current DateTime.Utc. It is serialized as a number.

It is omitted from updates and is available for selection.

Signature

export declare const DateTimeInsertFromNumber: DateTimeInsertFromNumber

Added in v1.0.0

DateTimeInsertFromNumber (interface)

Signature

export interface DateTimeInsertFromNumber
  extends VariantSchema.Field<{
    readonly select: typeof Schema.DateTimeUtcFromNumber
    readonly insert: VariantSchema.Overrideable<DateTime.Utc, number>
    readonly json: typeof Schema.DateTimeUtcFromNumber
  }> {}

Added in v1.0.0

DateTimeUpdate

A field that represents a date-time value that is updated as the current DateTime.Utc. It is serialized as a string for the database.

It is set to the current DateTime.Utc on updates and inserts and is available for selection.

Signature

export declare const DateTimeUpdate: DateTimeUpdate

Added in v1.0.0

DateTimeUpdate (interface)

Signature

export interface DateTimeUpdate
  extends VariantSchema.Field<{
    readonly select: typeof Schema.DateTimeUtc
    readonly insert: VariantSchema.Overrideable<DateTime.Utc, string>
    readonly update: VariantSchema.Overrideable<DateTime.Utc, string>
    readonly json: typeof Schema.DateTimeUtc
  }> {}

Added in v1.0.0

DateTimeUpdateFromDate

A field that represents a date-time value that is updated as the current DateTime.Utc. It is serialized as a Date for the database.

It is set to the current DateTime.Utc on updates and inserts and is available for selection.

Signature

export declare const DateTimeUpdateFromDate: DateTimeUpdateFromDate

Added in v1.0.0

DateTimeUpdateFromDate (interface)

Signature

export interface DateTimeUpdateFromDate
  extends VariantSchema.Field<{
    readonly select: DateTimeFromDate
    readonly insert: VariantSchema.Overrideable<DateTime.Utc, globalThis.Date>
    readonly update: VariantSchema.Overrideable<DateTime.Utc, globalThis.Date>
    readonly json: typeof Schema.DateTimeUtc
  }> {}

Added in v1.0.0

DateTimeUpdateFromNumber

A field that represents a date-time value that is updated as the current DateTime.Utc. It is serialized as a number.

It is set to the current DateTime.Utc on updates and inserts and is available for selection.

Signature

export declare const DateTimeUpdateFromNumber: DateTimeUpdateFromNumber

Added in v1.0.0

DateTimeUpdateFromNumber (interface)

Signature

export interface DateTimeUpdateFromNumber
  extends VariantSchema.Field<{
    readonly select: typeof Schema.DateTimeUtcFromNumber
    readonly insert: VariantSchema.Overrideable<DateTime.Utc, number>
    readonly update: VariantSchema.Overrideable<DateTime.Utc, number>
    readonly json: typeof Schema.DateTimeUtcFromNumber
  }> {}

Added in v1.0.0

DateTimeWithNow

Signature

export declare const DateTimeWithNow: any

Added in v1.0.0

DateWithNow

Signature

export declare const DateWithNow: any

Added in v1.0.0

extraction

extract

Signature

export declare const extract: any

Added in v1.0.0

fields

Field

Signature

export declare const Field: any

Added in v1.0.0

FieldExcept

Signature

export declare const FieldExcept: any

Added in v1.0.0

FieldOnly

Signature

export declare const FieldOnly: any

Added in v1.0.0

fieldEvolve

Signature

export declare const fieldEvolve: any

Added in v1.0.0

fieldFromKey

Signature

export declare const fieldFromKey: any

Added in v1.0.0

fields

Signature

export declare const fields: <A extends VariantSchema.Struct<any>>(self: A) => A[VariantSchema.TypeId]

Added in v1.0.0

generated

Generated

A field that represents a column that is generated by the database.

It is available for selection and update, but not for insertion.

Signature

export declare const Generated: <S extends Schema.Schema.All | Schema.PropertySignature.All>(schema: S) => Generated<S>

Added in v1.0.0

Generated (interface)

Signature

export interface Generated<S extends Schema.Schema.All | Schema.PropertySignature.All>
  extends VariantSchema.Field<{
    readonly select: S
    readonly update: S
    readonly json: S
  }> {}

Added in v1.0.0

GeneratedByApp

A field that represents a column that is generated by the application.

It is required by the database, but not by the JSON variants.

Signature

export declare const GeneratedByApp: <S extends Schema.Schema.All | Schema.PropertySignature.All>(
  schema: S
) => GeneratedByApp<S>

Added in v1.0.0

GeneratedByApp (interface)

Signature

export interface GeneratedByApp<S extends Schema.Schema.All | Schema.PropertySignature.All>
  extends VariantSchema.Field<{
    readonly select: S
    readonly insert: S
    readonly update: S
    readonly json: S
  }> {}

Added in v1.0.0

json

JsonFromString

A field that represents a JSON value stored as text in the database.

The “json” variants will use the object schema directly.

Signature

export declare const JsonFromString: <S extends Schema.Schema.All | Schema.PropertySignature.All>(
  schema: S
) => JsonFromString<S>

Added in v1.0.0

JsonFromString (interface)

Signature

export interface JsonFromString<S extends Schema.Schema.All | Schema.PropertySignature.All>
  extends VariantSchema.Field<{
    readonly select: Schema.Schema<Schema.Schema.Type<S>, string, Schema.Schema.Context<S>>
    readonly insert: Schema.Schema<Schema.Schema.Type<S>, string, Schema.Schema.Context<S>>
    readonly update: Schema.Schema<Schema.Schema.Type<S>, string, Schema.Schema.Context<S>>
    readonly json: S
    readonly jsonCreate: S
    readonly jsonUpdate: S
  }> {}

Added in v1.0.0

models

Any (type alias)

Signature

export type Any = Schema.Schema.Any & {
  readonly fields: Schema.Struct.Fields
  readonly insert: Schema.Schema.Any
  readonly update: Schema.Schema.Any
  readonly json: Schema.Schema.Any
  readonly jsonCreate: Schema.Schema.Any
  readonly jsonUpdate: Schema.Schema.Any
}

Added in v1.0.0

AnyNoContext (type alias)

Signature

export type AnyNoContext = Schema.Schema.AnyNoContext & {
  readonly fields: Schema.Struct.Fields
  readonly insert: Schema.Schema.AnyNoContext
  readonly update: Schema.Schema.AnyNoContext
  readonly json: Schema.Schema.AnyNoContext
  readonly jsonCreate: Schema.Schema.AnyNoContext
  readonly jsonUpdate: Schema.Schema.AnyNoContext
}

Added in v1.0.0

VariantsDatabase (type alias)

Signature

export type VariantsDatabase = "select" | "insert" | "update"

Added in v1.0.0

VariantsJson (type alias)

Signature

export type VariantsJson = "json" | "jsonCreate" | "jsonUpdate"

Added in v1.0.0

optional

FieldOption

Convert a field to one that is optional for all variants.

For the database variants, it will accept nullable values. For the JSON variants, it will also accept missing keys.

Signature

export declare const FieldOption: <Field extends VariantSchema.Field<any> | Schema.Schema.Any>(
  self: Field
) => Field extends Schema.Schema.Any
  ? FieldOption<Field>
  : Field extends VariantSchema.Field<infer S>
    ? VariantSchema.Field<{
        readonly [K in keyof S]: S[K] extends Schema.Schema.Any
          ? K extends VariantsDatabase
            ? Schema.OptionFromNullOr<S[K]>
            : Schema.optionalWith<S[K], { as: "Option"; nullable: true }>
          : never
      }>
    : never

Added in v1.0.0

FieldOption (interface)

Convert a field to one that is optional for all variants.

For the database variants, it will accept nullable values. For the JSON variants, it will also accept missing keys.

Signature

export interface FieldOption<S extends Schema.Schema.Any>
  extends VariantSchema.Field<{
    readonly select: Schema.OptionFromNullOr<S>
    readonly insert: Schema.OptionFromNullOr<S>
    readonly update: Schema.OptionFromNullOr<S>
    readonly json: Schema.optionalWith<S, { as: "Option" }>
    readonly jsonCreate: Schema.optionalWith<S, { as: "Option"; nullable: true }>
    readonly jsonUpdate: Schema.optionalWith<S, { as: "Option"; nullable: true }>
  }> {}

Added in v1.0.0

overrideable

Override

Signature

export declare const Override: <A>(value: A) => A & Brand<"Override">

Added in v1.0.0

repository

makeDataLoaders

Create some simple data loaders from a model.

Signature

export declare const makeDataLoaders: <
  S extends AnyNoContext,
  Id extends keyof S["Type"] & keyof S["update"]["Type"] & keyof S["fields"]
>(
  Model: S,
  options: {
    readonly tableName: string
    readonly spanPrefix: string
    readonly idColumn: Id
    readonly window: DurationInput
    readonly maxBatchSize?: number | undefined
  }
) => Effect.Effect<
  {
    readonly insert: (insert: S["insert"]["Type"]) => Effect.Effect<S["Type"]>
    readonly insertVoid: (insert: S["insert"]["Type"]) => Effect.Effect<void>
    readonly findById: (id: Schema.Schema.Type<S["fields"][Id]>) => Effect.Effect<Option.Option<S["Type"]>>
    readonly delete: (id: Schema.Schema.Type<S["fields"][Id]>) => Effect.Effect<void>
  },
  never,
  SqlClient | Scope
>

Added in v1.0.0

makeRepository

Create a simple CRUD repository from a model.

Signature

export declare const makeRepository: <
  S extends Any,
  Id extends keyof S["Type"] & keyof S["update"]["Type"] & keyof S["fields"]
>(
  Model: S,
  options: { readonly tableName: string; readonly spanPrefix: string; readonly idColumn: Id }
) => Effect.Effect<
  {
    readonly insert: (
      insert: S["insert"]["Type"]
    ) => Effect.Effect<S["Type"], never, S["Context"] | S["insert"]["Context"]>
    readonly insertVoid: (
      insert: S["insert"]["Type"]
    ) => Effect.Effect<void, never, S["Context"] | S["insert"]["Context"]>
    readonly update: (
      update: S["update"]["Type"]
    ) => Effect.Effect<S["Type"], never, S["Context"] | S["update"]["Context"]>
    readonly updateVoid: (
      update: S["update"]["Type"]
    ) => Effect.Effect<void, never, S["Context"] | S["update"]["Context"]>
    readonly findById: (
      id: Schema.Schema.Type<S["fields"][Id]>
    ) => Effect.Effect<Option.Option<S["Type"]>, never, S["Context"] | Schema.Schema.Context<S["fields"][Id]>>
    readonly delete: (
      id: Schema.Schema.Type<S["fields"][Id]>
    ) => Effect.Effect<void, never, Schema.Schema.Context<S["fields"][Id]>>
  },
  never,
  SqlClient
>

Added in v1.0.0

sensitive

Sensitive

A field that represents a sensitive value that should not be exposed in the JSON variants.

Signature

export declare const Sensitive: <S extends Schema.Schema.All | Schema.PropertySignature.All>(schema: S) => Sensitive<S>

Added in v1.0.0

Sensitive (interface)

Signature

export interface Sensitive<S extends Schema.Schema.All | Schema.PropertySignature.All>
  extends VariantSchema.Field<{
    readonly select: S
    readonly insert: S
    readonly update: S
  }> {}

Added in v1.0.0