RQLite : Lightweight distributed relational database client

更新时间:
2024-05-14
下载文档

RQLite : Lightweight distributed relational database client

rqlite is an easy-to-use, lightweight, distributed relational database, which uses SQLite as its storage engine. rqlite is simple to deploy, operating it is very straightforward, and its clustering capabilities provide you with fault-tolerance and high-availability. rqlite is available for Linux, SylixOS, macOS, and Microsoft Windows.

This module is the client module of rqlite, developers can use this module to operate the rqlite database.

User can use the following code to import the RQLite module.

var RQLite = require('rqlite');

Support

The following shows RQLite module APIs available for each permissions.

 User ModePrivilege Mode
RQLite
rqlite.status
rqlite.exec
rqlite.query
rqlite.batch

RQLite Class

new RQLite(server[, tlsOpt])

  • server {String} Server URL.
  • tlsOpt {Object} If you need a secure connection, you need to specify this information, see TLS Client for details. default: no secure connection.
  • Returns: {Object} rqlite object.

Create rqlite client object.

Example

var rqlite = new RQLite('http://192.168.1.1:4000');
  • URL with username and password
var rqlite = new RQLite('http://user:passwd@192.168.1.1:4000');

new RQLite(servers[, tlsOpt])

  • servers {Array} Server URLs.
  • tlsOpt {Object} If you need a secure connection, you need to specify this information, see TLS Client for details. default: no secure connection.
  • Returns: {Object} rqlite object.

Create rqlite client object with the specified list of cluster servers. When the server in use in the cluster fails, it will automatically switch to other servers to try.

Example

var rqlite = new RQLite([
  'http://192.168.1.1:4000', 'http://92.168.1.2:4000'
]);

RQLite Object

async rqlite.status()

  • Returns: {Object} rqlite server status.

async rqlite.exec(sql[, vars])

  • sql {String} SQL statement to be executed.
  • vars {Array} List of variable values to bind.
  • Returns: {Integer} If it is an insert statement, it returns the last rowid.

Execute a common SQL statement.

Example

rqlite.exec('CREATE TABLE user(name text, age int);')
.then(() => console.log('OK!'), error => console.error(error));
  • Async function
async function init() {
  await rqlite.exec('CREATE TABLE user(name text, age int);');
  await rqlite.exec('INSERT INTO user("Zhang.San", 20);');
}
  • Variable binding
async function varbind(name, age) {
  await rqlite.exec('INSERT INTO user(?, ?);', [ name, age ]);
}

async rqlite.query(sql[, vars])

  • sql {String} Query statement to be executed.
  • vars {Array} List of variable values to bind.
  • Returns: {Array} Query result array, each member of the array is a query result object.

Execute a query SQL statement.

Example

async function test() {
  await rqlite.exec('INSERT INTO user("Zhang.San", 20);');
  await rqlite.exec('INSERT INTO user("Li.Si", 30);');
  let rows = await rqlite.query('SELECT * from user;');
  // [
  //  { name: "Zhang.San", age: 20 },
  //  { name: "Li.Si", age: 30 },
  // ]
}

async rqlite.batch(sqls[, transaction])

  • sqls {Array} List of SQL statements to be executed.
  • transaction {Boolean} Whether it is the transaction operation. default: true.
  • Returns: {Integer} If it is an insert statement, it returns the last rowid.

Perform a bulk operation, in the rqlite database bulk operation is contained within a single Raft log entry, and only one Raft log entry is ever processed at one time, a bulk operation will never be interleaved with other requests.

Example

async function batch() {
  await rqlite.exec([
    'INSERT INTO user("Zhang.San", 20);',
    'INSERT INTO user("Li.Si", 30);'
  ]);
}
  • Variable binding
async function batch(name, age) {
  await rqlite.exec([
    'INSERT INTO user("Zhang.San", 20);',
    [ 'INSERT INTO user(?, ?);', name, age ]
  ]);
}
文档内容是否对您有所帮助?
有帮助
没帮助