# SqliteEventStore

Durable storage on SQLite, through any `effect/sql` SQLite driver.

The schema relies on SQLite’s single writer for positions to follow commit order, so it is not portable to other databases.

An append is one statement, which checks the stream’s version and inserts the events only if it still matches. Appends that arrive while others are being written go together into one transaction, so they share the cost of a commit.

## Interfaces

### Options

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

#### Properties

##### checkpointsTable?

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

Defined in: [src/SqliteEventStore.ts:20](https://github.com/davidnussio/effect-cqrs/blob/9d66af7141782f3f89655e6868abe3f141c97632/src/SqliteEventStore.ts#L20)

##### eventsTable?

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

Defined in: [src/SqliteEventStore.ts:19](https://github.com/davidnussio/effect-cqrs/blob/9d66af7141782f3f89655e6868abe3f141c97632/src/SqliteEventStore.ts#L19)

##### migrate?

> `readonly` `optional` **migrate?**: `boolean`

Defined in: [src/SqliteEventStore.ts:26](https://github.com/davidnussio/effect-cqrs/blob/9d66af7141782f3f89655e6868abe3f141c97632/src/SqliteEventStore.ts#L26)

Creates the tables, or brings tables of an earlier version up to date, on startup. Defaults to `true`. Set it to `false` where the application may not change the schema, and apply [migrations](https://effect-cqrs.dambox.ch/reference/api/sqliteeventstore/#migrations) with your own tooling.

##### pollInterval?

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

Defined in: [src/SqliteEventStore.ts:31](https://github.com/davidnussio/effect-cqrs/blob/9d66af7141782f3f89655e6868abe3f141c97632/src/SqliteEventStore.ts#L31)

Reads the head this often, so subscribers notice appends that other processes make to the same file. Off by default. See `EventStore.MakeOptions`.

## Type Aliases

### Migration

> **Migration** = [`Migration`](https://effect-cqrs.dambox.ch/reference/api/eventstore/#migration)

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

A schema change, see `EventStore.Migration`. Migrations are listed in the order to apply them.

## Variables

### make

> `const` **make**: (…`args`) => `Effect`<{ `afterCommit?`: <`R`>(`effect`) => `Effect`<`void`, `never`, `R`>; `changes`: `Stream`<`number`>; `head`: `Effect`<`number`, [`EventStoreError`](https://effect-cqrs.dambox.ch/reference/api/eventstore/#eventstoreerror)>; `append`(`input`): `Effect`\<readonly `object`\[], [`EventStoreError`](https://effect-cqrs.dambox.ch/reference/api/eventstore/#eventstoreerror) | [`ConcurrencyConflict`](https://effect-cqrs.dambox.ch/reference/api/eventstore/#concurrencyconflict)>; `read`(`filter?`): `Stream`<{ `aggregateId`: `string`; `aggregateName`: `string`; `causationId`: `string` | `null`; `correlationId`: `string` | `null`; `metadata`: {\[`key`: `string`]: `Json`; }; `payload`: `Json`; `position`: `number`; `schemaVersion`: `number`; `timestamp`: `Utc`; `type`: `string`; `version`: `number`; }, [`EventStoreError`](https://effect-cqrs.dambox.ch/reference/api/eventstore/#eventstoreerror)>; `subscribe`(`filter?`): `Stream`<{ `aggregateId`: `string`; `aggregateName`: `string`; `causationId`: `string` | `null`; `correlationId`: `string` | `null`; `metadata`: {\[`key`: `string`]: `Json`; }; `payload`: `Json`; `position`: `number`; `schemaVersion`: `number`; `timestamp`: `Utc`; `type`: `string`; `version`: `number`; }, [`EventStoreError`](https://effect-cqrs.dambox.ch/reference/api/eventstore/#eventstoreerror)>; }, [`EventStoreError`](https://effect-cqrs.dambox.ch/reference/api/eventstore/#eventstoreerror), `Scope` | `SqlClient`>

Defined in: [src/SqliteEventStore.ts:173](https://github.com/davidnussio/effect-cqrs/blob/9d66af7141782f3f89655e6868abe3f141c97632/src/SqliteEventStore.ts#L173)

#### Parameters

##### args

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

#### Returns

`Effect`<{ `afterCommit?`: <`R`>(`effect`) => `Effect`<`void`, `never`, `R`>; `changes`: `Stream`<`number`>; `head`: `Effect`<`number`, [`EventStoreError`](https://effect-cqrs.dambox.ch/reference/api/eventstore/#eventstoreerror)>; `append`(`input`): `Effect`\<readonly `object`\[], [`EventStoreError`](https://effect-cqrs.dambox.ch/reference/api/eventstore/#eventstoreerror) | [`ConcurrencyConflict`](https://effect-cqrs.dambox.ch/reference/api/eventstore/#concurrencyconflict)>; `read`(`filter?`): `Stream`<{ `aggregateId`: `string`; `aggregateName`: `string`; `causationId`: `string` | `null`; `correlationId`: `string` | `null`; `metadata`: {\[`key`: `string`]: `Json`; }; `payload`: `Json`; `position`: `number`; `schemaVersion`: `number`; `timestamp`: `Utc`; `type`: `string`; `version`: `number`; }, [`EventStoreError`](https://effect-cqrs.dambox.ch/reference/api/eventstore/#eventstoreerror)>; `subscribe`(`filter?`): `Stream`<{ `aggregateId`: `string`; `aggregateName`: `string`; `causationId`: `string` | `null`; `correlationId`: `string` | `null`; `metadata`: {\[`key`: `string`]: `Json`; }; `payload`: `Json`; `position`: `number`; `schemaVersion`: `number`; `timestamp`: `Utc`; `type`: `string`; `version`: `number`; }, [`EventStoreError`](https://effect-cqrs.dambox.ch/reference/api/eventstore/#eventstoreerror)>; }, [`EventStoreError`](https://effect-cqrs.dambox.ch/reference/api/eventstore/#eventstoreerror), `Scope` | `SqlClient`>

## Functions

### layer()

> **layer**(`options?`): `Layer`<[`EventStore`](https://effect-cqrs.dambox.ch/reference/api/eventstore/#eventstore), [`EventStoreError`](https://effect-cqrs.dambox.ch/reference/api/eventstore/#eventstoreerror), `SqlClient`>

Defined in: [src/SqliteEventStore.ts:310](https://github.com/davidnussio/effect-cqrs/blob/9d66af7141782f3f89655e6868abe3f141c97632/src/SqliteEventStore.ts#L310)

#### Parameters

##### options?

[`Options`](https://effect-cqrs.dambox.ch/reference/api/sqliteeventstore/#options)

#### Returns

`Layer`<[`EventStore`](https://effect-cqrs.dambox.ch/reference/api/eventstore/#eventstore), [`EventStoreError`](https://effect-cqrs.dambox.ch/reference/api/eventstore/#eventstoreerror), `SqlClient`>

***

### layerCheckpoints()

> **layerCheckpoints**(`options?`): `Layer`<[`Checkpoints`](https://effect-cqrs.dambox.ch/reference/api/checkpoints/#checkpoints), [`EventStoreError`](https://effect-cqrs.dambox.ch/reference/api/eventstore/#eventstoreerror), `SqlClient`>

Defined in: [src/SqliteEventStore.ts:316](https://github.com/davidnussio/effect-cqrs/blob/9d66af7141782f3f89655e6868abe3f141c97632/src/SqliteEventStore.ts#L316)

Keeps checkpoints in the same database, for view models that are durable too.

#### Parameters

##### options?

[`Options`](https://effect-cqrs.dambox.ch/reference/api/sqliteeventstore/#options)

#### Returns

`Layer`<[`Checkpoints`](https://effect-cqrs.dambox.ch/reference/api/checkpoints/#checkpoints), [`EventStoreError`](https://effect-cqrs.dambox.ch/reference/api/eventstore/#eventstoreerror), `SqlClient`>

***

### migrations()

> **migrations**(`options?`): readonly [`Migration`](https://effect-cqrs.dambox.ch/reference/api/eventstore/#migration)\[]

Defined in: [src/SqliteEventStore.ts:123](https://github.com/davidnussio/effect-cqrs/blob/9d66af7141782f3f89655e6868abe3f141c97632/src/SqliteEventStore.ts#L123)

The schema of the events and checkpoints tables, for tools that apply migrations themselves. `layer` and `layerCheckpoints` run the same statements on startup unless `migrate` is `false`. SQLite can’t add a column only if it is missing, so the `ALTER TABLE` statements must run exactly once.

#### Parameters

##### options?

[`Options`](https://effect-cqrs.dambox.ch/reference/api/sqliteeventstore/#options)

#### Returns

readonly [`Migration`](https://effect-cqrs.dambox.ch/reference/api/eventstore/#migration)\[]
