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 @@
* @constructor
*/
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) {
var options = {
//query: 'id: "' + id_in + '"',
//sort_on: [['_revision', 'descending']],
//id: "tmp",
include_docs: true
};
var substorage = this._sub_storage,
options = {
query: '(_doc_id: "' + id_in + '")',// AND (include_docs: true)',
sort_on: [['_revision', 'descending']]
//include_docs: true
};
return this._sub_storage.allDocs(options)
// Return document with most recent revision
.push(function (results) {
//<0 => a < b
var sorted_results = results.data.rows.sort(function (a, b) {
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 query results if there are any, else throw error
.push(function (query_results) {
var docs = query_results.data.rows;
if (docs.length > 0) {
return substorage.get(docs[0].id);
}
return [];
throw new jIO.util.jIOError(
"bryanstorage: cannot find object '" + id_in + "'",
404
);
});
};
// Not implemented for IndexedDB
BryanStorage.prototype.post = function (metadata) {
function S4() {
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);
// Uses UuidStorage post
return this._sub_storage.post(metadata);
};
BryanStorage.prototype.put = function (id, new_metadata) {
var storage = this;
new_metadata.id = id;
new_metadata._doc_id = id;
return storage.get(id)
.push(
function (metadata) {
......
......@@ -455,12 +455,12 @@
stop();
expect(1);
var jio = jIO.createJIO({
type: "bryan",
sub_storage: {
type: "indexeddb",
database: "db_test1"
}
});
type: "bryan",
sub_storage: {
type: "indexeddb",
database: "newdb4"
}
});
jio.put("doc1", {
"title": "rev0",
"subtitle": "subrev0"
......@@ -471,7 +471,7 @@
"title": "rev0",
"subtitle": "subrev0",
"_revision": 0,
"id": "doc1"
"_doc_id": "doc1"
}, "Retrieve document correctly");
})
.fail(function (error) {
......@@ -490,7 +490,7 @@
type: "bryan",
sub_storage: {
type: "indexeddb",
database: "db_test2"
database: "otherdb5"
}
});
jio.put("other_doc", {
......@@ -527,7 +527,7 @@
"title": "rev2",
"subtitle": "subrev2",
"_revision": 2,
"id": "main_doc"
"_doc_id": "main_doc"
}, "Retrieve main document correctly");
})
.push(function () {return jio.get("other_doc"); })
......@@ -536,7 +536,7 @@
"attr": "version1",
"subattr": "subversion1",
"_revision": 1,
"id": "other_doc"
"_doc_id": "other_doc"
}, "Retrieve other document correctly");
})
.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