Skip to content

Persistable

Describes request values whose results can be persisted.

A Persistable request has a primary key and schemas for its success and error results. Persistence and PersistedCache use that information to store the request's Exit value and restore it later from a backing store.

15 exports Added in v4.0.0 Source

Accessors

exitSchema

Added in v4.0.0 Source

Returns the cached Exit schema for a persistable request's success and error schemas.

Signature

declare function exitSchema<A extends Constraint, E extends Constraint>(
  self: Persistable<A, E>,
): Exit<A, E, Defect>;

Constructors

Class

Added in v4.0.0 Source

Creates request classes that implement Persistable and Request.Request.

Details

The generated class stores the supplied tag, derives its primary key from the payload, and carries schemas for persisted success and error exits.

Signature

declare function Class<
  Config extends {
    payload: Record<string, unknown>;
    requestError?: any;
    requires?: any;
  } = {
    payload: {};
  },
>(): <Tag extends string, A extends Constraint = Void, E extends Constraint = Never>(
  tag: Tag,
  options: {
    readonly error?: E;
    readonly primaryKey: (payload: Config["payload"]) => string;
    readonly success?: A;
  },
) => (
  args: EqualsWith<
    Config["payload"],
    {},
    void,
    { [P in string | number | symbol]: Config["payload"][P] }
  >,
) => {
  readonly _tag: Tag;
} & { [K in string | number | symbol]: Config["payload"][K] } & Persistable<A, E> &
  Request<
    A["Type"],
    E["Type"] | "requestError" extends keyof Config
      ? Config["requestError"]
      : SchemaError | PersistenceError,
      | A["DecodingServices"]
      | A["EncodingServices"]
      | E["DecodingServices"]
      | E["EncodingServices"]
      | "requires" extends keyof Config
      ? Config["requires"]
      : never
  >;

Models

Any type

Added in v4.0.0 Source

Any persistable request regardless of its success and error schemas.

Signature

type Any = Persistable<Schema.Constraint, Schema.Constraint>;

Persistable interface

Added in v4.0.0 Source

A primary-keyed request value whose success and error results can be serialized for persistence.

Signature

interface Persistable<A extends Schema.Constraint, E extends Schema.Constraint> extends PrimaryKey {
  readonly "~effect/persistence/Persistable": {
    readonly error: E;
    readonly success: A;
  };
}

Serialization

Decodes a persisted value into an Exit for a persistable request using its success and error schemas.

Signature

declare function deserializeExit<A extends Constraint, E extends Constraint>(
  self: Persistable<A, E>,
  encoded: unknown,
): Effect<Exit<A["Type"], E["Type"]>, SchemaError, A["DecodingServices"] | E["DecodingServices"]>;

Encodes an Exit for a persistable request using its success and error schemas.

Signature

declare function serializeExit<A extends Constraint, E extends Constraint>(
  self: Persistable<A, E>,
  exit: Exit<A["Type"], E["Type"]>,
): Effect<unknown, SchemaError, A["EncodingServices"] | E["EncodingServices"]>;

Symbols

symbol

Added in v4.0.0 Source

Defines the property key used to attach success and error schemas to persistable requests.

When to use

Use to implement persistable request values by attaching success and error schemas at this property key.

Signature

declare const symbol: "~effect/persistence/Persistable";

Utility Types

DecodingServices type

Added in v4.0.0 Source

Services required to decode a persisted success or error value for the request.

Signature

type DecodingServices<A extends Any> =
  | A["~effect/persistence/Persistable"]["success"]["DecodingServices"]
  | A["~effect/persistence/Persistable"]["error"]["DecodingServices"];

EncodingServices type

Added in v4.0.0 Source

Services required to encode a success or error value for persistence.

Signature

type EncodingServices<A extends Any> =
  | A["~effect/persistence/Persistable"]["success"]["EncodingServices"]
  | A["~effect/persistence/Persistable"]["error"]["EncodingServices"];

Error type

Added in v4.0.0 Source

Extracts the error value type from a persistable request.

Signature

type Error<A extends Any> = A["~effect/persistence/Persistable"]["error"]["Type"];

ErrorSchema type

Added in v4.0.0 Source

Extracts the error schema from a persistable request.

Signature

type ErrorSchema<A extends Any> = A["~effect/persistence/Persistable"]["error"];

Services type

Added in v4.0.0 Source

All schema services required to encode and decode a persistable request result.

Signature

type Services<A extends Any> =
  | A["~effect/persistence/Persistable"]["success"]["DecodingServices"]
  | A["~effect/persistence/Persistable"]["success"]["EncodingServices"]
  | A["~effect/persistence/Persistable"]["error"]["DecodingServices"]
  | A["~effect/persistence/Persistable"]["error"]["EncodingServices"];

Success type

Added in v4.0.0 Source

Extracts the success value type from a persistable request.

Signature

type Success<A extends Any> = A["~effect/persistence/Persistable"]["success"]["Type"];

SuccessSchema type

Added in v4.0.0 Source

Extracts the success schema from a persistable request.

Signature

type SuccessSchema<A extends Any> = A["~effect/persistence/Persistable"]["success"];

TimeToLiveFn type

Added in v4.0.0 Source

Computes the time to live for a persisted result from the result Exit and request value.

Signature

type TimeToLiveFn<K extends Any> = (
  exit: Exit.Exit<Success<K>, Error<K>>,
  request: K,
) => Duration.Input;