AvionDB: A MongoDB-like Distributed Database
Tags: mongodb, distributedsystems, node, javascript
Published: 2020-05-09 20:00:40

In the past few months we have been getting this question a lot:

> What do I use for managing dynamic data for my Decentralized App? 

We have the [InterPlanetary File System](https://simpleaswater.com/ipfs) for storing static data, but what about the data that keeps changing?

There are a number of existing distributed databases like [GUN](https://gun.eco/), [OrbitDB](https://orbitdb.org/), [Scuttlebutt](https://scuttlebot.io/), [Textile Threads](https://docs.textile.io/threads/introduction/), etc.

Although these existing databases are great, we wanted a distributed database that will feel familiar when you are coming from Web 2.0 & still has all the features that you would expect from a distributed database.

Something like a distributed [MongoDB](https://mongodb.com/).

## Enter AvionDB

[AvionDB](https://github.com/dappkit/aviondb) is a distributed database built on top of [OrbitDB](https://orbitdb.org/) & [IPFS](https://ipfs.io/).

When [we](https://github.com/dappkit) started our work on AvionDB, we had a few design goals in our mind.

### Design Goals

- **Familiar developer interface**: MongoDB/Mongoose like interface.
- **Local-first**: Your app can work locally without the internet. For example, you can take notes on your notepad without (or even sync with other devices) connecting to the Internet.
- **Privacy-first**: All the data, whether it is on your device or on out in the network, it should be encrypted by default.
- **Cross-platform**: The database can be used natively on Mobile, Web & Desktop platforms.

### How does it work?

![AvionDB Achitecture](https://simpleaswater.com/content/images/2020/05/Architecture.png)

AvionDB uses OrbitDB stores to model MongoDB-like Databases.

- Each AvionDB instance can have several **Databases**.
- Each Database can have several **Collections**.
- Each Collection can have several **Documents**.

The Javascript implementation works both in **Browsers** and **Node.js** with support for Linux, OS X, and Windows. The minimum required version of Node.js is now 8.6.0 due to the usage of `...` spread syntax. LTS versions (even-numbered versions 8, 10, etc) are preferred.

## Using AvionDB with NodeJS

### Install

```
// Using npm
npm install --save aviondb

// Using Github
npm install git+https://github.com/dappkit/aviondb.git
```

### Usage

```javascript
// Import modules
const AvionDB = require("aviondb");
const IPFS = require("ipfs");
const ipfs = new IPFS();
 
const runExample = async () => {
  await ipfs.ready;
    
  // Creates a db named "DatabaseName"
  const aviondb = await AvionDB.init("DatabaseName", ipfs); 
  
  // Returns the List of database names
  await AvionDB.listDatabases()
  // prints ['DatabaseName']

  // Creates a Collection named "employees"
  const collection = await aviondb.initCollection("employees");
 
  // Returns the List of collection names
  await aviondb.listCollections() 
  // prints ['employees'] 
 
  // Adding an employee document
  await collection.insertOne({
    hourly_pay: "$15",
    name: "Elon",
    ssn: "562-48-5384",
    weekly_hours: 100,
  });
 
  // We also support multi-insert using collection.insert()
  // See https://github.com/dappkit/aviondb/blob/master/API.md
    
    
  // Search by a single field Or many!
  var employee = await collection.findOne({
    ssn: "562-48-5384", 
  });
 
  // We also support find(), findById()
  // See https://github.com/dappkit/aviondb/blob/master/API.md
    
  // Returns the matching document
  console.log(employee); 
  // Prints the above added JSON document
    
    
  // Update a document
  var updatedEmployee = await collection.update(
   { ssn: "562-48-5384" },
   { $set: { hourly_pay: '$100' } }
  );
    
  // We also support updateMany(), findOneAndUpdate()
  // See https://github.com/dappkit/aviondb/blob/master/API.md

  // Returns the updated document
  console.log(updatedEmployee); 
  // Prints the updated JSON document

    
  await collection.close(); // Collection will be closed.
  await aviondb.drop(); // Drops the database 
  await aviondb.close(); // Closes all collections and binding database.
  await ipfs.stop();
};
 
runExample();
```

## Using AvionDB with Browser


#### **through Browserify**
Same as in Node.js, you just have to [browserify](http://browserify.org/) to bundle the code before serving it.
 > Note: The code uses `es6`, so you have to use [babel](https://babeljs.io/) to convert the code into `es5` before using `browserify`. 

#### **through webpack**
Same as in Node.js, you just have to [webpack](https://webpack.js.org/) to bundle the the code before serving it.
 > Note: The code uses `es6`, so you have to use [babel](https://babeljs.io/) to convert the code into `es5` before using `webpack`.

#### **from CDN**

Instead of a local installation (and browserification) you may request a remote copy of AvionDB from unpkg CDN.

To always request the latest version, use the following:
```html
<!-- loading the minified version -->
<script src="https://unpkg.com/aviondb/dist/aviondb.min.js"></script>
```

CDN-based AvionDB provides the `AvionDB` constructor as a method of the global `window` object. Example:

```javascript
// create an AvionDB instance
const aviondb = await AvionDB.create("DatabaseName", ipfs) 
```

### Usage

```html
<html>
  <head>
    <title>AvionDB Example</title>
    <script src="https://cdn.jsdelivr.net/npm/ipfs/dist/index.min.js"></script>
    <script src="https://unpkg.com/aviondb/dist/aviondb.min.js"></script>
  </head>
  <body>
    Open your console logs to see AvionDB in action!
  </body>
  <script type="text/javascript">
  const runExample = async () => {
    const ipfs = await window.Ipfs.create();
    await ipfs.ready;
      
    // Creates a db named "DatabaseName"
    const aviondb = await AvionDB.init("DatabaseName", ipfs);
    
    // Returns the List of database names
    await AvionDB.listDatabases()
    // prints ['DatabaseName'] 

    // Creates a Collection named "employees"
    const collection = await aviondb.initCollection("employees");
  
    // Returns the List of collection names
    await aviondb.listCollections() 
    // prints ['employees'] 
  
    // Adding an employee document
    await collection.insertOne({
      hourly_pay: "$15",
      name: "Elon",
      ssn: "562-48-5384",
      weekly_hours: 100,
    });
  
    // We also support multi-insert using collection.insert()
    // See https://github.com/dappkit/aviondb/blob/master/API.md
      
      
    // Search by a single field Or many!
    var employee = await collection.findOne({
      ssn: "562-48-5384", 
    });
  
    // We also support find(), findById()
    // See https://github.com/dappkit/aviondb/blob/master/API.md
      
    // Returns the matching document
    console.log(employee); 
    // Prints the above added JSON document
      
      
    // Update a document
    var updatedEmployee = await collection.update(
    { ssn: "562-48-5384" },
    { $set: { hourly_pay: '$100' } }
    );
      
    // We also support updateMany(), findOneAndUpdate()
    // See https://github.com/dappkit/aviondb/blob/master/API.md

    // Returns the updated document
    console.log(updatedEmployee); 
    // Prints the updated JSON document

      
    await collection.close(); // Collection will be closed.
    await aviondb.drop(); // Drops the database 
    await aviondb.close(); // Closes all collections and binding database.
    await ipfs.stop();
  };
  
  runExample();
  </script>
</html>
```

## Demo Example of Firebase Authentication with AvionDB

![A Firebase Auth based Access Controller for AvionDB & OrbitDB](https://github.com/dappkit/aviondb-firebase/raw/master/assets/ezgif.com-video-to-gif.gif)

- Check out the app [here](https://bit.ly/aviondb-firebase).
- Check out the code [here](https://github.com/dappkit/aviondb-firebase)


## Frequently Asked Questions

### Are there implementations in other languages?

We are working to implement AvionDB for following languages:

- NodeJS & Browser JS
- Typescript
- Golang
- Python

The best place to find out what is out there and what is being actively worked on is likely by asking in the [Discord](https://discord.gg/88YpNuQ). 

If you want or are planning to create an implementation in a language that is not listed here, then feel free to reach us out and discuss about it in the [Discord](https://discord.gg/88YpNuQ).

### Where can I see your Roadmap?

You can find our Roadmap [here](https://github.com/dappkit/aviondb/issues/10). The features in the Roadmap are taken from 2 separate issues([#7](https://github.com/dappkit/aviondb/issues/7), [#8](https://github.com/dappkit/aviondb/issues/8)) which individually maintain a list of feature proposals related to OrbitDB-specific improvements & AvionDB-specific improvements respectively.

The Roadmap is an open discussion, feel free to add your suggestions, comments.

### What mongodb features does aviondb support?

You can find all the supported MongoDB-like features in our [API docs](https://github.com/dappkit/aviondb/blob/master/API.md).

### Other Questions?

If you didn't find the answer to your question(s), feel free to reach us out on [Discord](https://discord.gg/88YpNuQ).


## Contributing

**Take a look at our organization-wide [Contributing Guide](https://github.com/dappkit/aviondb/blob/master/CONTRIBUTING.md).**

As far as code goes, we would be happy to accept PRs! If you want to work on something, it'd be good to talk beforehand to make sure nobody else is working on it. You can reach us [on Discord](https://discord.gg/88YpNuQ), or in the [issues section](https://github.com/dappkit/aviondb/issues).

If you want to code but don't know where to start, check out the issues labeled ["help wanted"](https://github.com/dappkit/aviondb/issues?q=is%3Aopen+is%3Aissue+label%3A%22help+wanted%22+sort%3Areactions-%2B1-desc), ["discussion"](https://github.com/dappkit/aviondb/issues?q=is%3Aopen+is%3Aissue+label%3A%22discussion%22+sort%3Areactions-%2B1-desc).

Please note that we have a [Code of Conduct](CODE_OF_CONDUCT.md), and that all activity in the [@dappkit](https://github.com/dappkit) organization falls under it. Read it when you get the chance, as being part of this community means that you agree to abide by it. Thanks.

## Contact Us

Feel free to reach us out on [Discord](https://discord.gg/88YpNuQ).