Commit e5ac3ede authored by Tristan Cavelier's avatar Tristan Cavelier

getAttachment and remaveAttachment added to the JIO API + test updated

jio.get({"_id": "a/b"});
changed to
jio.getAttachment({"_id": "a", "_attachment": "b"});
parent 04f8deb9
This diff is collapsed.
......@@ -45,16 +45,14 @@
setTimeout(function () {
that.success({
"ok": true,
"id": command.getDocId() + "/" + command.getAttachmentId()
"id": command.getDocId(),
"attachment": command.getAttachmentId()
});
}, 100); // 100 ms, for jiotests simple job waiting
}; // end putAttachment
that.get = function (command) {
setTimeout(function () {
if (command.getAttachmentId()) {
return that.success('0123456789');
}
that.success({
"_id": command.getDocId(),
"title": 'get_title'
......@@ -62,33 +60,42 @@
}, 100); // 100 ms, for jiotests simple job waiting
}; // end get
that.getAttachment = function (command) {
setTimeout(function () {
that.success('0123456789');
}, 100); // 100 ms, for jiotests simple job waiting
}; // end putAttachment
that.allDocs = function () {
setTimeout(function () {
that.error({
"status": 405,
"statusText": "Method Not Allowed",
"error": "method_not_allowed",
"message": "Your are not allowed to use" + "this command",
"reason": "LocalStorage forbids AllDocs" + "command executions"
"message": "Your are not allowed to use this command",
"reason": "Dummystorage forbids AllDocs command executions"
});
});
}; // end allDocs
that.remove = function (command) {
setTimeout(function () {
if (command.getAttachmentId()) {
that.success({
"ok": true,
"id": command.getDocId() + "/" + command.getAttachmentId()
"id": command.getDocId()
});
} else {
}, 100); // 100 ms, for jiotests simple job waiting
}; // end remove
that.removeAttachment = function (command) {
setTimeout(function () {
that.success({
"ok": true,
"id": command.getDocId()
"id": command.getDocId(),
"attachment": command.getAttachmentId()
});
}
}, 100); // 100 ms, for jiotests simple job waiting
}; // end remove
}; // end putAttachment
return that;
},
......@@ -130,6 +137,10 @@
priv.error();
}; // end get
that.getAttachment = function () {
priv.error();
};
that.allDocs = function () {
setTimeout(function () {
that.error({
......@@ -145,6 +156,11 @@
that.remove = function () {
priv.error();
}; // end remove
that.removeAttachment = function () {
priv.error();
};
return that;
},
// end Dummy Storage All Fail
......@@ -177,7 +193,8 @@
setTimeout(function () {
that.success({
"ok": true,
"id": command.getDocId() + "/" + command.getAttachmentId()
"id": command.getDocId(),
"attachment": command.getAttachmentId()
});
}, 100);
}; // end put
......@@ -193,28 +210,55 @@
});
}, 100);
}; // end get
that.getAttachment = function (command) {
setTimeout(function () {
that.error({
"status": 404,
"statusText": "Not Found",
"error": "not_found",
"message": "Attachment not found",
"reason": "Document '" + command.getDocId() + "'does not exist"
});
}, 100);
}; // end get
that.allDocs = function () {
setTimeout(function () {
that.error({
"status": 405,
"statusText": "Method Not Allowed",
"error": "method_not_allowed",
"message": "Your are not allowed to use" + "this command",
"reason": "LocalStorage forbids AllDocs" + "command executions"
"message": "Your are not allowed to use this command",
"reason": "Dummystorage forbids AllDocs command executions"
});
});
}; // end allDocs
that.remove = function () {
setTimeout(function () {
that.error({
"status": 404,
"statusText": "Not Found",
"error": "not_found",
"message": "Cannot remove an unexistant" + "document",
"message": "Cannot remove an inexistent document",
"reason": "missing" // or deleted
});
}, 100);
}; // end remove
that.removeAttachment = function () {
setTimeout(function () {
that.error({
"status": 404,
"statusText": "Not Found",
"error": "not_found",
"message": "Cannot remove an inexistent attachment",
"reason": "missing" // or deleted
});
}, 100);
}; // end remove
return that;
},
// end Dummy Storage All Not Found
......@@ -449,45 +493,50 @@
setTimeout(function () {
that.success({
"ok": true,
"id": command.getDocId() + "/" + command.getAttachmentId()
"id": command.getDocId(),
"attachment": command.getAttachmentId()
});
}, 100);
}; // end put
}; // end putAttachment
that.get = function (command) {
setTimeout(function () {
if (command.getAttachmentId()) {
return that.success('0123456789');
}
that.success({
"_id": command.getDocId(),
"title": 'get_title'
});
}, 100); // 100 ms, for jiotests simple job waiting
}; // end get
that.getAttachment = function (command) {
setTimeout(function () {
that.success('0123456789');
}, 100); // 100 ms, for jiotests simple job waiting
}; // end getAttachment
that.allDocs = function () {
setTimeout(function () {
that.error({
"status": 405,
"statusText": "Method Not Allowed",
"error": "method_not_allowed",
"message": "Your are not allowed to use" + "this command",
"reason": "LocalStorage forbids AllDocs" + "command executions"
"message": "Your are not allowed to use this command",
"reason": "Dummystorage forbids AllDocs command executions"
});
});
}; // end allDocs
that.remove = function (command) {
setTimeout(function () {
if (command.getAttachmentId()) {
that.success({
"ok": true,
"id": command.getDocId() + "/" + command.getAttachmentId()
"id": command.getDocId()
});
} else {
}, 100); // 100 ms, for jiotests simple job waiting
}; // end remove
that.removeAttachment = function (command) {
setTimeout(function () {
that.success({
"ok": true,
"id": command.getDocId()
"id": command.getDocId(),
"attachment": command.getAttachmentId()
});
}
}, 100); // 100 ms, for jiotests simple job waiting
}; // end remove
return that;
......
......@@ -569,22 +569,17 @@ jIO.addStorageType('indexed', function (spec, my) {
* @param {string} source The source of the function call
*/
priv.postOrPut = function (command, source) {
var f = {}, indices, doc, docid;
var f = {}, indices, doc;
doc = command.cloneDoc();
docid = command.getDocId();
if (typeof docid !== "string") {
if (typeof doc._id !== "string") {
doc._id = priv.generateUuid();
docid = doc._id;
}
f.getIndices = function () {
var option = command.cloneOption();
if (option.max_retry === 0) {
option.max_retry = 3;
}
that.addJob(
"get",
priv.substorage,
priv.index_suffix,
{"_id": priv.index_suffix},
option,
function (response) {
indices = response;
......@@ -639,10 +634,10 @@ jIO.addStorageType('indexed', function (spec, my) {
if (source !== 'PUTATTACHMENT') {
f.sendIndices(index_update_method);
} else {
docid = docid + '/' + command.getAttachmentId();
that.success({
"ok": true,
"id": docid
"id": doc._id,
"attachment": doc._attachment
});
}
},
......@@ -655,7 +650,7 @@ jIO.addStorageType('indexed', function (spec, my) {
} else {
that.success({
"ok": true,
"id": docid
"id": doc._id
});
}
break;
......@@ -677,7 +672,7 @@ jIO.addStorageType('indexed', function (spec, my) {
function () {
that.success({
"ok": true,
"id": docid
"id": doc._id
});
},
function (err) {
......@@ -718,47 +713,47 @@ jIO.addStorageType('indexed', function (spec, my) {
};
/**
* Get the document metadata or attachment.
* Options:
* - {boolean} revs Add simple revision history (false by default).
* - {boolean} revs_info Add revs info (false by default).
* - {boolean} conflicts Add conflict object (false by default).
* Get the document metadata
* @method get
* @param {object} command The JIO command
*/
that.get = function (command) {
var option, docid;
option = command.cloneOption();
if (option.max_retry === 0) {
option.max_retry = 3;
}
if (command.getAttachmentId() !== undefined) {
docid = command.getDocId() + '/' + command.getAttachmentId();
} else {
docid = command.getDocId();
}
that.addJob(
"get",
priv.substorage,
docid,
option,
command.cloneDoc(),
command.cloneOption(),
function (response) {
that.success(response);
},
function () {
that.error({
"status": 404,
"statusText": "Not Found",
"error": "not_found",
"message": "Cannot find the attachment",
"reason": "Document/Attachment not found"
});
function (err) {
that.error(err);
}
);
};
/**
* Remove document or attachment - removing documents updates index!.
* Get the attachment.
* @method getAttachment
* @param {object} command The JIO command
*/
that.getAttachment = function (command) {
that.addJob(
"getAttachment",
priv.substorage,
command.cloneDoc(),
command.cloneOption(),
function (response) {
that.success(response);
},
function (err) {
that.error(err);
}
);
};
/**
* Remove document - removing documents updates index!.
* @method remove
* @param {object} command The JIO command
*/
......@@ -767,20 +762,12 @@ jIO.addStorageType('indexed', function (spec, my) {
doc = command.cloneDoc();
option = command.cloneOption();
if (option.max_retry === 0) {
option.max_retry = 3;
}
f.removeDocument = function (type) {
if (type === 'doc') {
docid = command.getDocId();
} else {
docid = command.getDocId() + '/' + command.getAttachmentId();
}
that.addJob(
"remove",
priv.substorage,
docid,
doc,
option,
function (response) {
that.success(response);
......@@ -800,7 +787,7 @@ jIO.addStorageType('indexed', function (spec, my) {
that.addJob(
"get",
priv.substorage,
priv.index_suffix,
{"_id": priv.index_suffix},
option,
function (response) {
// if deleting an attachment
......@@ -840,6 +827,62 @@ jIO.addStorageType('indexed', function (spec, my) {
f.getIndices();
};
/**
* Remove document - removing documents updates index!.
* @method remove
* @param {object} command The JIO command
*/
that.removeAttachment = function (command) {
var f = {}, indices, doc, docid, option;
doc = command.cloneDoc();
option = command.cloneOption();
f.removeDocument = function (type) {
that.addJob(
"removeAttachment",
priv.substorage,
doc,
option,
that.success,
that.error
);
};
f.getIndices = function () {
that.addJob(
"get",
priv.substorage,
{"_id": priv.index_suffix},
option,
function (response) {
// if deleting an attachment
if (typeof command.getAttachmentId() === 'string') {
f.removeDocument('attachment');
} else {
indices = priv.cleanIndices(response, doc);
// store update index file
that.addJob(
"put",
priv.substorage,
indices,
command.cloneOption(),
function () {
// remove actual document
f.removeDocument('doc');
},
function (err) {
err.message = "Cannot save index file";
that.error(err);
}
);
}
},
function (err) {
that.error(err);
}
);
};
f.getIndices();
};
/**
* Gets a document list from the substorage
* Options:
......@@ -863,15 +906,12 @@ jIO.addStorageType('indexed', function (spec, my) {
var f = {}, option, all_docs_response, query_object, query_syntax,
query_response;
option = command.cloneOption();
if (option.max_retry === 0) {
option.max_retry = 3;
}
f.getIndices = function () {
that.addJob(
"get",
priv.substorage,
priv.index_suffix,
{"_id": priv.index_suffix},
option,
function (response) {
query_syntax = command.getOption('query');
......@@ -887,31 +927,8 @@ jIO.addStorageType('indexed', function (spec, my) {
priv.substorage,
undefined,
option,
function (data) {
that.success(data);
},
function (err) {
switch (err.status) {
case 405:
that.error({
"status": 405,
"statusText": "Method Not Allowed",
"error": "method_not_allowed",
"message": "Method not allowed",
"reason": "Could not run AllDocs on this storage"
});
break;
default:
that.error({
"status": 404,
"statusText": "Not Found",
"error": "not_found",
"message": "Could not run allDocs command",
"reason": "There are no documents in the storage"
});
break;
}
}
that.success,
that.error
);
} else {
// we can use index, run query on index
......@@ -927,16 +944,7 @@ jIO.addStorageType('indexed', function (spec, my) {
that.success(all_docs_response);
}
},
function () {
that.error({
"status": 404,
"statusText": "Not Found",
"error": "not_found",
"message": "Document index not found",
"reason": "There are no documents in the storage"
});
return;
}
that.error
);
};
f.getIndices();
......
......@@ -203,23 +203,20 @@ jIO.addStorageType('local', function (spec, my) {
localstorage.setItem(priv.localpath + "/" + command.getDocId(), doc);
that.success({
"ok": true,
"id": command.getDocId() + "/" + command.getAttachmentId()
"id": command.getDocId(),
"attachment": command.getAttachmentId()
});
});
};
/**
* Get a document or attachment
* Get a document
* @method get
* @param {object} command The JIO command
*/
that.get = function (command) {
setTimeout(function () {
var doc;
if (typeof command.getAttachmentId() === "string") {
// seeking for an attachment
doc = localstorage.getItem(priv.localpath + "/" + command.getDocId() +
"/" + command.getAttachmentId());
var doc = localstorage.getItem(priv.localpath + "/" + command.getDocId());
if (doc !== null) {
that.success(doc);
} else {
......@@ -227,13 +224,22 @@ jIO.addStorageType('local', function (spec, my) {
"status": 404,
"statusText": "Not Found",
"error": "not_found",
"message": "Cannot find the attachment",
"reason": "Attachment does not exist"
"message": "Cannot find the document",
"reason": "Document does not exist"
});
}
} else {
// seeking for a document
doc = localstorage.getItem(priv.localpath + "/" + command.getDocId());
});
};
/**
* Get a attachment
* @method getAttachment
* @param {object} command The JIO command
*/
that.getAttachment = function (command) {
setTimeout(function () {
var doc = localstorage.getItem(priv.localpath + "/" + command.getDocId() +
"/" + command.getAttachmentId());
if (doc !== null) {
that.success(doc);
} else {
......@@ -241,20 +247,60 @@ jIO.addStorageType('local', function (spec, my) {
"status": 404,
"statusText": "Not Found",
"error": "not_found",
"message": "Cannot find the document",
"reason": "Document does not exist"
"message": "Cannot find the attachment",
"reason": "Attachment does not exist"
});
}
}
});
};
/**
* Remove a document or attachment
* Remove a document
* @method remove
* @param {object} command The JIO command
*/
that.remove = function (command) {
setTimeout(function () {
var doc, i, attachment_list;
doc = localstorage.getItem(priv.localpath + "/" + command.getDocId());
attachment_list = [];
if (doc !== null && typeof doc === "object") {
if (typeof doc._attachments === "object") {
// prepare list of attachments
for (i in doc._attachments) {
if (doc._attachments.hasOwnProperty(i)) {
attachment_list.push(i);
}
}
}
} else {
return that.error({
"status": 404,
"statusText": "Not Found",
"error": "not_found",
"message": "Document not found",
"reason": "missing"
});
}
localstorage.removeItem(priv.localpath + "/" + command.getDocId());
// delete all attachments
for (i = 0; i < attachment_list.length; i += 1) {
localstorage.removeItem(priv.localpath + "/" + command.getDocId() +
"/" + attachment_list[i]);
}
that.success({
"ok": true,
"id": command.getDocId()
});
});
};
/**
* Remove an attachment
* @method removeAttachment
* @param {object} command The JIO command
*/
that.removeAttachment = function (command) {
setTimeout(function () {
var doc, error, i, attachment_list;
error = function (word) {
......@@ -267,7 +313,6 @@ jIO.addStorageType('local', function (spec, my) {
});
};
doc = localstorage.getItem(priv.localpath + "/" + command.getDocId());
if (typeof command.getAttachmentId() === "string") {
// remove attachment from document
if (doc !== null && typeof doc === "object" &&
typeof doc._attachments === "object") {
......@@ -283,7 +328,8 @@ jIO.addStorageType('local', function (spec, my) {
"/" + command.getAttachmentId());
that.success({
"ok": true,
"id": command.getDocId() + "/" + command.getAttachmentId()
"id": command.getDocId(),
"attachment": command.getAttachmentId()
});
} else {
error("Attachment");
......@@ -291,32 +337,6 @@ jIO.addStorageType('local', function (spec, my) {
} else {
error("Document");
}
} else {
// seeking for a document
attachment_list = [];
if (doc !== null && typeof doc === "object") {
if (typeof doc._attachments === "object") {
// prepare list of attachments
for (i in doc._attachments) {
if (doc._attachments.hasOwnProperty(i)) {
attachment_list.push(i);
}
}
}
} else {
return error("Document");
}
localstorage.removeItem(priv.localpath + "/" + command.getDocId());
// delete all attachments
for (i = 0; i < attachment_list.length; i += 1) {
localstorage.removeItem(priv.localpath + "/" + command.getDocId() +
"/" + attachment_list[i]);
}
that.success({
"ok": true,
"id": command.getDocId()
});
}
});
};
......
......@@ -413,10 +413,11 @@ jIO.addStorageType('replicaterevision', function (spec, my) {
if ((parsed_response._attachments).hasOwnProperty(attachment)) {
functions.get_attachment_count += 1;
priv.send(
"get",
"getAttachment",
param.responses.stats[response][0],
{
"_id": param.doc._id + "/" + attachment,
"_id": param.doc._id,
"_attachment": attachment,
"_rev": JSON.parse(response)._rev
},
param.option,
......@@ -550,8 +551,6 @@ jIO.addStorageType('replicaterevision', function (spec, my) {
// "_revs_info": param.responses.list[index]._revs_info,
"_data": param.responses.attachments[attachment_to_put[i]._id]
};
attachment._id += "/" + attachment._attachment;
delete attachment._attachment;
priv.send(
"putAttachment",
index,
......
......@@ -366,9 +366,15 @@ jIO.addStorageType("revision", function (spec, my) {
priv.remove = function (doc, option, callback) {
priv.send("remove", doc, option, callback);
};
priv.getAttachment = function (attachment, option, callback) {
priv.send("getAttachment", attachment, option, callback);
};
priv.putAttachment = function (attachment, option, callback) {
priv.send("putAttachment", attachment, option, callback);
};
priv.removeAttachment = function (attachment, option, callback) {
priv.send("removeAttachment", attachment, option, callback);
};
priv.getDocument = function (doc, option, callback) {
doc = priv.clone(doc);
......@@ -379,7 +385,6 @@ jIO.addStorageType("revision", function (spec, my) {
delete doc._revs_info;
priv.get(doc, option, callback);
};
priv.getAttachment = priv.get;
priv.putDocument = function (doc, option, callback) {
doc = priv.clone(doc);
doc._id = doc._id + "." + doc._rev;
......@@ -428,8 +433,8 @@ jIO.addStorageType("revision", function (spec, my) {
for (attachment_id in doc._attachments) {
if (doc._attachments.hasOwnProperty(attachment_id)) {
count += 1;
priv.get(
{"_id": doc._id + "/" + attachment_id},
priv.getAttachment(
{"_id": doc._id, "_attachment": attachment_id},
option,
dealResults(attachment_id, doc._attachments[attachment_id])
);
......@@ -463,9 +468,7 @@ jIO.addStorageType("revision", function (spec, my) {
attachment = attachment_list[i];
if (attachment !== undefined) {
count += 1;
attachment._id = doc._id + "." + doc._rev + "/" +
attachment._attachment;
delete attachment._attachment;
attachment._id = doc._id + "." + doc._rev;
priv.putAttachment(attachment, option, dealResults(i));
}
}
......@@ -729,18 +732,22 @@ jIO.addStorageType("revision", function (spec, my) {
priv.putDocumentTree(doc, option, doc_tree, callback.putDocumentTree);
};
callback.putDocumentTree = function (err, response) {
var response_object;
if (err) {
err.message = "Cannot update the document history";
return onEnd(err, undefined);
}
onEnd(undefined, {
response_object = {
"ok": true,
"id": doc._id + (specific_parameter.putAttachment ||
specific_parameter.removeAttachment ||
specific_parameter.getAttachment ?
"/" + doc._attachment : ""),
"id": doc._id,
"rev": doc._rev
});
};
if (specific_parameter.putAttachment ||
specific_parameter.removeAttachment ||
specific_parameter.getAttachment) {
response_object.attachment = doc._attachment;
}
onEnd(undefined, response_object);
// if (option.keep_revision_history !== true) {
// // priv.remove(prev_doc, option, function () {
// // - change "available" status to "deleted"
......
/*jslint indent: 2, maxlen: 80, sloppy: true, nomen: true */
/*global postCommand: true, putCommand: true, getCommand: true,
removeCommand: true, allDocsCommand: true,
getAttachmentCommand: true, removeAttachmentCommand: true,
putAttachmentCommand: true, failStatus: true, doneStatus: true,
checkCommand: true, repairCommand: true,
hex_md5: true */
......@@ -17,7 +18,9 @@ var command = function (spec, my) {
'get': getCommand,
'remove': removeCommand,
'allDocs': allDocsCommand,
'getAttachment': getAttachmentCommand,
'putAttachment': putAttachmentCommand,
'removeAttachment': removeAttachmentCommand,
'check': checkCommand,
'repair': repairCommand
};
......@@ -35,7 +38,6 @@ var command = function (spec, my) {
"_id": priv.doc.toString()
};
}
priv.docid = spec.docid || priv.doc._id;
priv.option = spec.options || {};
priv.callbacks = spec.callbacks || {};
priv.success = [priv.callbacks.success || function () {}];
......@@ -82,10 +84,7 @@ var command = function (spec, my) {
* @return {string} The document id
*/
that.getDocId = function () {
if (typeof priv.docid !== "string") {
return undefined;
}
return priv.docid.split('/')[0];
return priv.doc._id;
};
/**
......@@ -94,19 +93,7 @@ var command = function (spec, my) {
* @return {string} The attachment id
*/
that.getAttachmentId = function () {
if (typeof priv.docid !== "string") {
return undefined;
}
return priv.docid.split('/')[1];
};
/**
* Returns the label of the command.
* @method getDoc
* @return {object} The document.
*/
that.getDoc = function () {
return priv.doc;
return priv.doc._attachment;
};
/**
......@@ -170,8 +157,8 @@ var command = function (spec, my) {
* @param {object} storage The storage.
*/
that.validate = function (storage) {
if (typeof priv.docid === "string" &&
!priv.docid.match("^[^\/]+([\/][^\/]+)?$")) {
if (typeof priv.doc._id === "string" &&
!priv.doc._id.match("^[^\/]+([\/][^\/]+)?$")) {
that.error({
status: 21,
statusText: 'Invalid Document Id',
......
/*jslint indent: 2, maxlen: 80, sloppy: true */
/*global command: true */
var getAttachmentCommand = function (spec, my) {
var that = command(spec, my);
spec = spec || {};
my = my || {};
// Attributes //
// Methods //
that.getLabel = function () {
return 'getAttachment';
};
that.executeOn = function (storage) {
storage.getAttachment(that);
};
that.validateState = function () {
if (!(typeof that.getDocId() === "string" && that.getDocId() !==
"")) {
that.error({
"status": 20,
"statusText": "Document Id Required",
"error": "document_id_required",
"message": "The document id is not provided",
"reason": "Document id is undefined"
});
return false;
}
if (typeof that.getAttachmentId() !== "string") {
that.error({
"status": 22,
"statusText": "Attachment Id Required",
"error": "attachment_id_required",
"message": "The attachment id must be set",
"reason": "Attachment id not set"
});
return false;
}
if (that.getAttachmentId() === "") {
that.error({
"status": 23,
"statusText": "Invalid Attachment Id",
"error": "invalid_attachment_id",
"message": "The attachment id must not be an empty string",
"reason": "Attachment id is empty"
});
}
return true;
};
return that;
};
......@@ -23,18 +23,6 @@ var getCommand = function (spec, my) {
});
return false;
}
if (typeof that.getAttachmentId() === "string") {
if (that.getAttachmentId() === "") {
that.error({
"status": 23,
"statusText": "Invalid Attachment Id",
"error": "invalid_attachment_id",
"message": "The attachment id must not be an empty string",
"reason": "Attachment id is empty"
});
return false;
}
}
return true;
};
......
......@@ -12,17 +12,6 @@ var postCommand = function (spec, my) {
};
that.validateState = function () {
if (that.getAttachmentId() !== undefined) {
that.error({
"status": 21,
"statusText": "Invalid Document Id",
"error": "invalid_document_id",
"message": "The document id contains '/' characters " +
"which are forbidden",
"reason": "Document id contains '/' character(s)"
});
return false;
}
return true;
};
that.executeOn = function (storage) {
......
......@@ -22,17 +22,6 @@ var putCommand = function (spec, my) {
});
return false;
}
if (that.getAttachmentId() !== undefined) {
that.error({
"status": 21,
"statusText": "Invalid Document Id",
"error": "invalid_document_id",
"message": "The document id contains '/' characters " +
"which are forbidden",
"reason": "Document id contains '/' character(s)"
});
return false;
}
return true;
};
that.executeOn = function (storage) {
......
/*jslint indent: 2, maxlen: 80, sloppy: true */
/*global command: true */
var removeAttachmentCommand = function (spec, my) {
var that = command(spec, my);
spec = spec || {};
my = my || {};
// Attributes //
// Methods //
that.getLabel = function () {
return 'removeAttachment';
};
that.executeOn = function (storage) {
storage.removeAttachment(that);
};
that.validateState = function () {
if (!(typeof that.getDocId() === "string" && that.getDocId() !==
"")) {
that.error({
"status": 20,
"statusText": "Document Id Required",
"error": "document_id_required",
"message": "The document id is not provided",
"reason": "Document id is undefined"
});
return false;
}
if (typeof that.getAttachmentId() !== "string") {
that.error({
"status": 22,
"statusText": "Attachment Id Required",
"error": "attachment_id_required",
"message": "The attachment id must be set",
"reason": "Attachment id not set"
});
return false;
}
if (that.getAttachmentId() === "") {
that.error({
"status": 23,
"statusText": "Invalid Attachment Id",
"error": "invalid_attachment_id",
"message": "The attachment id must not be an empty string",
"reason": "Attachment id is empty"
});
}
return true;
};
return that;
};
......@@ -22,18 +22,6 @@ var removeCommand = function (spec, my) {
});
return false;
}
if (typeof that.getAttachmentId() === "string") {
if (that.getAttachmentId() === "") {
that.error({
"status": 23,
"statusText": "Invalid Attachment Id",
"error": "invalid_attachment_id",
"message": "The attachment id must not be an empty string",
"reason": "Attachment id is empty"
});
return false;
}
}
return true;
};
......
......@@ -4,6 +4,7 @@
storage_type_object: true, invalidStorageType: true, jobRules: true,
job: true, postCommand: true, putCommand: true, getCommand:true,
allDocsCommand: true, putAttachmentCommand: true,
getAttachmentCommand: true, removeAttachmentCommand: true,
removeCommand: true, checkCommand: true, repairCommand: true */
// Class jio
var that = {}, priv = {}, jio_id_array_name = 'jio/id_array';
......@@ -213,12 +214,13 @@ priv.addJob = function (commandCreator, spec) {
* Post a document.
* @method post
* @param {object} doc The document object. Contains at least:
* - {string} _id The document id (optional), "/" are forbidden
* - {string} _id The document id (optional)
* For revision managing: choose at most one of the following informations:
* - {string} _rev The revision we want to update
* - {string} _revs_info The revision information we want the document to have
* - {string} _revs The revision history we want the document to have
* @param {object} options (optional) Contains some options:
* - {number} max_retry The number max of retries, 0 = infinity.
* - {boolean} revs Include revision history of the document.
* - {boolean} revs_info Retreive the revisions.
* - {boolean} conflicts Retreive the conflict list.
* @param {function} callback (optional) The callback(err,response).
* @param {function} error (optional) The callback on error, if this
* callback is given in parameter, "callback" is changed as "success",
......@@ -246,12 +248,13 @@ Object.defineProperty(that, "post", {
* Put a document.
* @method put
* @param {object} doc The document object. Contains at least:
* - {string} _id The document id, "/" are forbidden
* - {string} _id The document id
* For revision managing: choose at most one of the following informations:
* - {string} _rev The revision we want to update
* - {string} _revs_info The revision information we want the document to have
* - {string} _revs The revision history we want the document to have
* @param {object} options (optional) Contains some options:
* - {number} max_retry The number max of retries, 0 = infinity.
* - {boolean} revs Include revision history of the document.
* - {boolean} revs_info Retreive the revisions.
* - {boolean} conflicts Retreive the conflict list.
* @param {function} callback (optional) The callback(err,response).
* @param {function} error (optional) The callback on error, if this
* callback is given in parameter, "callback" is changed as "success",
......@@ -279,10 +282,12 @@ Object.defineProperty(that, "put", {
* Get a document.
* @method get
* @param {string} doc The document object. Contains at least:
* - {string} _id The document id: "doc_id" or "doc_id/attachment_id"
* - {string} _id The document id
* For revision managing:
* - {string} _rev The revision we want to get. (optional)
* @param {object} options (optional) Contains some options:
* - {number} max_retry The number max of retries, 0 = infinity.
* - {string} rev The revision we want to get.
* For revision managing:
* - {boolean} revs Include revision history of the document.
* - {boolean} revs_info Include list of revisions, and their availability.
* - {boolean} conflicts Include a list of conflicts.
......@@ -313,12 +318,11 @@ Object.defineProperty(that, "get", {
* Remove a document.
* @method remove
* @param {object} doc The document object. Contains at least:
* - {string} _id The document id: "doc_id" or "doc_id/attachment_id"
* - {string} _id The document id
* For revision managing:
* - {string} _rev The revision we want to remove
* @param {object} options (optional) Contains some options:
* - {number} max_retry The number max of retries, 0 = infinity.
* - {boolean} revs Include revision history of the document.
* - {boolean} revs_info Include list of revisions, and their availability.
* - {boolean} conflicts Include a list of conflicts.
* @param {function} callback (optional) The callback(err,response).
* @param {function} error (optional) The callback on error, if this
* callback is given in parameter, "callback" is changed as "success",
......@@ -348,9 +352,6 @@ Object.defineProperty(that, "remove", {
* @param {object} options (optional) Contains some options:
* - {number} max_retry The number max of retries, 0 = infinity.
* - {boolean} include_docs Include document metadata
* - {boolean} revs Include revision history of the document.
* - {boolean} revs_info Include revisions.
* - {boolean} conflicts Include conflicts.
* @param {function} callback (optional) The callback(err,response).
* @param {function} error (optional) The callback on error, if this
* callback is given in parameter, "callback" is changed as "success",
......@@ -373,19 +374,51 @@ Object.defineProperty(that, "allDocs", {
}
});
/**
* Get an attachment from a document.
* @method gettAttachment
* @param {object} doc The document object. Contains at least:
* - {string} _id The document id
* - {string} _attachment The attachment id
* For revision managing:
* - {string} _rev The document revision
* @param {object} options (optional) Contains some options:
* - {number} max_retry The number max of retries, 0 = infinity.
* @param {function} callback (optional) The callback(err,respons)
* @param {function} error (optional) The callback on error, if this
* callback is given in parameter, "callback" is changed as "success",
* called on success.
*/
Object.defineProperty(that, "getAttachment", {
configurable: false,
enumerable: false,
writable: false,
value: function (doc, options, success, error) {
var param = priv.parametersToObject(
[options, success, error],
{max_retry: 3}
);
priv.addJob(getAttachmentCommand, {
doc: doc,
options: param.options,
callbacks: {success: param.success, error: param.error}
});
}
});
/**
* Put an attachment to a document.
* @method putAttachment
* @param {object} doc The document object. Contains at least:
* - {string} _id The document id: "doc_id/attchment_id"
* - {string} _data Base64 attachment data
* - {string} _id The document id
* - {string} _attachment The attachment id
* - {string} _data The attachment data
* - {string} _mimetype The attachment mimetype
* - {string} _rev The attachment revision
* For revision managing:
* - {string} _rev The document revision
* @param {object} options (optional) Contains some options:
* - {number} max_retry The number max of retries, 0 = infinity.
* - {boolean} revs Include revision history of the document.
* - {boolean} revs_info Include revisions.
* - {boolean} conflicts Include conflicts.
* @param {function} callback (optional) The callback(err,respons)
* @param {function} error (optional) The callback on error, if this
* callback is given in parameter, "callback" is changed as "success",
......@@ -409,6 +442,39 @@ Object.defineProperty(that, "putAttachment", {
}
});
/**
* Put an attachment to a document.
* @method putAttachment
* @param {object} doc The document object. Contains at least:
* - {string} _id The document id
* - {string} _attachment The attachment id
* For revision managing:
* - {string} _rev The document revision
* @param {object} options (optional) Contains some options:
* - {number} max_retry The number max of retries, 0 = infinity.
* @param {function} callback (optional) The callback(err,respons)
* @param {function} error (optional) The callback on error, if this
* callback is given in parameter, "callback" is changed as "success",
* called on success.
*/
Object.defineProperty(that, "removeAttachment", {
configurable: false,
enumerable: false,
writable: false,
value: function (doc, options, success, error) {
var param = priv.parametersToObject(
[options, success, error],
{max_retry: 0}
);
priv.addJob(removeAttachmentCommand, {
doc: doc,
options: param.options,
callbacks: {success: param.success, error: param.error}
});
}
});
/**
* Check a document.
* @method check
......
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