Storage

The Storage API provides a simple and efficient interface for storing and retrieving persistent data, supporting basic data types and JSON.


Overview

The Storage API is designed to persist simple data types asynchronously to disk. It is suitable for settings, user preferences, or lightweight data that needs to be retained between app launches.

Key Features:

  • Simple interface for storing, retrieving, and removing data.
  • Supports essential data types and JSON.
  • Persistent across app launches.

Supported Data Types

The following data types are supported for storage:

  • string
  • number
  • boolean
  • JSON objects (Record<string, any> or arrays)

API Reference

Methods

Storage.set<T>(key: string, value: T): boolean

Saves a value to persistent storage in the background.

  • Parameters:
    • key: A unique identifier for the data.
    • value: The data to be stored. Must be one of the supported data types.
  • Returns:
    • A boolean indicating whether the operation was successful.

Example:

1const success = Storage.set("username", "JohnDoe")
2console.log("Save successful:", success)

Storage.get<T>(key: string): T | null

Reads a value from persistent storage. If the key does not exist, it returns null.

  • Parameters:
    • key: The unique identifier of the data to retrieve.
  • Returns:
    • The stored value, or null if the key does not exist.

Example:

1const username = Storage.get<string>("username")
2console.log("Stored username:", username)

Storage.remove(key: string): void

Removes an entry from persistent storage.

  • Parameters:
    • key: The unique identifier of the data to remove.

Example:

1Storage.remove("username")
2console.log("Username removed from storage")

Storage.contains(key: string): boolean

Checks if the persistent storage contains the given key.

  • Parameters:
    • key: The unique identifier of the data.
  • Returns:
    • true if the key exists, otherwise false.

Example:

1const hasKey = Storage.contains("username")
2console.log("Username exists in storage:", hasKey)

Common Use Cases

Save and Retrieve a User Setting

1Storage.set("theme", "dark")
2const theme = Storage.get<string>("theme")
3console.log("Current theme:", theme)

Check for a Key and Remove It

1if (Storage.contains("theme")) {
2  Storage.remove("theme")
3  console.log("Theme preference removed")
4}

Store JSON Data

1const userData = { name: "John Doe", age: 30 }
2Storage.set("user", userData)
3
4const storedUser = Storage.get<Record<string, any>>("user")
5console.log("User data:", storedUser)

Best Practices

  1. Key Management: Use consistent and descriptive keys to avoid conflicts and make your code more maintainable.
  2. Validate Data: Always validate the data type and structure when retrieving stored JSON objects.
  3. Error Handling: Check the return value of Storage.set to ensure that the operation was successful.
  4. Optimize Access: Minimize frequent read/write operations to avoid unnecessary overhead.

Full Example

1async function main() {
2  // Save user preferences
3  const saveSuccess = Storage.set("language", "English")
4  if (saveSuccess) {
5    console.log("Language preference saved successfully")
6  }
7
8  // Retrieve user preferences
9  const language = Storage.get<string>("language")
10  console.log("Preferred language:", language)
11
12  // Save JSON data
13  const profile = { name: "Jane Doe", age: 28 }
14  Storage.set("profile", profile)
15
16  // Retrieve JSON data
17  const storedProfile = Storage.get<Record<string, any>>("profile")
18  console.log("Stored profile:", storedProfile)
19
20  // Remove a key
21  if (Storage.contains("language")) {
22    Storage.remove("language")
23    console.log("Language preference removed")
24  }
25}
26
27main()