Coder Social home page Coder Social logo

derived's Introduction

Objects with derived indexes!

Build Status NPM Link

What does it do?

It creates automatic, synchronous and always-updated "views" for your data. You instantiate a main data object and define functions for creating derived indexes on it. Later you can easily access the data on the derived indexes.

It reduces bloat, verbose value getters and automatizes data organization in the context of your app. Perhaps others would call this "a database". This name would be misleading, but the power of automatic synchronous subindexes cannot be overstated.

Example

(view and fiddle with this example on Tonic)

const assert = require('chai').assert
const D = require('derived')

/* instantiate your data fundamentals -- or just begin from zero with `new D()` */
const data = new D({
  '#1': {
    name: 'shangri-la',
    zipcode: 27498,
    inhabitants: [{
      name: 'catherine',
      birthday: '11-17'
    }]
  },
  '#2': {
    name: 'el dorado',
    zipcode: 69712,
    inhabitants: [{
      name: 'pizarro',
      birthday: '04-01'
    }]
  },
  '#3': {
    name: 'atlantis',
    zipcode: 18315,
    inhabitants: [{
      name: 'plato',
      birthday: '07-18'
    }]
  },
  '#4': {
    name: 'avalon',
    zipcode: 37851
  }
})

/* reindex the data on the go, for much faster and easy access later */
data.derived('cityId', (k, v) => [v.name, k])

/* now you can access it easily, no need for costly searches on independent index management */
assert.equal(data.cityId['shangri-la'], '#1')

/* add data using super useful methods (you can't use normal methods, it's a Javascript limitation) */
let cityId = data.cityId
data.push([cityId.avalon, 'inhabitants'], {name: 'arthur', birthday: '12-30'})
data.push([cityId.avalon, 'inhabitants'], {name: 'morgana', birthday: '12-31'})

/* create multiple derived indexes */
data.derived('personZip', function (cityId, cityData) {
  if (cityData.inhabitants) {
    cityData.inhabitants.forEach(person => {
      this.emit(person.name, cityData.zipcode)
    })
  }
})

/* no more `var personZip = zipcodesIndex[personsIndex[personId]]` or other verbose tricks
   access your data directly */
let personzip = data.personZip
assert.equal(personzip['catherine'], 27498)
assert.equal(personzip['arthur'], 37851)
assert.equal(personzip['morgana'], 37851)

/* indexes can be of anything (and they don't need names): */
let birthdaysByMonth = data.derived(function (_, city) {
  if (city.inhabitants) {
    city.inhabitants.forEach(person => {
      let month = parseInt(person.birthday.split('-')[0], 10)
      this.emit(month, person.name)
    })
  }
})
assert.deepEqual(birthdaysByMonth.getAll(12), ['arthur', 'morgana'])

/* everything changes synchronously and automatically whenever you update the main data source */
data.set(`${cityId['atlantis']}.inhabitants.0.birthday`, '01-01')
assert.equal(birthdaysByMonth[1], 'plato')

Other

  • The derivation functions are inspired by CouchDB. Not equal, though, but the emit thing... well, it doesn't matter.
  • These getters and setters only work on the main data source, the data in derived indexes is read-only and can be accessed through normal Javascript syntax.
  • Other features are planned (please say what would you find more useful):
    • immutable main data source
    • immutable derived indexes
    • queryable derived indexes, returning arrays of values sorted by emitted keys
    • derived indexes based on the entire data object, instead of in each of its keys
    • derived indexes on sublevels

derived's People

Contributors

fiatjaf avatar

Stargazers

Jason Holloway avatar H avatar  avatar stagas avatar Nikolay Kolev avatar

Watchers

James Cloos avatar  avatar Jason Holloway 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.