[@acaldas/document-model-libs](../README.md) / [Exports](../modules.md) / [Document](../modules/Document.md) / BaseDocument

# Class: BaseDocument<T, A\>

[Document](../modules/Document.md).BaseDocument

This is an abstract class representing a document and provides methods
for creating and manipulating documents.

**`Typeparam`**

T - The type of data stored in the document.

**`Typeparam`**

A - The type of action the document can take.

## Type parameters

| Name | Type |
| :------ | :------ |
| `T` | `T` |
| `A` | extends [`Action`](../modules/Document.md#action) |

## Hierarchy

- **`BaseDocument`**

  ↳ [`BudgetStatement`](BudgetStatement.BudgetStatement.md)

## Table of contents

### Constructors

- [constructor](Document.BaseDocument.md#constructor)

### Properties

- [\_document](Document.BaseDocument.md#_document)
- [\_reducer](Document.BaseDocument.md#_reducer)

### Accessors

- [created](Document.BaseDocument.md#created)
- [documentType](Document.BaseDocument.md#documenttype)
- [initialState](Document.BaseDocument.md#initialstate)
- [lastModified](Document.BaseDocument.md#lastmodified)
- [name](Document.BaseDocument.md#name)
- [operations](Document.BaseDocument.md#operations)
- [revision](Document.BaseDocument.md#revision)
- [state](Document.BaseDocument.md#state)

### Methods

- [dispatch](Document.BaseDocument.md#dispatch)
- [getAttachment](Document.BaseDocument.md#getattachment)
- [loadFromFile](Document.BaseDocument.md#loadfromfile)
- [loadState](Document.BaseDocument.md#loadstate)
- [prune](Document.BaseDocument.md#prune)
- [redo](Document.BaseDocument.md#redo)
- [saveToFile](Document.BaseDocument.md#savetofile)
- [setName](Document.BaseDocument.md#setname)
- [undo](Document.BaseDocument.md#undo)
- [stateFromFile](Document.BaseDocument.md#statefromfile)

## Constructors

### constructor

• **new BaseDocument**<`T`, `A`\>(`reducer`, `initialState?`)

Constructs a BaseDocument instance with an initial state.

#### Type parameters

| Name | Type |
| :------ | :------ |
| `T` | `T` |
| `A` | extends [`Action`](../modules/Document.md#action)<`string`, `unknown`\> |

#### Parameters

| Name | Type | Description |
| :------ | :------ | :------ |
| `reducer` | [`Reducer`](../modules/Document.md#reducer)<`T`, `A`\> | The reducer function that updates the state. |
| `initialState?` | `Partial`<[`ExtendedState`](../modules/Document.md#extendedstate)<`T`\>\> | The initial state of the document. |

#### Defined in

[src/document/object.ts:27](https://github.com/acaldas/document-model-libs/blob/d48c213/src/document/object.ts#L27)

## Properties

### \_document

• `Protected` **\_document**: [`Document`](../modules/Document.md#document)<`T`, `A`\>

#### Defined in

[src/document/object.ts:19](https://github.com/acaldas/document-model-libs/blob/d48c213/src/document/object.ts#L19)

___

### \_reducer

• `Private` **\_reducer**: [`Reducer`](../modules/Document.md#reducer)<`T`, `A`\>

#### Defined in

[src/document/object.ts:20](https://github.com/acaldas/document-model-libs/blob/d48c213/src/document/object.ts#L20)

## Accessors

### created

• `get` **created**(): `string`

Gets the timestamp of the date the document was created.

#### Returns

`string`

#### Defined in

[src/document/object.ts:108](https://github.com/acaldas/document-model-libs/blob/d48c213/src/document/object.ts#L108)

___

### documentType

• `get` **documentType**(): `string`

Gets the type of document.

#### Returns

`string`

#### Defined in

[src/document/object.ts:101](https://github.com/acaldas/document-model-libs/blob/d48c213/src/document/object.ts#L101)

___

### initialState

• `get` **initialState**(): [`ExtendedState`](../modules/Document.md#extendedstate)<`T`\>

Gets the initial state of the document.

#### Returns

[`ExtendedState`](../modules/Document.md#extendedstate)<`T`\>

#### Defined in

[src/document/object.ts:129](https://github.com/acaldas/document-model-libs/blob/d48c213/src/document/object.ts#L129)

___

### lastModified

• `get` **lastModified**(): `string`

Gets the timestamp of the date the document was last modified.

#### Returns

`string`

#### Defined in

[src/document/object.ts:115](https://github.com/acaldas/document-model-libs/blob/d48c213/src/document/object.ts#L115)

___

### name

• `get` **name**(): `string`

Gets the name of the document.

#### Returns

`string`

#### Defined in

[src/document/object.ts:94](https://github.com/acaldas/document-model-libs/blob/d48c213/src/document/object.ts#L94)

___

### operations

• `get` **operations**(): [`Operation`](../modules/Document.md#operation)<[`BaseAction`](../modules/Document.md#baseaction) \| `A`\>[]

Gets the list of operations performed on the document.

#### Returns

[`Operation`](../modules/Document.md#operation)<[`BaseAction`](../modules/Document.md#baseaction) \| `A`\>[]

#### Defined in

[src/document/object.ts:87](https://github.com/acaldas/document-model-libs/blob/d48c213/src/document/object.ts#L87)

___

### revision

• `get` **revision**(): `number`

Gets the revision number of the document.

#### Returns

`number`

#### Defined in

[src/document/object.ts:122](https://github.com/acaldas/document-model-libs/blob/d48c213/src/document/object.ts#L122)

___

### state

• `get` **state**(): `T`

Gets the current state of the document.

#### Returns

`T`

#### Defined in

[src/document/object.ts:80](https://github.com/acaldas/document-model-libs/blob/d48c213/src/document/object.ts#L80)

## Methods

### dispatch

▸ `Protected` **dispatch**(`action`): [`BaseDocument`](Document.BaseDocument.md)<`T`, `A`\>

Dispatches an action to update the state of the document.

#### Parameters

| Name | Type | Description |
| :------ | :------ | :------ |
| `action` | [`BaseAction`](../modules/Document.md#baseaction) \| `A` | The action to dispatch. |

#### Returns

[`BaseDocument`](Document.BaseDocument.md)<`T`, `A`\>

The Document instance.

#### Defined in

[src/document/object.ts:40](https://github.com/acaldas/document-model-libs/blob/d48c213/src/document/object.ts#L40)

___

### getAttachment

▸ **getAttachment**(`attachment`): [`DocumentFile`](../modules/Document.md#documentfile)

Gets the attachment associated with the given key.

#### Parameters

| Name | Type | Description |
| :------ | :------ | :------ |
| `attachment` | `string` | The key of the attachment to retrieve. |

#### Returns

[`DocumentFile`](../modules/Document.md#documentfile)

#### Defined in

[src/document/object.ts:137](https://github.com/acaldas/document-model-libs/blob/d48c213/src/document/object.ts#L137)

___

### loadFromFile

▸ **loadFromFile**(`path`): `Promise`<`void`\>

Loads the state of the document from a file.

#### Parameters

| Name | Type | Description |
| :------ | :------ | :------ |
| `path` | `string` | The file path where the state is stored. |

#### Returns

`Promise`<`void`\>

#### Defined in

[src/document/object.ts:59](https://github.com/acaldas/document-model-libs/blob/d48c213/src/document/object.ts#L59)

___

### loadState

▸ **loadState**(`state`, `operations`): `void`

Loads a document state and a set of operations.

#### Parameters

| Name | Type | Description |
| :------ | :------ | :------ |
| `state` | `Pick`<[`ExtendedState`](../modules/Document.md#extendedstate)<`T`\>, ``"name"`` \| ``"state"``\> | The state to load. |
| `operations` | `number` | The operations to apply to the document. |

#### Returns

`void`

#### Defined in

[src/document/object.ts:178](https://github.com/acaldas/document-model-libs/blob/d48c213/src/document/object.ts#L178)

___

### prune

▸ **prune**(`start?`, `end?`): `void`

Removes a range of operations from the document.

#### Parameters

| Name | Type | Description |
| :------ | :------ | :------ |
| `start?` | `number` | The starting index of the range to remove. |
| `end?` | `number` | The ending index of the range to remove. |

#### Returns

`void`

#### Defined in

[src/document/object.ts:169](https://github.com/acaldas/document-model-libs/blob/d48c213/src/document/object.ts#L169)

___

### redo

▸ **redo**(`count`): `void`

Reapplies a number of actions to the document.

#### Parameters

| Name | Type | Description |
| :------ | :------ | :------ |
| `count` | `number` | The number of actions to reapply. |

#### Returns

`void`

#### Defined in

[src/document/object.ts:161](https://github.com/acaldas/document-model-libs/blob/d48c213/src/document/object.ts#L161)

___

### saveToFile

▸ `Protected` **saveToFile**(`path`, `extension`, `name?`): `Promise`<`string`\>

Saves the state of the document to a file.

#### Parameters

| Name | Type | Description |
| :------ | :------ | :------ |
| `path` | `string` | The file path where the state should be saved. |
| `extension` | `string` | The file extension to use when saving the state. |
| `name?` | `string` | - |

#### Returns

`Promise`<`string`\>

The file path where the state was saved.

#### Defined in

[src/document/object.ts:51](https://github.com/acaldas/document-model-libs/blob/d48c213/src/document/object.ts#L51)

___

### setName

▸ **setName**(`name`): `void`

Sets the name of the document.

#### Parameters

| Name | Type | Description |
| :------ | :------ | :------ |
| `name` | `string` | The new name of the document. |

#### Returns

`void`

#### Defined in

[src/document/object.ts:145](https://github.com/acaldas/document-model-libs/blob/d48c213/src/document/object.ts#L145)

___

### undo

▸ **undo**(`count`): `void`

Reverts a number of actions from the document.

#### Parameters

| Name | Type | Description |
| :------ | :------ | :------ |
| `count` | `number` | The number of actions to revert. |

#### Returns

`void`

#### Defined in

[src/document/object.ts:153](https://github.com/acaldas/document-model-libs/blob/d48c213/src/document/object.ts#L153)

___

### stateFromFile

▸ `Static` `Protected` **stateFromFile**<`T`, `A`\>(`path`, `reducer`): `Promise`<[`Document`](../modules/Document.md#document)<`T`, `A`\>\>

Loads the state of the document from a file and returns it.

#### Type parameters

| Name | Type |
| :------ | :------ |
| `T` | `T` |
| `A` | extends [`Action`](../modules/Document.md#action)<`string`, `unknown`\> |

#### Parameters

| Name | Type | Description |
| :------ | :------ | :------ |
| `path` | `string` | The file path where the state is stored. |
| `reducer` | [`Reducer`](../modules/Document.md#reducer)<`T`, `A`\> | The reducer function that updates the state. |

#### Returns

`Promise`<[`Document`](../modules/Document.md#document)<`T`, `A`\>\>

The state of the document.

#### Defined in

[src/document/object.ts:69](https://github.com/acaldas/document-model-libs/blob/d48c213/src/document/object.ts#L69)
