Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

testing all the storage strategies

  • Loading branch information...
commit 6ccdf3aa5ce0f5cb504a33d3683661b8da87d1fa 1 parent 1333acc
Pedro Teixeira authored
13 dist/dribbledb.js
View
@@ -15,7 +15,7 @@
* limitations under the license.
*
* VERSION: 0.1.0
- * BUILD DATE: Sun Dec 4 15:34:20 2011 +0000
+ * BUILD DATE: Sun Dec 4 18:06:43 2011 +0000
*/
(function() {
@@ -1435,12 +1435,16 @@ function store_strategy_memstore(base_url) {
if (i >= keys.length) { return done(); }
function next() {
- i ++;
+ i += 1;
iterate();
}
key = keys[i];
- cb(key.slice(path.length + 1), mem_get(key), next);
+ key = key.slice(path.length + 1);
+ mem_get(prefix, key, function(err, val) {
+ if (err) { return done(err); }
+ cb(key, val, next);
+ })
}());
}
@@ -1526,6 +1530,7 @@ function store_strategy_memstore(base_url) {
change = results[i];
key = change.id || change._id;
theirs = change.doc;
+ theirs._id = key;
store.meta.get(key, function(err, metaVal) {
if (err) { return cb(err); }
if (metaVal) {
@@ -1588,7 +1593,7 @@ function store_strategy_memstore(base_url) {
, uri = base_url + '/' + key;
method = op === 'p' ? 'put' : (op === 'd' ? 'del' : undefined);
- if (! method) { throw new Error('Invalid meta action: ' + value); }
+ if (! method) { throw new Error('Invalid meta action: ' + JSON.stringify(value)); }
if (rev) { uri += '?rev=' + rev; }
get(key, function(err, mine) {
4 dist/dribbledb.min.js
View
@@ -15,5 +15,5 @@
* limitations under the license.
*
* VERSION: 0.1.0
- * BUILD DATE: Fri Nov 25 11:04:26 2011 +0000
- */(function(){function b(){this.callbacks={}}function d(){}function e(a,b,d,e){"function"==typeof arguments[2]&&(e=d,d=undefined),c[a](b,d).type("application/javascript").expectResponseType("json").end(e)}function f(a,b){e("get",a,undefined,b)}function k(a,c){function i(a,b){var c=a;return b!==undefined&&(c+="/"+b),c}function n(a){return function(b){function d(a,b){return c.get(k,a,b)}function e(a,b,d){return c.put(k,a,b,d)}function f(a,b){return c.destroy(k,a,b)}function g(a,b){return c.get(l,a,b)}function h(a,b,d){return c.put(l,a,b,d)}function i(a,b){return c.destroy(l,a,b)}function j(a,b){return c.all_keys_iterator(k,a,b)}function n(a){return c.all_keys(k,a)}function o(a,b){return c.all_keys_iterator(l,a,b)}function p(a){return c.all_keys(l,a)}function q(a,b){if("function"!=typeof b)throw new Error("2nd argument must be a function");if(!a)c.get(m,undefined,function(a,c){if(a)return b(a);b(null,c||0)});else return c.put(m,undefined,a,b)}var c=a(b);return{stratName:c.stratName,doc:{get:d,put:e,destroy:f,all_keys_iterator:j,all_keys:n},meta:{get:g,put:h,destroy:i,all_keys_iterator:o,all_keys:p},pulled_since:q}}}function o(a){function f(a,b){e[a].get(b)}function g(a,b,c){e[a].add(c,b)}function h(a,b){e[a].delete(b)}function i(a,b,c){function f(){cursor.continue()?(d=cursor.value,b(d.id,d,f)):c()}var d;e[a].openCursor()}function k(a){var b=[];return browser_all_keys_iterator(a,function(a,c,d){b.push(a),d()}),b}var b=window.indexedDB||window.webkitIndexedDB||window.mozIndexedDB||window.msIndexedDB,c=b.open(j),d=["d","m","s"],e={};return function(){var b;for(var f in d)b=d[f],e[b]=c.createObjectStore(b+":"+a)}(),{get:f,put:g,destroy:h,all_keys_iterator:i,all_keys:k,stratName:"idbstore"}}function p(a,b,c){function e(b,c){if(b.length!=1)throw new Error("Invalid prefix: "+b);var d=j+":"+b+":"+a;return"undefined"!=typeof c&&(d+="/"+c),d}function f(a,c,d){if("function"!=typeof d)throw new Error("3rd argument must be a function");var f=b.getItem(e(a,c));d(null,JSON.parse(f))}function g(a,c,d,f){if("function"!=typeof f)throw new Error("3rd argument must be a function");var g=e(a,c),h=JSON.stringify(d);b.setItem(g,h),f()}function h(a,c,d){if("function"!=typeof d)throw new Error("3rd argument must be a function");var f=e(a,c);return null!==b.getItem(f)?(b.removeItem(f),d(null,!0)):d(null,!1)}function i(a,c,g){var h=b,i,j=0,k=e(a);g=g||d,i=function(){var a,b,c=[];for(b=0;b<h.length;b++)a=h.key(b),a&&0===a.indexOf(k)&&c.push(a);return c}(),function l(){function e(){j++,l()}var b,d;if(j<i.length)b=i[j],d=b.slice(k.length+1),f(a,d,function(a,b){if(a)return g(a);c(d,b,e)});else return g()}()}function k(a,b){var c=[];i(a,function(a,b,d){c.push(a),d()},function(a){if(a)return b(a);b(null,c)})}if(!b)throw new Error("At the moment this only works in modern browsers");return{get:f,put:g,destroy:h,all_keys_iterator:i,all_keys:k,stratName:c}}function q(a){return p(a,h.localStorage,"localstore")}function r(a){return p(a,h.sessionStorage,"sessionstore")}function s(a){function c(b,c){if(b.length>1)throw new Error("Invalid prefix: "+b);var d=j+":"+b+":"+a;return"undefined"!=typeof c&&(d+="/"+c),d}function e(a){return JSON.parse(JSON.stringify(a))}function f(a,d,f){var g=b[c(a,d)],h=null;"undefined"!=typeof g&&(h=e(g)),f(null,h)}function g(a,d,f,g){b[c(a,d)]=e(f),g(null,d)}function h(a,d,e){delete b[c(a,d)],e(null)}function i(a,e,g){var h=b,i,j=0,k=c(a);g=g||d,i=function(){var a,c=[];for(a in b)a&&0===a.indexOf(k)&&c.push(a);return c}(),function l(){function b(){j++,l()}var a;if(j<i.length)a=i[j],e(a.slice(k.length+1),f(a),b);else return g()}()}function k(a,b){var c=[];i(a,function(a,b,d){c.push(a),d()}),b(null,c)}var b={};return{get:f,put:g,destroy:h,all_keys_iterator:i,all_keys:k,stratName:"memstore"}}function t(a){var b={idbstore:o,localstore:q,sessionstore:r,memstore:s};return n(b[a])}function u(a){var b;if("function"==typeof a)b=a;else switch(a){case"couchdb_bulk":b=v;break;default:throw new Error("Unknown pull strategy: "+a)}return b}function v(){return function(b,c){var d=!1;F(function(d,e){if(d)return c(d);var g=a+"/_changes?since="+e+"&include_docs=true";f(g,function(a,d){var e,f,h,i,j,k,l,m;if(a)return c(a);if(!d.ok)return c(new Error("Pull response not ok for URI: "+g));if(!d.body)return c(new Error("Pull response does not have body for URI: "+g));f=d.body;if("object"!=typeof f)return c(new Error("Pull response body is not object for URI: "+g));if(!f.hasOwnProperty("last_seq"))return l=new Error("response body does not have .last_seq: "+g),l.body=f,c(l);if(!f.hasOwnProperty("results"))return l=new Error("response body does not have .results: "+g),l.body=f,c(l);h=f.results,e=-1,function n(){e+=1,e<h.length?(i=h[e],j=i.id,k=i.doc,A.meta.get(j,function(a,d){if(a)return c(a);if(d)if(b)A.doc_get(j,function(a,d){if(a)return c(a);b(d,k,function(a){H(j,a,c),n()})});else return l=new Error("Conflict"),l.key=j,l.mine=m,l.theirs=k,c(l);else i.deleted?L(j):I(j,k,function(a){if(a)return c(a);n()})})):F(f.last_seq,c)}()})})}}function w(a){var b;if("function"==typeof a)b=a;else switch(a){case"restful_ajax":b=x;break;default:throw new Error("Unknown push strategy: "+a)}return b}function x(){return function(b,c){function g(d,h,i){var j,k=h.charAt(0),l=h.substr(1),m=a+"/"+d;j=k==="p"?"put":k==="d"?"del":undefined;if(!j)throw new Error("Invalid meta action: "+h);l&&(m+="?rev="+l),J(d,function(a,k){function l(a,e){if(a)return c(a);if(e.conflict)f(m,function(a,e){if(a)return c(a);if(b)b(k,e.body,function(a){H(d,a),g(d,h,i)});else return a=new Error("Conflict"),a.key=d,a.mine=k,a.theirs=e.body,c(a)});else{if(("del"!==j||!e.notFound)&&!e.ok)return c(new Error(j+" "+m+" failed with response status "+e.status+": "+e.text));A.meta.destroy(d,function(a){if(a)return c(a);i()})}}e(j,m,k,l)})}var d=!1;E(g,c)}}function D(a){return A.meta.all_keys(a)}function E(a,b){A.meta.all_keys_iterator(a,b)}function F(a,b){return arguments.length<2&&(b=a,a=undefined),A.pulled_since(a,b)}function G(a){a&&y.emit("error",a)}function H(a,b,c){arguments.length<3&&("function"==typeof b?(c=b,b=a,a=b.id||b._id||g()):c=G);if(!b.id||b._id)b._id=a;return A.doc.put(a,b,function(b){if(b)return c(b);A.meta.put(a,"p",function(b){if(b)return c(b);c(null,a)})}),a}function I(a,b,c){return c||(c=G),A.doc.put(a,b,c)}function J(a,b){return b||(b=G),A.doc.get(a,b)}function K(a,b){var c="d";J(a,function(d,e){if(d)return b(d);e&&e._rev&&(c+=e._rev),A.doc.destroy(a,function(d,e){if(d)return b(d);e?A.meta.put(a,c,b):b()})})}function L(a,b){A.doc.destroy(a,b)}function M(a){function c(a,c,d){b.push(c),d()}function d(c){if(c)return a(c);a(null,b)}var b=[];A.doc.all_keys_iterator(c,d)}function N(a){(function(b){A.doc.all_keys(function(c,d){if(c)return a(c);var e,f=-1;(function g(){f+=1;if(f<d.length)e=d[f],A.doc.destroy(e,function(b){if(b)return a(b);g()});else return b()})()})})(function(){A.meta.all_keys(function(b,c){if(b)return a(b);var d,e=-1;(function f(){e+=1;if(e<c.length)d=c[e],A.meta.destroy(d,function(b){if(b)return a(b);f()});else return a()})()})})}function O(a,b){B(a,b)}function P(a,b){C(a,b)}var k="d",l="m",m="s",y=new b,z,A,B,C;return c=c||{},c.storage_strategy||(c.storage_strategy="localstore"),A=t(c.storage_strategy)(a),c.pull_strategy||(c.pull_strategy="couchdb_bulk"),B=u(c.pull_strategy)(),c.push_strategy||(c.push_strategy="restful_ajax"),C=w(c.push_strategy)(),z=function(){function c(b,c){function e(){return!d&&typeof c=="function"?(d=!0,c.apply(y,arguments),!0):!1}function f(b){b&&!e(b)&&a.emit("error",b)}var d=!1;arguments.length<2&&(c=b,b=undefined),P(b,function(a){if(a)return f(a);O(b,function(a){if(a)return f(a);e()})})}var a=new b;return c.on=function(){a.on.apply(a,arguments)},c}(),y.storageStrategy=A.stratName,y.sync=z,y.put=H,y.get=J,y.destroy=K,y.unsynced_keys=D,y.all=M,y.nuke=N,y}var a=[].slice;b.prototype.on=function(a,b){return(this.callbacks[a]=this.callbacks[a]||[]).push(b),this},b.prototype.emit=function(b){var c=a.call(arguments,1),d=this.callbacks[b],e,f;if(d&&d.length>0)for(e=0,f=d.length;e<f;++e)d[e](c);else if("error"===b)throw c[0]||new Error("Unspecified error");return this};var c=function(a){function d(){if(window.XMLHttpRequest&&("file:"!==window.location.protocol||!window.ActiveXObject))return new XMLHttpRequest;try{return new ActiveXObject("Microsoft.XMLHTTP")}catch(a){}try{return new ActiveXObject("Msxml2.XMLHTTP.6.0")}catch(b){}try{return new ActiveXObject("Msxml2.XMLHTTP.3.0")}catch(c){}try{return new ActiveXObject("Msxml2.XMLHTTP")}catch(d){}return!1}function f(a){return"function"==typeof a}function g(a){if(null===a||undefined==a)return!1;var b=a.constructor;return b&&Object===b}function h(a){var b;if(!g(a))return a;var c=[];for(b in a)a.hasOwnProperty(b)&&c.push(encodeURIComponent(b)+"="+encodeURIComponent(a[b]));return c.join("&")}function i(a){var b={},c=a.split("&"),d,e,f,g;for(f=0,g=c.length;f<g;++f)e=c[f],d=e.split("="),b[decodeURIComponent(d[0])]=decodeURIComponent(d[1]);return b}function j(a){var b=a.split(/\r?\n/),c={},d,f,g,h,i,j;b.pop();for(i=0,j=b.length;i<j;++i)f=b[i],d=f.indexOf(":"),g=f.slice(0,d).toLowerCase(),h=e(f.slice(d+1)),c[g]=h;return c}function k(a,b){b=b||{},this.options=b,this.xhr=a,this.text=a.responseText,this.setStatusProperties(a.status),this.header=j(a.getAllResponseHeaders()),this.setHeaderProperties(this.header),this.body=this.parseBody(this.text)}function l(a,c){var d=this;b.call(this),this.method=a,this.url=c,this.header={},this.set("X-Requested-With","XMLHttpRequest"),this.on("end",function(){var a=new k(d.xhr,{expectResponseType:d._expectResponseType}),b;a.status===0&&(b=new Error("Unknown XHR Error")),d.callback(b,a)})}function m(a,b){return"function"==typeof b?(new l("GET",a)).end(b):1===arguments.length?new l("GET",a):new l(a,b)}a=m,a.version="0.1.1";var c=function(){},e="".trim?function(a){return a.trim()}:function(a){return a.replace(/(^\s*|\s*$)/g,"")};return a.serializeObject=h,a.parseString=i,a.types={html:"text/html",json:"application/json",urlencoded:"application/x-www-form-urlencoded","form-data":"application/x-www-form-urlencoded"},a.serialize={"application/x-www-form-urlencoded":h,"application/json":JSON.stringify},a.parse={"application/x-www-form-urlencoded":i,"application/json":JSON.parse},k.prototype.setHeaderProperties=function(a){var b=(this.header["content-type"]||"").split(/ *; */);this.contentType=b.shift(),this.setParams(b)},k.prototype.setParams=function(a){var b,c,d;for(c=0,d=a.length;c<d;++c)b=a[c].split(/ *= */),this[b[0]]=b[1]},k.prototype.parseBody=function(b){var c=a.parse[this.ok&&this.options.expectResponseType||this.contentType];return c?c(b):null},k.prototype.setStatusProperties=function(a){var b=a/100|0;this.status=a,this.statusType=b,this.info=1===b,this.ok=2===b,this.clientError=4===b,this.serverError=5===b,this.error=4===b||5===b,this.accepted=202===a,this.noContent=204===a||1223===a,this.badRequest=400===a,this.unauthorized=401===a,this.notFound=404===a,this.notAcceptable=406===a,this.conflict=409===a},a.Response=k,l.prototype=new b,l.prototype.constructor=l,l.prototype.set=function(a,b){var c;if(g(a)){for(c in a)a.hasOwnProperty(c)&&this.set(c,a[c]);return this}return this.header[a.toLowerCase()]=b,this},l.prototype.expectResponseType=function(b){return this._expectResponseType=a.types[b]||b,this},l.prototype.type=function(b){return this.set("Content-Type",a.types[b]||b),this},l.prototype.data=function(a){var b=g(a),c;if(b&&g(this._data))for(c in a)a.hasOwnProperty(c)&&(this._data[c]=a[c]);else this._data=a;return"GET"===this.method?this:b?this.header["content-type"]?this:(this.type("json"),this):this},l.prototype.send=function(a,b){return f(a)?this.end(a):a?this.data(a).end(b):this.end(),this},l.prototype.end=function(b){var e=this,f=this.xhr=d(),g=this._data||null,h;this.callback=b||c,f.onreadystatechange=function(){4===f.readyState&&e.emit("end")},"GET"===this.method&&null!==g&&(this.url+="?"+a.serializeObject(g),g=null),f.open(this.method,this.url,!0);if("GET"!==this.method&&"HEAD"!==this.method){var i=a.serialize[this.header["content-type"]];i&&(g=i(g))}for(h in this.header)this.header.hasOwnProperty(h)&&f.setRequestHeader(h,this.header[h]);return f.send(g),this},a.Request=l,m.get=function(a,b,c){var d=m("GET",a);return f(b)&&(c=b,b=null),b&&d.data(b),c&&d.end(c),d},m.del=function(a,b){var c=m("DELETE",a);return b&&c.end(b),c},m.post=function(a,b,c){var d=m("POST",a);return b&&d.data(b),c&&d.end(c),d},m.put=function(a,b,c){var d=m("PUT",a);return b&&d.data(b),c&&d.end(c),d},a}({}),g=function(){function h(a){var b=new c(16),d=0;return a.toLowerCase().replace(/[0-9a-f][0-9a-f]/g,function(a){b[d++]=f[a]}),b}function i(a){var b=e,c=a;return b[c[0]]+b[c[1]]+b[c[2]]+b[c[3]]+"-"+b[c[4]]+b[c[5]]+"-"+b[c[6]]+b[c[7]]+"-"+b[c[8]]+b[c[9]]+"-"+b[c[10]]+b[c[11]]+b[c[12]]+b[c[13]]+b[c[14]]+b[c[15]]}function q(e,f,g){typeof e=="string"&&(e={format:e}),e=e||{};var h=e.format!="binary"?d:f?f:new c(16),k=f&&g||0,l=(e.timestamp!==undefined?e.timestamp:(new Date).getTime())+a;p=l===o?p+1:0,p=e.count||p;if(l<o||p>b)n++,p=0;o=l;var q=l,r=((q&268435455)*1e4+p)%4294967296,s=q/4294967296*1e4&268435455,t=s&65535,u=s>>16,v=u&4095|4096;n=e.clockseq!==undefined?e.clockseq:n;var w=n&255,x=n>>>8|128;h[k++]=r>>>24&j,h[k++]=r>>>16&j,h[k++]=r>>>8&j,h[k++]=r&j,h[k++]=t>>>8&j,h[k++]=t&j,h[k++]=v>>>8&j,h[k++]=v&j,h[k++]=x,h[k++]=w,m=e.node||m;var y=0;return h[k++]=m[y++],h[k++]=m[y++],h[k++]=m[y++],h[k++]=m[y++],h[k++]=m[y++],h[k++]=m[y++],e.format===undefined?i(h):h}function r(a,b,e){typeof a=="string"&&(a={format:a}),a=a||{};var f=a.format!="binary"?d:b?b:new c(16),g=b&&e||0;a.random?l=a.random:k?crypto.getRandomValues(l):(l[0]=Math.random()*4294967296,l[1]=Math.random()*4294967296,l[2]=Math.random()*4294967296,l[3]=Math.random()*4294967296);var h=l[0];return f[g++]=h&j,f[g++]=h>>>8&j,f[g++]=h>>>16&j,f[g++]=h>>>24&j,h=l[1],f[g++]=h&j,f[g++]=h>>>8&j,f[g++]=h>>>16&15|64,f[g++]=h>>>24&j,h=l[2],f[g++]=h&63|128,f[g++]=h>>>8&j,f[g++]=h>>>16&j,f[g++]=h>>>24&j,h=l[3],f[g++]=h&j,f[g++]=h>>>8&j,f[g++]=h>>>16&j,f[g++]=h>>>24&j,a.format===undefined?i(f):f}var a=122192928e5,b=1e4,c=typeof Buffer=="function"?Buffer:Array,d=new c(16),e=[],f={};for(var g=0;g<256;g++)e[g]=(g+256).toString(16).substr(1),f[e[g]]=g;var j=255,k=this.crypto&&crypto.getRandomValues,l=k?new Uint32Array(4):Array(4);k?crypto.getRandomValues(l):(l[0]=Math.random()*4294967296,l[1]=Math.random()*4294967296,l[2]=Math.random()*4294967296);var m=[l[0]&j|1,l[0]>>>8&j,l[0]>>>16&j,l[0]>>>24&j,l[1]&j,l[1]>>>8&j],n=l[2]&16383,o=0,p=0,s=r;s.v1=q,s.v4=r,s.parse=h,s.unparse=i,s.BufferClass=c;if(typeof module!="undefined")module.exports=s;else return s}().v4,h=this,i=h.dribbledb,j="dbd";k.version="0.1.0","function"==typeof define&&define.amd?define("dribbledb",function(){return k}):h.dribbledb=k,function(){function c(){var c=[],d,e;for(e in a)e=a[e],d=b[e],d()&&c.push(e);return c}var a=["idbstore","localstore","sessionstore","memstore"],b={idbstore:function(){return"undefined"!=typeof (window.indexedDB||window.webkitIndexedDB||window.mozIndexedDB||window.msIndexedDB)},localstore:function(){return typeof window.localStorage!="undefined"},sessionstore:function(){return typeof window.sessionStorage!="undefined"},memstore:function(){return!0}};k.supportedStorageStrategies=c}()})();
+ * BUILD DATE: Sun Dec 4 15:34:20 2011 +0000
+ */(function(){function b(){this.callbacks={}}function d(){}function e(a,b,d,e){"function"==typeof arguments[2]&&(e=d,d=undefined),c[a](b,d).type("application/javascript").expectResponseType("json").end(e)}function f(a,b){e("get",a,undefined,b)}function k(a,c){function i(a,b){var c=a;return b!==undefined&&(c+="/"+b),c}function n(a){return function(b){function d(a,b){return c.get(k,a,b)}function e(a,b,d){return c.put(k,a,b,d)}function f(a,b){return c.destroy(k,a,b)}function g(a,b){return c.get(l,a,b)}function h(a,b,d){return c.put(l,a,b,d)}function i(a,b){return c.destroy(l,a,b)}function j(a,b){return c.all_keys_iterator(k,a,b)}function n(a){return c.all_keys(k,a)}function o(a,b){return c.all_keys_iterator(l,a,b)}function p(a){return c.all_keys(l,a)}function q(a,b){if("function"!=typeof b)throw new Error("2nd argument must be a function");if(!a)c.get(m,"last",function(a,c){if(a)return b(a);b(null,c&&c._id||0)});else return c.put(m,"last",{_id:a},b)}var c=a(b);return{stratName:c.stratName,internalName:c.internalName,doc:{get:d,put:e,destroy:f,all_keys_iterator:j,all_keys:n},meta:{get:g,put:h,destroy:i,all_keys_iterator:o,all_keys:p},pulled_since:q,ready:c.ready}}}function p(a){if(o[a])return o[a];var b=function(){function r(a){var b=a.target.errorCode,c=a.target.webkitErrorMessage||f[b].toLowerCase(),d=new Error(c);return d.code=b,d.event=a,d}function s(a){return function(b){a(r(b))}}function t(a){if(k.version!==b){var c=k.setVersion(b);c.onerror=c.onblocked=s(a),c.onsuccess=function(b){a()}}else a()}function u(b){i=j+"::"+a;var e=c.open(i,"DribbleDB",{keyPath:"_id"});e.onerror=s(b),e.onsuccess=function(a){k=a.target.result,t(function(a,c){if(a)return b(a);var e=-1;(function f(){var a;e+=1;if(e>=l.length)return b();a=l[e];if(k.objectStoreNames.contains(a)){var c=k.transaction([],d.READ_ONLY);m[a]=c.objectStore(a),f()}else m[a]=k.createObjectStore(a,{keyPath:"_id"}),f()})()})}}function v(a){if("function"!=typeof a)throw new Error("onStoreReady needs callabck function");if(n)a();else if(!o)o=!0,u(function(b){o=!1,b&&(p=b),setTimeout(function(){n=!0;for(var b in q)q[b](p);a()},0)});else{if(p)return a(p);q.push(a)}}function w(a,b,c){v(function(e){if(e)return c(e);var f=k.transaction([a],d.READ_ONLY).objectStore(a).get(b);f.onsuccess=function(a){c(null,a.target.result)},f.onerror=s(c)})}function x(a,b,c,e){v(function(f){if(f)return e(f);c._id||(c._id=b);var g=k.transaction([a],d.READ_WRITE).objectStore(a).put(c);g.onsuccess=function(a){e(null,a.target.result)},g.onerror=s(e)})}function y(a,b,c){v(function(e){if(e)return c(e);var f=k.transaction([a],d.READ_WRITE).objectStore(a).delete(b);f.onsuccess=function(a){c(null,!0)},f.onerror=function(a){if(a.target.errorCode===3)return c(null,!1);s(c)}})}function z(a,b,c){v(function(d){A(a,function(d,e){if(d)return b(d);var f=-1;(function g(){var d;f+=1;if(f<e.length)d=e[f],w(a,d,function(a,c){if(a)return b(a);b(d,c,g)});else return c()})()})})}function A(a,b){v(function(c){var e=[],f=k.transaction([a],d.READ_ONLY).objectStore(a).openCursor(undefined,h.NEXT);f.onsuccess=function(a){var c=event.target.result;if(!c)return b(null,e);e.push(c.key),c.continue()},f.onerror=s(b)})}var b="1.2",c=window.indexedDB||window.webkitIndexedDB||window.mozIndexedDB||window.msIndexedDB,d=window.IDBTransaction||window.webkitIDBTransaction||window.msIndexedDB,e=window.IDBDatabaseException||window.webkitIDBDatabaseException,f=Object.keys(e),g=window.IDBKeyRange||window.webkitIDBKeyRange,h=window.IDBCursor||window.webkitIDBCursor,i,k,l=["d","m","s"],m={},n=!1,o=!1,p,q=[];return{get:w,put:x,destroy:y,all_keys_iterator:z,all_keys:A,stratName:"idbstore",ready:v,internalName:i}}();return o[a]=b,b}function q(a,b,c){function e(b,c){if(b.length!=1)throw new Error("Invalid prefix: "+b);var d=j+":"+b+":"+a;return"undefined"!=typeof c&&(d+="/"+c),d}function f(a,c,d){if("function"!=typeof d)throw new Error("3rd argument must be a function");var f=b.getItem(e(a,c));d(null,JSON.parse(f))}function g(a,c,d,f){if("function"!=typeof f)throw new Error("3rd argument must be a function");var g=e(a,c),h=JSON.stringify(d);b.setItem(g,h),f()}function h(a,c,d){if("function"!=typeof d)throw new Error("3rd argument must be a function");var f=e(a,c);return null!==b.getItem(f)?(b.removeItem(f),d(null,!0)):d(null,!1)}function i(a,c,g){var h=b,i,j=0,k=e(a);g=g||d,i=function(){var a,b,c=[];for(b=0;b<h.length;b++)a=h.key(b),a&&0===a.indexOf(k)&&c.push(a);return c}(),function l(){function e(){j++,l()}var b,d;if(j<i.length)b=i[j],d=b.slice(k.length+1),f(a,d,function(a,b){if(a)return g(a);c(d,b,e)});else return g()}()}function k(a,b){var c=[];i(a,function(a,b,d){c.push(a),d()},function(a){if(a)return b(a);b(null,c)})}function l(a){a()}if(!b)throw new Error("At the moment this only works in modern browsers");return{get:f,put:g,destroy:h,all_keys_iterator:i,all_keys:k,stratName:c,ready:l}}function r(a){return q(a,h.localStorage,"localstore")}function s(a){return q(a,h.sessionStorage,"sessionstore")}function t(a){function c(b,c){if(b.length>1)throw new Error("Invalid prefix: "+b);var d=j+":"+b+":"+a;return"undefined"!=typeof c&&(d+="/"+c),d}function e(a){return JSON.parse(JSON.stringify(a))}function f(a,d,f){var g=b[c(a,d)],h=null;"undefined"!=typeof g&&(h=e(g)),f(null,h)}function g(a,d,f,g){b[c(a,d)]=e(f),g(null,d)}function h(a,d,e){delete b[c(a,d)],e(null)}function i(a,e,g){var h=b,i,j=0,k=c(a);g=g||d,i=function(){var a,c=[];for(a in b)a&&0===a.indexOf(k)&&c.push(a);return c}(),function l(){function b(){j++,l()}var a;if(j<i.length)a=i[j],e(a.slice(k.length+1),f(a),b);else return g()}()}function k(a,b){var c=[];i(a,function(a,b,d){c.push(a),d()}),b(null,c)}function l(a){a()}var b={};return{get:f,put:g,destroy:h,all_keys_iterator:i,all_keys:k,ready:l,stratName:"memstore"}}function u(a){var b={idbstore:p,localstore:r,sessionstore:s,memstore:t};return n(b[a])}function v(a){var b;if("function"==typeof a)b=a;else switch(a){case"couchdb_bulk":b=w;break;default:throw new Error("Unknown pull strategy: "+a)}return b}function w(){return function(b,c){var d=!1;H(function(d,e){if(d)return c(d);var g=a+"/_changes?since="+e+"&include_docs=true";f(g,function(a,d){var e,f,h,i,j,k,l,m;if(a)return c(a);if(!d.ok)return c(new Error("Pull response not ok for URI: "+g));if(!d.body)return c(new Error("Pull response does not have body for URI: "+g));f=d.body;if("object"!=typeof f)return c(new Error("Pull response body is not object for URI: "+g));if(!f.hasOwnProperty("last_seq"))return l=new Error("response body does not have .last_seq: "+g),l.body=f,c(l);if(!f.hasOwnProperty("results"))return l=new Error("response body does not have .results: "+g),l.body=f,c(l);h=f.results,e=-1,function n(){e+=1,e<h.length?(i=h[e],j=i.id||i._id,k=i.doc,B.meta.get(j,function(a,d){if(a)return c(a);if(d)if(b)B.doc_get(j,function(a,d){if(a)return c(a);b(d,k,function(a){a._id=j,J(j,a,c),n()})});else return l=new Error("Conflict"),l.key=j,l.mine=m,l.theirs=k,c(l);else i.deleted?N(j):K(j,k,function(a){if(a)return c(a);n()})})):H(f.last_seq,c)}()})})}}function x(a){var b;if("function"==typeof a)b=a;else switch(a){case"restful_ajax":b=y;break;default:throw new Error("Unknown push strategy: "+a)}return b}function y(){return function(b,c){function g(d,h,i){var j,k=h.o,l=h.v,m=a+"/"+d;j=k==="p"?"put":k==="d"?"del":undefined;if(!j)throw new Error("Invalid meta action: "+h);l&&(m+="?rev="+l),L(d,function(a,k){function l(a,e){if(a)return c(a);if(e.conflict)f(m,function(a,e){if(a)return c(a);if(b)b(k,e.body,function(a){J(d,a),g(d,h,i)});else return a=new Error("Conflict"),a.key=d,a.mine=k,a.theirs=e.body,c(a)});else{if(("del"!==j||!e.notFound)&&!e.ok)return c(new Error(j+" "+m+" failed with response status "+e.status+": "+e.text));B.meta.destroy(d,function(a){if(a)return c(a);i()})}}e(j,m,k,l)})}var d=!1;G(g,c)}}function E(a,b){var c={o:a};return b&&(c.v=b),c}function F(a){return B.meta.all_keys(a)}function G(a,b){B.meta.all_keys_iterator(a,b)}function H(a,b){return arguments.length<2&&(b=a,a=undefined),B.pulled_since(a,b)}function I(a){a&&z.emit("error",a)}function J(a,b,c){return arguments.length<3&&("function"==typeof b?(c=b,b=a,a=b.id||b._id||g()):c=I),"object"!=typeof b?c(new Error("You can only put objects, not "+typeof b)):(b.id||b._id||(b._id=a),B.ready(function(d){if(d)return c(d);B.doc.put(a,b,function(b){if(b)return c(b);B.meta.put(a,E("p"),function(b){if(b)return c(b);c(null,a)})})}),a)}function K(a,b,c){c||(c=I),B.ready(function(d){if(d)return c(d);B.doc.put(a,b,c)})}function L(a,b){b||(b=I),B.ready(function(c){return c?callback(c):B.doc.get(a,b)})}function M(a,b){L(a,function(c,d){if(c)return b(c);if(d===null||d===undefined)return b();B.ready(function(c){if(c)return b(c);B.doc.destroy(a,function(c,e){if(c)return b(c);e?B.meta.put(a,E("d",d&&d._rev),b):b()})})})}function N(a,b){B.ready(function(c){if(c)return b(c);B.doc.destroy(a,b)})}function O(a){function c(a,c,d){b.push(c),d()}function d(c){if(c)return a(c);a(null,b)}var b=[];B.doc.all_keys_iterator(c,d)}function P(a){a||(a=I),B.ready(function(b){if(b)return a(b);(function(b){B.doc.all_keys(function(c,d){if(c)return a(c);var e,f=-1;(function g(){f+=1;if(f<d.length)e=d[f],B.doc.destroy(e,function(b){if(b)return a(b);g()});else return b()})()})})(function(){B.meta.all_keys(function(b,c){if(b)return a(b);var d,e=-1;(function f(){e+=1;if(e<c.length)d=c[e],B.meta.destroy(d,function(b){if(b)return a(b);f()});else return a()})()})})})}function Q(a,b){B.ready(function(c){if(c)return b(c);C(a,b)})}function R(a,b){B.ready(function(c){if(c)return b(c);D(a,b)})}var k="d",l="m",m="s",o={},z=new b,A,B,C,D;return c=c||{},c.storage_strategy||(c.storage_strategy="idbstore"),B=u(c.storage_strategy)(a),c.pull_strategy||(c.pull_strategy="couchdb_bulk"),C=v(c.pull_strategy)(),c.push_strategy||(c.push_strategy="restful_ajax"),D=x(c.push_strategy)(),A=function(){function c(b,c){function e(){return!d&&typeof c=="function"?(d=!0,c.apply(z,arguments),!0):!1}function f(b){b&&!e(b)&&a.emit("error",b)}var d=!1;arguments.length<2&&(c=b,b=undefined),R(b,function(a){if(a)return f(a);Q(b,function(a){if(a)return f(a);e()})})}var a=new b;return c.on=function(){a.on.apply(a,arguments)},c}(),z.storageStrategy=B.stratName,z.internalNAme=B.internalName,z.sync=A,z.put=J,z.get=L,z.destroy=M,z.unsynced_keys=F,z.all=O,z.nuke=P,z}var a=[].slice;b.prototype.on=function(a,b){return(this.callbacks[a]=this.callbacks[a]||[]).push(b),this},b.prototype.emit=function(b){var c=a.call(arguments,1),d=this.callbacks[b],e,f;if(d&&d.length>0)for(e=0,f=d.length;e<f;++e)d[e](c);else if("error"===b)throw c[0]||new Error("Unspecified error");return this};var c=function(a){function d(){if(window.XMLHttpRequest&&("file:"!==window.location.protocol||!window.ActiveXObject))return new XMLHttpRequest;try{return new ActiveXObject("Microsoft.XMLHTTP")}catch(a){}try{return new ActiveXObject("Msxml2.XMLHTTP.6.0")}catch(b){}try{return new ActiveXObject("Msxml2.XMLHTTP.3.0")}catch(c){}try{return new ActiveXObject("Msxml2.XMLHTTP")}catch(d){}return!1}function f(a){return"function"==typeof a}function g(a){if(null===a||undefined==a)return!1;var b=a.constructor;return b&&Object===b}function h(a){var b;if(!g(a))return a;var c=[];for(b in a)a.hasOwnProperty(b)&&c.push(encodeURIComponent(b)+"="+encodeURIComponent(a[b]));return c.join("&")}function i(a){var b={},c=a.split("&"),d,e,f,g;for(f=0,g=c.length;f<g;++f)e=c[f],d=e.split("="),b[decodeURIComponent(d[0])]=decodeURIComponent(d[1]);return b}function j(a){var b=a.split(/\r?\n/),c={},d,f,g,h,i,j;b.pop();for(i=0,j=b.length;i<j;++i)f=b[i],d=f.indexOf(":"),g=f.slice(0,d).toLowerCase(),h=e(f.slice(d+1)),c[g]=h;return c}function k(a,b){b=b||{},this.options=b,this.xhr=a,this.text=a.responseText,this.setStatusProperties(a.status),this.header=j(a.getAllResponseHeaders()),this.setHeaderProperties(this.header),this.body=this.parseBody(this.text)}function l(a,c){var d=this;b.call(this),this.method=a,this.url=c,this.header={},this.set("X-Requested-With","XMLHttpRequest"),this.on("end",function(){var a=new k(d.xhr,{expectResponseType:d._expectResponseType}),b;a.status===0&&(b=new Error("Unknown XHR Error")),d.callback(b,a)})}function m(a,b){return"function"==typeof b?(new l("GET",a)).end(b):1===arguments.length?new l("GET",a):new l(a,b)}a=m,a.version="0.1.1";var c=function(){},e="".trim?function(a){return a.trim()}:function(a){return a.replace(/(^\s*|\s*$)/g,"")};return a.serializeObject=h,a.parseString=i,a.types={html:"text/html",json:"application/json",urlencoded:"application/x-www-form-urlencoded","form-data":"application/x-www-form-urlencoded"},a.serialize={"application/x-www-form-urlencoded":h,"application/json":JSON.stringify},a.parse={"application/x-www-form-urlencoded":i,"application/json":JSON.parse},k.prototype.setHeaderProperties=function(a){var b=(this.header["content-type"]||"").split(/ *; */);this.contentType=b.shift(),this.setParams(b)},k.prototype.setParams=function(a){var b,c,d;for(c=0,d=a.length;c<d;++c)b=a[c].split(/ *= */),this[b[0]]=b[1]},k.prototype.parseBody=function(b){var c=a.parse[this.ok&&this.options.expectResponseType||this.contentType];return c?c(b):null},k.prototype.setStatusProperties=function(a){var b=a/100|0;this.status=a,this.statusType=b,this.info=1===b,this.ok=2===b,this.clientError=4===b,this.serverError=5===b,this.error=4===b||5===b,this.accepted=202===a,this.noContent=204===a||1223===a,this.badRequest=400===a,this.unauthorized=401===a,this.notFound=404===a,this.notAcceptable=406===a,this.conflict=409===a},a.Response=k,l.prototype=new b,l.prototype.constructor=l,l.prototype.set=function(a,b){var c;if(g(a)){for(c in a)a.hasOwnProperty(c)&&this.set(c,a[c]);return this}return this.header[a.toLowerCase()]=b,this},l.prototype.expectResponseType=function(b){return this._expectResponseType=a.types[b]||b,this},l.prototype.type=function(b){return this.set("Content-Type",a.types[b]||b),this},l.prototype.data=function(a){var b=g(a),c;if(b&&g(this._data))for(c in a)a.hasOwnProperty(c)&&(this._data[c]=a[c]);else this._data=a;return"GET"===this.method?this:b?this.header["content-type"]?this:(this.type("json"),this):this},l.prototype.send=function(a,b){return f(a)?this.end(a):a?this.data(a).end(b):this.end(),this},l.prototype.end=function(b){var e=this,f=this.xhr=d(),g=this._data||null,h;this.callback=b||c,f.onreadystatechange=function(){4===f.readyState&&e.emit("end")},"GET"===this.method&&null!==g&&(this.url+="?"+a.serializeObject(g),g=null),f.open(this.method,this.url,!0);if("GET"!==this.method&&"HEAD"!==this.method){var i=a.serialize[this.header["content-type"]];i&&(g=i(g))}for(h in this.header)this.header.hasOwnProperty(h)&&f.setRequestHeader(h,this.header[h]);return f.send(g),this},a.Request=l,m.get=function(a,b,c){var d=m("GET",a);return f(b)&&(c=b,b=null),b&&d.data(b),c&&d.end(c),d},m.del=function(a,b){var c=m("DELETE",a);return b&&c.end(b),c},m.post=function(a,b,c){var d=m("POST",a);return b&&d.data(b),c&&d.end(c),d},m.put=function(a,b,c){var d=m("PUT",a);return b&&d.data(b),c&&d.end(c),d},a}({}),g=function(){function h(a){var b=new c(16),d=0;return a.toLowerCase().replace(/[0-9a-f][0-9a-f]/g,function(a){b[d++]=f[a]}),b}function i(a){var b=e,c=a;return b[c[0]]+b[c[1]]+b[c[2]]+b[c[3]]+"-"+b[c[4]]+b[c[5]]+"-"+b[c[6]]+b[c[7]]+"-"+b[c[8]]+b[c[9]]+"-"+b[c[10]]+b[c[11]]+b[c[12]]+b[c[13]]+b[c[14]]+b[c[15]]}function q(e,f,g){typeof e=="string"&&(e={format:e}),e=e||{};var h=e.format!="binary"?d:f?f:new c(16),k=f&&g||0,l=(e.timestamp!==undefined?e.timestamp:(new Date).getTime())+a;p=l===o?p+1:0,p=e.count||p;if(l<o||p>b)n++,p=0;o=l;var q=l,r=((q&268435455)*1e4+p)%4294967296,s=q/4294967296*1e4&268435455,t=s&65535,u=s>>16,v=u&4095|4096;n=e.clockseq!==undefined?e.clockseq:n;var w=n&255,x=n>>>8|128;h[k++]=r>>>24&j,h[k++]=r>>>16&j,h[k++]=r>>>8&j,h[k++]=r&j,h[k++]=t>>>8&j,h[k++]=t&j,h[k++]=v>>>8&j,h[k++]=v&j,h[k++]=x,h[k++]=w,m=e.node||m;var y=0;return h[k++]=m[y++],h[k++]=m[y++],h[k++]=m[y++],h[k++]=m[y++],h[k++]=m[y++],h[k++]=m[y++],e.format===undefined?i(h):h}function r(a,b,e){typeof a=="string"&&(a={format:a}),a=a||{};var f=a.format!="binary"?d:b?b:new c(16),g=b&&e||0;a.random?l=a.random:k?crypto.getRandomValues(l):(l[0]=Math.random()*4294967296,l[1]=Math.random()*4294967296,l[2]=Math.random()*4294967296,l[3]=Math.random()*4294967296);var h=l[0];return f[g++]=h&j,f[g++]=h>>>8&j,f[g++]=h>>>16&j,f[g++]=h>>>24&j,h=l[1],f[g++]=h&j,f[g++]=h>>>8&j,f[g++]=h>>>16&15|64,f[g++]=h>>>24&j,h=l[2],f[g++]=h&63|128,f[g++]=h>>>8&j,f[g++]=h>>>16&j,f[g++]=h>>>24&j,h=l[3],f[g++]=h&j,f[g++]=h>>>8&j,f[g++]=h>>>16&j,f[g++]=h>>>24&j,a.format===undefined?i(f):f}var a=122192928e5,b=1e4,c=typeof Buffer=="function"?Buffer:Array,d=new c(16),e=[],f={};for(var g=0;g<256;g++)e[g]=(g+256).toString(16).substr(1),f[e[g]]=g;var j=255,k=this.crypto&&crypto.getRandomValues,l=k?new Uint32Array(4):Array(4);k?crypto.getRandomValues(l):(l[0]=Math.random()*4294967296,l[1]=Math.random()*4294967296,l[2]=Math.random()*4294967296);var m=[l[0]&j|1,l[0]>>>8&j,l[0]>>>16&j,l[0]>>>24&j,l[1]&j,l[1]>>>8&j],n=l[2]&16383,o=0,p=0,s=r;s.v1=q,s.v4=r,s.parse=h,s.unparse=i,s.BufferClass=c;if(typeof module!="undefined")module.exports=s;else return s}().v4,h=this,i=h.dribbledb,j="dbd";k.version="0.1.0","function"==typeof define&&define.amd?define("dribbledb",function(){return k}):h.dribbledb=k,function(){function c(){var c=[],d,e;for(e in a)e=a[e],d=b[e],d()&&c.push(e);return c}var a=["idbstore","localstore","sessionstore","memstore"],b={idbstore:function(){return"undefined"!=typeof (window.indexedDB||window.webkitIndexedDB||window.mozIndexedDB||window.msIndexedDB)},localstore:function(){return typeof window.localStorage!="undefined"},sessionstore:function(){return typeof window.sessionStorage!="undefined"},memstore:function(){return!0}};k.supportedStorageStrategies=c}()})();
1  src/pull_strategies/couchdb_bulk.js
View
@@ -38,6 +38,7 @@ function pull_strategy_couchdb_bulk() {
change = results[i];
key = change.id || change._id;
theirs = change.doc;
+ theirs._id = key;
store.meta.get(key, function(err, metaVal) {
if (err) { return cb(err); }
if (metaVal) {
56 src/push_strategies/restful_ajax.js
View
@@ -10,40 +10,40 @@ function push_strategy_restful_ajax() {
, uri = base_url + '/' + key;
method = op === 'p' ? 'put' : (op === 'd' ? 'del' : undefined);
- if (! method) { throw new Error('Invalid meta action: ' + value); }
+ if (! method) { throw new Error('Invalid meta action: ' + JSON.stringify(value)); }
if (rev) { uri += '?rev=' + rev; }
get(key, function(err, mine) {
- function handleResponse(err, res) {
- if (err) { return cb(err); }
+ function handleResponse(err, res) {
+ if (err) { return cb(err); }
- // ======= conflict! ~==
- if (res.conflict) {
- remote_get(uri, function(err, resp) {
- if (err) { return cb(err); }
- if (resolveConflicts) {
- resolveConflicts(mine, resp.body, function(resolved) {
- put(key, resolved);
- push_one(key, value, done);
- });
- } else {
- err = new Error('Conflict');
- err.key = key;
- err.mine = mine;
- err.theirs = resp.body;
- return cb(err);
- }
- });
- } else {
- if (('del' !== method || ! res.notFound) && ! res.ok) { return cb(new Error(method + ' ' + uri + ' failed with response status ' + res.status + ': ' + res.text)); }
- store.meta.destroy(key, function(err) {
- if (err) { return cb(err); }
- done();
- });
- }
+ // ======= conflict! ~==
+ if (res.conflict) {
+ remote_get(uri, function(err, resp) {
+ if (err) { return cb(err); }
+ if (resolveConflicts) {
+ resolveConflicts(mine, resp.body, function(resolved) {
+ put(key, resolved);
+ push_one(key, value, done);
+ });
+ } else {
+ err = new Error('Conflict');
+ err.key = key;
+ err.mine = mine;
+ err.theirs = resp.body;
+ return cb(err);
+ }
+ });
+ } else {
+ if (('del' !== method || ! res.notFound) && ! res.ok) { return cb(new Error(method + ' ' + uri + ' failed with response status ' + res.status + ': ' + res.text)); }
+ store.meta.destroy(key, function(err) {
+ if (err) { return cb(err); }
+ done();
+ });
}
+ }
- remote(method, uri, mine, handleResponse);
+ remote(method, uri, mine, handleResponse);
});
}
8 src/storage_strategies/memstore.js
View
@@ -56,12 +56,16 @@ function store_strategy_memstore(base_url) {
if (i >= keys.length) { return done(); }
function next() {
- i ++;
+ i += 1;
iterate();
}
key = keys[i];
- cb(key.slice(path.length + 1), mem_get(key), next);
+ key = key.slice(path.length + 1);
+ mem_get('d', key, function(err, val) {
+ if (err) { return done(err); }
+ cb(key, val, next);
+ })
}());
}
859 tests/spec/DribbleDBSpec.js
View
@@ -8,7 +8,8 @@ describe('DribbleDB', function() {
function openDB(name, type) {
var opts;
- if (type) { opts = {storage_strategy: type}; }
+ if (! type) { throw new Error('please provide a type'); }
+ opts = {storage_strategy: type};
var db = dbd(name, opts);
if ('idbstore' === db.storageStrategy) {
idb_dbs.push(db.internalName);
@@ -62,532 +63,430 @@ describe('DribbleDB', function() {
beforeEach(removeAll);
afterEach(removeAll);
- describe('stuff happens', function() {
- var db = openDB('http://foo.com/posts');
-
- it("should exist", function(done) {
- expect(dbd).toBeDefined();
- done();
- });
-
- it("should support feature detection", function(done) {
- expect(dbd.supportedStorageStrategies()).toEqual(['idbstore', 'localstore', 'sessionstore', 'memstore']);
- done();
- });
-
- it("should have a version number", function(done) {
- expect(dbd.version).toBeDefined();
- expect(dbd.version).toNotEqual('@VERSION');
+ it("should exist", function(done) {
+ expect(dbd).toBeDefined();
+ done();
+ });
+
+ it("should support feature detection", function(done) {
+ expect(dbd.supportedStorageStrategies()).toEqual(['idbstore', 'localstore', 'sessionstore', 'memstore']);
+ done();
+ });
+
+ it("should have a version number", function(done) {
+ expect(dbd.version).toBeDefined();
+ expect(dbd.version).toNotEqual('@VERSION');
+ done();
+ });
+
+ it("should be able to produce db instances", function(done) {
+ var db = openDB('http://foo.com/posts', 'memstore');
+ db.ready(function() {
+ expect(db).toBeDefined();
done();
});
-
- it("should be able to produce db instances", function(done) {
- db.ready(function() {
- expect(db).toBeDefined();
- done();
- });
- });
});
-
- describe("when a dribbledb has been instantiated", function() {
- var db = openDB('http://foo.com/postsABC');
- expect(db.storageStrategy).toEqual('idbstore');
-
- it("should be able to put a string and get it back", function(done) {
- db.ready(function() {
- db.put('a', {a: 'abc'}, function(err, id) {
- expect(err).toBeNull();
- expect(id).toEqual('a');
- db.get('a', function(err, val) {
- expect(err).toBeNullOrUndefined();
- expect(val).toEqual({a: 'abc', _id: 'a'});
- done();
- });
- });
- })
- });
-
- it("should be able to store and retrieve objects", function(done) {
- db.ready(function() {
- db.put('b', {a: 1, b : 2}, function(err) {
- db.get('b', function(err, val) {
- expect(err).toBeNull();
- expect(val).toEqual({a: 1, b : 2, _id: 'b'});
- done();
+ var storages = dbd.supportedStorageStrategies();
+ for (var i = 0, len = storages.length; i < len; i += 1) {
+ (function(storage) {
+ describe(storage, function() {
+ describe("when a dribbledb has been instantiated", function() {
+ var db = openDB('http://foo.com/postsABC', storage);
+
+ it("should be able to put a string and get it back", function(done) {
+ db.ready(function() {
+ db.put('a', {a: 'abc'}, function(err, id) {
+ expect(err).toBeNull();
+ expect(id).toEqual('a');
+ db.get('a', function(err, val) {
+ expect(err).toBeNullOrUndefined();
+ expect(val).toEqual({a: 'abc', _id: 'a'});
+ done();
+ });
+ });
+ })
});
- });
- });
- });
-
- it("should be able to remove by key", function(done) {
- db.ready(function() {
- db.put("c", {a:1}, function(err) {
- expect(err).toBeNull();
- db.get("c", function(err, val) {
- expect(err).toBeNullOrUndefined();
- expect(val).toEqual({a:1, _id:'c'});
- db.destroy("c", function(err) {
- expect(err).toBeNullOrUndefined();
- db.get("c", function(val) {
- expect(val).toBeNull();
- done();
+
+ it("should be able to store and retrieve objects", function(done) {
+ db.ready(function() {
+ db.put('b', {a: 1, b : 2}, function(err) {
+ db.get('b', function(err, val) {
+ expect(err).toBeNull();
+ expect(val).toEqual({a: 1, b : 2, _id: 'b'});
+ done();
+ });
});
});
});
- });
- });
- });
- });
-
- describe("when a dribbledb using sessionstore has been instantiated", function() {
- var db = openDB('http://foo.com/sessionposts', 'sessionstore');
-
- expect(db.storageStrategy).toEqual('sessionstore');
-
- it("should be able to put a string and get it back 2", function(done) {
- db.ready(function() {
- db.put('a', {a: 'abc'}, function(err) {
- expect(err).toBeNullOrUndefined();
- db.get('a', function(err, val) {
- expect(err).toBeNullOrUndefined();
- expect(val).toEqual({a: 'abc', _id:"a"});
- done();
- });
- });
- });
- });
-
- it("should be able to store and retrieve objects", function(done) {
- db.ready(function() {
- db.put('b', {a: 1, b : 2}, function(err) {
- expect(err).toBeNullOrUndefined();
- db.get('b', function(err, val) {
- expect(val).toEqual({a: 1, b : 2, _id: 'b'});
- done();
+
+ it("should be able to remove by key", function(done) {
+ db.ready(function() {
+ db.put("c", {a:1}, function(err) {
+ expect(err).toBeNull();
+ db.get("c", function(err, val) {
+ expect(err).toBeNullOrUndefined();
+ expect(val).toEqual({a:1, _id:'c'});
+ db.destroy("c", function(err) {
+ expect(err).toBeNullOrUndefined();
+ db.get("c", function(val) {
+ expect(val).toBeNull();
+ done();
+ });
+ });
+ });
+ });
+ });
});
});
- });
- });
-
- it("should be able to remove by key", function(done) {
- db.ready(function() {
- db.put("c", {a:1}, function(err) {
- expect(err).toBeNullOrUndefined();
- db.get("c", function(err, val) {
- expect(err).toBeNullOrUndefined();
- expect(val).toEqual({a:1,_id:'c'});
- db.destroy("c", function(err) {
- expect(err).toBeNullOrUndefined();
- db.get("c", function(err, val) {
+
+
+ describe("when you have a db just for yourself", function() {
+ var db = openDB('http://foo.com/posts2', storage);
+
+ it("should be able to tell me which keys have not yet been synced", function(done) {
+ var unsynced;
+ db.ready(function() {
+ db.put("a", {a:1}, function(err) {
expect(err).toBeNullOrUndefined();
- expect(val).toBeNull();
- done();
- })
+ db.put("b", {b:2}, function(err) {
+ expect(err).toBeNullOrUndefined();
+ db.put("c", {c:3}, function(err) {
+ expect(err).toBeNullOrUndefined();
+ db.unsynced_keys(function(err, unsynced) {
+ expect(err).toBeNullOrUndefined();
+ expect(unsynced.length).toEqual(3);
+ expect(unsynced).toContain("a");
+ expect(unsynced).toContain("b");
+ expect(unsynced).toContain("c");
+ done();
+ });
+ });
+ });
+ });
});
});
});
- });
- });
- });
-
- describe("when a dribbledb using memstore has been instantiated", function() {
- var db = openDB('http://foo.com/memposts', 'memstore');
-
- expect(db.storageStrategy).toEqual('memstore');
-
- it("should be able to put a string and get it back", function(done) {
- db.ready(function() {
- db.put('a', {a:1}, function(err) {
- expect(err).toBeNullOrUndefined();
- db.get('a', function(err, val) {
- expect(err).toBeNullOrUndefined();
- expect(val).toEqual({a:1,_id:'a'});
- done();
+
+ describe("when you have a second db just for yourself", function() {
+ var db = openDB('http://foo.com/posts2', storage);
+
+ it("should be able to tell me which keys have not yet been synced", function(done) {
+ var unsynced;
+ db.ready(function() {
+ db.put({a:1}, function(err, id) {
+ expect(err).toBeNullOrUndefined();
+ expect(id).toBeDefined();
+ db.get(id, function(err, val) {
+ expect(val).toEqual({a:1,_id:id});
+ done();
+ });
+ });
+ });
});
});
- });
- });
-
- it("should be able to store and retrieve objects", function(done) {
- db.ready(function() {
- db.put('b', {a: 1, b : 2}, function(err) {
- expect(err).toBeNullOrUndefined();
- db.get('b', function(err, val) {
- expect(err).toBeNullOrUndefined();
- expect(val).toEqual({a: 1, b : 2, _id: 'b'});
- done();
- })
- });
- });
- });
-
- it("should be able to remove by key", function(done) {
- db.ready(function() {
- db.put("c", {a:1}, function(err) {
- expect(err).toBeNullOrUndefined();
- db.get("c", function(err, val) {
- expect(err).toBeNullOrUndefined();
- expect(val).toEqual({a:1,_id:'c'});
- db.destroy("c", function(err) {
- expect(err).toBeNullOrUndefined();
- db.get("c", function(err, val) {
+
+ describe("when you have a shumble db just for yourself", function() {
+ var db = openDB('http://foo.com/shumble', storage);
+ it("it should be able to iterate over all the keys", function(done) {
+ var all;
+
+ db.ready(function() {
+ db.put("a", {a:1}, function(err) {
expect(err).toBeNullOrUndefined();
- expect(val).toBeNull();
- done();
+ db.put("b", {b:2}, function(err) {
+ expect(err).toBeNullOrUndefined();
+ db.put("c", {c:3}, function(err) {
+ expect(err).toBeNullOrUndefined();
+ db.all(function(err, all) {
+ expect(err).toBeNullOrUndefined();
+ expect(all).toBeDefined();
+ expect(all).toContain({a:1, _id: 'a'});
+ expect(all).toContain({b:2, _id: 'b'});
+ expect(all).toContain({c:3, _id: 'c'});
+ done();
+ });
+ });
+ });
});
});
- })
+ });
});
- });
- });
- });
-
- describe("when you have a db just for yourself", function() {
- var db = openDB('http://foo.com/posts2');
-
- it("should be able to tell me which keys have not yet been synced", function(done) {
- var unsynced;
- db.ready(function() {
- db.put("a", {a:1}, function(err) {
- expect(err).toBeNullOrUndefined();
- db.put("b", {b:2}, function(err) {
- expect(err).toBeNullOrUndefined();
- db.put("c", {c:3}, function(err) {
- expect(err).toBeNullOrUndefined();
- db.unsynced_keys(function(err, unsynced) {
+
+ describe("when you have a shumble 2 db just for yourself", function() {
+ var db = openDB('http://foo.com/shumble2', storage);
+ it("it should be able to nuke everything", function(done) {
+ var all;
+
+ db.ready(function() {
+ db.put("a", {a:1}, function(err) {
expect(err).toBeNullOrUndefined();
- expect(unsynced.length).toEqual(3);
- expect(unsynced).toContain("a");
- expect(unsynced).toContain("b");
- expect(unsynced).toContain("c");
- done();
+ db.put("b", {b:2}, function(err) {
+ expect(err).toBeNullOrUndefined();
+ db.put("c", {c:3}, function(err) {
+ expect(err).toBeNullOrUndefined();
+ db.nuke(function(err) {
+ expect(err).toBeNullOrUndefined();
+ db.all(function(err, all) {
+ expect(err).toBeNullOrUndefined();
+ expect(all.length).toEqual(0);
+ db.unsynced_keys(function(err, unsynced_keys) {
+ expect(err).toBeNullOrUndefined();
+ expect(unsynced_keys.length).toEqual(0);
+ done();
+ });
+ })
+ })
+ });
+ });
});
});
});
});
- });
- });
- });
-
- describe("when you have a second db just for yourself", function() {
- var db = openDB('http://foo.com/posts2');
-
- it("should be able to tell me which keys have not yet been synced", function(done) {
- var unsynced;
- db.ready(function() {
- db.put({a:1}, function(err, id) {
- expect(err).toBeNullOrUndefined();
- expect(id).toBeDefined();
- db.get(id, function(err, val) {
- expect(val).toEqual({a:1,_id:id});
- done();
+
+ describe("when you have another db just for yourself where you have overriden request", function() {
+ var db = openDB('http://foo.com/syncables', storage);
+ var xhr, requests;
+
+ beforeEach(function() {
+ xhr = sinon.useFakeXMLHttpRequest();
+ requests = [];
+ xhr.onCreate = function(xhr) {
+ requests.push(xhr);
+ };
});
- });
- });
- });
- });
-
- describe("when you have a shumble db just for yourself", function() {
- var db = openDB('http://foo.com/shumble');
- it("it should be able to iterate over all the keys", function(done) {
- var all;
-
- db.ready(function() {
- db.put("a", {a:1}, function(err) {
- expect(err).toBeNullOrUndefined();
- db.put("b", {b:2}, function(err) {
- expect(err).toBeNullOrUndefined();
- db.put("c", {c:3}, function(err) {
- expect(err).toBeNullOrUndefined();
- db.all(function(err, all) {
+
+ afterEach(function() {
+ xhr.restore();
+ });
+
+ it("should try to sync the unsynced actions", function(done) {
+ var unsynced
+ , callback = sinon.spy();
+
+ db.ready(function() {
+ db.put("a", {a:1}, function(err) {
expect(err).toBeNullOrUndefined();
- expect(all).toBeDefined();
- expect(all).toContain({a:1, _id: 'a'});
- expect(all).toContain({b:2, _id: 'b'});
- expect(all).toContain({c:3, _id: 'c'});
- done();
+ db.put("b", {b:2}, function(err) {
+ expect(err).toBeNullOrUndefined();
+ db.put("c", {c:3}, function(err) {
+ db.destroy("b", function(err) {
+ expect(err).toBeNullOrUndefined();
+ db.sync(callback);
+ var i = 0;
+ (function respond(done) {
+ (function schedule() {
+ if (requests.length > i) {
+ requests[i].respond(201, {}, '{}');
+ i += 1;
+ if (i >= 4) { done(); }
+ else { setTimeout(schedule, 100); }
+ } else {
+ setTimeout(schedule, 100);
+ }
+ }());
+ }(function() {
+ expect(callback.calledWith()).toEqual(true);
+ db.unsynced_keys(function(err, unsyncked_keys) {
+ expect(err).toBeNullOrUndefined();
+ expect(unsyncked_keys.length).toEqual(0)
+ done();
+ });
+ }));
+ });
+ });
+ });
});
});
});
});
- });
- });
- });
-
- describe("when you have a shumble 2 db just for yourself", function() {
- var db = openDB('http://foo.com/shumble2');
- it("it should be able to nuke everything", function(done) {
- var all;
-
- db.ready(function() {
- db.put("a", {a:1}, function(err) {
- expect(err).toBeNullOrUndefined();
- db.put("b", {b:2}, function(err) {
- expect(err).toBeNullOrUndefined();
- db.put("c", {c:3}, function(err) {
- expect(err).toBeNullOrUndefined();
- db.nuke(function(err) {
+
+ describe("when you have yet another db just for yourself where you have overriden request", function() {
+ var db = openDB('http://foo.com/syncables2', storage);
+ var xhr, requests;
+
+ beforeEach(function() {
+ xhr = sinon.useFakeXMLHttpRequest();
+ requests = [];
+ xhr.onCreate = function(xhr) {
+ requests.push(xhr);
+ };
+ });
+
+ afterEach(function() {
+ xhr.restore();
+ });
+
+ it("should be able to detect conflicts", function(done) {
+ var unsynced
+ , callback;
+
+ callback = function(err) {
+ expect(err instanceof Error).toEqual(true);
+ expect(err.message).toEqual('Conflict');
+ expect(err.mine).toEqual({a:1,_id:'a'});
+ expect(err.theirs).toEqual({a:2,_id:"a"});
+ done();
+ };
+
+ db.ready(function() {
+ db.put("a", {a:1}, function(err) {
+ if (err) { throw err; }
+ db.sync(callback);
+ var responses = [
+ [409, {}, '{}']
+ , [200, {}, '{"a":2,"_id":"a"}']
+ ];
+ (function respond() {
+ var i = 0;
+ (function schedule() {
+ var req;
+ if (requests.length > i) {
+ req = requests[i];
+ req.respond.apply(req, responses[i]);
+ i += 1;
+ if (i >= responses.length) { return; }
+ setTimeout(schedule, 100);
+ } else {
+ setTimeout(schedule, 100);
+ }
+ }());
+ }());
+ });
+ });
+ });
+ });
+
+ describe("when you have yet another second db just for yourself where you have overriden request", function() {
+ var db = openDB('http://foo.com/syncables3', storage);
+ var xhr, requests;
+
+ beforeEach(function() {
+ xhr = sinon.useFakeXMLHttpRequest();
+ requests = [];
+ xhr.onCreate = function(xhr) {
+ requests.push(xhr);
+ };
+ });
+
+ afterEach(function() {
+ xhr.restore();
+ });
+
+ it("should be able to resolve conflicts", function(done) {
+ var unsynced
+ , resolveConflictCalled = false
+ , callback = sinon.spy()
+
+ function resolveConflict(a, b, done) {
+ resolveConflictCalled = true;
+ done({a:3});
+ }
+
+ db.ready(function() {
+ db.put("a", {a:1}, function(err) {
+ if (err) { throw err; }
+ db.sync(resolveConflict, callback);
+
+ (function respond(done) {
+ var responses = {
+ 0: [409, {}, '{}']
+ , 1: [200, {'Content-Type': 'application/json'}, '2']
+ , 2: [201, {}, '{}']
+ , 3: [200, {'Content-Type': 'application/json'}, '{"results":[], "last_seq":0}']
+ };
+ var i = 0;
+ (function schedule() {
+ var req;
+ if (requests.length > i) {
+ req = requests[i];
+ req.respond.apply(req, responses[i]);
+ i += 1;
+ if (i >= 4) {
+ return done();
+ }
+ setTimeout(schedule, 100);
+ } else {
+ setTimeout(schedule, 100);
+ }
+ }());
+ }(function() {
+ setTimeout(function() {
+ expect(resolveConflictCalled).toEqual(true);
+ expect(callback.called).toEqual(true);
+ expect(callback.callCount).toEqual(1);
+ expect(callback.getCall(0).args.length).toEqual(0);
+ db.get("a", function(err, val) {
+ expect(err).toBeNullOrUndefined();
+ expect(val).toEqual({a:3,_id:'a'});
+ done();
+ });
+ }, 200);
+ }));
+ });
+ });
+ });
+ });
+
+ describe("when you have yet another third db just for yourself where you have overriden request", function() {
+ var db = openDB('http://foo.com/syncables4', storage);
+ var xhr, requests;
+
+ beforeEach(function() {
+ xhr = sinon.useFakeXMLHttpRequest();
+ requests = [];
+ xhr.onCreate = function(xhr) {
+ requests.push(xhr);
+ };
+ });
+
+ afterEach(function() {
+ xhr.restore();
+ });
+
+ it("should be able to pull changes from remote", function(done) {
+ var callback = function(err) {
expect(err).toBeNullOrUndefined();
- db.all(function(err, all) {
+ db.get("a", function(err, val) {
expect(err).toBeNullOrUndefined();
- expect(all.length).toEqual(0);
- db.unsynced_keys(function(err, unsynced_keys) {
+ expect(val).toEqual({a:1, _id:'a'});
+ db.get("b", function(err, val) {
expect(err).toBeNullOrUndefined();
- expect(unsynced_keys.length).toEqual(0);
+ expect(val).toEqual({a:2, _id:'b'});
done();
});
- })
- })
- });
- });
- });
- });
- });
- });
+ });
+ }
- describe("when you have another db just for yourself where you have overriden request", function() {
- var db = openDB('http://foo.com/syncables');
- var xhr, requests;
-
- beforeEach(function() {
- xhr = sinon.useFakeXMLHttpRequest();
- requests = [];
- xhr.onCreate = function(xhr) {
- requests.push(xhr);
- };
- });
-
- afterEach(function() {
- xhr.restore();
- });
-
- it("should try to sync the unsynced actions", function(done) {
- var unsynced
- , callback = sinon.spy();
-
- db.ready(function() {
- db.put("a", {a:1}, function(err) {
- expect(err).toBeNullOrUndefined();
- db.put("b", {b:2}, function(err) {
- expect(err).toBeNullOrUndefined();
- db.put("c", {c:3}, function(err) {
- db.destroy("b", function(err) {
- expect(err).toBeNullOrUndefined();
+ db.ready(function() {
db.sync(callback);
- var i = 0;
- (function respond(done) {
+
+ (function respond() {
+ var responses = {
+ 0: [200, {'Content-Type': 'application/json'}, '{"results":[{"_id":"a","doc":{"a":1}},{"_id":"b","doc":{"a":2}}], "last_seq":2}']
+ };
+ var i = 0;
(function schedule() {
+ var req;
if (requests.length > i) {
- requests[i].respond(201, {}, '{}');
+ req = requests[i];
+ req.respond.apply(req, responses[i]);
i += 1;
- if (i >= 4) { done(); }
- else { setTimeout(schedule, 100); }
+ if (i >= 1) {
+ return done();
+ }
+ setTimeout(schedule, 100);
} else {
setTimeout(schedule, 100);
}
}());
- }(function() {
- expect(callback.calledWith()).toEqual(true);
- db.unsynced_keys(function(err, unsyncked_keys) {
- expect(err).toBeNullOrUndefined();
- expect(unsyncked_keys.length).toEqual(0)
- done();
- });
- }));
+ }());
});
});
});
- });
- });
- });
- });
-
- describe("when you have yet another db just for yourself where you have overriden request", function() {
- var db = openDB('http://foo.com/syncables2');
- var xhr, requests;
-
- beforeEach(function() {
- xhr = sinon.useFakeXMLHttpRequest();
- requests = [];
- xhr.onCreate = function(xhr) {
- requests.push(xhr);
- };
- });
-
- afterEach(function() {
- xhr.restore();
- });
-
- it("should be able to detect conflicts", function(done) {
- var unsynced
- , callback;
-
- callback = function(err) {
- expect(err instanceof Error).toEqual(true);
- expect(err.message).toEqual('Conflict');
- expect(err.mine).toEqual({a:1,_id:'a'});
- expect(err.theirs).toEqual({a:2,_id:"a"});
- done();
- };
-
- db.ready(function() {
- db.put("a", {a:1}, function(err) {
- if (err) { throw err; }
- db.sync(callback);
- var responses = [
- [409, {}, '{}']
- , [200, {}, '{"a":2,"_id":"a"}']
- ];
- (function respond() {
- var i = 0;
- (function schedule() {
- var req;
- if (requests.length > i) {
- req = requests[i];
- req.respond.apply(req, responses[i]);
- i += 1;
- if (i >= responses.length) { return; }
- setTimeout(schedule, 100);
- } else {
- setTimeout(schedule, 100);
- }
- }());
- }());
- });
- });
- });
- });
-
- describe("when you have yet another second db just for yourself where you have overriden request", function() {
- var db = openDB('http://foo.com/syncables3');
- var xhr, requests;
-
- beforeEach(function() {
- xhr = sinon.useFakeXMLHttpRequest();
- requests = [];
- xhr.onCreate = function(xhr) {
- requests.push(xhr);
- };
- });
-
- afterEach(function() {
- xhr.restore();
- });
-
- it("should be able to resolve conflicts", function(done) {
- var unsynced
- , resolveConflictCalled = false
- , callback = sinon.spy()
-
- function resolveConflict(a, b, done) {
- resolveConflictCalled = true;
- done({a:3});
- }
-
- db.ready(function() {
- db.put("a", {a:1}, function(err) {
- if (err) { throw err; }
- db.sync(resolveConflict, callback);
-
- (function respond(done) {
- var responses = {
- 0: [409, {}, '{}']
- , 1: [200, {'Content-Type': 'application/json'}, '2']
- , 2: [201, {}, '{}']
- , 3: [200, {'Content-Type': 'application/json'}, '{"results":[], "last_seq":0}']
- };
- var i = 0;
- (function schedule() {
- var req;
- if (requests.length > i) {
- req = requests[i];
- req.respond.apply(req, responses[i]);
- i += 1;
- if (i >= 4) {
- return done();
- }
- setTimeout(schedule, 100);
- } else {
- setTimeout(schedule, 100);
- }
- }());
- }(function() {
- setTimeout(function() {
- expect(resolveConflictCalled).toEqual(true);
- expect(callback.called).toEqual(true);
- expect(callback.callCount).toEqual(1);
- expect(callback.getCall(0).args.length).toEqual(0);
- db.get("a", function(err, val) {
- expect(err).toBeNullOrUndefined();
- expect(val).toEqual({a:3,_id:'a'});
- done();
- });
- }, 200);
- }));
- });
- });
- });
- });
-
- describe("when you have yet another third db just for yourself where you have overriden request", function() {
- var db = openDB('http://foo.com/syncables4');
- var xhr, requests;
-
- beforeEach(function() {
- xhr = sinon.useFakeXMLHttpRequest();
- requests = [];
- xhr.onCreate = function(xhr) {
- requests.push(xhr);
- };
});
-
- afterEach(function() {
- xhr.restore();
- });
-
- it("should be able to pull changes from remote", function(done) {
- var callback = sinon.spy()
-
- db.ready(function() {
- db.sync(callback);
-
- (function respond(done) {
- var responses = {
- 0: [200, {'Content-Type': 'application/json'}, '{"results":[{"_id":"a","doc":{"a":1}},{"id":"b","doc":{"a":2}}], "last_seq":2}']
- };
- var i = 0;
- (function schedule() {
- var req;
- if (requests.length > i) {
- req = requests[i];
- req.respond.apply(req, responses[i]);
- i += 1;
- if (i >= 1) {
- return done();
- }
- setTimeout(schedule, 100);
- } else {
- setTimeout(schedule, 100);
- }
- }());
- }(function() {
- setTimeout(function() {
- expect(callback.called).toEqual(true);
- expect(callback.callCount).toEqual(1);
- expect(callback.getCall(0).args.length).toEqual(0);
- db.get("a", function(err, val) {
- expect(err).toBeNullOrUndefined();
- expect(val).toEqual({a:1, _id:'a'});
- db.get("b", function(err, val) {
- expect(err).toBeNullOrUndefined();
- expect(val).toEqual({a:2, _id:'b'});
- done();
- });
- });
- }, 200);
- }));
- });
- });
- });
+ }(storages[i]));
+
+ }
+
});
Please sign in to comment.
Something went wrong with that request. Please try again.