vblz-client is a v module library that can be used to access the Bluzelle database service.
Via vpm:
$ v install vblz
Via vpkg:
$ vpkg get https://github.com/developer/vblz
To test the module, just type the following command:
$ make test
You will generally use blzjs from code running under the Node.js platform. For instructions on installing Node.js on your platform please see https://nodejs.org. Use of blzjs in web browser content is not currently supported.
You can use blzjs either in-place, or by building a library. In either case you will want to get the latest stable version of blzjs by doing the following:
git clone https://github.com/bluzelle/blzjs
This will create a directory named blzjs and download the library code to it.
For in-place use, simply create a JavaScript source file and include the following at the top of the file:
const { bluzelle } = require('path_to_blzjs/main.js');
You can then use the various functions described below.
To build a library and use it locally, you will need npm and npx installed. Please see the relevant documentation for your platform for instructions on how to do that.
You will then do the following:
cd blzjs
npm install
npx webpack
sudo npm link
Then in the directory where you create your source code, run
npm link bluzelle
And at the top of your source fill add the following:
const { bluzelle } = require ('bluzelle')
The file crud.js in the samples directory contains example code that demonstrates how to use the blzjs API.
In general, you must initialize blzjs before calling any other functions. Do the following (using your own configuration parameters as applicable) to initialize:
let bz = await bluzelle({
address: my_account_address,
mnemonic: my_mnemonic,
uuid: my_uuid,
endpoint: my_endpoint,
chain_id: my_chain_id
});
This performs some initial checks, retrieves your account information, and returns an object through which you can call the rest of the API functions.
You may now use the functions described below to perform database operations, as well as retrieve account and status information.
Read below for detailed documentation on how to use the Bluzelle database service.
The Bluzelle JavaScript library works with promises (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) to model asynchronous behavior. Ensure that dependent calls to the Bluzelle database are within
.then()
blocks or within asynchronous functions (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function). Also ensure that promises exceptions are caught and handled.
Keys and values in the Bluzelle database are plain strings to ensure compatibility for all forms of serialization. JavaScript applications will probably want to use
JSON.stringify(obj)
and JSON.parse(str)
to convert object data to and from string format. Please note: keys may not contain the forward-slash (/) character.
Some API functions take gas_info as a parameter. This is a JavaScript object containing parameters related to gas consumption as follows:
{
'max_gas': '0', // maximum amount of gas to consume for this call (integer)
'max_fee': '0', // maximum amount to charge for this call (integer, in ubnt)
'gas_price': '0' // maximum price to pay for gas (integer, in ubnt)
};
All values are optional. The max_gas
value will always be honored if present, otherwise a default value will be used. If both max_fee
and gas_price
are specified, gas_price
will be ignored and calculated based on the provided max_fee
.
The example code in the
samples
directory requires Node.js in order to run. For instructions on how to install Node.js for your platform, please see https://nodejs.org
The samples require configuration to be set in the file blz-config.js
. You can copy and modify the blz-config.js.sample
file for this purpose.
Configures the Bluzelle connection. This may be called multiple times to create multiple clients.
const {bluzelle} = require('bluzelle');
const api = await bluzelle({
address: 'bluzelle1xhz23a58mku7ch3hx8f9hrx6he6gyujq57y3kp',
mnemonic: 'volcano arrest ceiling physical concert sunset absent hungry tobacco canal census era pretty car code crunch inside behind afraid express giraffe reflect stadium luxury',
endpoint: "https://localhost:1317",
uuid: "20fc19d4-7c9d-4b5c-9578-8cedd756e0ea",
chain_id: "bluzelle"
});
Argument | Description |
---|---|
address | The address of your Bluzelle account |
mnemonic | The mnemonic of the private key for your Bluzelle account |
endpoint | (Optional) The hostname and port of your rest server. Default: https://localhost:1317 |
uuid | (Optional) Bluzelle uses UUID 's to identify distinct databases on a single swarm. We recommend using Version 4 of the universally unique identifier. Defaults to the account address. |
chain_id | (Optional) The chain id of your Bluzelle account. Default: bluzelle |
bluzelle
is the only export of the Bluzelle library. The calls below are methods of the object generated by calling bluzelle
.
Retrieve the version of the Bluzelle service.
// promise syntax
api.version().then(() => { ... }, error => { ... });
// async/await syntax
await api.version();
Returns a promise resolving to a string containing the version information, e.g.
0.0.0-39-g8895e3e
Throws an exception if a response is not received from the connection.
Retrieve information about the currently active Bluzelle account.
// promise syntax
api.account().then(() => { ... }, error => { ... });
// async/await syntax
await api.account();
Returns a promise resolving to a JSON object representing the account information, e.g.
{ address: 'bluzelle1lgpau85z0hueyz6rraqqnskzmcz4zuzkfeqls7',
coins: [ { denom: 'bnt', amount: '9899567400' } ],
public_key: 'bluzellepub1addwnpepqd63w08dcrleyukxs4kq0n7ngalgyjdnu7jpf5khjmpykskyph2vypv6wms',
account_number: 3,
sequence: 218 }
Throws an exception if a response is not received from the connection.
Create a field in the database.
// promise syntax
api.create('mykey', '{ a: 13 }', {gas_price: 10}).then(() => { ... }, error => { ... });
// async/await syntax
await api.create('mykey', '{ a: 13 }, {gas_price: 10}');
Argument | Description |
---|---|
key | The name of the key to create |
value | The string value to set the key |
gas_info (Optional) | Object containing gas parameters (see above) |
Returns a promise resolving to nothing.
Throws an exception when a response is not received from the connection, the key already exists, or invalid value.
Retrieve the value of a key without consensus verification. Can optionally require the result to have a cryptographic proof (slower).
// promise syntax
api.read('mykey').then(value => { ... }, error => { ... });
// async/await syntax
const value = await api.read('mykey');
Argument | Description |
---|---|
key | The key to retrieve |
prove | A proof of the value is required from the network (requires 'config trust-node false' to be set) |
Returns a promise resolving the string value of the key.
Throws an exception when the key does not exist in the database. Throws an exception when the prove is true and the result fails verification.
Updates a field in the database.
// promise syntax
api.update('mykey', '{ a: 13 }', {gas_price: 10}).then(() => { ... }, error => { ... });
// async/await syntax
await api.update('mykey', '{ a: 13 }, {gas_price: 10}');
Argument | Description |
---|---|
key | The name of the key to create |
value | The string value to set the key |
gas_info (Optional) | Object containing gas parameters (see above) |
Returns a promise resolving to nothing.
Throws an exception when the key doesn't exist, or invalid value.
Deletes a field from the database.
// promise syntax
api.delete('mykey', {gas_price: 10}).then(() => { ... }, error => { ... });
// async/await syntax
await bluzelle.delete('mykey', {gas_price: 10});
Argument | Description |
---|---|
key | The name of the key to delete |
gas_info (Optional) | Object containing gas parameters (see above) |
Returns a promise resolving to nothing.
Throws an exception when the key is not in the database.
Query to see if a key is in the database. This function bypasses the consensus and cryptography mechanisms in favor of speed.
// promise syntax
api.has('mykey').then(hasMyKey => { ... }, error => { ... });
// async/await syntax
const hasMyKey = await api.has('mykey');
Argument | Description |
---|---|
key | The name of the key to query |
Returns a promise resolving to a boolean value - true
or false
, representing whether the key is in the database.
Retrieve a list of all keys. This function bypasses the consensus and cryptography mechanisms in favor of speed.
// promise syntax
api.keys().then(keys => { ... }, error => { ... });
// async/await syntax
const keys = await api.keys();
Returns a promise resolving to an array of strings. ex. ["key1", "key2", ...]
.
Change the name of an existing key.
// promise syntax
api.rename("key", "newkey", {gas_price: 10}).then(() => { ... }, error => { ... });
// async/await syntax
await api.rename("key", "newkey", {gas_price: 10});
Argument | Description |
---|---|
key | The name of the key to rename |
new_key | The new name for the key |
gas_info (Optional) | Object containing gas parameters (see above) |
Returns a promise resolving to nothing.
Throws an exception if the key doesn't exist.
Argument | Description |
---|---|
key | The name of the key to query |
Returns a promise resolving to a boolean value - true
or false
, representing whether the key is in the database.
Retrieve the number of keys in the current database/uuid. This function bypasses the consensus and cryptography mechanisms in favor of speed.
// promise syntax
api.count().then(number => { ... }, error => { ... });
// async/await syntax
const number = await api.count();
Returns a promise resolving to an integer value.
Remove all keys in the current database/uuid.
// promise syntax
api.deleteall({gas_price: 10}).then(() => { ... }, error => { ... });
// async/await syntax
await api.deleteall({gas_price: 10});
Argument | Description |
---|---|
gas_info (Optional) | Object containing gas parameters (see above) |
Returns a promise resolving to nothing.
Enumerate all keys and values in the current database/uuid. This function bypasses the consensus and cryptography mechanisms in favor of speed.
// promise syntax
api.keyvalues().then(kvs => { ... }, error => { ... });
// async/await syntax
const kvs = await api.keyvalues();
Returns a promise resolving to a JSON array containing key/value pairs, e.g.
[{"key": "key1", "value": "value1"}, {"key": "key2", "value": "value2"}]
Retrieve the value of a key via a transaction (i.e. uses consensus).
// promise syntax
api.txread('mykey', {gas_price: 10}).then(value => { ... }, error => { ... });
// async/await syntax
const value = await api.txread('mykey', {gas_price: 10});
Argument | Description |
---|---|
key | The key to retrieve |
gas_info (Optional) | Object containing gas parameters (see above) |
Returns a promise resolving the string value of the key.
Throws an exception when the key does not exist in the database.
Query to see if a key is in the database via a transaction (i.e. uses consensus).
// promise syntax
api.txhas('mykey', {gas_price: 10}).then(hasMyKey => { ... }, error => { ... });
// async/await syntax
const hasMyKey = await api.txhas('mykey', {gas_price: 10});
Argument | Description |
---|---|
key | The name of the key to query |
gas_info (Optional) | Object containing gas parameters (see above) |
Returns a promise resolving to a boolean value - true
or false
, representing whether the key is in the database.
Retrieve a list of all keys via a transaction (i.e. uses consensus).
// promise syntax
api.txkeys({gas_price: 10}).then(keys => { ... }, error => { ... });
// async/await syntax
const keys = await api.txkeys({gas_price: 10});
Argument | Description |
---|---|
gas_info (Optional) | Object containing gas parameters (see above) |
Returns a promise resolving to an array of strings. ex. ["key1", "key2", ...]
.
Retrieve the number of keys in the current database/uuid via a transaction.
// promise syntax
api.txcount({gas_price: 10}).then(number => { ... }, error => { ... });
// async/await syntax
const number = await api.txcount({gas_price: 10});
Argument | Description |
---|---|
gas_info (Optional) | Object containing gas parameters (see above) |
Returns a promise resolving to an integer value.
Enumerate all keys and values in the current database/uuid via a transaction.
// promise syntax
api.txkeyvalues({gas_price: 10}).then(kvs => { ... }, error => { ... });
// async/await syntax
const kvs = await api.txkeyvalues({gas_price: 10});
Argument | Description |
---|---|
gas_info (Optional) | Object containing gas parameters (see above) |
Returns a promise resolving to a JSON array containing key/value pairs, e.g.
[{"key": "key1", "value": "value1"}, {"key": "key2", "value": "value2"}]
Updates multiple fields in the database.
// promise syntax
api.update([{key: "key1", value: "value1"}, {key: "key2", value: "value2"}], {gas_price: 10}).then(() => { ... }, error => { ... });
// async/await syntax
await api.update([{key: "key1", value: "value1"}, {key: "key2", value: "value2"}, {gas_price: 10}');
Argument | Description |
---|---|
keyvalues | An array of objects containing keys and values (see example avove) |
gas_info (Optional) | Object containing gas parameters (see above) |
Returns a promise resolving to nothing.
Throws an exception when any of the keys doesn't exist.