> ## Documentation Index
> Fetch the complete documentation index at: https://botmesh.afaya.com.co/llms.txt
> Use this file to discover all available pages before exploring further.

# MemoryStore

> Class exported by @botmesh/core.

Defined in: [store/MemoryStore.ts:80](https://github.com/andresayac/botmesh/blob/main/packages/core/src/store/MemoryStore.ts#L80)

The built-in [IStore](https://botmesh.afaya.com.co/api/core/interfaces/IStore.md): everything in RAM, optionally saved to a JSON `file` on
an interval and when the bot stops. It is the default store. It keeps the newest 1000 messages
per chat unless `maxMessagesPerChat` says otherwise. For a database, use
`@botmesh/sqlite-store` or `@botmesh/redis-store`.

## Example

```ts theme={null}
const bot = new Bot({ provider, store: new MemoryStore({ file: './data/store.json' }) })
```

## Implements

* [`IStore`](https://botmesh.afaya.com.co/api/core/interfaces/IStore)

## Constructors

### Constructor

```ts theme={null}
new MemoryStore(opts?): MemoryStore;
```

Defined in: [store/MemoryStore.ts:97](https://github.com/andresayac/botmesh/blob/main/packages/core/src/store/MemoryStore.ts#L97)

#### Parameters

| Parameter | Type |
| - | - |
| `opts?` | [`MemoryStoreOptions`](https://botmesh.afaya.com.co/api/core/interfaces/MemoryStoreOptions) |

#### Returns

`MemoryStore`

## Methods

### start()

```ts theme={null}
start(): Promise<void>;
```

Defined in: [store/MemoryStore.ts:112](https://github.com/andresayac/botmesh/blob/main/packages/core/src/store/MemoryStore.ts#L112)

#### Returns

`Promise`\<`void`>

#### Implementation of

[`IStore`](https://botmesh.afaya.com.co/api/core/interfaces/IStore).[`start`](https://botmesh.afaya.com.co/api/core/interfaces/IStore#start)

***

### stop()

```ts theme={null}
stop(): Promise<void>;
```

Defined in: [store/MemoryStore.ts:134](https://github.com/andresayac/botmesh/blob/main/packages/core/src/store/MemoryStore.ts#L134)

#### Returns

`Promise`\<`void`>

#### Implementation of

[`IStore`](https://botmesh.afaya.com.co/api/core/interfaces/IStore).[`stop`](https://botmesh.afaya.com.co/api/core/interfaces/IStore#stop)

***

### saveMessage()

```ts theme={null}
saveMessage(msg): Promise<void>;
```

Defined in: [store/MemoryStore.ts:199](https://github.com/andresayac/botmesh/blob/main/packages/core/src/store/MemoryStore.ts#L199)

#### Parameters

| Parameter | Type |
| - | - |
| `msg` | [`StoredMessage`](https://botmesh.afaya.com.co/api/core/interfaces/StoredMessage) |

#### Returns

`Promise`\<`void`>

#### Implementation of

[`IStore`](https://botmesh.afaya.com.co/api/core/interfaces/IStore).[`saveMessage`](https://botmesh.afaya.com.co/api/core/interfaces/IStore#savemessage)

***

### getMessage()

```ts theme={null}
getMessage(ref): Promise<StoredMessage | undefined>;
```

Defined in: [store/MemoryStore.ts:206](https://github.com/andresayac/botmesh/blob/main/packages/core/src/store/MemoryStore.ts#L206)

#### Parameters

| Parameter | Type |
| - | - |
| `ref` | [`MessageRef`](https://botmesh.afaya.com.co/api/core/interfaces/MessageRef) |

#### Returns

`Promise`\<[`StoredMessage`](https://botmesh.afaya.com.co/api/core/interfaces/StoredMessage) | `undefined`>

#### Implementation of

[`IStore`](https://botmesh.afaya.com.co/api/core/interfaces/IStore).[`getMessage`](https://botmesh.afaya.com.co/api/core/interfaces/IStore#getmessage)

***

### getMessagesByChat()

```ts theme={null}
getMessagesByChat(chatId, limit?): Promise<StoredMessage[]>;
```

Defined in: [store/MemoryStore.ts:210](https://github.com/andresayac/botmesh/blob/main/packages/core/src/store/MemoryStore.ts#L210)

#### Parameters

| Parameter | Type | Default value |
| - | - | - |
| `chatId` | `string` | `undefined` |
| `limit?` | `number` | `50` |

#### Returns

`Promise`\<[`StoredMessage`](https://botmesh.afaya.com.co/api/core/interfaces/StoredMessage)\[]>

#### Implementation of

[`IStore`](https://botmesh.afaya.com.co/api/core/interfaces/IStore).[`getMessagesByChat`](https://botmesh.afaya.com.co/api/core/interfaces/IStore#getmessagesbychat)

***

### deleteMessage()

```ts theme={null}
deleteMessage(ref): Promise<void>;
```

Defined in: [store/MemoryStore.ts:217](https://github.com/andresayac/botmesh/blob/main/packages/core/src/store/MemoryStore.ts#L217)

#### Parameters

| Parameter | Type |
| - | - |
| `ref` | [`MessageRef`](https://botmesh.afaya.com.co/api/core/interfaces/MessageRef) |

#### Returns

`Promise`\<`void`>

#### Implementation of

[`IStore`](https://botmesh.afaya.com.co/api/core/interfaces/IStore).[`deleteMessage`](https://botmesh.afaya.com.co/api/core/interfaces/IStore#deletemessage)

***

### upsertChat()

```ts theme={null}
upsertChat(chat): Promise<void>;
```

Defined in: [store/MemoryStore.ts:223](https://github.com/andresayac/botmesh/blob/main/packages/core/src/store/MemoryStore.ts#L223)

Shallow-merge `chat` onto the stored record. An own field set to `undefined` UNSETS
that field, so a partial upsert must omit what it does not know (the Bot does).

#### Parameters

| Parameter | Type |
| - | - |
| `chat` | [`StoredChat`](https://botmesh.afaya.com.co/api/core/interfaces/StoredChat) |

#### Returns

`Promise`\<`void`>

#### Implementation of

[`IStore`](https://botmesh.afaya.com.co/api/core/interfaces/IStore).[`upsertChat`](https://botmesh.afaya.com.co/api/core/interfaces/IStore#upsertchat)

***

### getChat()

```ts theme={null}
getChat(chatId): Promise<StoredChat | undefined>;
```

Defined in: [store/MemoryStore.ts:228](https://github.com/andresayac/botmesh/blob/main/packages/core/src/store/MemoryStore.ts#L228)

#### Parameters

| Parameter | Type |
| - | - |
| `chatId` | `string` |

#### Returns

`Promise`\<[`StoredChat`](https://botmesh.afaya.com.co/api/core/interfaces/StoredChat) | `undefined`>

#### Implementation of

[`IStore`](https://botmesh.afaya.com.co/api/core/interfaces/IStore).[`getChat`](https://botmesh.afaya.com.co/api/core/interfaces/IStore#getchat)

***

### listChats()

```ts theme={null}
listChats(): Promise<StoredChat[]>;
```

Defined in: [store/MemoryStore.ts:232](https://github.com/andresayac/botmesh/blob/main/packages/core/src/store/MemoryStore.ts#L232)

#### Returns

`Promise`\<[`StoredChat`](https://botmesh.afaya.com.co/api/core/interfaces/StoredChat)\[]>

#### Implementation of

[`IStore`](https://botmesh.afaya.com.co/api/core/interfaces/IStore).[`listChats`](https://botmesh.afaya.com.co/api/core/interfaces/IStore#listchats)

***

### deleteChat()

```ts theme={null}
deleteChat(chatId): Promise<void>;
```

Defined in: [store/MemoryStore.ts:236](https://github.com/andresayac/botmesh/blob/main/packages/core/src/store/MemoryStore.ts#L236)

#### Parameters

| Parameter | Type |
| - | - |
| `chatId` | `string` |

#### Returns

`Promise`\<`void`>

#### Implementation of

[`IStore`](https://botmesh.afaya.com.co/api/core/interfaces/IStore).[`deleteChat`](https://botmesh.afaya.com.co/api/core/interfaces/IStore#deletechat)

***

### upsertContact()

```ts theme={null}
upsertContact(contact): Promise<void>;
```

Defined in: [store/MemoryStore.ts:243](https://github.com/andresayac/botmesh/blob/main/packages/core/src/store/MemoryStore.ts#L243)

Shallow-merge `contact` onto the stored record; same `undefined` = unset rule as [upsertChat](https://botmesh.afaya.com.co/api/core/interfaces/IStore.md#upsertchat).

#### Parameters

| Parameter | Type |
| - | - |
| `contact` | [`StoredContact`](https://botmesh.afaya.com.co/api/core/interfaces/StoredContact) |

#### Returns

`Promise`\<`void`>

#### Implementation of

[`IStore`](https://botmesh.afaya.com.co/api/core/interfaces/IStore).[`upsertContact`](https://botmesh.afaya.com.co/api/core/interfaces/IStore#upsertcontact)

***

### getContact()

```ts theme={null}
getContact(userId): Promise<StoredContact | undefined>;
```

Defined in: [store/MemoryStore.ts:248](https://github.com/andresayac/botmesh/blob/main/packages/core/src/store/MemoryStore.ts#L248)

#### Parameters

| Parameter | Type |
| - | - |
| `userId` | `string` |

#### Returns

`Promise`\<[`StoredContact`](https://botmesh.afaya.com.co/api/core/interfaces/StoredContact) | `undefined`>

#### Implementation of

[`IStore`](https://botmesh.afaya.com.co/api/core/interfaces/IStore).[`getContact`](https://botmesh.afaya.com.co/api/core/interfaces/IStore#getcontact)

***

### listContacts()

```ts theme={null}
listContacts(): Promise<StoredContact[]>;
```

Defined in: [store/MemoryStore.ts:252](https://github.com/andresayac/botmesh/blob/main/packages/core/src/store/MemoryStore.ts#L252)

#### Returns

`Promise`\<[`StoredContact`](https://botmesh.afaya.com.co/api/core/interfaces/StoredContact)\[]>

#### Implementation of

[`IStore`](https://botmesh.afaya.com.co/api/core/interfaces/IStore).[`listContacts`](https://botmesh.afaya.com.co/api/core/interfaces/IStore#listcontacts)

***

### deleteContact()

```ts theme={null}
deleteContact(userId): Promise<void>;
```

Defined in: [store/MemoryStore.ts:256](https://github.com/andresayac/botmesh/blob/main/packages/core/src/store/MemoryStore.ts#L256)

#### Parameters

| Parameter | Type |
| - | - |
| `userId` | `string` |

#### Returns

`Promise`\<`void`>

#### Implementation of

[`IStore`](https://botmesh.afaya.com.co/api/core/interfaces/IStore).[`deleteContact`](https://botmesh.afaya.com.co/api/core/interfaces/IStore#deletecontact)

***

### getCursor()

```ts theme={null}
getCursor(key): Promise<string | undefined>;
```

Defined in: [store/MemoryStore.ts:262](https://github.com/andresayac/botmesh/blob/main/packages/core/src/store/MemoryStore.ts#L262)

Delivery cursor for long-poll/webhook transports (Telegram update\_id).

#### Parameters

| Parameter | Type |
| - | - |
| `key` | `string` |

#### Returns

`Promise`\<`string` | `undefined`>

#### Implementation of

[`IStore`](https://botmesh.afaya.com.co/api/core/interfaces/IStore).[`getCursor`](https://botmesh.afaya.com.co/api/core/interfaces/IStore#getcursor)

***

### setCursor()

```ts theme={null}
setCursor(key, value): Promise<void>;
```

Defined in: [store/MemoryStore.ts:266](https://github.com/andresayac/botmesh/blob/main/packages/core/src/store/MemoryStore.ts#L266)

#### Parameters

| Parameter | Type |
| - | - |
| `key` | `string` |
| `value` | `string` |

#### Returns

`Promise`\<`void`>

#### Implementation of

[`IStore`](https://botmesh.afaya.com.co/api/core/interfaces/IStore).[`setCursor`](https://botmesh.afaya.com.co/api/core/interfaces/IStore#setcursor)

***

### getCheckpoint()

```ts theme={null}
getCheckpoint(key): Promise<FlowCheckpoint | undefined>;
```

Defined in: [store/MemoryStore.ts:272](https://github.com/andresayac/botmesh/blob/main/packages/core/src/store/MemoryStore.ts#L272)

#### Parameters

| Parameter | Type |
| - | - |
| `key` | `string` |

#### Returns

`Promise`\<[`FlowCheckpoint`](https://botmesh.afaya.com.co/api/core/interfaces/FlowCheckpoint) | `undefined`>

#### Implementation of

[`IStore`](https://botmesh.afaya.com.co/api/core/interfaces/IStore).[`getCheckpoint`](https://botmesh.afaya.com.co/api/core/interfaces/IStore#getcheckpoint)

***

### setCheckpoint()

```ts theme={null}
setCheckpoint(key, cp): Promise<void>;
```

Defined in: [store/MemoryStore.ts:276](https://github.com/andresayac/botmesh/blob/main/packages/core/src/store/MemoryStore.ts#L276)

#### Parameters

| Parameter | Type |
| - | - |
| `key` | `string` |
| `cp` | [`FlowCheckpoint`](https://botmesh.afaya.com.co/api/core/interfaces/FlowCheckpoint) |

#### Returns

`Promise`\<`void`>

#### Implementation of

[`IStore`](https://botmesh.afaya.com.co/api/core/interfaces/IStore).[`setCheckpoint`](https://botmesh.afaya.com.co/api/core/interfaces/IStore#setcheckpoint)

***

### clearCheckpoint()

```ts theme={null}
clearCheckpoint(key): Promise<void>;
```

Defined in: [store/MemoryStore.ts:280](https://github.com/andresayac/botmesh/blob/main/packages/core/src/store/MemoryStore.ts#L280)

#### Parameters

| Parameter | Type |
| - | - |
| `key` | `string` |

#### Returns

`Promise`\<`void`>

#### Implementation of

[`IStore`](https://botmesh.afaya.com.co/api/core/interfaces/IStore).[`clearCheckpoint`](https://botmesh.afaya.com.co/api/core/interfaces/IStore#clearcheckpoint)

***

### setCheckpointIf()

```ts theme={null}
setCheckpointIf(
   key, 
   cp, 
   expectedRevision
): Promise<boolean>;
```

Defined in: [store/MemoryStore.ts:285](https://github.com/andresayac/botmesh/blob/main/packages/core/src/store/MemoryStore.ts#L285)

Compare-and-set (see `IStore.setCheckpointIf`): one synchronous step, so concurrent callers see one winner.

#### Parameters

| Parameter | Type |
| - | - |
| `key` | `string` |
| `cp` | [`FlowCheckpoint`](https://botmesh.afaya.com.co/api/core/interfaces/FlowCheckpoint) |
| `expectedRevision` | `number` |

#### Returns

`Promise`\<`boolean`>

#### Implementation of

[`IStore`](https://botmesh.afaya.com.co/api/core/interfaces/IStore).[`setCheckpointIf`](https://botmesh.afaya.com.co/api/core/interfaces/IStore#setcheckpointif)

***

### clearCheckpointIf()

```ts theme={null}
clearCheckpointIf(key, expectedRevision): Promise<boolean>;
```

Defined in: [store/MemoryStore.ts:292](https://github.com/andresayac/botmesh/blob/main/packages/core/src/store/MemoryStore.ts#L292)

Conditional delete: `true` when the row is absent, or its revision (`0` without one)
matched `expectedRevision` and it was deleted; `false` when a different revision is
stored (the row is left untouched). Same atomicity rule as [setCheckpointIf](https://botmesh.afaya.com.co/api/core/interfaces/IStore.md#setcheckpointif).

#### Parameters

| Parameter | Type |
| - | - |
| `key` | `string` |
| `expectedRevision` | `number` |

#### Returns

`Promise`\<`boolean`>

#### Implementation of

[`IStore`](https://botmesh.afaya.com.co/api/core/interfaces/IStore).[`clearCheckpointIf`](https://botmesh.afaya.com.co/api/core/interfaces/IStore#clearcheckpointif)

***

### listCheckpoints()

```ts theme={null}
listCheckpoints(): Promise<[string, FlowCheckpoint][]>;
```

Defined in: [store/MemoryStore.ts:301](https://github.com/andresayac/botmesh/blob/main/packages/core/src/store/MemoryStore.ts#L301)

#### Returns

`Promise`\<\[`string`, [`FlowCheckpoint`](https://botmesh.afaya.com.co/api/core/interfaces/FlowCheckpoint)]\[]>

#### Implementation of

[`IStore`](https://botmesh.afaya.com.co/api/core/interfaces/IStore).[`listCheckpoints`](https://botmesh.afaya.com.co/api/core/interfaces/IStore#listcheckpoints)

***

### getConversation()

```ts theme={null}
getConversation(key): Promise<AIMessage[] | undefined>;
```

Defined in: [store/MemoryStore.ts:307](https://github.com/andresayac/botmesh/blob/main/packages/core/src/store/MemoryStore.ts#L307)

#### Parameters

| Parameter | Type |
| - | - |
| `key` | `string` |

#### Returns

`Promise`\<[`AIMessage`](https://botmesh.afaya.com.co/api/core/interfaces/AIMessage)\[] | `undefined`>

#### Implementation of

[`IStore`](https://botmesh.afaya.com.co/api/core/interfaces/IStore).[`getConversation`](https://botmesh.afaya.com.co/api/core/interfaces/IStore#getconversation)

***

### setConversation()

```ts theme={null}
setConversation(key, messages): Promise<void>;
```

Defined in: [store/MemoryStore.ts:312](https://github.com/andresayac/botmesh/blob/main/packages/core/src/store/MemoryStore.ts#L312)

#### Parameters

| Parameter | Type |
| - | - |
| `key` | `string` |
| `messages` | [`AIMessage`](https://botmesh.afaya.com.co/api/core/interfaces/AIMessage)\[] |

#### Returns

`Promise`\<`void`>

#### Implementation of

[`IStore`](https://botmesh.afaya.com.co/api/core/interfaces/IStore).[`setConversation`](https://botmesh.afaya.com.co/api/core/interfaces/IStore#setconversation)

***

### clearConversation()

```ts theme={null}
clearConversation(key): Promise<void>;
```

Defined in: [store/MemoryStore.ts:316](https://github.com/andresayac/botmesh/blob/main/packages/core/src/store/MemoryStore.ts#L316)

#### Parameters

| Parameter | Type |
| - | - |
| `key` | `string` |

#### Returns

`Promise`\<`void`>

#### Implementation of

[`IStore`](https://botmesh.afaya.com.co/api/core/interfaces/IStore).[`clearConversation`](https://botmesh.afaya.com.co/api/core/interfaces/IStore#clearconversation)

***

### getUserData()

```ts theme={null}
getUserData(
   userId, 
   namespace, 
   key
): Promise<UserDataEntry | undefined>;
```

Defined in: [store/MemoryStore.ts:322](https://github.com/andresayac/botmesh/blob/main/packages/core/src/store/MemoryStore.ts#L322)

Read one entry. MUST return `undefined` when the entry is missing OR its
`expiresAt` is `<= Date.now()` (lazy expiry — expired rows may still exist
physically until purged).

#### Parameters

| Parameter | Type |
| - | - |
| `userId` | `string` |
| `namespace` | `string` |
| `key` | `string` |

#### Returns

`Promise`\<[`UserDataEntry`](https://botmesh.afaya.com.co/api/core/spi/interfaces/UserDataEntry) | `undefined`>

#### Implementation of

[`IStore`](https://botmesh.afaya.com.co/api/core/interfaces/IStore).[`getUserData`](https://botmesh.afaya.com.co/api/core/interfaces/IStore#getuserdata)

***

### setUserData()

```ts theme={null}
setUserData(entry): Promise<void>;
```

Defined in: [store/MemoryStore.ts:326](https://github.com/andresayac/botmesh/blob/main/packages/core/src/store/MemoryStore.ts#L326)

Upsert one entry as a SINGLE atomic write that replaces both `value` and
`expiresAt` (no read-modify-write inside the store). An entry whose
`expiresAt` is already in the past may be dropped instead of written.

#### Parameters

| Parameter | Type |
| - | - |
| `entry` | [`UserDataEntry`](https://botmesh.afaya.com.co/api/core/spi/interfaces/UserDataEntry) |

#### Returns

`Promise`\<`void`>

#### Implementation of

[`IStore`](https://botmesh.afaya.com.co/api/core/interfaces/IStore).[`setUserData`](https://botmesh.afaya.com.co/api/core/interfaces/IStore#setuserdata)

***

### deleteUserData()

```ts theme={null}
deleteUserData(
   userId, 
   namespace?, 
   key?
): Promise<void>;
```

Defined in: [store/MemoryStore.ts:330](https://github.com/andresayac/botmesh/blob/main/packages/core/src/store/MemoryStore.ts#L330)

Delete one key (`namespace` + `key`), one whole namespace (`key` omitted)
or every namespace of the user (`namespace` omitted; `key` is then ignored).
Deleting missing data is a no-op.

#### Parameters

| Parameter | Type |
| - | - |
| `userId` | `string` |
| `namespace?` | `string` |
| `key?` | `string` |

#### Returns

`Promise`\<`void`>

#### Implementation of

[`IStore`](https://botmesh.afaya.com.co/api/core/interfaces/IStore).[`deleteUserData`](https://botmesh.afaya.com.co/api/core/interfaces/IStore#deleteuserdata)

***

### listUserData()

```ts theme={null}
listUserData(userId, namespace?): Promise<UserDataEntry[]>;
```

Defined in: [store/MemoryStore.ts:334](https://github.com/andresayac/botmesh/blob/main/packages/core/src/store/MemoryStore.ts#L334)

Live (non-expired) entries of a user — all namespaces, or just `namespace`. Order unspecified.

#### Parameters

| Parameter | Type |
| - | - |
| `userId` | `string` |
| `namespace?` | `string` |

#### Returns

`Promise`\<[`UserDataEntry`](https://botmesh.afaya.com.co/api/core/spi/interfaces/UserDataEntry)\[]>

#### Implementation of

[`IStore`](https://botmesh.afaya.com.co/api/core/interfaces/IStore).[`listUserData`](https://botmesh.afaya.com.co/api/core/interfaces/IStore#listuserdata)

***

### purgeExpiredUserData()

```ts theme={null}
purgeExpiredUserData(now?): Promise<number>;
```

Defined in: [store/MemoryStore.ts:338](https://github.com/andresayac/botmesh/blob/main/packages/core/src/store/MemoryStore.ts#L338)

Physically remove entries whose `expiresAt <= now` (default `Date.now()`)
across the whole store; resolves with how many were removed. Stores with
native expiry (Redis) resolve `0`.

#### Parameters

| Parameter | Type |
| - | - |
| `now?` | `number` |

#### Returns

`Promise`\<`number`>

#### Implementation of

[`IStore`](https://botmesh.afaya.com.co/api/core/interfaces/IStore).[`purgeExpiredUserData`](https://botmesh.afaya.com.co/api/core/interfaces/IStore#purgeexpireduserdata)

## Accessors

### durable

#### Get Signature

```ts theme={null}
get durable(): boolean;
```

Defined in: [store/MemoryStore.ts:110](https://github.com/andresayac/botmesh/blob/main/packages/core/src/store/MemoryStore.ts#L110)

`true` when this store mirrors itself to a JSON `file`, so its data (including
`persistent()` flow checkpoints) survives a process restart. `false` = RAM only.

##### Returns

`boolean`


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.