npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

swarmdb.js

v0.1.0

Published

SWARMDB JavaScript API

Downloads

8

Readme

SWARMDB JavaScript API

Join the chat at https://gitter.im/wolkdb/swarmdb.js

This is a client library for the SWARMDB API, implemented in JavaScript. It's available on npm as a node module.

To use swarmdb.js, you MUST install our SWARMDB Docker image and set up the SWARMDB node first. Instructions can be found HERE.

Please note that the library is still under heavy development and it might not be stable.

Install

> npm install swarmdb.js

## Configure
Upon installation and startup of your SWARMDB node, a default user and associated private key are generated and stored in the [SWARMDB configuration file](https://github.com/wolkdb/swarm.wolk.com/wiki/9.-SWARMDB-Server-Configuration,--Authentication-and-Voting#configuration-file). 

Set the private key as an environment variable.
This private key is associated with the user configured on the SWARMDB node.
```bash
> export PRIVATE_KEY=PRIVATE_KEY_IN_YOUR_CONFIG_FILE

Import module

var swarmdbAPI = require("swarmdb.js");

Open connection

Open a connection by specifying configuration options like host and port

swarmdbAPI.createConnection(options)

var swarmdb = swarmdbAPI.createConnection({
    host: SWARMDB_HOST, //e.g. "localhost"
    port: SWARMDB_PORT  //e.g. 2001
});

Create database

Create a database by specifying owner, database name and encrypted status.

Owner should be a valid ENS domain.
For encrypted status, 1 means true and 0 means false.

swarmdb.createDatabase(owner, databaseName, encrypted, callback)

swarmdb.createDatabase("test.eth", "testdb", 1, function (err, result) {
    if (err) {
      throw err;
    }
    console.log(result);
});

Open database

Open a database by specifying owner and database name.

swarmdb.openDatabase(owner, databaseName)

swarmdb.openDatabase("test.eth", "testdb");

List databases

List existing databases.

swarmdb.listDatabases(callback)

swarmdb.listDatabases(function (err, result) {
    if (err) {
      throw err;
    }
    console.log(result);
});

Drop database

Drop a database by specifying owner and database name.

swarmdb.dropDatabase(owner, databaseName, callback)

swarmdb.dropDatabase("test.eth", "testdb", function (err, result) {
    if (err) {
        throw err;
    }
    console.log(result);
});

Create table

Create a table by specifying table name and column details.

Columns consist of the following parameters:

  • indextype: enter the desired indextype, defined HERE
  • columnname: enter the desired column name (no spaces allowed)
  • columntype: enter the desired columntype, defined HERE
  • primary: enter 1 if the column is the primary key and 0 for any other column.

swarmdb.createTable(tableName, columns, callback)

var columns = [
    { "indextype": "BPLUS", "columnname": "email", "columntype": "STRING",  "primary": 1 },
    { "indextype": "HASH",  "columnname": "name",  "columntype": "STRING",  "primary": 0 },
    { "indextype": "BPLUS", "columnname": "age",   "columntype": "INTEGER", "primary": 0 }
];
swarmdb.createTable("contacts", columns, function (err, result) {
    if (err) {
      throw err;
    }
    console.log(result);
});

Open table

Open a table by specifying table name.

swarmdb.openTable(tableName)

swarmdb.openTable("contacts");

Describe table

Describe a table by specifying table name.

swarmdb.describeTable(tableName, callback)

swarmdb.describeTable("contacts", function (err, result) {
    if (err) {
      throw err;
    }
    console.log(result);
});

Drop table

Drop a table by specifying table name.

swarmdb.dropTable(tableName, callback)

swarmdb.dropTable("contacts", function (err, result) {
    if (err) {
      throw err;
    }
    console.log(result);
});

List tables

List existing tables.

swarmdb.listTables(callback)

swarmdb.listTables(function (err, result) {
    if (err) {
      throw err;
    }
    console.log(result);
});

Read / Get / Select

Read a row (or rows) may be done via a Get call or a SQL Select query.

Get

Get calls allow for the retrieval of a single row by specifying the value of a row's primary key.

swarmdb.get(key, callback)

swarmdb.get("[email protected]", function (err, result) {
    if (err) {
      throw err;
    }
    console.log(result);
});

Select

Select Query calls allow for the retrieval of rows by specifying a SELECT query using standard SQL. The supported query operands are allowed to be used on both primary and secondary keys.

swarmdb.query(sqlQuery, callback)

swarmdb.query("SELECT email, name, age FROM contacts WHERE email = '[email protected]'", function (err, result) {
    if (err) {
      throw err;
    }
    console.log(result);
});

swarmdb.query("SELECT email, name, age FROM contacts WHERE age >= 5", function (err, result) {
    if (err) {
      throw err;
    }
    console.log(result);
});

Write / Put / Insert / Update

Writing a row (or rows) may be done via a Put call or a SQL Insert query.

Put

Put calls allow for writing rows.

Note: The "rows" input consists of an array of rows, where a row is a defined as a JSON object containing column/value pairs. Each row must at least include a column/value pair for the primary key.

swarmdb.put(rows, callback)

swarmdb.put( [ { "email": "[email protected]", "name": "Bertie Basset", "age": 7 }, { "email": "[email protected]", "name": "Paul", "age": 25 } ],  function (err, result) {
    if (err) {
      throw err;
    }
    console.log(result);
});

Insert

Insert Query calls allow for the insertion of rows by specifying an INSERT query using standard SQL. SWARMDB currently only supports one row insert per call.

swarmdb.query(sqlQuery, callback)

swarmdb.query("INSERT INTO contacts (email, name, age) VALUES ('[email protected]', 'Bertie Basset', 7);", function (err, result) {
    if (err) {
      throw err;
    }
    console.log(result);
});

Update

Update Query calls allow for the update on non-primary key using standard SQL.

swarmdb.query(sqlQuery, callback)

swarmdb.query("UPDATE contacts SET age=8 WHERE email='[email protected]';", function (err, result) {
    if (err) {
      throw err;
    }
    console.log(result);
});

Try out the example

Find the script inside example folder and run it in your Node.js project folder.

> npm install swarmdb.js
> node apitest.js

Run tests for developement

Make sure the private key is configured before running the tests

> git clone https://github.com/wolkdb/swarmdb.js.git
> cd swarmdb.js && npm install
> npm test

Feedback / Question

Email us at [email protected]