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

cubesat-db

v1.0.0-alpha

Published

A distributed database build on PouchDB and IPFS.

Downloads

2

Readme

cubesat-db

stability js-standard-style

A small connector for big things.

CubeSatDB uses PouchDB and the ipfs-log module from OrbitDB to store, index, and replicate documents between peers over IPFS and CouchDB.

Install

Install CubeSatDB using npm:

$ npm install -S cubesat-db

Usage

CubeSatDB is just a database: you can add, update, remove, and query records:

const CubeSatDB = require('cubesat-db')

let cube = new CubeSatDB('hello-world')

cube.all().then(function (result) {
    // Print all of the documents in the db
    console.log(result)
    // Add a document to the database
    return cube.post({ status: 'ok', date: Date.now() })
}).then(function () {
    // Add a document with a known ID
    return cube.put({ _id: 'mars-rover', status: 'landing' })
}).then(function (result) {
    // extract the updated document from the response
    let doc = result.payload
    // Remove a document
    return cube.del(doc)
})

But you can also replicate it between computers:

// on one computer:
let cube = new CubeSatDB('hello-world')
/*
... add documents ...
then get the cube's multihash
 */
cube.toMultihash().then(function (hash) {
    // pass this value to your friend
    console.log(hash)
})

// on another computer
const hash = '...' // that value from above
let cube = new CubeSatDB({ hash, name: 'hello-world' })
cube.load().then(function () {
    console.log('Cube up to date!')
})

And you can join one CubeSatDB instances with another. It will merge all the entries from the other that it does not already have:

let cube1 = new CubeSatDB('cube-1')
let cube2 = new CubeSatDB('cube-2', { ipfs: cube1.ipfs })
Promise.all([
    cube1.post.bind(cube1, { status: 'ok' })
    cube2.post.bind(cube2, { status: 'fine' })
]).then(function () {
    return cube1.join(cube2)
}).then(function () {
    // cube1 has all the values in cube2
    // but cube2 doesn't have cube1's records
    console.log(cube1.log.values)
    })

If you have any questions, please leave an issue!

API

Table of Contents

CubeSatDB

Parameters

  • name String A name, URL (of a CouchDB instance), or multihash (of an IPFS log)
  • options Object An object of settings and configuration values. (optional, default {})
    • options.ipfs (IPFS | Object) [description]
    • options.pouch Object [description]
    • options._IpfsLog IpfsLog [description]
    • options._IPFS IPFS [description]
    • options._PouchDB PouchDB [description]

put

Add or update a document.

If you pass an array of docs, it will pass each of them to this method and resolve once they all resolve.

Parameters

  • doc (Object | Array) The document to save, or an array of such documents.
    • doc._id String The document's ID. Required for .put()

Returns Promise

post

Post a document to the store, creating an ID for it. To add a document with an ID, see .put().

If you pass an array of docs, it will pass each of them to this method and resolve once they all resolve.

Parameters

  • doc (Object | Array) The document to save, or an array of such documents.

Returns Promise

get

Retrieve a document. Wrapper around PouchDB#get()

Returns Promise<Object> A promise that resolves to the specified document.

all

Retrieve all documents. Wrapper around PouchDB#allDocs

Parameters

  • options Object (optional, default {})

Returns Promise<Array<Object>> Resolves to an array of retrieved documents.

del

Delete a document using its ID and revision value.

Parameters

  • doc Object The document to delete.
    • doc._id String The document's ID. Required.
    • doc._rev String The document's revision value. Required.

Returns Promise

find

Wrapper around PouchDB#find that returns each selected document.

Returns Promise<Array<Object>> Selected documents.

query

Alias to PouchDB#query().

Returns Promise<Object> Query result.

join

Merges another CubeSatDB or IpfsLog into this one.

Parameters

  • log (IpfsLog | CubeSatDB) An instance of IpfsLog or CubeSatDB.

Returns Promise [description]

load

Loads the oplog from the store's IPFS hash.

Errors out if the store wasnt constructed with a hash or hasn't established one yet using .toMultihash().

Returns Promise A promise that resolves once the store has loaded.

validate

A document validator. It makes sure a document is an object but not an array.

Subclasses can extend this method to enforce a schema.

Parameters

  • doc Object A document.

  • Throws CubeError An error about the document.

toMultihash

Returns Promise<String> The multihash for this store. Use this value to clone the store.

hash

  • Throws CubeError If CubeSatDB#toMultihash() has not been called yet.

Returns String The multihash for the store.

name

Returns String The name of this database.

pouch

Returns PouchDB The CubeSatDB instance's instance of PouchDB.

log

Returns IpfsLog The CubeSatDB instance's instance of IpfsLog.

ipfs

Returns IPFS The CubeSatDB instance's instance of an IPFS node.

Error

A subclass of Error for describing cube-related problems.

Returns CubeError

Development

Get the source with git:

git clone https://github.com/garbados/cubesat-db
cd cubesat-db
npm i
npm test

Contributions

All contributions are welcome: bug reports, feature requests, "why doesn't this work" questions, patches for fixes and features, etc. For all of the above, file an issue or submit a pull request.

License

Apache-2.0