DEV Community

桃花镇童长老
桃花镇童长老

Posted on

KvUtil, key-value database tool class

Introduction and description of harmony-utils


harmony-utils A HarmonyOS tool library with rich features and extremely easy to use, with the help of many practical tools, is committed to helping developers quickly build Hongmeng applications. Its encapsulated tools cover APP, device, screen, authorization, notification, inter-thread communication, pop-up frames, toast, biometric authentication, user preferences, taking photos, albums, scanning codes, files, logs, exception capture, characters, strings, numbers, collections, dates, random, base64, encryption, decryption, JSON and other functions, which can meet various development needs.

picker_utils It is a sub-store split by harmony-utils, including PickerUtil, PhotoHelper, and ScanUtil.

Download and install

ohpm i @pura/harmony-utils

ohpm i @pura/picker_utils

  //Global initialization method, initialized in the onCreate method of UIAbility AppUtil.init()
  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
    AppUtil.init(this.context);
  }
Enter fullscreen mode Exit fullscreen mode

API methods and usage


put Add key-value pairs of the specified type to the database
KvUtil.put("name", "张三叁");
KvUtil.put("id", 10018);
KvUtil.put("sex", RandomUtil.getRandomBoolean());
KvUtil.put("info", RandomUtil.getRandomUint8Array(8));
Enter fullscreen mode Exit fullscreen mode
get get the value of the specified key
 let kv1 = await KvUtil.get("name");
Enter fullscreen mode Exit fullscreen mode
getString Gets the value of the specified key, string type
 let kv2 = await KvUtil.getString('name');
Enter fullscreen mode Exit fullscreen mode
getNumber Gets the value of the specified key, number type
 let kv3 = await KvUtil.getNumber("id");
Enter fullscreen mode Exit fullscreen mode
getBoolean Gets the value of the specified key, boolean type
 let kv4 = await KvUtil.getBoolean('sex');
Enter fullscreen mode Exit fullscreen mode
getUint8Array Gets the value of the specified key, uint8Array type
 let kv5 = await KvUtil.getUint8Array('info');
Enter fullscreen mode Exit fullscreen mode
delete delete data with specified key value from the database
KvUtil.delete("name");
ToastUtil.showToast('删除缓存成功');
Enter fullscreen mode Exit fullscreen mode
putBatch Insert key-value pairs in SingleKVStore database in batches
let entries: distributedKVStore.Entry[] = [];
for (let i = 0; i < 10; i++) {
  let key = 'batch_test_string_key';
  let entry: distributedKVStore.Entry = {
    key: key + i,
    value: {
      type: distributedKVStore.ValueType.STRING,
      value: 'batch_test_string_value'
    }
  }
  entries.push(entry);
}
KvUtil.putBatch(entries).then(() => {
  ToastUtil.showToast('批量插入成功');
}).catch((err: BusinessError) => {
  ToastUtil.showToast('批量插入异常!');
});
Enter fullscreen mode Exit fullscreen mode
deleteBatch deletes key-value pairs in SingleKVStore database in batches
let keys = ["batch_test_string_key0","batch_test_string_key1","batch_test_string_key2","batch_test_string_key3"];
KvUtil.deleteBatch(keys).then(() => {
  ToastUtil.showToast('批量删除成功');
}).catch((err: BusinessError) => {
  ToastUtil.showToast('批量删除异常!');
});
Enter fullscreen mode Exit fullscreen mode
getEntries Get all key-value pairs that match the specified key prefix
let keyPrefix = "batch_test_string_key";
KvUtil.getEntries(keyPrefix).then((entries: distributedKVStore.Entry[]) => {
  Utils.showSheetText(JSON.stringify(entries, null, 2))
}).catch((err: BusinessError) => {
  ToastUtil.showToast(`异常信息:${err.message}`);
});
Enter fullscreen mode Exit fullscreen mode
backup backup database with a specified name
let backupFile = "BK001";
KvUtil.backup(backupFile, (err: BusinessError) => {
  if (err) {
    console.error(`Failed to backup.code is ${err.code},message is ${err.message} `);
  } else {
    console.info(`Succeeded in backupping data`);
  }
});
Enter fullscreen mode Exit fullscreen mode
deleteBackup deletes backup file according to the specified name
let files = ["BK001", "BK002"];
KvUtil.deleteBackup(files, (err: BusinessError, data: [string, number][]) => {
  if (err) {
    console.error(`Failed to delete Backup.code is ${err.code},message is ${err.message}`);
  } else {
    console.info(`Succeed in deleting Backup.data=${data}`);
  }
});
Enter fullscreen mode Exit fullscreen mode
restore restore database from specified database file
let backupFile = "BK001";
KvUtil.restore(backupFile, (err: BusinessError) => {
  if (err) {
    console.error(`Failed to backup.code is ${err.code},message is ${err.message} `);
  } else {
    console.info(`Succeeded in backupping data`);
  }
});
Enter fullscreen mode Exit fullscreen mode
onDataChange Subscribe to data change notifications of specified types
  private callBack: Callback<distributedKVStore.ChangeNotification> = (change) => {
    let pStr = JSON.stringify(change, null, 2);
    LogUtil.info(pStr);
  }

  KvUtil.onDataChange(distributedKVStore.SubscribeType.SUBSCRIBE_TYPE_ALL, callBack);
Enter fullscreen mode Exit fullscreen mode
offDataChange Unsubscribe to data change notifications
KvUtil.offDataChange(callBack);
ToastUtil.showToast('取消订阅数据变更通知');
Enter fullscreen mode Exit fullscreen mode

Top comments (0)