# Leases

Decides which process runs each view model.

Replicas that share an event store would otherwise all run the same view models and race on their checkpoints. `ViewModel.layer` takes a lease on each view model before running it: the process that holds the lease runs it, and the others wait as standbys until it is given up.

## Classes

### Leases

Defined in: [src/Leases.ts:21](https://github.com/davidnussio/effect-cqrs/blob/9d66af7141782f3f89655e6868abe3f141c97632/src/Leases.ts#L21)

#### Extends

* `Shape`<`"effect-cqrs/Leases"`, { `acquire`(`name`): `Effect`<[`Lease`](https://effect-cqrs.dambox.ch/reference/api/leases/#lease), `never`, `Scope`>; }, `this`>

#### Constructors

##### Constructor

> **new Leases**(`_`): [`Leases`](https://effect-cqrs.dambox.ch/reference/api/leases/#leases)

Defined in: node_modules/.pnpm/effect\@4.0.6/node_modules/effect/dist/Context.d.ts:121

###### Parameters

###### \_

`never`

###### Returns

[`Leases`](https://effect-cqrs.dambox.ch/reference/api/leases/#leases)

###### Inherited from

``Context.Service<Leases, { /** * Competes for the lease called `name` until the scope closes, which gives it * up. The first attempt is made before this returns. */ acquire(name: string): Effect.Effect<Lease, never, Scope.Scope> }>()("effect-cqrs/Leases").constructor``

## Interfaces

### Lease

Defined in: [src/Leases.ts:14](https://github.com/davidnussio/effect-cqrs/blob/9d66af7141782f3f89655e6868abe3f141c97632/src/Leases.ts#L14)

#### Properties

##### changes

> `readonly` **changes**: `Stream`<`boolean`>

Defined in: [src/Leases.ts:18](https://github.com/davidnussio/effect-cqrs/blob/9d66af7141782f3f89655e6868abe3f141c97632/src/Leases.ts#L18)

Emits whether the lease is held, then every change.

##### held

> `readonly` **held**: `Effect`<`boolean`>

Defined in: [src/Leases.ts:16](https://github.com/davidnussio/effect-cqrs/blob/9d66af7141782f3f89655e6868abe3f141c97632/src/Leases.ts#L16)

Whether this process holds the lease now.

***

### PostgresOptions

Defined in: [src/Leases.ts:39](https://github.com/davidnussio/effect-cqrs/blob/9d66af7141782f3f89655e6868abe3f141c97632/src/Leases.ts#L39)

#### Properties

##### interval?

> `readonly` `optional` **interval?**: `Input`

Defined in: [src/Leases.ts:44](https://github.com/davidnussio/effect-cqrs/blob/9d66af7141782f3f89655e6868abe3f141c97632/src/Leases.ts#L44)

How often a standby tries to take a lease over, and the holder checks that its connection is alive. Defaults to 5 seconds.

##### prefix?

> `readonly` `optional` **prefix?**: `string`

Defined in: [src/Leases.ts:46](https://github.com/davidnussio/effect-cqrs/blob/9d66af7141782f3f89655e6868abe3f141c97632/src/Leases.ts#L46)

Prefixes the names, so that several applications can share a database. Defaults to `effect-cqrs/`.

## Variables

### layerMemory

> `const` **layerMemory**: `Layer.Layer`<[`Leases`](https://effect-cqrs.dambox.ch/reference/api/leases/#leases)>

Defined in: [src/Leases.ts:37](https://github.com/davidnussio/effect-cqrs/blob/9d66af7141782f3f89655e6868abe3f141c97632/src/Leases.ts#L37)

***

### makePostgres

> `const` **makePostgres**: (…`args`) => `Effect`<{ `acquire`(`name`): `Effect`<[`Lease`](https://effect-cqrs.dambox.ch/reference/api/leases/#lease), `never`, `Scope`>; }, `never`, `Scope` | `SqlClient`>

Defined in: [src/Leases.ts:70](https://github.com/davidnussio/effect-cqrs/blob/9d66af7141782f3f89655e6868abe3f141c97632/src/Leases.ts#L70)

Leases on PostgreSQL advisory locks, `pg_try_advisory_lock` on a hash of the name. The locks belong to one connection that the service reserves from the client’s pool for as long as anything competes, so a process holds them until it gives them up or its connection closes: when it dies, the database releases them and a standby takes over at its next attempt.

A holder whose connection fails stops at its next check, up to `interval` later, while a standby may already have started: handlers can run in two processes for that long.

#### Parameters

##### args

…\[`options?`: [`PostgresOptions`](https://effect-cqrs.dambox.ch/reference/api/leases/#postgresoptions)]

#### Returns

`Effect`<{ `acquire`(`name`): `Effect`<[`Lease`](https://effect-cqrs.dambox.ch/reference/api/leases/#lease), `never`, `Scope`>; }, `never`, `Scope` | `SqlClient`>

***

### memory

> `const` **memory**: [`Leases`](https://effect-cqrs.dambox.ch/reference/api/leases/#leases)\[`"Service"`]

Defined in: [src/Leases.ts:35](https://github.com/davidnussio/effect-cqrs/blob/9d66af7141782f3f89655e6868abe3f141c97632/src/Leases.ts#L35)

Leases that are always held. It is the default of `ViewModel.layer`, for a single process or for view models that keep their data in that process.

## Functions

### layerPostgres()

> **layerPostgres**(`options?`): `Layer`<[`Leases`](https://effect-cqrs.dambox.ch/reference/api/leases/#leases), `never`, `SqlClient`>

Defined in: [src/Leases.ts:156](https://github.com/davidnussio/effect-cqrs/blob/9d66af7141782f3f89655e6868abe3f141c97632/src/Leases.ts#L156)

Leases on PostgreSQL advisory locks, see [makePostgres](https://effect-cqrs.dambox.ch/reference/api/leases/#makepostgres).

#### Parameters

##### options?

[`PostgresOptions`](https://effect-cqrs.dambox.ch/reference/api/leases/#postgresoptions)

#### Returns

`Layer`<[`Leases`](https://effect-cqrs.dambox.ch/reference/api/leases/#leases), `never`, `SqlClient`>
