bredele / datastore

:hamster: Bloat free and flexible interface for data store and database access.
101 stars 9 forks source link

Get all data #30

Closed hendrikcech closed 10 years ago

hendrikcech commented 10 years ago

I may be missing the obvious, but how do I get all the data stored in a datastore object as an array? I would like to iterate over the items with forEach, map and so on.

bredele commented 10 years ago

@hendrikcech you can initialize (or reset) a store with an array:

var store = new Store(['foo']);
store.get(0);
// =>  foo

you can loop through the store as following:

store.loop(function(idx, value){
  // do something
});

store supports all the things in common between objects and arrays. By default you can't map or filter a store but I'm doing right now a plugin to extend a store with array-like features. Here's an example:

// create plugin that adds map to a store
store.use(function(ctx) {
  ctx.map = function(fn) {
    ctx.loop(function(idx, value) {
      ctx.set(idx, fn(value));
    });
  };
});
hendrikcech commented 10 years ago

Thanks! You may want to add the loop function to the README.

bredele commented 10 years ago

@hendrikcech yes I will. Do you like the project? Do you think some things are missing?