# MemoryIndexedKeyValueUnifiedStorage

Defined in: [atomiq-storage-memory-indexed-kv/src/MemoryIndexedKeyValueUnifiedStorage.ts:88](https://github.com/atomiqlabs/atomiq-storage-memory-indexed-kv/blob/e65067d59b4e9456247363a9f25e09e57be9f4c1/src/MemoryIndexedKeyValueUnifiedStorage.ts#L88)

Unified storage wrapper that can be used on top of a simple key-value storage, this should only ever be used for a single-user swap databases (e.g. to be used on the client-side), because:

* in-memory indexes are used (which can get out of hand for large datasets & don't support multi-process access)
* uses a single write queue, meaning even concurrent writes are always processed sequentially

## Extended by

* [`RNAsyncUnifiedStorage`](https://docs.atomiq.exchange/sdk-reference/api/atomiq-storage-rn-async/src/classes/RNAsyncUnifiedStorage.md)

## Implements

* [`IUnifiedStorage`](https://docs.atomiq.exchange/sdk-reference/api/atomiq-sdk/src/interfaces/IUnifiedStorage.md)<[`UnifiedStorageIndexes`](https://docs.atomiq.exchange/sdk-reference/api/atomiq-sdk/src/type-aliases/UnifiedStorageIndexes.md), [`UnifiedStorageCompositeIndexes`](https://docs.atomiq.exchange/sdk-reference/api/atomiq-sdk/src/type-aliases/UnifiedStorageCompositeIndexes.md)>

## Constructors

### Constructor

```
new MemoryIndexedKeyValueUnifiedStorage(storageBackend, options?): MemoryIndexedKeyValueUnifiedStorage;
```

Defined in: [atomiq-storage-memory-indexed-kv/src/MemoryIndexedKeyValueUnifiedStorage.ts:108](https://github.com/atomiqlabs/atomiq-storage-memory-indexed-kv/blob/e65067d59b4e9456247363a9f25e09e57be9f4c1/src/MemoryIndexedKeyValueUnifiedStorage.ts#L108)

Creates a new MemoryIndexedKeyValueUnifiedStorage instance

#### Parameters

| Parameter        | Type                                                                                                                                                                                           | Description                              |
| ---------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------- |
| `storageBackend` | [`IKeyValueStorage`](https://docs.atomiq.exchange/sdk-reference/api/atomiq-storage-memory-indexed-kv/src/interfaces/IKeyValueStorage.md)<`boolean`>                                            | The underlying key-value storage backend |
| `options?`       | [`MemoryIndexedKeyValueUnifiedStorageOptions`](https://docs.atomiq.exchange/sdk-reference/api/atomiq-storage-memory-indexed-kv/src/type-aliases/MemoryIndexedKeyValueUnifiedStorageOptions.md) | Configuration options                    |

#### Returns

`MemoryIndexedKeyValueUnifiedStorage`

## Properties

### compositeIndexes?

```
optional compositeIndexes: UnifiedStorageCompositeIndexes;
```

Defined in: [atomiq-storage-memory-indexed-kv/src/MemoryIndexedKeyValueUnifiedStorage.ts:92](https://github.com/atomiqlabs/atomiq-storage-memory-indexed-kv/blob/e65067d59b4e9456247363a9f25e09e57be9f4c1/src/MemoryIndexedKeyValueUnifiedStorage.ts#L92)

***

### compositeIndexesMaps?

```
optional compositeIndexesMaps: object;
```

Defined in: [atomiq-storage-memory-indexed-kv/src/MemoryIndexedKeyValueUnifiedStorage.ts:99](https://github.com/atomiqlabs/atomiq-storage-memory-indexed-kv/blob/e65067d59b4e9456247363a9f25e09e57be9f4c1/src/MemoryIndexedKeyValueUnifiedStorage.ts#L99)

#### Index Signature

```
[compositeIndexIdentifier: string]: Map<string, Set<string>>
```

***

### indexes?

```
optional indexes: UnifiedStorageIndexes;
```

Defined in: [atomiq-storage-memory-indexed-kv/src/MemoryIndexedKeyValueUnifiedStorage.ts:91](https://github.com/atomiqlabs/atomiq-storage-memory-indexed-kv/blob/e65067d59b4e9456247363a9f25e09e57be9f4c1/src/MemoryIndexedKeyValueUnifiedStorage.ts#L91)

***

### indexesMaps?

```
optional indexesMaps: object;
```

Defined in: [atomiq-storage-memory-indexed-kv/src/MemoryIndexedKeyValueUnifiedStorage.ts:98](https://github.com/atomiqlabs/atomiq-storage-memory-indexed-kv/blob/e65067d59b4e9456247363a9f25e09e57be9f4c1/src/MemoryIndexedKeyValueUnifiedStorage.ts#L98)

#### Index Signature

```
[indexField: string]: Map<any, Set<string>>
```

***

### options

```
options: object;
```

Defined in: [atomiq-storage-memory-indexed-kv/src/MemoryIndexedKeyValueUnifiedStorage.ts:94](https://github.com/atomiqlabs/atomiq-storage-memory-indexed-kv/blob/e65067d59b4e9456247363a9f25e09e57be9f4c1/src/MemoryIndexedKeyValueUnifiedStorage.ts#L94)

#### allowQueryWithoutIndexes

```
allowQueryWithoutIndexes: boolean;
```

#### maxBatchItems

```
maxBatchItems: number;
```

***

### storageBackend

```
storageBackend: IKeyValueStorage<boolean>;
```

Defined in: [atomiq-storage-memory-indexed-kv/src/MemoryIndexedKeyValueUnifiedStorage.ts:90](https://github.com/atomiqlabs/atomiq-storage-memory-indexed-kv/blob/e65067d59b4e9456247363a9f25e09e57be9f4c1/src/MemoryIndexedKeyValueUnifiedStorage.ts#L90)

***

### writeQueue

```
writeQueue: PromiseQueue;
```

Defined in: [atomiq-storage-memory-indexed-kv/src/MemoryIndexedKeyValueUnifiedStorage.ts:101](https://github.com/atomiqlabs/atomiq-storage-memory-indexed-kv/blob/e65067d59b4e9456247363a9f25e09e57be9f4c1/src/MemoryIndexedKeyValueUnifiedStorage.ts#L101)

## Methods

### init()

```
init(indexes, compositeIndexes): Promise<void>;
```

Defined in: [atomiq-storage-memory-indexed-kv/src/MemoryIndexedKeyValueUnifiedStorage.ts:319](https://github.com/atomiqlabs/atomiq-storage-memory-indexed-kv/blob/e65067d59b4e9456247363a9f25e09e57be9f4c1/src/MemoryIndexedKeyValueUnifiedStorage.ts#L319)

Initializes the storage with given indexes and composite indexes

#### Parameters

| Parameter          | Type                                                                                                                                             | Description |
| ------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------ | ----------- |
| `indexes`          | [`UnifiedStorageIndexes`](https://docs.atomiq.exchange/sdk-reference/api/atomiq-sdk/src/type-aliases/UnifiedStorageIndexes.md)                   |             |
| `compositeIndexes` | [`UnifiedStorageCompositeIndexes`](https://docs.atomiq.exchange/sdk-reference/api/atomiq-sdk/src/type-aliases/UnifiedStorageCompositeIndexes.md) |             |

#### Returns

`Promise`<`void`>

#### Implementation of

[`IUnifiedStorage`](https://docs.atomiq.exchange/sdk-reference/api/atomiq-sdk/src/interfaces/IUnifiedStorage.md).[`init`](https://docs.atomiq.exchange/sdk-reference/api/atomiq-sdk/src/interfaces/IUnifiedStorage.md#init)

***

### query()

```
query(params): Promise<any[]>;
```

Defined in: [atomiq-storage-memory-indexed-kv/src/MemoryIndexedKeyValueUnifiedStorage.ts:362](https://github.com/atomiqlabs/atomiq-storage-memory-indexed-kv/blob/e65067d59b4e9456247363a9f25e09e57be9f4c1/src/MemoryIndexedKeyValueUnifiedStorage.ts#L362)

Params are specified in the following way:

* \[\[condition1, condition2]] - returns all rows where condition1 AND condition2 is met
* \[\[condition1], \[condition2]] - returns all rows where condition1 OR condition2 is met
* \[\[condition1, condition2], \[condition3]] - returns all rows where (condition1 AND condition2) OR condition3 is met

You can also add an optional `_meta` field in the returned unified storage object which gets attached to that returned object and will be present for subsequent saves and removal of this object, if you specify the `_meta` field here, you need to explicitly handle it in the all the saving and remove functions and not simply serialize it into the storage

#### Parameters

| Parameter | Type                                                                                                             | Description |
| --------- | ---------------------------------------------------------------------------------------------------------------- | ----------- |
| `params`  | [`QueryParams`](https://docs.atomiq.exchange/sdk-reference/api/atomiq-sdk/src/type-aliases/QueryParams.md)\[]\[] |             |

#### Returns

`Promise`<`any`\[]>

#### Implementation of

[`IUnifiedStorage`](https://docs.atomiq.exchange/sdk-reference/api/atomiq-sdk/src/interfaces/IUnifiedStorage.md).[`query`](https://docs.atomiq.exchange/sdk-reference/api/atomiq-sdk/src/interfaces/IUnifiedStorage.md#query)

***

### querySingle()

```
querySingle(params): Promise<any[]>;
```

Defined in: [atomiq-storage-memory-indexed-kv/src/MemoryIndexedKeyValueUnifiedStorage.ts:380](https://github.com/atomiqlabs/atomiq-storage-memory-indexed-kv/blob/e65067d59b4e9456247363a9f25e09e57be9f4c1/src/MemoryIndexedKeyValueUnifiedStorage.ts#L380)

Queries storage with a single set of AND conditions

#### Parameters

| Parameter | Type                                                                                                          | Description                              |
| --------- | ------------------------------------------------------------------------------------------------------------- | ---------------------------------------- |
| `params`  | [`QueryParams`](https://docs.atomiq.exchange/sdk-reference/api/atomiq-sdk/src/type-aliases/QueryParams.md)\[] | Array of conditions that must all be met |

#### Returns

`Promise`<`any`\[]>

Array of matching objects

***

### remove()

```
remove(value): Promise<void>;
```

Defined in: [atomiq-storage-memory-indexed-kv/src/MemoryIndexedKeyValueUnifiedStorage.ts:557](https://github.com/atomiqlabs/atomiq-storage-memory-indexed-kv/blob/e65067d59b4e9456247363a9f25e09e57be9f4c1/src/MemoryIndexedKeyValueUnifiedStorage.ts#L557)

Removes an object from storage

If the object contains a `_meta` field, this will be also present in the to-be-removed value, to mutate the `_meta` field of the object that is saved, you can mutate the `_meta` field directly on the passed value, which then gets reflected automatically in the existing object. The mutated `_meta` field is copied even if the function throws, hence the implementations must be careful with setting the `_meta` field on the still in-flight requests that might fail.

#### Parameters

| Parameter | Type  | Description                                 |
| --------- | ----- | ------------------------------------------- |
| `value`   | `any` | Object to remove (must have an id property) |

#### Returns

`Promise`<`void`>

#### Implementation of

[`IUnifiedStorage`](https://docs.atomiq.exchange/sdk-reference/api/atomiq-sdk/src/interfaces/IUnifiedStorage.md).[`remove`](https://docs.atomiq.exchange/sdk-reference/api/atomiq-sdk/src/interfaces/IUnifiedStorage.md#remove)

***

### removeAll()

```
removeAll(_values): Promise<void>;
```

Defined in: [atomiq-storage-memory-indexed-kv/src/MemoryIndexedKeyValueUnifiedStorage.ts:578](https://github.com/atomiqlabs/atomiq-storage-memory-indexed-kv/blob/e65067d59b4e9456247363a9f25e09e57be9f4c1/src/MemoryIndexedKeyValueUnifiedStorage.ts#L578)

Removes multiple objects from storage in a batch operation

If the objects contain a `_meta` field, this will be also present in the to-be-removed values, to mutate the `_meta` field of the objects that are saved, you can mutate the `_meta` field directly on the passed values, which then gets reflected automatically in the existing objects. The mutated `_meta` field is copied even if the function throws, hence the implementations must be careful with setting the `_meta` field on the still in-flight requests that might fail.

#### Parameters

| Parameter | Type     | Description                |
| --------- | -------- | -------------------------- |
| `_values` | `any`\[] | Array of objects to remove |

#### Returns

`Promise`<`void`>

#### Implementation of

[`IUnifiedStorage`](https://docs.atomiq.exchange/sdk-reference/api/atomiq-sdk/src/interfaces/IUnifiedStorage.md).[`removeAll`](https://docs.atomiq.exchange/sdk-reference/api/atomiq-sdk/src/interfaces/IUnifiedStorage.md#removeall)

***

### save()

```
save(value): Promise<void>;
```

Defined in: [atomiq-storage-memory-indexed-kv/src/MemoryIndexedKeyValueUnifiedStorage.ts:472](https://github.com/atomiqlabs/atomiq-storage-memory-indexed-kv/blob/e65067d59b4e9456247363a9f25e09e57be9f4c1/src/MemoryIndexedKeyValueUnifiedStorage.ts#L472)

Saves an object to storage, updating indexes as needed

If the object contains a `_meta` field, this will be also present in the to-be-saved value, to mutate the `_meta` field of the object that is saved, you can mutate the `_meta` field directly on the passed value, which then gets reflected automatically in the existing object. The mutated `_meta` field is copied even if the function throws, hence the implementations must be careful with setting the `_meta` field on the still in-flight requests that might fail.

#### Parameters

| Parameter | Type  | Description                               |
| --------- | ----- | ----------------------------------------- |
| `value`   | `any` | Object to save (must have an id property) |

#### Returns

`Promise`<`void`>

#### Implementation of

[`IUnifiedStorage`](https://docs.atomiq.exchange/sdk-reference/api/atomiq-sdk/src/interfaces/IUnifiedStorage.md).[`save`](https://docs.atomiq.exchange/sdk-reference/api/atomiq-sdk/src/interfaces/IUnifiedStorage.md#save)

***

### saveAll()

```
saveAll(_values): Promise<void>;
```

Defined in: [atomiq-storage-memory-indexed-kv/src/MemoryIndexedKeyValueUnifiedStorage.ts:496](https://github.com/atomiqlabs/atomiq-storage-memory-indexed-kv/blob/e65067d59b4e9456247363a9f25e09e57be9f4c1/src/MemoryIndexedKeyValueUnifiedStorage.ts#L496)

Saves multiple objects to storage in a batch operation

If the objects contain a `_meta` field, this will be also present in the to-be-saved values, to mutate the `_meta` field of the objects that are saved, you can mutate the `_meta` field directly on the passed values, which then gets reflected automatically in the existing objects. The mutated `_meta` field is copied even if the function throws, hence the implementations must be careful with setting the `_meta` field on the still in-flight requests that might fail.

#### Parameters

| Parameter | Type     | Description              |
| --------- | -------- | ------------------------ |
| `_values` | `any`\[] | Array of objects to save |

#### Returns

`Promise`<`void`>

#### Implementation of

[`IUnifiedStorage`](https://docs.atomiq.exchange/sdk-reference/api/atomiq-sdk/src/interfaces/IUnifiedStorage.md).[`saveAll`](https://docs.atomiq.exchange/sdk-reference/api/atomiq-sdk/src/interfaces/IUnifiedStorage.md#saveall)

***

### \_get()

```
protected _get(key): any;
```

Defined in: [atomiq-storage-memory-indexed-kv/src/MemoryIndexedKeyValueUnifiedStorage.ts:117](https://github.com/atomiqlabs/atomiq-storage-memory-indexed-kv/blob/e65067d59b4e9456247363a9f25e09e57be9f4c1/src/MemoryIndexedKeyValueUnifiedStorage.ts#L117)

#### Parameters

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

#### Returns

`any`

***

### \_getAll()

```
protected _getAll(keys): any[] | Promise<any[]>;
```

Defined in: [atomiq-storage-memory-indexed-kv/src/MemoryIndexedKeyValueUnifiedStorage.ts:126](https://github.com/atomiqlabs/atomiq-storage-memory-indexed-kv/blob/e65067d59b4e9456247363a9f25e09e57be9f4c1/src/MemoryIndexedKeyValueUnifiedStorage.ts#L126)

#### Parameters

| Parameter | Type        |
| --------- | ----------- |
| `keys`    | `string`\[] |

#### Returns

`any`\[] | `Promise`<`any`\[]>

***

### \_getAllSequential()

```
protected _getAllSequential(keys): Promise<any[]>;
```

Defined in: [atomiq-storage-memory-indexed-kv/src/MemoryIndexedKeyValueUnifiedStorage.ts:218](https://github.com/atomiqlabs/atomiq-storage-memory-indexed-kv/blob/e65067d59b4e9456247363a9f25e09e57be9f4c1/src/MemoryIndexedKeyValueUnifiedStorage.ts#L218)

#### Parameters

| Parameter | Type        |
| --------- | ----------- |
| `keys`    | `string`\[] |

#### Returns

`Promise`<`any`\[]>

***

### \_removeAll()

```
protected _removeAll(values): void | Promise<void>;
```

Defined in: [atomiq-storage-memory-indexed-kv/src/MemoryIndexedKeyValueUnifiedStorage.ts:183](https://github.com/atomiqlabs/atomiq-storage-memory-indexed-kv/blob/e65067d59b4e9456247363a9f25e09e57be9f4c1/src/MemoryIndexedKeyValueUnifiedStorage.ts#L183)

#### Parameters

| Parameter | Type        |
| --------- | ----------- |
| `values`  | `object`\[] |

#### Returns

`void` | `Promise`<`void`>

***

### \_removeIndex()

```
protected _removeIndex(

   indexMap, 

   indexValue, 

   obj): void;
```

Defined in: [atomiq-storage-memory-indexed-kv/src/MemoryIndexedKeyValueUnifiedStorage.ts:241](https://github.com/atomiqlabs/atomiq-storage-memory-indexed-kv/blob/e65067d59b4e9456247363a9f25e09e57be9f4c1/src/MemoryIndexedKeyValueUnifiedStorage.ts#L241)

#### Parameters

| Parameter    | Type                          |
| ------------ | ----------------------------- |
| `indexMap`   | `Map`<`any`, `Set`<`string`>> |
| `indexValue` | `any`                         |
| `obj`        | `any`                         |

#### Returns

`void`

***

### \_removeObjectIndexes()

```
protected _removeObjectIndexes(obj): void;
```

Defined in: [atomiq-storage-memory-indexed-kv/src/MemoryIndexedKeyValueUnifiedStorage.ts:273](https://github.com/atomiqlabs/atomiq-storage-memory-indexed-kv/blob/e65067d59b4e9456247363a9f25e09e57be9f4c1/src/MemoryIndexedKeyValueUnifiedStorage.ts#L273)

#### Parameters

| Parameter | Type  |
| --------- | ----- |
| `obj`     | `any` |

#### Returns

`void`

***

### \_saveIndex()

```
protected _saveIndex(

   indexMap, 

   indexValue, 

   obj): void;
```

Defined in: [atomiq-storage-memory-indexed-kv/src/MemoryIndexedKeyValueUnifiedStorage.ts:235](https://github.com/atomiqlabs/atomiq-storage-memory-indexed-kv/blob/e65067d59b4e9456247363a9f25e09e57be9f4c1/src/MemoryIndexedKeyValueUnifiedStorage.ts#L235)

#### Parameters

| Parameter    | Type                          |
| ------------ | ----------------------------- |
| `indexMap`   | `Map`<`any`, `Set`<`string`>> |
| `indexValue` | `any`                         |
| `obj`        | `any`                         |

#### Returns

`void`

***

### \_saveObjectIndexes()

```
protected _saveObjectIndexes(obj): void;
```

Defined in: [atomiq-storage-memory-indexed-kv/src/MemoryIndexedKeyValueUnifiedStorage.ts:254](https://github.com/atomiqlabs/atomiq-storage-memory-indexed-kv/blob/e65067d59b4e9456247363a9f25e09e57be9f4c1/src/MemoryIndexedKeyValueUnifiedStorage.ts#L254)

#### Parameters

| Parameter | Type  |
| --------- | ----- |
| `obj`     | `any` |

#### Returns

`void`

***

### \_set()

```
protected _set(key, value): void | Promise<void>;
```

Defined in: [atomiq-storage-memory-indexed-kv/src/MemoryIndexedKeyValueUnifiedStorage.ts:141](https://github.com/atomiqlabs/atomiq-storage-memory-indexed-kv/blob/e65067d59b4e9456247363a9f25e09e57be9f4c1/src/MemoryIndexedKeyValueUnifiedStorage.ts#L141)

#### Parameters

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

#### Returns

`void` | `Promise`<`void`>

***

### \_setAll()

```
protected _setAll(values): void | Promise<void>;
```

Defined in: [atomiq-storage-memory-indexed-kv/src/MemoryIndexedKeyValueUnifiedStorage.ts:145](https://github.com/atomiqlabs/atomiq-storage-memory-indexed-kv/blob/e65067d59b4e9456247363a9f25e09e57be9f4c1/src/MemoryIndexedKeyValueUnifiedStorage.ts#L145)

#### Parameters

| Parameter | Type        |
| --------- | ----------- |
| `values`  | `object`\[] |

#### Returns

`void` | `Promise`<`void`>

***

### \_updateIndex()

```
protected _updateIndex(

   indexMap, 

   indexOldValue, 

   indexNewValue, 

   obj): void;
```

Defined in: [atomiq-storage-memory-indexed-kv/src/MemoryIndexedKeyValueUnifiedStorage.ts:249](https://github.com/atomiqlabs/atomiq-storage-memory-indexed-kv/blob/e65067d59b4e9456247363a9f25e09e57be9f4c1/src/MemoryIndexedKeyValueUnifiedStorage.ts#L249)

#### Parameters

| Parameter       | Type                          |
| --------------- | ----------------------------- |
| `indexMap`      | `Map`<`any`, `Set`<`string`>> |
| `indexOldValue` | `any`                         |
| `indexNewValue` | `any`                         |
| `obj`           | `any`                         |

#### Returns

`void`

***

### \_updateObjectIndexes()

```
protected _updateObjectIndexes(obj, existingValue): void;
```

Defined in: [atomiq-storage-memory-indexed-kv/src/MemoryIndexedKeyValueUnifiedStorage.ts:292](https://github.com/atomiqlabs/atomiq-storage-memory-indexed-kv/blob/e65067d59b4e9456247363a9f25e09e57be9f4c1/src/MemoryIndexedKeyValueUnifiedStorage.ts#L292)

#### Parameters

| Parameter       | Type  |
| --------------- | ----- |
| `obj`           | `any` |
| `existingValue` | `any` |

#### Returns

`void`
