Skip to content

Instantly share code, notes, and snippets.

@zbabtkis
Created December 17, 2013 16:47
Show Gist options
  • Save zbabtkis/8008126 to your computer and use it in GitHub Desktop.
Save zbabtkis/8008126 to your computer and use it in GitHub Desktop.
Composition based database tool.
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);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment