Skip to content

Instantly share code, notes, and snippets.

@lucnat
Created September 6, 2018 12:09
Show Gist options
  • Save lucnat/ffb87bf1f1224f2213760c7fb8444aaf to your computer and use it in GitHub Desktop.
Save lucnat/ffb87bf1f1224f2213760c7fb8444aaf to your computer and use it in GitHub Desktop.
Stone Collection: An extremely simple persistent collection - written in stone.
/*
usage:
Tables = new Stone.Collection('tables'); // create a stone collection
Profile = new Stone.Singleton(''); // create a stone singleton
------- shared API -------
get() returns all in DB
set(obj) sets entire DB to obj
update(modifier) modify the entry
------- API for collections only ------
find(selector) find documents matching selector (not yet implemented, currently returns everything)
findOne(id) find one document with id
update(id, obj) updates document with id to obj
insert(obj) inserts new obj to DB
remove(id) removes document with id
*/
var StoneCollection = class {
constructor(label){
// constructs new LucDB. Needs a label passed
if(! label){
alert('You are a fool. You have not provided a label');
return;
}
this.label = label;
this.dependency = new Tracker.Dependency;
if(!localStorage[label]){
localStorage[label] = JSON.stringify([]);
}
}
set(obj){
// Sets entire collection to obj! Often used for singletons
if(!obj._id){
obj._id = Random.id();
}
if(obj.constructor == Array){
obj.forEach(function(o){
if(!o._id){
o._id = Random.id();
}
});
}
localStorage[this.label] = JSON.stringify(obj);
this.dependency.changed();
}
get(){
// Returns everything in collection
this.dependency.depend();
if(localStorage[this.label]){
return JSON.parse(localStorage[this.label]);
}
}
find(selector){
// TODO
// Finds all that match selector. Foolish if it is a singleton
this.dependency.depend();
if(!localStorage[this.label]) return null;
var everything = JSON.parse(localStorage[this.label]);
var notEverything = [];
// filter stuff
notEverything = everything;
return notEverything;
}
findOne(id){
// takes id and returns one document. Foolish if it is a singleton
this.dependency.depend();
var everything = JSON.parse(localStorage[this.label]);
var result;
everything.forEach(function(obj){
if(obj._id == id){
result = obj;
}
});
return result;
}
update(id, obj){
// updates the one with id to new one. Foolish if singleton
obj._id = id;
var everything = JSON.parse(localStorage[this.label]);
for(var i=0; i < everything.length; i++){
if(everything[i]._id == id){
everything[i] = obj;
}
}
this.set(everything);
}
insert(obj){
// Inserts something into collection. Foolish if collection is a singleton
var before = this.get();
if(! before.push){
alert('You fool! This is a singleton! You cannot push objects into this');
return;
}
obj._id = Random.id();
before.push(obj);
var after = before;
this.set(after);
}
remove(id){
// removes doc with _id: id
var tables = this.get();
for(var i=0; i < tables.length; i++){
if(tables[i]._id == id){
tables.splice(i,1);
}
}
this.set(tables);
}
count(){
this.dependency.changed();
var everything = JSON.parse(localStorage[this.label]);
return everything.length;
}
}
var StoneSingleton = class {
constructor(label){
// constructs new LucDB. Needs a label passed
if(! label){
alert('You are a fool. You have not provided a label');
return;
}
this.label = label;
this.dependency = new Tracker.Dependency;
if(!localStorage[label]){
localStorage[label] = JSON.stringify({_id: Random.id()});
}
}
update(modifier) {
// updates the singleton with a modifier
const keys = Object.keys(modifier);
let entry = JSON.parse(localStorage[this.label]);
keys.forEach(key => {
const value = modifier[key];
entry[key] = value;
});
this.set(entry);
}
set(obj){
// Sets entire collection to obj!
if(!obj._id){
obj._id = Random.id();
}
if(obj.constructor == Array){
obj.forEach(function(o){
if(!o._id){
o._id = Random.id();
}
});
}
localStorage[this.label] = JSON.stringify(obj);
this.dependency.changed();
}
get(){
// Returns everything in collection
this.dependency.depend();
if(localStorage[this.label]){
return JSON.parse(localStorage[this.label]);
}
}
}
Stone = {
Collection: StoneCollection,
Singleton: StoneSingleton
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment