๐Ÿ“ฆ Easy Storage Library for ZeppOS

EasyStorage 2.0 Update: AsyncStorage & EasyStorageAsync added!

Note

This solution should be useful for mini apps that have menus with dynamic updates (quick ON/OFF switch), games and apps that have to write / read data during runtime, as well as advanced watch face developers that rely on storage operations.

How to use new AsyncStorage & EasyStorageAsync (bare minimum code samples)

Tip

you can download working example shown above from the example-apps/showcase folder

Install EasyStorage

npm i @silver-zepp/easy-storage

1. AsyncStorage

Tip

AsyncStorage example page async-storage.js

import { AsyncStorage } from "@silver-zepp/easy-storage";

// write
AsyncStorage.WriteJson('config.json', { theme: 'dark' }, (err, ok) => {
  if (ok) console.log('config saved!');
});

// read
AsyncStorage.ReadJson('config.json', (err, config) => {
  if (!err) {
    console.log('theme:', config.theme);
    // apply your theme to UI widget
    // ...
  }
});

// === ADDITIONAL HELPERS === //

// example: throttle file operations to prevent overload and RAM runout
if (!AsyncStorage.IsBusy()) {
  AsyncStorage.WriteJson('data.json', big_obj, callback);
} else {
  console.log('storage busy, try again later');
}

// immediately save all pending writes synchronously (if any)
// making sure no data is lost on app crash or a force-close during write times
Page({
  // ...
  onDestroy() {
    AsyncStorage.SaveAndQuit();
  },
})

2. EasyStorageAsync

Tip

EasyStorageAsync example page easy-storage-async.js

For the most part API remains the same as EasyStorage (sync) and works as a drop in replacement, reusing its old storage database file easy_storage.js but there are a few areas you have to pay attention to observing the example.

// basic interaction remains the same
import { EasyStorageAsync } from "@silver-zepp/easy-storage";
const storage = new EasyStorageAsync();

Page({
  onInit() {
    // ===============================
    // Manual Storage Synchronization:
    // ===============================
    // by default EasyStorageAsync loads its database file in a lazy-load async manner to avoid UI lag
    // without manual syncronization, your keys might not be available at access time
    // this is (optional) and should be used only when data concistency is necessary right away.
    // the sync, if necessary, has to be done only once. after that, the whole DB remains in RAM
    if (!storage.isReady()) storage.synchronize();
  },
  build() {
    storage.setKey("name", "John");       // set the key
    const name = storage.getKey("name");  // get the key
  },
  onDestroy() {
    // immediately save all pending writes synchronously (if any)
    // making sure no data is lost on app crash or a force-close during write times
    storage.saveAndQuit();
  }
})

// === ADDITIONAL ASYNC BENEFITS === //

// now that the writes are asynchronous you can rely on write callback when the value finished storing
storage.setKey('theme', 'dark', (err, ok) => {
  if (ok) console.log('saved without blocking UI');
});

The rest of the API remains mostly the same and you can read about it below

3. EasyStorage [Read API๐Ÿ”—]

import { EasyStorage } from "@silver-zepp/easy-storage";
const storage = new EasyStorage();

storage.setKey("name", "John Doe");
console.log(storage.getKey("user")); // "John Doe"

4. EasyTSDB [Read API๐Ÿ”—]

import { EasyTSDB } from "@silver-zepp/easy-storage";
const db = new EasyTSDB();

// write some data points
db.writePoint('temperature', 22.5); // if not provided -> use the current timestamp
db.writePoint('humidity', 55, Date.now() - 60 * 60 * 1000); // 1 hour ago

// query the average temperature over the last 2 hours
const start_time = Date.now() - 2 * 60 * 60 * 1000; // 2 hours ago
const end_time = Date.now();
const avg_temp = db.query(start_time, end_time, 'average');

console.log(`Average temperature over the last 2 hours: ${avg_temp}`);

5. EasyFlashStorage [Read API๐Ÿ”—]

import { EasyFlashStorage } from "@silver-zepp/easy-storage";
const flash = new EasyFlashStorage();

flash.setKey("config", { theme: "dark", notifications: true, ... });
console.log(flash.getKey("config"));

6. EasyTempStorage [Read API๐Ÿ”—]

import { EasyTempStorage } from "@silver-zepp/easy-storage";
const temp = new EasyTempStorage();

temp.setKey("session", { token: "abc123" });
console.log(temp.getKey("session"));

7. Storage [Read API๐Ÿ”—]

import { Storage } from "@silver-zepp/easy-storage";

Storage.WriteFile("log.txt", "log entry example");
console.log(Storage.ReadFile("log.txt"));

๐Ÿ“ Library Quick API

EasyStorageAsync

AsyncStorage

EasyStorage

EasyTSDB (time-series database)

EasyFlashStorage

EasyTempStorage

Storage

๐Ÿ“ EasyStorage API Reference

1. EasyStorage API๐Ÿ”—

2. EasyTSDB API๐Ÿ”—

3. EasyFlashStorage API๐Ÿ”—

4. EasyTempStorage API๐Ÿ”—

5. Storage API๐Ÿ”—