Model.ts overview
Since v1.0.0
Exports Grouped by Category
- constructors
- date & time
- Date
- Date (interface)
- DateTimeFromDate
- DateTimeFromDate (interface)
- DateTimeFromDateWithNow
- DateTimeFromNumberWithNow
- DateTimeInsert
- DateTimeInsert (interface)
- DateTimeInsertFromDate
- DateTimeInsertFromDate (interface)
- DateTimeInsertFromNumber
- DateTimeInsertFromNumber (interface)
- DateTimeUpdate
- DateTimeUpdate (interface)
- DateTimeUpdateFromDate
- DateTimeUpdateFromDate (interface)
- DateTimeUpdateFromNumber
- DateTimeUpdateFromNumber (interface)
- DateTimeWithNow
- DateWithNow
- extraction
- fields
- generated
- json
- models
- optional
- overrideable
- repository
- sensitive
- uuid
constructors
Class
A base class used for creating domain model schemas.
It supports common variants for database and JSON apis.
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()
}
}
Signature
declare const Class: any
Since v1.0.0
Struct
Signature
declare const Struct: any
Since v1.0.0
Union
Signature
declare const Union: any
Since 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
declare const Date: Date
Since v1.0.0
Date (interface)
Signature
export interface Date extends Schema.transformOrFail<typeof Schema.String, typeof Schema.DateTimeUtcFromSelf> {}
Since v1.0.0
DateTimeFromDate
Signature
declare const DateTimeFromDate: DateTimeFromDate
Since v1.0.0
DateTimeFromDate (interface)
Signature
export interface DateTimeFromDate
extends Schema.transform<typeof Schema.ValidDateFromSelf, typeof Schema.DateTimeUtcFromSelf> {}
Since v1.0.0
DateTimeFromDateWithNow
Signature
declare const DateTimeFromDateWithNow: any
Since v1.0.0
DateTimeFromNumberWithNow
Signature
declare const DateTimeFromNumberWithNow: any
Since 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
declare const DateTimeInsert: DateTimeInsert
Since 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
}> {}
Since 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
declare const DateTimeInsertFromDate: DateTimeInsertFromDate
Since 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
}> {}
Since 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
declare const DateTimeInsertFromNumber: DateTimeInsertFromNumber
Since 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
}> {}
Since 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
declare const DateTimeUpdate: DateTimeUpdate
Since 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
}> {}
Since 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
declare const DateTimeUpdateFromDate: DateTimeUpdateFromDate
Since 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
}> {}
Since 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
declare const DateTimeUpdateFromNumber: DateTimeUpdateFromNumber
Since 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
}> {}
Since v1.0.0
DateTimeWithNow
Signature
declare const DateTimeWithNow: any
Since v1.0.0
DateWithNow
Signature
declare const DateWithNow: any
Since v1.0.0
extraction
extract
Signature
declare const extract: any
Since v1.0.0
fields
Field
Signature
declare const Field: any
Since v1.0.0
FieldExcept
Signature
declare const FieldExcept: any
Since v1.0.0
FieldOnly
Signature
declare const FieldOnly: any
Since v1.0.0
fieldEvolve
Signature
declare const fieldEvolve: any
Since v1.0.0
fieldFromKey
Signature
declare const fieldFromKey: any
Since v1.0.0
fields
Signature
declare const fields: <A extends VariantSchema.Struct<any>>(self: A) => A[VariantSchema.TypeId]
Since 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
declare const Generated: <S extends Schema.Schema.All | Schema.PropertySignature.All>(schema: S) => Generated<S>
Since 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
}> {}
Since 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
declare const GeneratedByApp: <S extends Schema.Schema.All | Schema.PropertySignature.All>(
schema: S
) => GeneratedByApp<S>
Since 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
}> {}
Since 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
declare const JsonFromString: <S extends Schema.Schema.All | Schema.PropertySignature.All>(
schema: S
) => JsonFromString<S>
Since 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
}> {}
Since v1.0.0
models
Any (type alias)
Signature
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
}
Since v1.0.0
AnyNoContext (type alias)
Signature
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
}
Since v1.0.0
VariantsDatabase (type alias)
Signature
type VariantsDatabase = "select" | "insert" | "update"
Since v1.0.0
VariantsJson (type alias)
Signature
type VariantsJson = "json" | "jsonCreate" | "jsonUpdate"
Since v1.0.0
optional
FieldOption
Convert a field to one that is optional for all variants.
For the database variants, it will accept null
able values. For the JSON variants, it will also accept missing keys.
Signature
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
Since v1.0.0
FieldOption (interface)
Convert a field to one that is optional for all variants.
For the database variants, it will accept null
able 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 }>
}> {}
Since v1.0.0
overrideable
Override
Signature
declare const Override: <A>(value: A) => A & Brand<"Override">
Since v1.0.0
repository
makeDataLoaders
Create some simple data loaders from a model.
Signature
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
>
Since v1.0.0
makeRepository
Create a simple CRUD repository from a model.
Signature
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
>
Since v1.0.0
sensitive
Sensitive
A field that represents a sensitive value that should not be exposed in the JSON variants.
Signature
declare const Sensitive: <S extends Schema.Schema.All | Schema.PropertySignature.All>(schema: S) => Sensitive<S>
Since 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
}> {}
Since v1.0.0
uuid
BooleanFromNumber (class)
A boolean parsed from 0 or 1
Signature
declare class BooleanFromNumber
Since v1.0.0
UuidV4Insert
A field that represents a binary UUID v4 that is generated on inserts.
Signature
declare const UuidV4Insert: <const B extends string | symbol>(
schema: Schema.brand<typeof Schema.Uint8ArrayFromSelf, B>
) => UuidV4Insert<B>
Since v1.0.0
UuidV4Insert (interface)
Signature
export interface UuidV4Insert<B extends string | symbol>
extends VariantSchema.Field<{
readonly select: Schema.brand<typeof Schema.Uint8ArrayFromSelf, B>
readonly insert: VariantSchema.Overrideable<Uint8Array & Brand<B>, Uint8Array>
readonly update: Schema.brand<typeof Schema.Uint8ArrayFromSelf, B>
readonly json: Schema.brand<typeof Schema.Uint8ArrayFromSelf, B>
}> {}
Since v1.0.0
UuidV4WithGenerate
Signature
declare const UuidV4WithGenerate: <B extends string | symbol>(
schema: Schema.brand<typeof Schema.Uint8ArrayFromSelf, B>
) => VariantSchema.Overrideable<Uint8Array & Brand<B>, Uint8Array>
Since v1.0.0