Commit 5762d991 authored by Bryan Kaperick's avatar Bryan Kaperick

bryanstorage now uses querystorage and uuidstorage as substorages to implement...

bryanstorage now uses querystorage and uuidstorage as substorages to implement uuid posting and querying the sub storage.
parent 9bbcbeb5
...@@ -10,54 +10,46 @@ ...@@ -10,54 +10,46 @@
* @constructor * @constructor
*/ */
function BryanStorage(spec) { function BryanStorage(spec) {
this._sub_storage = jIO.createJIO(spec.sub_storage); //this._sub_storage = jIO.createJIO(spec.sub_storage);
this._sub_storage = jIO.createJIO({
type: "query",
sub_storage: {
type: "uuid",
sub_storage: spec.sub_storage
}
});
} }
BryanStorage.prototype.get = function (id_in) { BryanStorage.prototype.get = function (id_in) {
var options = { var substorage = this._sub_storage,
//query: 'id: "' + id_in + '"', options = {
//sort_on: [['_revision', 'descending']], query: '(_doc_id: "' + id_in + '")',// AND (include_docs: true)',
//id: "tmp", sort_on: [['_revision', 'descending']]
include_docs: true //include_docs: true
}; };
return this._sub_storage.allDocs(options) return this._sub_storage.allDocs(options)
// Return query results if there are any, else throw error
// Return document with most recent revision .push(function (query_results) {
.push(function (results) { var docs = query_results.data.rows;
//<0 => a < b if (docs.length > 0) {
var sorted_results = results.data.rows.sort(function (a, b) { return substorage.get(docs[0].id);
if (b.doc.id !== id_in || !b) {return -1; }
if (a.doc.id !== id_in || !a) {return 1; }
return b.doc._revision - a.doc._revision;
});
if (sorted_results.length > 0 && sorted_results[0].doc.id === id_in) {
return sorted_results[0].doc;
} }
return []; throw new jIO.util.jIOError(
"bryanstorage: cannot find object '" + id_in + "'",
404
);
}); });
}; };
// Not implemented for IndexedDB
BryanStorage.prototype.post = function (metadata) { BryanStorage.prototype.post = function (metadata) {
// Uses UuidStorage post
function S4() { return this._sub_storage.post(metadata);
return ('0000' + Math.floor(
Math.random() * 0x10000 // 65536
).toString(16)).slice(-4);
}
var id = S4() + S4() + "-" +
S4() + "-" +
S4() + "-" +
S4() + "-" +
S4() + S4() + S4();
return this._sub_storage.put(id, metadata);
}; };
BryanStorage.prototype.put = function (id, new_metadata) { BryanStorage.prototype.put = function (id, new_metadata) {
var storage = this; var storage = this;
new_metadata.id = id; new_metadata._doc_id = id;
return storage.get(id) return storage.get(id)
.push( .push(
function (metadata) { function (metadata) {
......
...@@ -455,12 +455,12 @@ ...@@ -455,12 +455,12 @@
stop(); stop();
expect(1); expect(1);
var jio = jIO.createJIO({ var jio = jIO.createJIO({
type: "bryan", type: "bryan",
sub_storage: { sub_storage: {
type: "indexeddb", type: "indexeddb",
database: "db_test1" database: "newdb4"
} }
}); });
jio.put("doc1", { jio.put("doc1", {
"title": "rev0", "title": "rev0",
"subtitle": "subrev0" "subtitle": "subrev0"
...@@ -471,7 +471,7 @@ ...@@ -471,7 +471,7 @@
"title": "rev0", "title": "rev0",
"subtitle": "subrev0", "subtitle": "subrev0",
"_revision": 0, "_revision": 0,
"id": "doc1" "_doc_id": "doc1"
}, "Retrieve document correctly"); }, "Retrieve document correctly");
}) })
.fail(function (error) { .fail(function (error) {
...@@ -490,7 +490,7 @@ ...@@ -490,7 +490,7 @@
type: "bryan", type: "bryan",
sub_storage: { sub_storage: {
type: "indexeddb", type: "indexeddb",
database: "db_test2" database: "otherdb5"
} }
}); });
jio.put("other_doc", { jio.put("other_doc", {
...@@ -527,7 +527,7 @@ ...@@ -527,7 +527,7 @@
"title": "rev2", "title": "rev2",
"subtitle": "subrev2", "subtitle": "subrev2",
"_revision": 2, "_revision": 2,
"id": "main_doc" "_doc_id": "main_doc"
}, "Retrieve main document correctly"); }, "Retrieve main document correctly");
}) })
.push(function () {return jio.get("other_doc"); }) .push(function () {return jio.get("other_doc"); })
...@@ -536,7 +536,7 @@ ...@@ -536,7 +536,7 @@
"attr": "version1", "attr": "version1",
"subattr": "subversion1", "subattr": "subversion1",
"_revision": 1, "_revision": 1,
"id": "other_doc" "_doc_id": "other_doc"
}, "Retrieve other document correctly"); }, "Retrieve other document correctly");
}) })
.fail(function (error) { .fail(function (error) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment