Commit 41b69b80 authored by Romain Courteaud's avatar Romain Courteaud

Add memorystorage in the build.

Add tests for memorystorage.
Drop copy/paste code in jio.post. Should be added in a standalone storage if needed.
parent e562fe8a
...@@ -171,6 +171,7 @@ module.exports = function (grunt) { ...@@ -171,6 +171,7 @@ module.exports = function (grunt) {
'src/jio.js', 'src/jio.js',
'src/jio.storage/memorystorage.js',
'src/jio.storage/localstorage.js', 'src/jio.storage/localstorage.js',
'src/jio.storage/davstorage.js', 'src/jio.storage/davstorage.js',
'src/jio.storage/unionstorage.js', 'src/jio.storage/unionstorage.js',
......
...@@ -4,8 +4,8 @@ ...@@ -4,8 +4,8 @@
* http://www.gnu.org/licenses/lgpl.html * http://www.gnu.org/licenses/lgpl.html
*/ */
/*jslint nomen: true, maxlen: 200*/ /*jslint nomen: true*/
/*global jIO, window, Blob, Uint8Array, RSVP, console */ /*global jIO*/
/** /**
* JIO Memory Storage. Type = 'memory'. * JIO Memory Storage. Type = 'memory'.
...@@ -34,19 +34,6 @@ ...@@ -34,19 +34,6 @@
} }
MemoryStorage.prototype.post = function (metadata) {
var doc_id = metadata._id;
if (doc_id === undefined) {
doc_id = jIO.util.generateUuid();
}
if (this._database.hasOwnProperty(doc_id)) {
// the document already exists
throw new jIO.util.jIOError("Cannot create a new document", 409);
}
metadata._id = doc_id;
return this.put(metadata);
};
MemoryStorage.prototype.put = function (metadata) { MemoryStorage.prototype.put = function (metadata) {
if (!this._database.hasOwnProperty(metadata._id)) { if (!this._database.hasOwnProperty(metadata._id)) {
this._database[metadata._id] = { this._database[metadata._id] = {
...@@ -58,24 +45,35 @@ ...@@ -58,24 +45,35 @@
}; };
MemoryStorage.prototype.get = function (param) { MemoryStorage.prototype.get = function (param) {
if (!this._database.hasOwnProperty(param._id)) { try {
throw new jIO.util.jIOError("Cannot find document", 404); return JSON.parse(this._database[param._id].doc);
} catch (error) {
if (error instanceof TypeError) {
throw new jIO.util.jIOError(
"Cannot find document: " + param._id,
404
);
} }
var doc = JSON.parse(this._database[param._id].doc), throw error;
key,
found = false,
attachments = {};
for (key in this._database[param._id].attachments) {
if (this._database[param._id].attachments.hasOwnProperty(key)) {
found = true;
attachments[key] = {};
} }
}
if (found) { // XXX NotImplemented: list all attachments
doc._attachments = attachments;
} // var doc = JSON.parse(this._database[param._id].doc),
return doc; // key,
// found = false,
// attachments = {};
//
// for (key in this._database[param._id].attachments) {
// if (this._database[param._id].attachments.hasOwnProperty(key)) {
// found = true;
// attachments[key] = {};
// }
// }
// if (found) {
// doc._attachments = attachments;
// }
// return doc;
}; };
MemoryStorage.prototype.remove = function (param) { MemoryStorage.prototype.remove = function (param) {
...@@ -84,24 +82,47 @@ ...@@ -84,24 +82,47 @@
}; };
MemoryStorage.prototype.getAttachment = function (param) { MemoryStorage.prototype.getAttachment = function (param) {
if (!((this._database.hasOwnProperty(param._id)) && try {
(this._database[param._id].attachments.hasOwnProperty(param._attachments)))) {
throw new jIO.util.jIOError("Cannot find attachment", 404);
}
return this._database[param._id].attachments[param._attachment]; return this._database[param._id].attachments[param._attachment];
} catch (error) {
if (error instanceof TypeError) {
throw new jIO.util.jIOError(
"Cannot find attachment: " + param._id + " , " + param._attachment,
404
);
}
throw error;
}
}; };
MemoryStorage.prototype.putAttachment = function (param) { MemoryStorage.prototype.putAttachment = function (param) {
this._database[param._id].attachments[param._attachment] = var attachment_dict;
param._blob; try {
attachment_dict = this._database[param._id].attachments;
} catch (error) {
if (error instanceof TypeError) {
throw new jIO.util.jIOError("Cannot find document: " + param._id, 404);
}
throw error;
}
attachment_dict[param._attachment] = param._blob;
}; };
MemoryStorage.prototype.removeAttachment = function (param) { MemoryStorage.prototype.removeAttachment = function (param) {
try {
delete this._database[param._id].attachments[param._attachment]; delete this._database[param._id].attachments[param._attachment];
} catch (error) {
if (error instanceof TypeError) {
throw new jIO.util.jIOError(
"Cannot find document: " + param._id,
404
);
}
throw error;
}
}; };
MemoryStorage.prototype.hasCapacity = function (name) { MemoryStorage.prototype.hasCapacity = function (name) {
return (name === "list"); return (name === "list");
}; };
......
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