Code Monkey home page Code Monkey logo

interface-datastore's Introduction

interface-datastore

standard-readme compliant Build Status Circle CI Coverage Status Dependency Status js-standard-style

Implementation of the datastore interface in JavaScript

Table of Contents

Implementations

If you want the same functionality as go-ds-flatfs, use sharding with fs.

const FsStore = require('datastore-fs')
const ShardingStore = require('datastore-core').ShardingDatatstore
const NextToLast = require('datastore-core').shard.NextToLast

const fs = new FsStore('path/to/store')
ShardingStore.createOrOpen(fs, new NextToLast(2), (err, flatfs) => {
  // flatfs now works like go-flatfs
})

Install

$ npm install interface-datastore

Usage

Wrapping Stores

const MemoryStore = require('interface-datastore').MemoryDatastore
const MountStore = require('datastore-core').MountDatastore
const Key = require('interface-datastore').Key

const store = new MountStore({prefix: new Key('/a'), datastore: new MemoryStore()})

Testsuite

Available under src/tests.js

describe('mystore', () => {
  require('interface-datastore/src/tests')({
    setup (callback) {
      callback(null, instanceOfMyStore)
    },
    teardown (callback) {
      // cleanup resources
      callback()
    }
  })
})

API

Keys

To allow a better abstraction on how to address values, there is a Key class which is used as identifier. It's easy to create a key from a Buffer or a string.

const a = new Key('a')
const b = new Key(new Buffer('hello'))

The key scheme is inspired by file systems and Google App Engine key model. Keys are meant to be unique across a system. They are typical hierarchical, incorporating more and more specific namespaces. Thus keys can be deemed 'children' or 'ancestors' of other keys:

  • new Key('/Comedy')
  • new Key('/Comedy/MontyPython')

Also, every namespace can be parametrized to embed relevant object information. For example, the Key name (most specific namespace) could include the object type:

  • new Key('/Comedy/MontyPython/Actor:JohnCleese')
  • new Key('/Comedy/MontyPython/Sketch:CheeseShop')
  • new Key('/Comedy/MontyPython/Sketch:CheeseShop/Character:Mousebender')

Methods

The exact types can be found in src/index.js.

These methods will be present on every datastore. Key always means an instance of the above mentioned Key type. Every datastore is generic over the Value type, though currently all backing implementations are implemented only for Buffer.

put(key, value, callback)

  • key: Key
  • value: Value
  • callback: function(Error)

Store a value with the given key.

store.put(new Key('awesome'), new Buffer('datastores'), (err) => {
  if (err) {
    throw err
  }
  console.log('put content')
})

get(key, callback)

  • key: Key
  • callback: function(Error, Value)

Retrieve the value stored under the given key.

store.get(new Key('awesome'), (err, value) => {
  if (err) {
    throw err
  }
  console.log('got content: %s', value.toString())
  // => got content: datastore
})

delete(key, callback)

  • key: Key
  • callback: function(Error)

Delete the content stored under the given key.

store.delete(new Key('awesome'), (err) => {
  if (err) {
    throw err
  }
  console.log('deleted awesome content :(')
})

query(query)

  • query: Query see below for possible values
  • Returns: pull-stream source

Search the store for some values. Returns a pull-stream with each item being a Value.

// retrieve __all__ values from the store
pull(
  store.query({}),
  pull.collect((err, list) => {
    if (err) {
      console.error(err)
    }
    console.log('ALL THE VALUES', list)
  })
)

Query

Object in the form with the following optional properties

  • prefix: string (optional) - only return values where the key starts with this prefix
  • filters: Array<Filter<Value>> (optional) - filter the results according to the these functions
  • orders: Array<Order<Value>> (optional) - order the results according to these functions
  • limit: number (optional) - only return this many records
  • offset: number (optional) - skip this many records at the beginning
  • keysOnly: bool (optional) - Only return keys, no values.

batch()

This will return an object with which you can chain multiple operations together, with them only being executed on calling commit.

const b = store.batch()

for (let i = 0; i < 100; i++) {
  b.put(new Key(`hello${i}`), new Buffer(`hello world ${i}`))
}

b.commit((err) => {
  if (err) {
    throw err
  }
  console.log('put 100 values')
})

put(key, value)

  • key: Key
  • value: Value

Queue a put operation to the store.

delete(key)

  • key: Key

Queue a delete operation to the store.

commit(callback)

  • callback: function(Error)

Write all queued operations to the underyling store. The batch object should not be used after calling this.

open(callback)

  • callback: function(Error)

Opens the datastore, this is only needed if the store was closed before, otherwise this is taken care of by the constructor.

close(callback)

  • callback: function(Error)

Close the datastore, this should always be called to ensure resources are cleaned up.

Contribute

PRs accepted.

Small note: If editing the Readme, please conform to the standard-readme specification.

License

MIT 2017 © IPFS

interface-datastore's People

Contributors

daviddias avatar dignifiedquire avatar jbenet avatar download13 avatar

Watchers

James Cloos 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.