Commit 3bd42c70 authored by Vincent Bechu's avatar Vincent Bechu

mappingstorage: add attachment_uri_template,query and tests

parent 558fde9f
/*jslint indent:2, maxlen: 80, nomen: true */ /*jslint indent:2, maxlen: 80, nomen: true */
/*global jIO, RSVP */ /*global jIO, RSVP, UriTemplate */
(function (jIO, RSVP) { (function (jIO, RSVP) {
"use strict"; "use strict";
...@@ -8,11 +8,29 @@ ...@@ -8,11 +8,29 @@
this._default_dict = spec.default_dict || {}; this._default_dict = spec.default_dict || {};
this._sub_storage = jIO.createJIO(spec.sub_storage); this._sub_storage = jIO.createJIO(spec.sub_storage);
this._map_all_property = spec.map_all_property || false; this._map_all_property = spec.map_all_property || false;
this._mapping_dict_attachment = spec.mapping_dict_attachment || undefined;
this._query = spec.query || {};
this._id_is_mapped = (this._mapping_dict.id !== undefined this._id_is_mapped = (this._mapping_dict.id !== undefined
&& this._mapping_dict.id.equal !== "id"); && this._mapping_dict.id.equal !== "id");
} }
function getAttachmentId(storage, sub_id, attachment_id, method) {
var mapping_dict = storage._mapping_dict_attachment;
return new RSVP.Queue()
.push(function () {
if (mapping_dict !== undefined
&& mapping_dict[attachment_id] !== undefined
&& mapping_dict[attachment_id].uri_template !== undefined) {
return UriTemplate.parse(
mapping_dict[attachment_id][method].uri_template
).expand({id: sub_id});
}
return attachment_id;
});
}
function getSubStorageId(storage, index) { function getSubStorageId(storage, index) {
var query; var query;
return new RSVP.Queue() return new RSVP.Queue()
...@@ -23,8 +41,8 @@ ...@@ -23,8 +41,8 @@
} }
if (storage._mapping_dict.id.equal !== undefined) { if (storage._mapping_dict.id.equal !== undefined) {
query = storage._mapping_dict.id.equal + ': "' + index + '"'; query = storage._mapping_dict.id.equal + ': "' + index + '"';
if (storage._mapping_dict.id.query_limit !== undefined) { if (storage._query.query !== undefined) {
query += ' AND ' + storage._mapping_dict.id.query_limit; query += ' AND ' + storage._query.query;
} }
for (property in storage._default_dict) { for (property in storage._default_dict) {
if (storage._default_dict.hasOwnProperty(property)) { if (storage._default_dict.hasOwnProperty(property)) {
...@@ -32,7 +50,12 @@ ...@@ -32,7 +50,12 @@
+ storage._default_dict[property].equal + '"'; + storage._default_dict[property].equal + '"';
} }
} }
return storage._sub_storage.allDocs({"query": query}) return storage._sub_storage.allDocs({
"query": query,
"sort_on": storage._query.sort_on,
"select_list": storage._query.select_list,
"limit": storage._query.limit
})
.push(function (data) { .push(function (data) {
if (data.data.rows.length === 0) { if (data.data.rows.length === 0) {
return undefined; return undefined;
...@@ -185,32 +208,45 @@ ...@@ -185,32 +208,45 @@
}); });
}; };
MappingStorage.prototype.putAttachment = function (doc_id) { MappingStorage.prototype.putAttachment = function (id, attachment_id) {
var that = this, argument_list = arguments; var context = this, argument_list = arguments;
return getSubStorageId(this, doc_id) return getSubStorageId(context, id)
.push(function (id) { .push(function (sub_id) {
argument_list[0] = id; argument_list[0] = sub_id;
return that._sub_storage.putAttachment.apply(that._sub_storage, return getAttachmentId(context, sub_id, attachment_id, "put");
})
.push(function (sub_attachment_id) {
argument_list[1] = sub_attachment_id;
return context._sub_storage.putAttachment.apply(context._sub_storage,
argument_list); argument_list);
}); });
}; };
MappingStorage.prototype.getAttachment = function (doc_id) {
var that = this, argument_list = arguments; MappingStorage.prototype.getAttachment = function (id, attachment_id) {
return getSubStorageId(this, doc_id) var context = this, argument_list = arguments;
.push(function (id) { return getSubStorageId(context, id)
argument_list[0] = id; .push(function (sub_id) {
return that._sub_storage.getAttachment.apply(that._sub_storage, argument_list[0] = sub_id;
return getAttachmentId(context, sub_id, attachment_id, "get");
})
.push(function (sub_attachment_id) {
argument_list[1] = sub_attachment_id;
return context._sub_storage.getAttachment.apply(context._sub_storage,
argument_list); argument_list);
}); });
}; };
MappingStorage.prototype.removeAttachment = function (doc_id) { MappingStorage.prototype.removeAttachment = function (id, attachment_id) {
var that = this, argument_list = arguments; var context = this, argument_list = arguments;
return getSubStorageId(this, doc_id) return getSubStorageId(context, id)
.push(function (id) { .push(function (sub_id) {
argument_list[0] = id; argument_list[0] = sub_id;
return that._sub_storage.removeAttachment.apply(that._sub_storage, return getAttachmentId(context, sub_id, attachment_id, "remove");
})
.push(function (sub_attachment_id) {
argument_list[1] = sub_attachment_id;
return context._sub_storage.removeAttachment.apply(context._sub_storage,
argument_list); argument_list);
}); });
}; };
...@@ -219,15 +255,27 @@ ...@@ -219,15 +255,27 @@
return this._sub_storage.hasCapacity.apply(this._sub_storage, arguments); return this._sub_storage.hasCapacity.apply(this._sub_storage, arguments);
}; };
MappingStorage.prototype.bulk = function () {
var i, that = this, mapped_result = [];
return this._sub_storage.bulk.apply(arguments)
.push(function (result) {
for (i = 0; i < result.length; i += 1) {
mapped_result.push(mapDocument(that, result[i]), false);
}
return mapped_result;
});
};
MappingStorage.prototype.buildQuery = function (option) { MappingStorage.prototype.buildQuery = function (option) {
var that = this, var that = this,
i, i,
query, query,
property,
select_list = [], select_list = [],
sort_on = []; sort_on = [];
function mapQuery(one_query) { function mapQuery(one_query) {
var i, result = "", key; var i, result = "(", key;
if (one_query.type === "complex") { if (one_query.type === "complex") {
for (i = 0; i < one_query.query_list.length; i += 1) { for (i = 0; i < one_query.query_list.length; i += 1) {
result += "(" + mapQuery(one_query.query_list[i]) + ")"; result += "(" + mapQuery(one_query.query_list[i]) + ")";
...@@ -235,6 +283,7 @@ ...@@ -235,6 +283,7 @@
result += " " + one_query.operator + " "; result += " " + one_query.operator + " ";
} }
} }
result += ")";
return result; return result;
} }
if (that._mapping_dict.hasOwnProperty(one_query.key)) { if (that._mapping_dict.hasOwnProperty(one_query.key)) {
...@@ -251,19 +300,42 @@ ...@@ -251,19 +300,42 @@
if (option.sort_on !== undefined) { if (option.sort_on !== undefined) {
for (i = 0; i < option.sort_on.length; i += 1) { for (i = 0; i < option.sort_on.length; i += 1) {
sort_on.push([this._mapping_dict[option.sort_on[i][0]].equal, property = [this._mapping_dict[option.sort_on[i][0]].equal,
option.sort_on[i][1]]); option.sort_on[i][1]];
if (sort_on.indexOf(property) < 0) {
sort_on.push(property);
}
}
}
if (this._query.sort_on !== undefined) {
for (i = 0; i < this._query.sort_on.length; i += 1) {
property = this._query.sort_on[i];
if (sort_on.indexOf(property) < 0) {
sort_on.push(property);
}
} }
} }
if (option.select_list !== undefined) { if (option.select_list !== undefined) {
for (i = 0; i < option.select_list.length; i += 1) { for (i = 0; i < option.select_list.length; i += 1) {
property = false;
if (this._mapping_dict.hasOwnProperty(option.select_list[i])) { if (this._mapping_dict.hasOwnProperty(option.select_list[i])) {
select_list.push(this._mapping_dict[option.select_list[i]].equal); property = this._mapping_dict[option.select_list[i]].equal;
} else { } else {
if (this._map_all_property) { if (this._map_all_property) {
select_list.push(option.select_list[i]); property = option.select_list[i];
} }
} }
if (property && sort_on.indexOf(property) < 0) {
select_list.push(property);
}
}
}
if (this._query.select_list !== undefined) {
for (i = 0; i < this._query.select_list; i += 1) {
property = this._query.select_list[i];
if (select_list.indexOf(property) < 0) {
select_list.push(property);
}
} }
} }
if (this._id_is_mapped) { if (this._id_is_mapped) {
...@@ -272,8 +344,14 @@ ...@@ -272,8 +344,14 @@
if (option.query !== undefined) { if (option.query !== undefined) {
query = mapQuery(jIO.QueryFactory.create(option.query)); query = mapQuery(jIO.QueryFactory.create(option.query));
} }
if (this._mapping_dict.id.query_limit !== undefined) {
query += 'AND( ' + this._mapping_dict.id.query_limit + ' )'; if (this._query.query !== undefined) {
if (query !== undefined) {
query += ' AND ';
} else {
query = "";
}
query += this._query.query;
} }
return this._sub_storage.allDocs( return this._sub_storage.allDocs(
{ {
...@@ -298,5 +376,4 @@ ...@@ -298,5 +376,4 @@
}; };
jIO.addStorage('mapping', MappingStorage); jIO.addStorage('mapping', MappingStorage);
}(jIO, RSVP)); }(jIO, RSVP));
\ No newline at end of file
...@@ -139,7 +139,7 @@ ...@@ -139,7 +139,7 @@
}; };
Storage2713.prototype.buildQuery = function (options) { Storage2713.prototype.buildQuery = function (options) {
deepEqual(options, {query: 'otherId: "42"'}, "allDoc 2713 called"); equal(options.query, 'otherId: "42"', "allDoc 2713 called");
return [{id: "2713"}]; return [{id: "2713"}];
}; };
...@@ -161,7 +161,7 @@ ...@@ -161,7 +161,7 @@
}); });
}); });
test("get with id mapped and query_limit", function () { test("get with id mapped and query", function () {
stop(); stop();
expect(3); expect(3);
...@@ -172,8 +172,9 @@ ...@@ -172,8 +172,9 @@
}, },
mapping_dict: { mapping_dict: {
"title": {"equal": "otherTitle"}, "title": {"equal": "otherTitle"},
"id": {"equal": "otherId", "query_limit": 'otherTitle: "foo"'} "id": {"equal": "otherId"}
} },
query: {"query": 'otherTitle: "foo"'}
}); });
Storage2713.prototype.hasCapacity = function () { Storage2713.prototype.hasCapacity = function () {
...@@ -181,9 +182,9 @@ ...@@ -181,9 +182,9 @@
}; };
Storage2713.prototype.buildQuery = function (options) { Storage2713.prototype.buildQuery = function (options) {
deepEqual( equal(
options, options.query,
{query: 'otherId: "42" AND otherTitle: "foo"'}, 'otherId: "42" AND otherTitle: "foo"',
"allDoc 2713 called" "allDoc 2713 called"
); );
return [{id: "2713"}]; return [{id: "2713"}];
...@@ -227,9 +228,9 @@ ...@@ -227,9 +228,9 @@
}; };
Storage2713.prototype.buildQuery = function (options) { Storage2713.prototype.buildQuery = function (options) {
deepEqual( equal(
options, options.query,
{query: 'otherId: "42"'}, 'otherId: "42"',
"allDoc 2713 called" "allDoc 2713 called"
); );
return [{id: "2713"}]; return [{id: "2713"}];
...@@ -342,7 +343,7 @@ ...@@ -342,7 +343,7 @@
}; };
Storage2713.prototype.buildQuery = function (option) { Storage2713.prototype.buildQuery = function (option) {
deepEqual(option, {"query": 'otherId: "42"'}, "allDocs 2713 called"); equal(option.query, 'otherId: "42"', "allDocs 2713 called");
return []; return [];
}; };
...@@ -440,7 +441,7 @@ ...@@ -440,7 +441,7 @@
}; };
Storage2713.prototype.buildQuery = function (options) { Storage2713.prototype.buildQuery = function (options) {
deepEqual(options, {query: 'otherId: "42"'}, "allDoc 2713 called"); equal(options.query, 'otherId: "42"', "allDoc 2713 called");
return [{id: "2713"}]; return [{id: "2713"}];
}; };
...@@ -494,6 +495,36 @@ ...@@ -494,6 +495,36 @@
}); });
}); });
test("putAttachment with UriTemplate", function () {
stop();
expect(4);
var jio = jIO.createJIO({
type: "mapping",
sub_storage: {
type: "mappingstorage2713"
},
mapping_dict_attachment: {"2713": {"uri_template": "www.2713.foo/{id}"}}
}),
blob = new Blob([""]);
Storage2713.prototype.putAttachment = function (doc_id,
attachment_id, attachment) {
equal(doc_id, "42", "putAttachment 2713 called");
equal(attachment_id, "www.2713.foo/42", "putAttachment 2713 called");
deepEqual(attachment, blob, "putAttachment 2713 called");
return doc_id;
};
jio.putAttachment("42", "2713", blob)
.then(function (result) {
equal(result, "42");
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
// mappingStorage.getAttachment // mappingStorage.getAttachment
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
...@@ -526,12 +557,42 @@ ...@@ -526,12 +557,42 @@
}); });
}); });
test("getAttachment using UriTemplate", function () {
stop();
expect(3);
var jio = jIO.createJIO({
type: "mapping",
sub_storage: {
type: "mappingstorage2713"
},
mapping_dict_attachment: {
"2713": {"uri_template": "www.2713/{id}/ok.com"}
}
}),
blob = new Blob([""]);
Storage2713.prototype.getAttachment = function (doc_id, attachment) {
equal(attachment, "www.2713/42/ok.com", "getAttachment 2713 called");
equal(doc_id, "42", "getAttachment 2713 called");
return blob;
};
jio.getAttachment("42", "2713")
.then(function (result) {
deepEqual(result, blob);
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
// mappingStorage.removeAttachment // mappingStorage.removeAttachment
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
module("mappingStorage.removeAttachment"); module("mappingStorage.removeAttachment");
test("putAttachment use sub_storage one's", function () { test("removeAttachment use sub_storage one's", function () {
stop(); stop();
expect(3); expect(3);
var jio = jIO.createJIO({ var jio = jIO.createJIO({
...@@ -557,6 +618,33 @@ ...@@ -557,6 +618,33 @@
}); });
}); });
test("removeAttachment use UriTemplate", function () {
stop();
expect(3);
var jio = jIO.createJIO({
type: "mapping",
sub_storage: {
type: "mappingstorage2713"
},
mapping_dict_attachment: {"2713": {"uri_template": "www.2713/{id}.bar"}}
});
Storage2713.prototype.removeAttachment = function (doc_id, attachment) {
equal(doc_id, "42", "putAttachment 2713 called");
equal(attachment, "www.2713/42.bar", "getAttachment 2713 called");
return doc_id;
};
jio.removeAttachment("42", "2713")
.then(function (result) {
deepEqual(result, "42");
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
// mappingStorage.allDocs // mappingStorage.allDocs
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
...@@ -698,13 +786,12 @@ ...@@ -698,13 +786,12 @@
jio.put("42", jio.put("42",
{ {
"title": "foo", "title": "foo",
"smth": "bar", "smth": "bar"
"stmth2": "bar2"
}) })
.push(function () { .push(function () {
return jio.allDocs({ return jio.allDocs({
query: 'title: "foo"', query: 'title: "foo"',
select_list: ["title", "smth2", "smth"] select_list: ["title", "smth"]
}); });
}) })
.push(function (result) { .push(function (result) {
...@@ -714,7 +801,7 @@ ...@@ -714,7 +801,7 @@
"rows": [ "rows": [
{ {
"id": "42", "id": "42",
"value": {"title": "foo", "smth": "bar", "smth2": "bar2"}, "value": {"title": "foo", "smth": "bar"},
"doc": {} "doc": {}
} }
], ],
...@@ -729,4 +816,65 @@ ...@@ -729,4 +816,65 @@
start(); start();
}); });
}); });
/////////////////////////////////////////////////////////////////
// mappingStorage.bulk
/////////////////////////////////////////////////////////////////
module("mappingstorage.bulk");
test("bulk with map_all_property", function () {
stop();
expect(2);
var jio = jIO.createJIO({
type: "mapping",
sub_storage: {
type: "mappingstorage2713"
},
map_all_doc: true,
mapping_dict: {
"title": {"equal": "otherTitle"},
"id": {"equal": "otherId"}
}
});
Storage2713.prototype.hasCapacity = function () {
return true;
};
Storage2713.prototype.bulk = function () {
deepEqual(arguments, ["some", "arguments"], "bulk 2713 called");
return [
{
"otherId": "foo",
"otherTitle": "bar",
"bar": "foo"
}, {
"otherId": "bar",
"otherTitle": "foo",
"foo": "bar"
}
];
};
jio.bulk("some", "arguments")
.push(function (result) {
deepEqual(
result,
[{
"id": "foo",
"title": "bar",
"bar": "foo"
}, {
"id": "bar",
"title": "foo",
"foo": "bar"
}],
"bulk test"
);
})
.always(function () {
start();
});
});
}(jIO, QUnit, Blob)); }(jIO, QUnit, Blob));
\ No newline at end of file
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