|
| 1 | +--- |
| 2 | +title: Persistent Storage |
| 3 | +description: Overview of persistent storage modules for files, flash memory, and key-value pairs. |
| 4 | +--- |
| 5 | + |
| 6 | +The Persistent Storage APIs provide mechanisms for reliable data storage on embedded systems. ECMA-419 defines four complementary storage modules, each optimized for different use cases and access patterns. |
| 7 | + |
| 8 | +## Storage Types |
| 9 | + |
| 10 | +### Files |
| 11 | +The **Files** module provides a hierarchical file system interface for storing, organizing, and retrieving data in a directory structure. It supports: |
| 12 | +- Creating and navigating directories |
| 13 | +- Reading and writing files with various access modes |
| 14 | +- Moving, copying, and deleting files and directories |
| 15 | +- Symbolic links for file system abstraction |
| 16 | +- Synchronous or asynchronous operations |
| 17 | + |
| 18 | +Use the Files module when you need a traditional file system interface with structured organization, similar to operating system file systems. |
| 19 | + |
| 20 | +**Module**: `embedded:storage/files` |
| 21 | +**Documentation**: [Files](/api/storage/files) |
| 22 | + |
| 23 | +### Flash |
| 24 | +The **Flash** module provides low-level access to flash memory partitions for advanced use cases requiring direct hardware control. It supports: |
| 25 | +- Opening flash partitions by name |
| 26 | +- Reading and writing raw data at byte offsets |
| 27 | +- Erasing memory blocks (sectors) |
| 28 | +- Querying partition size and block information |
| 29 | +- Multiple data format support |
| 30 | + |
| 31 | +Use the Flash module when you need direct control over flash memory partitions, such as for boot loaders, raw data storage, or performance-critical applications. |
| 32 | + |
| 33 | +**Module**: `embedded:storage/flash` |
| 34 | +**Documentation**: [Flash](/api/storage/flash) |
| 35 | + |
| 36 | +### Key-Value |
| 37 | +The **Key-Value** module provides simple key-value pair storage organized within named domains. It supports: |
| 38 | +- Reading and writing values by key |
| 39 | +- Deleting key-value pairs |
| 40 | +- Domain organization for logical separation |
| 41 | +- Multiple data format support |
| 42 | +- Transactional operations |
| 43 | + |
| 44 | +Use the Key-Value module when you need simple, fast access to configuration data, application settings, or small cached values without file system overhead. |
| 45 | + |
| 46 | +**Module**: `embedded:storage/key-value` |
| 47 | +**Documentation**: [Key-Value](/api/storage/key-value) |
| 48 | + |
| 49 | +### Update |
| 50 | +The **Update** module provides firmware update functionality, enabling in-place firmware upgrades for flash partitions. It supports: |
| 51 | +- Writing update data in append or random-access modes |
| 52 | +- Progress tracking and cancellation |
| 53 | +- Atomic activation of updates |
| 54 | +- Automatic rollback on failure |
| 55 | + |
| 56 | +Use the Update module to implement over-the-air (OTA) firmware updates, allowing devices to update themselves without external tools. |
| 57 | + |
| 58 | +**Module**: `embedded:update` |
| 59 | +**Documentation**: [Update](/api/storage/update) |
| 60 | + |
| 61 | +## Choosing a Storage Type |
| 62 | + |
| 63 | +| Use Case | Recommended Module | Reason | |
| 64 | +|----------|-------------------|--------| |
| 65 | +| Application files, documents | **Files** | Natural hierarchical organization | |
| 66 | +| Configuration settings | **Key-Value** | Fast, simple access to small data | |
| 67 | +| Raw data, boot code | **Flash** | Direct hardware control | |
| 68 | +| Firmware updates | **Update** | Safe, atomic firmware replacement | |
| 69 | +| Multi-file projects | **Files** | Directory structure and navigation | |
| 70 | +| Cached values | **Key-Value** | Low overhead, quick lookup | |
| 71 | +| Legacy binary formats | **Flash** | Arbitrary byte-level access | |
| 72 | + |
| 73 | +## Getting Started |
| 74 | + |
| 75 | +Each storage module is accessed through the `device.storage` global object: |
| 76 | + |
| 77 | +```js |
| 78 | +import files from "embedded:storage/files"; |
| 79 | +import flash from "embedded:storage/flash"; |
| 80 | +import keyValue from "embedded:storage/key-value"; |
| 81 | +import update from "embedded:update"; |
| 82 | + |
| 83 | +// Access persistent storage modules |
| 84 | +const rootDir = files; |
| 85 | +const partition = await flash.open({ path: "app" }); |
| 86 | +const config = await keyValue.open({ path: "settings" }); |
| 87 | +``` |
| 88 | + |
| 89 | +Refer to each module's documentation page for detailed API reference, parameters, and code examples. |
| 90 | + |
| 91 | +## Specifications |
| 92 | + |
| 93 | +[Persistent Storage (Section 26)](https://419.ecma-international.org/#-26-persistent-storage) |
0 commit comments