# Save To File

### Overview

The **Save to File Component** exports stored data to a file, allowing structured storage and retrieval of stored records in CSV or JSON format.

### Usage

This component is used to extract data from storage and save it as a downloadable file, providing flexible export options such as sorting and value flattening.

#### Input Ports

| Port | Description                                                                                    |
| ---- | ---------------------------------------------------------------------------------------------- |
| `in` | Receives parameters for file generation, including storage ID, file type, and sorting options. |

#### Output Ports

| Port  | Description                                                             |
| ----- | ----------------------------------------------------------------------- |
| `out` | Emits the file ID of the saved file and the number of records exported. |

#### Properties

| Property       | Type      | Description                                           |
| -------------- | --------- | ----------------------------------------------------- |
| `storeId`      | `string`  | The ID of the storage space to export data from.      |
| `fileName`     | `string`  | The name of the exported file.                        |
| `sortBy`       | `string`  | Sorts records by `key`, `createdAt`, or `updatedAt`.  |
| `fileType`     | `string`  | File format: `csv` or `json`.                         |
| `flattenValue` | `boolean` | If `true`, flattens nested JSON values before saving. |

#### Processing Logic

1. **Retrieves Stored Data**: Extracts data from the specified `storeId`.
2. **Sorts Data**: Orders records based on the selected `sortBy` property.
3. **Formats Output**:
   * If `fileType` is CSV, converts records to CSV format.
   * If `flattenValue` is enabled, nested JSON structures are flattened.
4. **Saves to File**: Exports the processed data and returns a file ID.

#### Output Data Schema

| Property   | Type     | Description                     |
| ---------- | -------- | ------------------------------- |
| `fileId`   | `string` | The ID of the generated file.   |
| `nRecords` | `number` | The number of records exported. |

### Notes

* **Flexible Export Options**: Supports JSON and CSV formats.
* **Sorting Capabilities**: Allows sorting by key or timestamps.
* **Flattening Support**: Enables structured flattening of nested values.

This component is ideal for workflows requiring data extraction, structured storage, and external file exports for further analysis or backup.
