# DocumentServices

## Content

# Class: DocumentServices<S, T\>

Provides document services for collaborative operational transformation.

## Type parameters

| Name | Description |
| :------ | :------ |
| `S` | The type of snapshot data. |
| `T` | The type of operation data. |

## Table of contents

### Constructors

- [constructor](DocumentServices#constructor)

### Properties

- [db](DocumentServices#db)
- [maxSubmitRetries](DocumentServices#maxsubmitretries)
- [milestoneDb](DocumentServices#milestonedb)
- [submitSnapshotBatchSize](DocumentServices#submitsnapshotbatchsize)

### Methods

- [fetch](DocumentServices#fetch)
- [fetchHistorySnapshot](DocumentServices#fetchhistorysnapshot)
- [getOps](DocumentServices#getops)
- [on](DocumentServices#on)
- [submit](DocumentServices#submit)
- [use](DocumentServices#use)

## Constructors

### <a id="constructor" name="constructor"></a> constructor

• **new DocumentServices**<`S`, `T`\>(`config?`)

#### Type parameters

| Name |
| :------ |
| `S` |
| `T` |

#### Parameters

| Name | Type |
| :------ | :------ |
| `config?` | [`IDocConfig`](../interfaces/IDocConfig)<`S`, `T`\> |

## Properties

### <a id="db" name="db"></a> db

• **db**: [`IDatabaseAdapter`](../interfaces/IDatabaseAdapter)<`S`, `T`\>

The database adapter for storing document data.

**`template`** The type of snapshot data.

**`template`** The type of operation data.

___

### <a id="maxsubmitretries" name="maxsubmitretries"></a> maxSubmitRetries

• `Optional` **maxSubmitRetries**: `number`

The maximum number of retries for submitting an operation.

**`default`** undefined - No retry limit.

___

### <a id="milestonedb" name="milestonedb"></a> milestoneDb

• **milestoneDb**: [`IMilestoneDatabaseAdapter`](../interfaces/IMilestoneDatabaseAdapter)<`S`\>

The milestone database adapter for snapshot milestones.

**`template`** The type of snapshot data.

___

### <a id="submitsnapshotbatchsize" name="submitsnapshotbatchsize"></a> submitSnapshotBatchSize

• **submitSnapshotBatchSize**: `number`

The batch size for submitting snapshots.

**`default`** 100

## Methods

### <a id="fetch" name="fetch"></a> fetch

▸ **fetch**(`id`, `context?`): `Promise`<[`ISnapshot`](../interfaces/ISnapshot)<`S`\>\>

Fetches the latest snapshot of a document.

#### Parameters

| Name | Type | Description |
| :------ | :------ | :------ |
| `id` | `string` | The ID of the document. |
| `context?` | [`IContext`](../interfaces/IContext) | - |

#### Returns

`Promise`<[`ISnapshot`](../interfaces/ISnapshot)<`S`\>\>

A promise resolving to the document snapshot.

___

### <a id="fetchhistorysnapshot" name="fetchhistorysnapshot"></a> fetchHistorySnapshot

▸ **fetchHistorySnapshot**(`id`, `version?`, `context?`): `Promise`<[`ISnapshot`](../interfaces/ISnapshot)<`S`\>\>

Fetches a historical snapshot of a document by version.

#### Parameters

| Name | Type | Description |
| :------ | :------ | :------ |
| `id` | `string` | The ID of the document. |
| `version?` | `number` | - |
| `context?` | [`IContext`](../interfaces/IContext) | - |

#### Returns

`Promise`<[`ISnapshot`](../interfaces/ISnapshot)<`S`\>\>

A promise resolving to the historical snapshot.

___

### <a id="getops" name="getops"></a> getOps

▸ **getOps**(`id`, `from`, `to?`, `context?`): `Promise`<[`IOp`](../interfaces/IOp)<`T`\>[]\>

Retrieves operations between two versions (inclusive of 'from', exclusive of 'to').

#### Parameters

| Name | Type | Description |
| :------ | :------ | :------ |
| `id` | `string` | The ID of the document. |
| `from` | `number` | The starting version. |
| `to?` | `number` | - |
| `context?` | [`IContext`](../interfaces/IContext) | - |

#### Returns

`Promise`<[`IOp`](../interfaces/IOp)<`T`\>[]\>

A promise resolving to an array of operations.

___

### <a id="on" name="on"></a> on

▸ **on**<`K`\>(`hookName`, `hook`): `void`

Registers a hook for a specific event in the document services.

#### Type parameters

| Name | Type | Description |
| :------ | :------ | :------ |
| `K` | extends keyof [`IDocHookContext`](../interfaces/IDocHookContext) | The type of the hook name, extending keyof IDocHookContext. |

#### Parameters

| Name | Type | Description |
| :------ | :------ | :------ |
| `hookName` | `K` | The name of the hook to register. |
| `hook` | `IHook`<[`IDocHookContext`](../interfaces/IDocHookContext)[`K`]\> | The hook to register. |

#### Returns

`void`

___

### <a id="submit" name="submit"></a> submit

▸ **submit**(`id`, `op`, `context?`): `Promise`<[`IOp`](../interfaces/IOp)<`T`\>[]\>

Submits an operation to the document (must include an {op:}, {create:}, or {del:} field).

#### Parameters

| Name | Type | Description |
| :------ | :------ | :------ |
| `id` | `string` | The ID of the document. |
| `op` | [`IOp`](../interfaces/IOp)<`T`\> | The operation to submit. |
| `context?` | [`IContext`](../interfaces/IContext) | - |

#### Returns

`Promise`<[`IOp`](../interfaces/IOp)<`T`\>[]\>

A promise resolving to the resulting operations.

___

### <a id="use" name="use"></a> use

▸ **use**<`K`\>(`action`, `middleware`): `void`

Registers a middleware for a specific action in the document services.

#### Type parameters

| Name | Type | Description |
| :------ | :------ | :------ |
| `K` | extends keyof [`IDocMiddlewareContext`](../interfaces/IDocMiddlewareContext)<`S`, `T`\> | The type of the action name, extending keyof IDocMiddlewareContext<S, T>. |

#### Parameters

| Name | Type | Description |
| :------ | :------ | :------ |
| `action` | `K` | The name of the action to associate with the middleware. |
| `middleware` | `IMiddleware`<[`IDocMiddlewareContext`](../interfaces/IDocMiddlewareContext)<`S`, `T`\>[`K`]\> | The middleware to register. |

#### Returns

`void`
