@abibv/kval-dstore 中文文档教程

发布于 4年前 浏览 22 项目主页 更新于 3年前

kval-dstore

一个简单的基于文件的键值数据存储,支持 nodejs 中的基本 CRUD 操作。

它创建一个 json 文件,可以用作键值数据库用于学习目的,甚至用于微应用程序。

它提供了五种不同的 API 来访问键值数据存储。

Initializing data store

new dstore(filename [,sizeLimit, initData]);

  • filename (String) - The file name with its path for the json file.
  • [optional] sizeLimit (String) - Specifies the sizeLimit in MegaByte (default is 1024 MB)
  • [optional] initData (JSON object) - Initial data to be stored at the time of file creation (default is empty json object {})
const dstore = require('@abibv/kval-dstore');

const store = new dstore('./dstore.json');
(or)
const store = new dstore(__dirname + '/dstore.json');

Write the data

store.write(object [,path]);

  • object (JSON object) - Data to be stored.
  • [optional] path (string) - Key path where the data should be placed (should be referred from the root) (default is the root of the json data '/')

write 是一个 不返回任何内容的异步方法

var object = {
  name: 'myname',
  no: 123456789,
  timeSpent: {
    tech: '50%',
    music: '30%',
    sleep: '20%',
  }
};


//Write the data at the root

store.write(object)
  .then(() => { console.log('data written'); })
  .catch((error) => { console.log(error); });


//Write the data at the specified 

store.write(object, '/department/year/students/')
  .then(() => { console.log('data written'); })
  .catch((error) => { console.log(error); });

Read the data

store.read([path]);

  • [optional] path (String) - JSON key path from where the data should be read. (default is root '/')

如果没有指定路径,则读取整个数据。

read 是一个返回读取数据的异步方法

store.read('/department/year/students/')
  .then((data) => { console.log(data); })
  .catch((error) => { console.log(error); });

Update the data

store.update(data, key [,path]);

  • data (any) - data to be updated.
  • key (String) - JSON key whose value should be replaced (must be a Single key string).
  • [optional] path (String) - path of the key in the JSON object referred from the root '/'.

update 是一个不返回任何内容的异步方法

store.update({ misc: '10%' }, '/timeSpent')
  .then(() => { console.log('updated'); })
  .catch((err) => { console.log(err); });

Delete a Data

store.delete([path])

  • [optional] path (String path) - JSON key path from where the key-value pair should be deleted (default is root '/')

如果没有指定路径,则包含该文件的整个数据存储将被删除

delete 是一个 < strong>异步方法,不返回任何内容。

store.delete('/timeSpent')
  .then(() => { console.log('timeSpent object deleted'); })
  .catch((error) => { console.log(error); });

Calculate data store size

store.calcFileSize()

calcFileSize 是一种同步方法,它以兆字节为单位返回数据存储文件的大小。

 console.log(store.calcFileSize());

kval-dstore

A simple file based key-value data store that supports basic CRUD operations in nodejs.

It creates a json file that can be used as a key-value database for learning purpose or even for micro-apps.

It provides five different API for accessing the key-value data store.

Initializing data store

new dstore(filename [,sizeLimit, initData]);

  • filename (String) - The file name with its path for the json file.
  • [optional] sizeLimit (String) - Specifies the sizeLimit in MegaByte (default is 1024 MB)
  • [optional] initData (JSON object) - Initial data to be stored at the time of file creation (default is empty json object {})
const dstore = require('@abibv/kval-dstore');

const store = new dstore('./dstore.json');
(or)
const store = new dstore(__dirname + '/dstore.json');

Write the data

store.write(object [,path]);

  • object (JSON object) - Data to be stored.
  • [optional] path (string) - Key path where the data should be placed (should be referred from the root) (default is the root of the json data '/')

write is an Asynchronous method which returns nothing.

var object = {
  name: 'myname',
  no: 123456789,
  timeSpent: {
    tech: '50%',
    music: '30%',
    sleep: '20%',
  }
};


//Write the data at the root

store.write(object)
  .then(() => { console.log('data written'); })
  .catch((error) => { console.log(error); });


//Write the data at the specified 

store.write(object, '/department/year/students/')
  .then(() => { console.log('data written'); })
  .catch((error) => { console.log(error); });

Read the data

store.read([path]);

  • [optional] path (String) - JSON key path from where the data should be read. (default is root '/')

If the path is not specified, then the whole data was read.

read is an Asynchronous method which returns the data read.

store.read('/department/year/students/')
  .then((data) => { console.log(data); })
  .catch((error) => { console.log(error); });

Update the data

store.update(data, key [,path]);

  • data (any) - data to be updated.
  • key (String) - JSON key whose value should be replaced (must be a Single key string).
  • [optional] path (String) - path of the key in the JSON object referred from the root '/'.

update is an Asynchronous method which returns nothing.

store.update({ misc: '10%' }, '/timeSpent')
  .then(() => { console.log('updated'); })
  .catch((err) => { console.log(err); });

Delete a Data

store.delete([path])

  • [optional] path (String path) - JSON key path from where the key-value pair should be deleted (default is root '/')

If no path is specified, then the whole data store with the file will be deleted

delete is an Asynchronous method which returns nothing.

store.delete('/timeSpent')
  .then(() => { console.log('timeSpent object deleted'); })
  .catch((error) => { console.log(error); });

Calculate data store size

store.calcFileSize()

calcFileSize is an Synchronous method which returns the size of the data store file in MegaBytes.

 console.log(store.calcFileSize());
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文