This repository has been archived by the owner on Dec 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbased.min.js
1 lines (1 loc) · 5.17 KB
/
based.min.js
1
"use strict";var db;!function(){function validateEntry(item,_invoker){(item instanceof Object==!1||item instanceof Array==!0)&&error(1002,"Item format is invalid");for(var key in item)-1==this.columnNames.indexOf(key)&&error(1002,"Table does not support this entry ["+key+"]");for(var key in this.columns)this.columns[key].primaryKey&&this.primaryKeys.hasOwnProperty(item[key])&&error(1002,"An item with the same primary key has already been added"),_invoker!==invoker&&this.columns[key].id&&void 0!==item[key]&&error(1002,"Column ["+key+"] cannot be set as it is an ID column"),this.columns[key].required&&isUndefinedOrNull(item[key])&&error(1002,"["+key+"] is a required field"),isUndefinedOrNull(item[key])||typeof item[key]==typeof this.columns[key].type()||this.columns[key].type==Object||error(1002,"["+key+"] was an unexpected type")}function localStorageSize(){var size=0;for(var key in localStorage)size+=encodeURI(localStorage[key]).split(/%..|./).length-1;return(size/1024).toFixed(2)+" KB"}function isUndefinedOrNull(obj){return void 0===obj||null===obj?!0:!1}function clone(obj,deep){"object"!=typeof obj&&error(1,"Can only clone objects and/or arrays");var result=obj instanceof Array?[]:{};for(var key in obj)result[key]="object"==typeof obj[key]&&deep===!0?clone(obj[key],deep):obj[key];return result}function where(obj,query,value,_invoker){obj instanceof Object==!1&&error(2001,"Can only query objects"),query instanceof Object||"string"==typeof query||error(2001,"Query is invalid");var q,match=!1,result=new TableResult;"string"==typeof query&&(q={},q[query]=value,query=q);for(var item in obj){match=!0;for(var q in query)obj[item][q]!=query[q]&&(match=!1);match&&result.push(_invoker===invoker?obj[item]:clone(obj[item],!0))}return result}var invoker={},errors={0:"Generic Error",1:"System Error",1001:"Table creation error",1002:"Table modification error",2001:"Filter Error"},error=function(code,message){var e=errors[code.toString()];if(void 0===e)throw error(1,"Error code not found");throw"["+code+"] "+e+" - "+message},DatabaseTable=function(name,columns){"string"==typeof name&&columns instanceof Object||error(1001,"Format is invalid");var id=0;this.name=name,this.items=[],this.primaryKeys=[],this.index={},this.columns={},this.columnNames=[],this.size="0 KB",this.getId=function(){return arguments[0]===invoker?id:void 0},this.getNextId=function(){return arguments[0]===invoker?id++:void 0},this.setId=function(_id){arguments[1]===invoker&&(id=_id)},where(columns,{id:!0}).length>1&&error(1001,"You can only have one id column"),where(columns,{primaryKey:!0}).length>1&&error(1001,"You can only have one primary key column");for(var key in columns)this.columns[key]={id:columns[key].id===!0,primaryKey:columns[key].primaryKey===!0,required:columns[key].primaryKey===!0||columns[key].required===!0,type:columns[key].hasOwnProperty("type")?columns[key].type:Object,name:key},this.columnNames.push(key);this.sync(!0)};DatabaseTable.prototype={add:function(item,_invoker){validateEntry.bind(this)(item,_invoker);for(var key in this.columns)if(this.columns[key].primaryKey&&(this.primaryKeys[item[key]]=0),_invoker===invoker)this.columns[key].id&&(this.index[item[key].toString()]=item);else if(this.columns[key].id){var id=this.getNextId(invoker);item[key]=id,this.index[id.toString()]=item}return this.items.push(item),clone(item,!0)},get:function(query,value,_invoker){if("number"==typeof query){var idCol=where(this.columns,{id:!0}).first();if(idCol){var result=new TableResult;return result.push(_invoker===invoker?this.index[query.toString()]:clone(this.index[query.toString()],!0)),result}}return where(this.items,query,value,_invoker)},first:function(query,value){return this.get(query,value).first()},all:function(){for(var result=new TableResult,itemClone=clone(this.items,!0),i=0;i<itemClone.length;i++)result.push(itemClone[i]);return result},update:function(query,data){var _this=this;this.get(query,null,invoker).forEach(function(item){validateEntry.bind(_this)(data),data=clone(data,!0);for(var key in data)item[key]=data[key]})},"delete":function(query){var _this=this;this.get(query).forEach(function(item){for(var key in _this.columns)if(_this.columns[key].id===!0){delete _this.index[item[key].toString()];break}_this.items.splice(_this.items.indexOf(item),1)})},sync:function(pull){if(pull){var data=localStorage.getItem("table_"+this.name);if(data){data=JSON.parse(data);for(var i=0;i<data.items.length;i++)this.add(data.items[i],invoker);this.setId(data.id,invoker)}}else try{localStorage.setItem("table_"+this.name,JSON.stringify({id:this.getId(invoker),items:this.items}))}catch(e){console.warn("You do not have enough local storage space to sync ["+this.name+"].\nYour local storage is currently using "+localStorageSize())}},unsync:function(){localStorage.removeItem("table_"+this.name)}};var TableResult=function(){};TableResult.prototype=new Array,TableResult.prototype.first=function(){return this[0]},TableResult.prototype.last=function(){return this[this.length>0?this.length-1:0]},TableResult.prototype.forEach=function(callback){"function"!=typeof callback&&error(0,"Missing argument");for(var i=0;i<this.length;i++)callback(this[i],i)},db={create:function(name,columns,options){return new DatabaseTable(name,columns,options)}}}();