Skip to content

Instantly share code, notes, and snippets.

@zbabtkis
Created December 17, 2013 16:47
Show Gist options
  • Select an option

  • Save zbabtkis/8008126 to your computer and use it in GitHub Desktop.

Select an option

Save zbabtkis/8008126 to your computer and use it in GitHub Desktop.

Revisions

  1. Zachary Babtkis created this gist Dec 17, 2013.
    41 changes: 41 additions & 0 deletions CDB.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,41 @@
    var DB = {};

    DB.collections = {};

    var Collection = function(Constructor, IndexInterface) {
    var d = []
    , i = new IndexInterface;

    return {

    add: function(data) {
    d.push(new Constructor(data));
    i.index(d.length - 1, data);
    },

    get: function(query) {
    return d[i.atIndex(query)];
    }

    };
    };

    var IdIndexer = function() {
    this.indexes = {};
    };

    IdIndexer.prototype.atIndex = function(query, col) {
    if(typeof query === 'number' ? query : query.id) {
    return this.indexes[query || query.id];
    } else {
    atIndex(query);
    }
    };

    IdIndexer.prototype.index = function(ind, data) {
    if(data.id) {
    this.indexes[data.id] = ind;
    } else {
    throw new Error("No id to index for data", data);
    }
    };