# Causation

Why an event was recorded. Every event carries two ids:

* `correlationId` is shared by everything that follows from one original request, across aggregates and view models.
* `causationId` names what directly caused the command that recorded it: the request, or the event a view model or saga was reacting to.

They travel in the fiber’s context rather than in payloads. A command run outside any causation starts a new correlation with a fresh id. Inside a view model handler the event being handled is the cause, so a chain of reactions can be drawn as a tree without passing ids around by hand.

## Interfaces

### Causation

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

#### Properties

##### causationId

> `readonly` **causationId**: `string`

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

##### correlationId

> `readonly` **correlationId**: `string`

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

## Variables

### CurrentCausation

> `const` **CurrentCausation**: `Reference`<[`Causation`](https://effect-cqrs.dambox.ch/reference/api/causation/#causation) | `undefined`>

Defined in: [src/Causation.ts:25](https://github.com/davidnussio/effect-cqrs/blob/9d66af7141782f3f89655e6868abe3f141c97632/src/Causation.ts#L25)

The causation of commands run in this fiber, `undefined` outside any.

***

### withCausation

> `const` **withCausation**: {(`causation`): <`A`, `E`, `R`>(`effect`) => `Effect`<`A`, `E`, `R`>; <`A`, `E`, `R`>(`effect`, `causation`): `Effect`<`A`, `E`, `R`>; }

Defined in: [src/Causation.ts:30](https://github.com/davidnussio/effect-cqrs/blob/9d66af7141782f3f89655e6868abe3f141c97632/src/Causation.ts#L30)

Runs an effect with this causation, for instance the id of an incoming HTTP request.

#### Call Signature

> (`causation`): <`A`, `E`, `R`>(`effect`) => `Effect`<`A`, `E`, `R`>

##### Parameters

###### causation

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

##### Returns

<`A`, `E`, `R`>(`effect`) => `Effect`<`A`, `E`, `R`>

#### Call Signature

> <`A`, `E`, `R`>(`effect`, `causation`): `Effect`<`A`, `E`, `R`>

##### Type Parameters

###### A

`A`

###### E

`E`

###### R

`R`

##### Parameters

###### effect

`Effect`<`A`, `E`, `R`>

###### causation

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

##### Returns

`Effect`<`A`, `E`, `R`>

## Functions

### causedBy()

> **causedBy**(`event`): <`A`, `E`, `R`>(`effect`) => `Effect`<`A`, `E`, `R`>

Defined in: [src/Causation.ts:49](https://github.com/davidnussio/effect-cqrs/blob/9d66af7141782f3f89655e6868abe3f141c97632/src/Causation.ts#L49)

Runs an effect in reaction to a stored event, so commands it runs record that event as their cause. View models do this for their handlers already.

#### Parameters

##### event

###### aggregateId

`string` = `Schema.String`

###### aggregateName

`string` = `Schema.String`

###### causationId

`string` | `null` = `...`

What directly caused the command that recorded the event: a request, a command or another event.

###### correlationId

`string` | `null` = `...`

Shared by everything that follows from one original request.

###### metadata

{\[`key`: `string`]: `Json`; } = `Schema.JsonObject`

What the command carried beside its payload, see `Metadata`. Events from before metadata existed have `{}`.

###### payload

`Json` = `Schema.Json`

###### position

`number` = `Schema.Int`

###### schemaVersion

`number` = `Schema.Int`

The version of the event’s schema the payload was written with. Events from before versions existed are `1`.

###### timestamp

`Utc` = `Schema.DateTimeUtcFromMillis`

###### type

`string` = `Schema.String`

###### version

`number` = `Schema.Int`

#### Returns

<`A`, `E`, `R`>(`effect`) => `Effect`<`A`, `E`, `R`>

***

### fromEvent()

> **fromEvent**(`event`): [`Causation`](https://effect-cqrs.dambox.ch/reference/api/causation/#causation)

Defined in: [src/Causation.ts:40](https://github.com/davidnussio/effect-cqrs/blob/9d66af7141782f3f89655e6868abe3f141c97632/src/Causation.ts#L40)

The causation of commands run in reaction to a stored event.

#### Parameters

##### event

###### aggregateId

`string` = `Schema.String`

###### aggregateName

`string` = `Schema.String`

###### causationId

`string` | `null` = `...`

What directly caused the command that recorded the event: a request, a command or another event.

###### correlationId

`string` | `null` = `...`

Shared by everything that follows from one original request.

###### metadata

{\[`key`: `string`]: `Json`; } = `Schema.JsonObject`

What the command carried beside its payload, see `Metadata`. Events from before metadata existed have `{}`.

###### payload

`Json` = `Schema.Json`

###### position

`number` = `Schema.Int`

###### schemaVersion

`number` = `Schema.Int`

The version of the event’s schema the payload was written with. Events from before versions existed are `1`.

###### timestamp

`Utc` = `Schema.DateTimeUtcFromMillis`

###### type

`string` = `Schema.String`

###### version

`number` = `Schema.Int`

#### Returns

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