Git Product home page Git Product logo

griffindb-js's Introduction

griffin

NOTICE This project never went beyond the idea phase and was never implemented into a working project. The code in this repository does not work.

Griffin is a decentralized, offline-first, key-value based, document-oriented database built on the Sia blockchain.

Users are empowered with the ability to store their own easily accessible encrypted data permanently without relying on centralized providers.

Developers are empowered with a flexible and easy-to-use database that can function as a MongoDB-like query language, key-value store, chain context graph or file storage engine.

Usage

npm i griffindb

yarn add griffindb

openbits install griffindb

import Griffin from "griffin-browser"

const griffin = Griffin("https://siasky.net")
const key = await griffin.gen() // The user's key
const db = await griffin.namespace("my-app", key)

const dogs = db.collection("dogs")

// Insertion
await dogs.insert([
  { name: "Gordon", color: "black", age: 3, owners: ["John", "Cindy"] },
  { name: "Pooch", color: "brown", age: 5, owners: ["John", "Cindy"] },
  { name: "Snuffles", color: "brown", age: 7, owners: ["Karen"] },
  ...
]).many()

// Finds and queries
console.log(await dogs.find({ color: "brown" }).sort({ name: 1 }).one())
console.log(await dogs.find({ name: { $or: ["Gordon", "Pooch"] } }).fields({ _id: 0 }).many())
console.log(await dogs.find({ age: { $lt: 7, $gte: 3 } }).limit(10).many())

// Update/replace
await dogs.update({ age: 5 }, { $inc: { age: 1 } }).one()
await dogs.replace({ name: "Gordon" }, { name: "Gordon Ramsey", color: "blonde", age: 54, owners: null }).one()

// Key-value store
await db.set("username", "gnufag")
await db.get("friends") // returns null in this scenario
await db.remove("typoo")

// Chain context
await db.chain("options").set("skynet-portal", "https://custom-sky.net")
await db.chain("deeply").chain("nested").chain("chains").chain("are").get("cool")

griffindb-js's People

Contributors

agarwalrounak avatar giraffekey avatar jojobyte avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

griffindb-js's Issues

Little push to get up and running

I have managed to get this far:


Jan@JLKM1 MINGW64 /f/mockups/gundb/griffinnode
$ npm install
npm WARN [email protected] requires a peer of bufferutil@^4.0.1 but none is installed. You must install peer dependencies yourself.
npm WARN [email protected] requires a peer of utf-8-validate@^5.0.2 but none is installed. You must install peer dependencies yourself.
npm WARN [email protected] No description
npm WARN [email protected] No repository field.

removed 1 package and audited 402 packages in 3.327s

57 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities


Jan@JLKM1 MINGW64 /f/mockups/gundb/griffinnode
$ node index.js
Hello wonderful person! :) Thanks for using GUN, please ask for help on http://c
hat.gun.eco if anything takes you longer than 5min to figure out!

Jan@JLKM1 MINGW64 /f/mockups/gundb/griffinnode
$

How do I get up and running with this repo. Just a minimum of instruction would come in handy.

Rewrite insert operation

The find operation has been rewritten although I haven't tested if it works yet.

Comparing the old version of find to the new one should provide some insight on what I'm going for with this rewrite.

Using this comparison one could probably figure out how the insert rewrite should look.

Here is the original insert code. All SEA, clean, GUN, etc stuff should be replaced with get_kv and set_kv.

const { v4: uuidv4 } = require("uuid")
const pmap = require("promise.map")
const { clean } = require("./util")

function insert(SEA, col, key, backup, docs, options) {
	return new Promise(async (res, rej) => {
		if (options.one) docs = [docs]
		else if (!Array.isArray(docs)) {
			rej("Documents must be an array")
			return
		}

		const { ordered } = options
		let ids = []
		let promises = []

		for (let i = 0; i < docs.length; i++) {
			let doc = docs[i]

			if (Object.prototype.toString.call(doc) !== "[object Object]") {
				if (ordered) {
					rej(`Document ${i} must be an object`)
					return
				} else {
					console.error(`Document ${i} must be an object`)
					continue
				}
			}

			const id = uuidv4()

			try {
				doc = { _id: id, ...clean(doc) }
				doc = await SEA.encrypt(doc, key)
			} catch(e) {
				if (ordered) {
					rej(e)
					return
				} else {
					console.error(e)
					continue
				}
			}

			if (ordered) {
				try {
					await new Promise((res, rej) => {
						col.get(id).put(doc, ack => {
							if (ack.err) {
								rej(`Failed to insert document ${i}`)
							} else {
								ids.push(id)
								col.once(d => {
									const key = `${d._["#"]}/${id}`
									const data = doc
									backup(key, data)
								})
								res()
							}
						})
					})
				} catch(e) {
					rej(e)
					return
				}
			} else {
				promises.push(new Promise((res, rej) => {
					col.get(id).put(doc, ack => {
						if (ack.err) {
							console.error(`Failed to insert document ${i}`)
						} else {
							ids.push(id)
							col.once(d => {
								const key = `${d._["#"]}/${id}`
								const data = doc
								backup(key, data)
							})
							res()
						}
					})
				}))
			}
		}

		if (!ordered) await pmap(promises, p => p, 30)

		col.once(d => {
			const key = d._["#"]
			backup(key, d)
		})

		res(ids)
	})
}

/*
 * Search through the entire collection and retrieve those who match the query
 * Options:
 *   ordered - Prevent inserting remaining documents if one insert fails
 */
function Insert(SEA, col, key, backup, docs, options) {
	function ordered(ordered) {
		if (ordered === undefined) ordered = true
		return Insert(SEA, col, key, backup, docs, {
			...options,
			ordered,
		})
	}

	function one() {
		options.one = true
		return insert(SEA, col, key, backup, docs, options)
	}

	function many() {
		options.one = false
		return insert(SEA, col, key, backup, docs, options)
	}

	return {
		ordered,
		one,
		many,
	}
}

module.exports = Insert

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.