Commit 9bbcbeb5 authored by Bryan Kaperick's avatar Bryan Kaperick

Updated bryanstorage to retain all versions of documents and only retrieve the...

Updated bryanstorage to retain all versions of documents and only retrieve the most recent copy when get(id) is called.
parent 6f693094
......@@ -13,37 +13,76 @@
this._sub_storage = jIO.createJIO(spec.sub_storage);
}
BryanStorage.prototype.get = function () {
return this._sub_storage.get.apply(this._sub_storage, arguments);
BryanStorage.prototype.get = function (id_in) {
var options = {
//query: 'id: "' + id_in + '"',
//sort_on: [['_revision', 'descending']],
//id: "tmp",
include_docs: true
};
BryanStorage.prototype.allAttachments = function () {
return this._sub_storage.allAttachments.apply(this._sub_storage, arguments);
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 [];
});
};
// Not implemented for IndexedDB
BryanStorage.prototype.post = function () {
return this._sub_storage.post.apply(this._sub_storage, arguments);
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);
};
BryanStorage.prototype.put = function (id, new_metadata) {
var substorage = this._sub_storage;
return this.get(id)
var storage = this;
new_metadata.id = id;
return storage.get(id)
.push(
function (metadata) {
// Increments existing "_revision" attribute
if (metadata.hasOwnProperty('_revision')) {
new_metadata._revision = metadata._revision + 1;
return substorage.put(id, new_metadata);
} else {
new_metadata._revision = 0;
}
//return storage.post.apply(substorage, new_metadata);
return storage.post(new_metadata);
},
function () {
// Creates new attribute "_revision" = 0
new_metadata._revision = 0;
return substorage.put(id, new_metadata);
return storage.post(new_metadata);
}
);
};
BryanStorage.prototype.allAttachments = function () {
return this._sub_storage.allAttachments.apply(this._sub_storage, arguments);
};
BryanStorage.prototype.remove = function () {
return this._sub_storage.remove.apply(this._sub_storage, arguments);
};
......@@ -103,8 +142,8 @@
BryanStorage.prototype.hasCapacity = function (name) {
return this._sub_storage.removeAttachment.apply(this._sub_storage, name);
};
BryanStorage.prototype.buildQuery = function (options) {
return this._sub_storage.removeAttachment.apply(this._sub_storage, options);
BryanStorage.prototype.buildQuery = function () {
return this._sub_storage.buildQuery.apply(this._sub_storage, arguments);
};
jIO.addStorage('bryan', BryanStorage);
......
This diff is collapsed.
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