Skip to content

Persistence.ts

Stores encoded results for Persistable requests.

The Persistence service creates scoped stores keyed by each request’s PrimaryKey. Stores read and write schema-encoded Exit values with optional TTLs, letting request workflows reuse expensive or idempotent results across fibers, process restarts, or workers that share a backing store.

Since v4.0.0



Service for creating raw backing stores for persistence store ids.

Signature

declare class BackingPersistence

Source

Since v4.0.0

Raw persistence backing store for JSON-compatible objects with optional TTLs.

Signature

export interface BackingPersistenceStore {
readonly get: (key: string) => Effect.Effect<object | undefined, PersistenceError>
readonly getMany: (
keys: Arr.NonEmptyArray<string>
) => Effect.Effect<Arr.NonEmptyArray<object | undefined>, PersistenceError>
readonly set: (
key: string,
value: object,
ttl: Duration.Duration | undefined
) => Effect.Effect<void, PersistenceError>
readonly setMany: (
entries: Arr.NonEmptyArray<readonly [key: string, value: object, ttl: Duration.Duration | undefined]>
) => Effect.Effect<void, PersistenceError>
readonly remove: (key: string) => Effect.Effect<void, PersistenceError>
readonly clear: Effect.Effect<void, PersistenceError>
}

Source

Since v4.0.0

Converts a TTL to an absolute expiration timestamp in milliseconds.

Details

Returns null for no TTL and uses clock.currentTimeMillisUnsafe, so it is intended for backing-store internals.

Signature

declare const unsafeTtlToExpires: (clock: Clock.Clock, ttl: Duration.Duration | undefined) => number | null

Source

Since v4.0.0

Error raised by persistence and backing-store operations.

Signature

declare class PersistenceError

Source

Since v4.0.0

Marks this value as a persistence error for runtime guards.

Signature

readonly [ErrorTypeId]: "~effect/persistence/Persistence/PersistenceError"

Source

Since v4.0.0

Provides Persistence from BackingPersistence.

Details

The layer serializes and deserializes Persistable exits, applies per-entry TTLs, and skips writes whose TTL is zero or negative.

Signature

declare const layer: Layer.Layer<Persistence, never, BackingPersistence>

Source

Since v4.0.0

Provides BackingPersistence using a KeyValueStore.

Details

Each store id becomes a key prefix, and values are stored as JSON with optional expiration timestamps.

Signature

declare const layerBackingKvs: Layer.Layer<BackingPersistence, never, KeyValueStore.KeyValueStore>

Source

Since v4.0.0

Provides an in-memory BackingPersistence grouped by store id.

Details

Entries are process-local and expire according to their stored TTL.

Signature

declare const layerBackingMemory: Layer.Layer<BackingPersistence, never, never>

Source

Since v4.0.0

Provides Redis-backed persistence.

Details

Each store id is used as a key prefix, values are JSON-encoded, and finite TTLs are stored with Redis expiration.

Signature

declare const layerBackingRedis: Layer.Layer<BackingPersistence, never, Redis.Redis>

Source

Since v4.0.0

Provides SQL-backed persistence using a shared effect_persistence table.

Details

Rows are partitioned by store_id and store JSON-encoded values with optional expiration timestamps.

Signature

declare const layerBackingSql: Layer.Layer<BackingPersistence, never, SqlClient.SqlClient>

Source

Since v4.0.0

Provides SQL-backed persistence using one table per store id.

Details

Each table is created if needed and stores JSON-encoded values with optional expiration timestamps.

Signature

declare const layerBackingSqlMultiTable: Layer.Layer<BackingPersistence, never, SqlClient.SqlClient>

Source

Since v4.0.0

Provides Persistence backed by the current KeyValueStore.

Signature

declare const layerKvs: Layer.Layer<Persistence, never, KeyValueStore.KeyValueStore>

Source

Since v4.0.0

Provides Persistence backed by process-local in-memory storage.

Signature

declare const layerMemory: Layer.Layer<Persistence, never, never>

Source

Since v4.0.0

Provides Persistence backed by the current Redis service.

Signature

declare const layerRedis: Layer.Layer<Persistence, never, Redis.Redis>

Source

Since v4.0.0

Provides Persistence backed by SQL using a shared persistence table.

Signature

declare const layerSql: Layer.Layer<Persistence, never, SqlClient.SqlClient>

Source

Since v4.0.0

Provides Persistence backed by SQL with one table per store id.

Signature

declare const layerSqlMultiTable: Layer.Layer<Persistence, never, SqlClient.SqlClient>

Source

Since v4.0.0

Service for creating scoped stores of persisted Persistable request results.

Signature

declare class Persistence

Source

Since v4.0.0

Typed store for persisted Exit values keyed by Persistable requests.

Signature

export interface PersistenceStore {
readonly get: <A extends Schema.Constraint, E extends Schema.Constraint>(
key: Persistable.Persistable<A, E>
) => Effect.Effect<
Exit.Exit<A["Type"], E["Type"]> | undefined,
PersistenceError | Schema.SchemaError,
A["DecodingServices"] | E["DecodingServices"]
>
readonly getMany: <A extends Schema.Constraint, E extends Schema.Constraint>(
keys: Iterable<Persistable.Persistable<A, E>>
) => Effect.Effect<
Array<Exit.Exit<A["Type"], E["Type"]> | undefined>,
PersistenceError | Schema.SchemaError,
A["DecodingServices"] | E["DecodingServices"]
>
readonly set: <A extends Schema.Constraint, E extends Schema.Constraint>(
key: Persistable.Persistable<A, E>,
value: Exit.Exit<A["Type"], E["Type"]>
) => Effect.Effect<void, PersistenceError | Schema.SchemaError, A["EncodingServices"] | E["EncodingServices"]>
readonly setMany: <A extends Schema.Constraint, E extends Schema.Constraint>(
entries: Iterable<readonly [Persistable.Persistable<A, E>, Exit.Exit<A["Type"], E["Type"]>]>
) => Effect.Effect<void, PersistenceError | Schema.SchemaError, A["EncodingServices"] | E["EncodingServices"]>
readonly remove: <A extends Schema.Constraint, E extends Schema.Constraint>(
key: Persistable.Persistable<A, E>
) => Effect.Effect<void, PersistenceError>
readonly clear: Effect.Effect<void, PersistenceError>
}

Source

Since v4.0.0