Object built-in object

ObjectLevelDB

evelDB is a built-in database operation object of fibjs, which is used to create and manage dictionary objects in the form of key-value pairs. Using LevelDB objects, you can easily implement key-value pair data storage, query, deletion, enumeration and other operations. It is implemented based on Google's open source LevelDB and has the advantages of efficiency, reliability, and scalability.

Creating a LevelDB object is very simple, just passdb.openLevelDBMethod creates a database object with a specified name. For example:

1 2
var db = require("db"); var test = db.openLevelDB("test.db");

in,dbis the database operation object of fibjs, the openLevelDB method is used to open the leveldb database, test.db is the database name, and the function returnstestThe object is the object that operates the database.

The main operations supported by LevelDB objects include:

  • set(key, value): Set a key value data. If the key value does not exist, insert new data.
  • get(key): Query the value of the specified key value.
  • has(key): Determine whether the specified key value exists.
  • remove(key): Delete all values ​​of the specified key value.
  • forEach(func): Enumerate all key-value pairs in the database.
  • between(from, to, func): Enumerate key-value pairs in the database with key values ​​between from and to.
  • toJSON(key): Returns the JSON format representation of the object, generally returning a set of readable attributes defined by the object.
  • begin(): Start a transaction on the current database.
  • commit(): Submit the current transaction.
  • close(): Close the current database connection or transaction.

For example:

1 2 3 4 5 6 7 8 9 10 11 12 13
var db = require("db"); var test = db.openLevelDB("test.db"); test.set("test_key", "test_value"); var value = test.get("test_key"); console.log("test_key:", value.toString()); test.remove("test_key"); console.log("has test_key:", test.has("test_key")); test.close();

The above are the basic usage and examples of LevelDB objects, which can conveniently and flexibly operate key-value pair data. In practical applications, it can be used in storage, caching, logging and other scenarios to improve data reading and writing efficiency, simplify program logic, reduce development complexity, etc.

inheritance relationship

member function

has

Check whether data with specified key value exists in the database

1
Boolean LevelDB.has(Buffer key) async;

Call parameters:

  • key:Buffer, specify the key value to be checked

Return results:

  • Boolean, returns whether the key value exists

get

Query the value of the specified key value

1
Buffer LevelDB.get(Buffer key) async;

Call parameters:

  • key:Buffer, specify the key value to be queried

Return results:

  • Buffer, returns the value corresponding to the key value, if it does not exist, returns null

mget

Query the value of a set of specified key values

1
NArray LevelDB.mget(Array keys);

Call parameters:

  • keys: Array, specifies the key value array to be queried

Return results:

  • NArray, returns an array containing key values

set

Set a key value data. If the key value does not exist, insert new data.

1 2
LevelDB.set(Buffer key, Buffer value) async;

Call parameters:

  • key:Buffer, specify the key value to be set
  • value:Buffer, specify the data to be set

mset

Set a set of key value data. If the key value does not exist, insert new data.

1
LevelDB.mset(Object map);

Call parameters:

  • map: Object, specifies the key-value data dictionary to be set

mremove

Delete a set of values ​​for a specified key

1
LevelDB.mremove(Array keys);

Call parameters:

  • keys: Array, specifies the key value array to be deleted

remove

Delete all values ​​of the specified key

1
LevelDB.remove(Buffer key) async;

Call parameters:

  • key:Buffer, specify the key value to be deleted

firstKey

Query the smallest key

1
Buffer LevelDB.firstKey() async;

Return results:

  • Buffer, returns the smallest key

lastKey

Query the largest key

1
Buffer LevelDB.lastKey() async;

Return results:

  • Buffer, returns the largest key

forEach

Enumerate all key-value pairs in the database

1
LevelDB.forEach(Function func);

Call parameters:

  • func: Function, enumeration callback function

The callback function has two parameters, (value, key)

1 2 3 4 5 6
var db = require("db"); var test = new db.openLevelDB("test.db"); test.forEach(function(value, key) { ... });

Enumerate all key-value pairs in the database

1 2
LevelDB.forEach(Buffer from, Function func);

Call parameters:

  • from:Buffer, the minimum key value of the enumeration, this key value is included during enumeration
  • func: Function, enumeration callback function

The callback function has two parameters, (value, key)

1 2 3 4 5 6
var db = require("db"); var test = new db.openLevelDB("test.db"); test.forEach("aaa", "bbb", function(value, key) { ... });

Enumerate all key-value pairs in the database

1 2 3
LevelDB.forEach(Buffer from, Buffer to, Function func);

Call parameters:

  • from:Buffer, the minimum key value of the enumeration, this key value is included during enumeration
  • to:Buffer, the maximum key value of the enumeration, this key value is not included in the enumeration
  • func: Function, enumeration callback function

The callback function has two parameters, (value, key)

1 2 3 4 5 6
var db = require("db"); var test = new db.openLevelDB("test.db"); test.forEach("aaa", "bbb", function(value, key) { ... });

Enumerate all key-value pairs in the database

1 2
LevelDB.forEach(Object opt, Function func);

Call parameters:

  • opt: Object, enumeration options, supports skip, limit, reverse
  • func: Function, enumeration callback function

The callback function has two parameters, (value, key)

1 2 3 4 5 6
var db = require("db"); var test = new db.openLevelDB("test.db"); test.forEach(function(value, key) { ... });

Enumerate all key-value pairs in the database

1 2 3
LevelDB.forEach(Buffer from, Object opt, Function func);

Call parameters:

  • from:Buffer, the minimum key value of the enumeration, this key value is included during enumeration
  • opt: Object, enumeration options, supports skip, limit, reverse
  • func: Function, enumeration callback function

The callback function has two parameters, (value, key)

1 2 3 4 5 6
var db = require("db"); var test = new db.openLevelDB("test.db"); test.forEach("aaa", "bbb", function(value, key) { ... });

Enumerate all key-value pairs in the database

1 2 3 4
LevelDB.forEach(Buffer from, Buffer to, Object opt, Function func);

Call parameters:

  • from:Buffer, the minimum key value of the enumeration, this key value is included during enumeration
  • to:Buffer, the maximum key value of the enumeration, this key value is not included in the enumeration
  • opt: Object, enumeration options, supports skip, limit, reverse
  • func: Function, enumeration callback function

The callback function has two parameters, (value, key)

1 2 3 4 5 6
var db = require("db"); var test = new db.openLevelDB("test.db"); test.forEach("aaa", "bbb", function(value, key) { ... });

begin

Start a transaction on the current database

1
LevelDB LevelDB.begin();

Return results:

  • LevelDB, returns an open transaction object

commit

Submit current transaction

1
LevelDB.commit();

close

Close the current database connection or transaction

1
LevelDB.close() async;

toString

Returns the string representation of the object. Generally, "[Native Object]" is returned. The object can be re-implemented according to its own characteristics.

1
String LevelDB.toString();

Return results:

  • String, returns the string representation of the object

toJSON

Returns a JSON format representation of the object, generally returning a collection of readable properties defined by the object.

1
Value LevelDB.toJSON(String key = "");

Call parameters:

  • key: String, not used

Return results:

  • Value, returns a value containing JSON serializable