Giter VIP home page Giter VIP logo

decentralized-db's Introduction

Decentralized DB

Decentralized DB is a storage wrapper that combines Blockstack storage, IPFS storage, and IndexedDB storage using TurtleDB. It is configurable on each storage request, allows for encryption using elliptic curve encryption, and provides a simple API.

Motivation
While Blockstack's Gaia storage, IPFS, and TurtleDB storage are all great on their own, there are many times when all three might be used. There are also times with combinations of the three might be used. This storage wrapper makes that possible with simple functions:

storeFile
loadFile

Getting Started

Install
npm install decentralized-db

Import/Require
import { storeFile, loadFile } from "decentralizedDB"
const decentralizedDB = require("decentralized-db")

Once you've imported/required the package, you can start using the functions. Remember, if you require the package rather than importing each function, you'll need to prefix your calls with "decentralizedDB". The docs will not include this so as a quick example, if you're not importing, you would make a call like this: decentralizedDB.storeFile(options).

storeFile

When you're ready to update a file or a store a new one, you'll simple need to call storeFile with the following options:

  • filename [String]
  • encrypt [boolean]
  • ipfs [boolean]
  • sync [boolean]
  • data [Object or String or Boolean or Array]

Important Notes: The filename String needs to have a suffix of '.json', '.txt', or some other valid extension that can contain string data. This is because storing to Blockstack and IPFS requires a valid pathname. The filename will act as both the pathname and your identifier for local IndexedDB storage.

Example:

import { storeFile } from "decentralized-db";

const options = {};
options.filename = 'testfile.json';
options.encrypt = true;
options.data = "Rage against the dying of the light";
options.ipfs = true;
options.sync = true;

storeFile(options).then((data) => {
  console.log(data)
})

You'll notice that you can chain on a .then to grab the response from your storeFile call. This is important because if you are storing to IPFS, the multihash will be included in that response. Since the only way to reference that multihash again would be to store it somewhere, you will likely want to make another storeFile call and post that multihash to Blockstack's Gaia storage. Here's what that would look like:

import { storeFile } from "decentralized-db";

const options = {};
options.filename = 'testfile.json';
options.encrypt = true;
options.data = "Rage against the dying of the light";
options.ipfs = true;
options.sync = true;

storeFile(options).then((data) => {
  const gaiaPost = {};
  gaiaPost.fileName = multihash.json;
  gaiaPost.encrypt = true;
  gaiaPost.data = data;
  gaiaPost.ipfs = false;
  gaiaPost.sync = true;
  storeFile(gaiaPost)
})

Finally, the sync property of the options object in your storeFile call is the way you indicate whether or not you want to store to Blockstack & IPFS/Blockstack-Only or if you just want to store to your local storage. Using this property wisely will allow you to cut down on http request and hopefully improve performance of your app.

If you indicate sync: false but also include ipfs: true, your ipfs property will be ignored.

Finally, to use IPFS, you will need to install js-ipfs and include it in your project somewhere so that a node is initiated and available before you push a file to IPFS. For example, in the parent component of a React project, you can initialize your node so that it is available across all components by requiring it:

const IPFS = require("ipfs");
const node = new IPFS();

Then, since the example we are talking about is a React project, you would include the following in your componentDidMount method:

node.on('ready', async () => {
  const version = await node.version()

  console.log('Version:', version.version)
})

decentralized-db's People

Contributors

polluterofminds avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.