GithubHelp home page GithubHelp logo

dunk.js's Introduction

Dunk.js

Object database for storing and searching javascript objects in memory

Create the database through its factory method:

var DunkFactory = require('dunkjs');

var DunkConfig = {
    appKey:     'myApp',
    dataCache:  '/tmp/Dunk'
}

var Dunk = DunkFactory(DunkConfig);

By setting this configuration, your database will be cached at /tmp/Dunk/MyApp when you call a "flush" operation.

Use regular javascript objects

Enter the object into a Dunk repository by calling the "persist" method on any object. The second parameter of "persist" can be used to specify the repository to save the object to. If no repository is given, Dunk will use the Object.prototype.constructor.name as the repository.

function Animal() {
    this.ears = 2;
    this.mouth = 1;
    this.tail = 0;
    this.legs = 4;
    this.type = null;
}

Dog.prototype = new Animal();
Dog.prototype.constructor = Dog;
function Dog(){
    this.tail = 1;
}

var testDog = new Dog();
testDog.type = 'German Shepherd';
var testDog2 = new Dog();
testDog2.type = 'Lab';

Dunk.persist(testDog);
Dunk.persist(testDog2, "Dog");

_id meta

Dunk assigns an auto incremented _id field to each object in its repository. Currently only auto-increment integer ids are supported.

Retrieving data

Once data is persisted you can continue to

var dogs = Dunk.getRepository('Dog').findAll();

var twoEaredOneTailedDogs = Dunk.getRepository('Dog').findBy({'ears':2, 'tail': 1});

var dog = Dunk.getRepository('Dog').findOneBy({'type': 'Lab'})

Persisted objects update automatically

With Dunk, you should only have to persist an object once. Dunk uses an observer pattern which monitors persisted objects for changes. When an object change is detected, dunk will update its indexes, accordingly, in asynchronous fashion.

var dog = Dunk.getRepository('Dog').findOneBy({'type': 'German Shepherd'});
dog.name = "Shep";

var dogs = Dunk.getRepository('Dog').findAll();

console.log(dogs);

output follows:

jgreathouse-mbp:dunk.js jgreathouse$ node test.js
[ { tail: 1, type: 'German Shepherd', _id: 1, name: 'Shep' },
  { tail: 1, type: 'Lab', _id: 2 } ]

Deleting an object

Deleting an object is really as simple as this:

var dogRepo = Dunk.getRepository('Dog');
var dog = dogRepo.findOneBy({'type': 'German Shepherd'});
dogRepo.delete(dog);

Caching

By calling Dunk.flush() every current repository, in memory, will be saved to the disk at the location you specify in the configuration. Flush is an expensive operation, but it should execute asynchronously so it won't interrupt the flow of your application. When dunk is initialized it will try to load any saved repositories into memory.

Dunk.flush();

dunk.js's People

Stargazers

Jake A. Stevenson avatar Jesse Greathouse avatar

Watchers

Jesse Greathouse avatar James Cloos 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.