# SqliteUnifiedStorage

Defined in: [atomiq-storage-sqlite/src/SqliteUnifiedStorage.ts:21](https://github.com/atomiqlabs/atomiq-storage-sqlite/blob/47deacc8ba114160a54297cd8b904bb666d60dd5/src/SqliteUnifiedStorage.ts#L21)

SQLite-based unified storage with indexed query support. Uses native SQLite indexes for efficient queries on swap data.

## Implements

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

## Constructors

### Constructor

```
new SqliteUnifiedStorage(filename): SqliteUnifiedStorage;
```

Defined in: [atomiq-storage-sqlite/src/SqliteUnifiedStorage.ts:34](https://github.com/atomiqlabs/atomiq-storage-sqlite/blob/47deacc8ba114160a54297cd8b904bb666d60dd5/src/SqliteUnifiedStorage.ts#L34)

Creates a new SqliteUnifiedStorage instance

#### Parameters

| Parameter  | Type     | Description                      |
| ---------- | -------- | -------------------------------- |
| `filename` | `string` | Path to the SQLite database file |

#### Returns

`SqliteUnifiedStorage`

## Properties

### db?

```
optional db: Database;
```

Defined in: [atomiq-storage-sqlite/src/SqliteUnifiedStorage.ts:26](https://github.com/atomiqlabs/atomiq-storage-sqlite/blob/47deacc8ba114160a54297cd8b904bb666d60dd5/src/SqliteUnifiedStorage.ts#L26)

SQLite database instance (available after init)

***

### filename

```
readonly filename: string;
```

Defined in: [atomiq-storage-sqlite/src/SqliteUnifiedStorage.ts:24](https://github.com/atomiqlabs/atomiq-storage-sqlite/blob/47deacc8ba114160a54297cd8b904bb666d60dd5/src/SqliteUnifiedStorage.ts#L24)

Path to the SQLite database file

***

### indexedColumns?

```
optional indexedColumns: string[];
```

Defined in: [atomiq-storage-sqlite/src/SqliteUnifiedStorage.ts:28](https://github.com/atomiqlabs/atomiq-storage-sqlite/blob/47deacc8ba114160a54297cd8b904bb666d60dd5/src/SqliteUnifiedStorage.ts#L28)

List of indexed column names

## Methods

### init()

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

Defined in: [atomiq-storage-sqlite/src/SqliteUnifiedStorage.ts:39](https://github.com/atomiqlabs/atomiq-storage-sqlite/blob/47deacc8ba114160a54297cd8b904bb666d60dd5/src/SqliteUnifiedStorage.ts#L39)

Initializes the storage with given indexes and composite indexes

#### Parameters

| Parameter          | Type                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                | Description |
| ------------------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------- |
| `indexes`          | readonly \[{ `key`: `"id"`; `nullable`: `false`; `type`: `"string"`; `unique`: `true`; }, { `key`: `"escrowHash"`; `nullable`: `true`; `type`: `"string"`; `unique`: `true`; }, { `key`: `"type"`; `nullable`: `false`; `type`: `"number"`; `unique`: `false`; }, { `key`: `"initiator"`; `nullable`: `false`; `type`: `"string"`; `unique`: `false`; }, { `key`: `"state"`; `nullable`: `false`; `type`: `"number"`; `unique`: `false`; }, { `key`: `"paymentHash"`; `nullable`: `true`; `type`: `"string"`; `unique`: `false`; }] |             |
| `compositeIndexes` | readonly \[{ `keys`: readonly \[`"initiator"`, `"id"`]; `unique`: `false`; }, { `keys`: readonly \[`"type"`, `"state"`]; `unique`: `false`; }, { `keys`: readonly \[`"type"`, `"paymentHash"`]; `unique`: `false`; }, { `keys`: readonly \[`"type"`, `"initiator"`, `"state"`]; `unique`: `false`; }]                                                                                                                                                                                                                               |             |

#### 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-sqlite/src/SqliteUnifiedStorage.ts:74](https://github.com/atomiqlabs/atomiq-storage-sqlite/blob/47deacc8ba114160a54297cd8b904bb666d60dd5/src/SqliteUnifiedStorage.ts#L74)

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)

***

### remove()

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

Defined in: [atomiq-storage-sqlite/src/SqliteUnifiedStorage.ts:129](https://github.com/atomiqlabs/atomiq-storage-sqlite/blob/47deacc8ba114160a54297cd8b904bb666d60dd5/src/SqliteUnifiedStorage.ts#L129)

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-sqlite/src/SqliteUnifiedStorage.ts:140](https://github.com/atomiqlabs/atomiq-storage-sqlite/blob/47deacc8ba114160a54297cd8b904bb666d60dd5/src/SqliteUnifiedStorage.ts#L140)

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-sqlite/src/SqliteUnifiedStorage.ts:147](https://github.com/atomiqlabs/atomiq-storage-sqlite/blob/47deacc8ba114160a54297cd8b904bb666d60dd5/src/SqliteUnifiedStorage.ts#L147)

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-sqlite/src/SqliteUnifiedStorage.ts:163](https://github.com/atomiqlabs/atomiq-storage-sqlite/blob/47deacc8ba114160a54297cd8b904bb666d60dd5/src/SqliteUnifiedStorage.ts#L163)

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)
