Commit b2d253f2 authored by Aurel's avatar Aurel

Merge branch 'vincent-mapping' into nodejs

Conflicts:
	src/jio.storage/mappingstorage.js
	test/jio.storage/mappingstorage.tests.js
	test/tests.html
parents 486af15d f56a0ffb
/*jslint indent:2, maxlen: 80, nomen: true */ /*jslint indent:2, maxlen: 80, nomen: true */
/*global jIO, RSVP, UriTemplate, SimpleQuery, ComplexQuery, QueryFactory, /*global jIO, RSVP, UriTemplate, SimpleQuery, ComplexQuery, QueryFactory,
Query*/ Query, FormData*/
(function (jIO, RSVP, UriTemplate, SimpleQuery, ComplexQuery, QueryFactory, (function (jIO, RSVP, UriTemplate, SimpleQuery, ComplexQuery, QueryFactory,
Query) { Query, FormData) {
"use strict"; "use strict";
function getSubIdEqualSubProperty(storage, value, key) { function getSubIdEqualSubProperty(storage, value, key) {
...@@ -10,6 +10,15 @@ ...@@ -10,6 +10,15 @@
if (storage._no_sub_query_id) { if (storage._no_sub_query_id) {
throw new jIO.util.jIOError('no sub query id active', 404); throw new jIO.util.jIOError('no sub query id active', 404);
} }
if (!value) {
throw new jIO.util.jIOError(
'can not find document with ' + key + ' : undefined',
404
);
}
if (storage._mapping_id_memory_dict[value]) {
return storage._mapping_id_memory_dict[value];
}
query = new SimpleQuery({ query = new SimpleQuery({
key: key, key: key,
value: value, value: value,
...@@ -30,16 +39,17 @@ ...@@ -30,16 +39,17 @@
"limit": storage._query.limit "limit": storage._query.limit
}) })
.push(function (data) { .push(function (data) {
if (data.data.rows.length === 0) { if (data.data.total_rows === 0) {
throw new jIO.util.jIOError( throw new jIO.util.jIOError(
"Can not find id", "Can not find document with (" + key + ", " + value + ")",
404 404
); );
} }
if (data.data.rows.length > 1) { if (data.data.total_rows > 1) {
throw new TypeError("id must be unique field: " + key throw new TypeError("id must be unique field: " + key
+ ", result:" + data.data.rows.toString()); + ", result:" + data.data.rows.toString());
} }
storage._mapping_id_memory_dict[value] = data.data.rows[0].id;
return data.data.rows[0].id; return data.data.rows[0].id;
}); });
} }
...@@ -63,9 +73,6 @@ ...@@ -63,9 +73,6 @@
doc.hasOwnProperty(storage._property_for_sub_id)) { doc.hasOwnProperty(storage._property_for_sub_id)) {
return doc[storage._property_for_sub_id]; return doc[storage._property_for_sub_id];
} }
if (doc.hasOwnProperty(args)) {
return doc[args];
}
} }
return getSubIdEqualSubProperty(storage, id, storage._map_id[1]); return getSubIdEqualSubProperty(storage, id, storage._map_id[1]);
}, },
...@@ -167,6 +174,9 @@ ...@@ -167,6 +174,9 @@
} }
} }
} }
if (storage._map_id[0] === "equalSubProperty") {
storage._mapping_dict[storage._map_id[1]] = ["keep"];
}
if (storage._query.query !== undefined) { if (storage._query.query !== undefined) {
query_list.push(QueryFactory.create(storage._query.query)); query_list.push(QueryFactory.create(storage._query.query));
} }
...@@ -196,6 +206,9 @@ ...@@ -196,6 +206,9 @@
this._query.query = QueryFactory.create(this._query.query); this._query.query = QueryFactory.create(this._query.query);
} }
this._default_mapping = {}; this._default_mapping = {};
this._mapping_id_memory_dict = {};
this._attachment_list = spec.attachment_list || [];
this._caching_dict = {id: {}};
initializeQueryAndDefaultMapping(this); initializeQueryAndDefaultMapping(this);
} }
...@@ -214,6 +227,12 @@ ...@@ -214,6 +227,12 @@
} }
function getSubStorageId(storage, id, doc) { function getSubStorageId(storage, id, doc) {
if (storage._caching_dict.id.hasOwnProperty(id)) {
return new RSVP.Queue()
.push(function () {
return storage._caching_dict.id[id];
});
}
return new RSVP.Queue() return new RSVP.Queue()
.push(function () { .push(function () {
var map_info = storage._map_id || ["equalSubId"]; var map_info = storage._map_id || ["equalSubId"];
...@@ -227,6 +246,10 @@ ...@@ -227,6 +246,10 @@
id, id,
map_info[1] map_info[1]
); );
})
.push(function (sub_id) {
storage._caching_dict.id[id] = sub_id;
return sub_id;
}); });
} }
...@@ -305,12 +328,20 @@ ...@@ -305,12 +328,20 @@
return getSubStorageId(storage, argument_list[0]) return getSubStorageId(storage, argument_list[0])
.push(function (sub_id) { .push(function (sub_id) {
argument_list[0] = sub_id; argument_list[0] = sub_id;
var old_id = argument_list[1];
argument_list[1] = getAttachmentId( argument_list[1] = getAttachmentId(
storage, storage,
sub_id, argument_list[0],
argument_list[1], argument_list[1],
method method
); );
if (storage._attachment_list.length > 0
&& storage._attachment_list.indexOf(old_id) < 0) {
if (method === "get") {
throw new jIO.util.jIOError("unhautorized attachment", 404);
}
return;
}
return storage._sub_storage[method + "Attachment"].apply( return storage._sub_storage[method + "Attachment"].apply(
storage._sub_storage, storage._sub_storage,
argument_list argument_list
...@@ -334,12 +365,25 @@ ...@@ -334,12 +365,25 @@
this, this,
doc doc
), ),
id = doc[this._property_for_sub_id]; id = doc[this._property_for_sub_id],
storage = this;
if (this._property_for_sub_id && id !== undefined) { if (this._property_for_sub_id && id !== undefined) {
return this._sub_storage.put(id, sub_doc); return this._sub_storage.put(id, sub_doc);
} }
if (!this._id_mapped || doc[this._id_mapped] !== undefined) { if (this._id_mapped && doc[this._id_mapped] !== undefined) {
return this._sub_storage.post(sub_doc); return getSubStorageId(storage, id, doc)
.push(function (sub_id) {
return storage._sub_storage.put(sub_id, sub_doc);
})
.push(function () {
return doc[storage._id_mapped];
})
.push(undefined, function (error) {
if (error instanceof jIO.util.jIOError) {
return storage._sub_storage.post(sub_doc);
}
throw error;
});
} }
throw new jIO.util.jIOError( throw new jIO.util.jIOError(
"post is not supported with id mapped", "post is not supported with id mapped",
...@@ -376,17 +420,42 @@ ...@@ -376,17 +420,42 @@
}); });
}; };
MappingStorage.prototype.putAttachment = function (id, attachment_id) { MappingStorage.prototype.getAttachment = function () {
return handleAttachment(this, arguments, "get");
};
MappingStorage.prototype.putAttachment = function (id, attachment_id, blob) {
var storage = this,
mapping_dict = storage._attachment_mapping_dict;
// THIS IS REALLY BAD, FIND AN OTHER WAY IN FUTURE
if (mapping_dict !== undefined
&& mapping_dict[attachment_id] !== undefined
&& mapping_dict[attachment_id].put !== undefined
&& mapping_dict[attachment_id].put.erp5_put_template !== undefined) {
return getSubStorageId(storage, id)
.push(function (sub_id) {
var url = UriTemplate.parse(
mapping_dict[attachment_id].put.erp5_put_template
).expand({id: sub_id}),
data = new FormData();
data.append("field_my_file", blob);
data.append("form_id", "File_view");
return jIO.util.ajax({
"type": "POST",
"url": url,
"data": data,
"xhrFields": {
withCredentials: true
}
});
});
}
return handleAttachment(this, arguments, "put", id) return handleAttachment(this, arguments, "put", id)
.push(function () { .push(function () {
return attachment_id; return attachment_id;
}); });
}; };
MappingStorage.prototype.getAttachment = function () {
return handleAttachment(this, arguments, "get");
};
MappingStorage.prototype.removeAttachment = function (id, attachment_id) { MappingStorage.prototype.removeAttachment = function (id, attachment_id) {
return handleAttachment(this, arguments, "remove", id) return handleAttachment(this, arguments, "remove", id)
.push(function () { .push(function () {
...@@ -404,7 +473,8 @@ ...@@ -404,7 +473,8 @@
.push(function (result) { .push(function (result) {
var attachment_id, var attachment_id,
attachments = {}, attachments = {},
mapping_dict = {}; mapping_dict = {},
i;
for (attachment_id in storage._attachment_mapping_dict) { for (attachment_id in storage._attachment_mapping_dict) {
if (storage._attachment_mapping_dict.hasOwnProperty(attachment_id)) { if (storage._attachment_mapping_dict.hasOwnProperty(attachment_id)) {
mapping_dict[getAttachmentId(storage, sub_id, attachment_id, "get")] mapping_dict[getAttachmentId(storage, sub_id, attachment_id, "get")]
...@@ -413,6 +483,8 @@ ...@@ -413,6 +483,8 @@
} }
for (attachment_id in result) { for (attachment_id in result) {
if (result.hasOwnProperty(attachment_id)) { if (result.hasOwnProperty(attachment_id)) {
if (!(storage._attachment_list.length > 0
&& storage._attachment_list.indexOf(attachment_id) < 0)) {
if (mapping_dict.hasOwnProperty(attachment_id)) { if (mapping_dict.hasOwnProperty(attachment_id)) {
attachments[mapping_dict[attachment_id]] = {}; attachments[mapping_dict[attachment_id]] = {};
} else { } else {
...@@ -420,6 +492,12 @@ ...@@ -420,6 +492,12 @@
} }
} }
} }
}
for (i = 0; i < storage._attachment_list.length; i += 1) {
if (!attachments.hasOwnProperty(storage._attachment_list[i])) {
attachments[storage._attachment_list[i]] = {};
}
}
return attachments; return attachments;
}); });
}; };
...@@ -483,7 +561,7 @@ ...@@ -483,7 +561,7 @@
return one_query; return one_query;
} }
key = mapToMainProperty(storage, one_query.key, {}, {}); key = mapToMainProperty(storage, one_query.key, {}, {});
if (key) { if (key !== undefined) {
one_query.key = key; one_query.key = key;
return one_query; return one_query;
} }
...@@ -574,4 +652,5 @@ ...@@ -574,4 +652,5 @@
}; };
jIO.addStorage('mapping', MappingStorage); jIO.addStorage('mapping', MappingStorage);
}(jIO, RSVP, UriTemplate, SimpleQuery, ComplexQuery, QueryFactory, Query)); }(jIO, RSVP, UriTemplate, SimpleQuery, ComplexQuery, QueryFactory, Query,
\ No newline at end of file FormData));
\ No newline at end of file
...@@ -56,7 +56,7 @@ ...@@ -56,7 +56,7 @@
deepEqual( deepEqual(
jio.__storage._mapping_dict, jio.__storage._mapping_dict,
{"bar": ["equalSubProperty", "foo"]} {"bar": ["equalSubProperty", "foo"], "otherId": ["keep"]}
); );
deepEqual(jio.__storage._map_id, ["equalSubProperty", "otherId"]); deepEqual(jio.__storage._map_id, ["equalSubProperty", "otherId"]);
equal(jio.__storage._query.query.key, "foo"); equal(jio.__storage._query.query.key, "foo");
...@@ -130,13 +130,14 @@ ...@@ -130,13 +130,14 @@
Storage2713.prototype.get = function (id) { Storage2713.prototype.get = function (id) {
equal(id, "2713", "get 2713 called"); equal(id, "2713", "get 2713 called");
return {"title": "foo"}; return {"title": "foo", "otherId": "42"};
}; };
jio.get("42") jio.get("42")
.push(function (result) { .push(function (result) {
deepEqual(result, { deepEqual(result, {
"title": "foo" "title": "foo",
"otherId": "42"
}); });
}).push(undefined, function (error) { }).push(undefined, function (error) {
ok(false, error); ok(false, error);
...@@ -169,13 +170,14 @@ ...@@ -169,13 +170,14 @@
Storage2713.prototype.get = function (id) { Storage2713.prototype.get = function (id) {
equal(id, "2713", "get 2713 called"); equal(id, "2713", "get 2713 called");
return {"title": "foo"}; return {"title": "foo", "otherId": "42"};
}; };
jio.get("42") jio.get("42")
.push(function (result) { .push(function (result) {
deepEqual(result, { deepEqual(result, {
"title": "foo" "title": "foo",
"otherId": "42"
}); });
}).push(undefined, function (error) { }).push(undefined, function (error) {
ok(false, error); ok(false, error);
...@@ -662,14 +664,28 @@ ...@@ -662,14 +664,28 @@
} }
}); });
Storage2713.prototype.buildQuery = function (options) {
equal(options.query, 'otherId: "bar"', "allDoc 2713 called");
return [];
};
Storage2713.prototype.post = function (doc) { Storage2713.prototype.post = function (doc) {
deepEqual(doc, {"title": "foo", "otherId": "bar"}, "post 2713 called"); deepEqual(doc, {"title": "foo", "otherId": "bar"}, "post 2713 called");
return "42"; return "bar";
};
Storage2713.prototype.put = function (id, doc) {
equal(id, "bar", "put 2713 called");
deepEqual(doc, {
"title": "foo",
"otherId": "bar"
}, "put 2713 called");
return "bar";
}; };
jio.post({"title": "foo", "otherId": "bar"}) jio.post({"title": "foo", "otherId": "bar"})
.push(function (result) { .push(function (result) {
equal(result, "42"); equal(result, "bar");
}) })
.push(undefined, function (error) { .push(undefined, function (error) {
ok(false, error); ok(false, error);
...@@ -1181,7 +1197,7 @@ ...@@ -1181,7 +1197,7 @@
"smth": "bar" "smth": "bar"
}) })
.push(function () { .push(function () {
jio.put( return jio.put(
"2713", "2713",
{ {
"title": "bar", "title": "bar",
...@@ -1205,7 +1221,8 @@ ...@@ -1205,7 +1221,8 @@
"id": "42", "id": "42",
"value": { "value": {
"title": "foo", "title": "foo",
"smth": "bar" "smth": "bar",
"otherId": "42"
}, },
"doc": {} "doc": {}
}, },
...@@ -1213,7 +1230,8 @@ ...@@ -1213,7 +1230,8 @@
"id": "2713", "id": "2713",
"value": { "value": {
"title": "bar", "title": "bar",
"smth": "foo" "smth": "foo",
"otherId": "2713"
}, },
"doc": {} "doc": {}
} }
...@@ -1266,7 +1284,9 @@ ...@@ -1266,7 +1284,9 @@
"rows": [ "rows": [
{ {
"id": "42", "id": "42",
"value": {}, "value": {
"otherId": "42"
},
"doc": {} "doc": {}
} }
], ],
...@@ -1321,7 +1341,7 @@ ...@@ -1321,7 +1341,7 @@
"rows": [ "rows": [
{ {
"id": "42", "id": "42",
"value": {"title": "foo", "smth": "bar"}, "value": {"title": "foo", "smth": "bar", "otherId": "42"},
"doc": {} "doc": {}
} }
], ],
...@@ -1377,7 +1397,63 @@ ...@@ -1377,7 +1397,63 @@
"rows": [ "rows": [
{ {
"id": "42", "id": "42",
"value": {"title": "foo"}, "value": {
"otherId": "42",
"title": "foo"
},
"doc": {}
}
],
"total_rows": 1
}
}, "allDocs check");
})
.push(undefined, function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
test("with extended_search", function () {
stop();
expect(1);
var jio = jIO.createJIO({
type: "mapping",
sub_storage: {
type: "query",
sub_storage: {
type: "uuid",
sub_storage: {
type: "memory"
}
}
}
});
jio.put("42",
{
"title": "foo",
"smth": "bar"
})
.push(function () {
return jio.allDocs({
query: "foo",
select_list: ["title"]
});
})
.push(function (result) {
deepEqual(result,
{
"data": {
"rows": [
{
"id": "42",
"value": {
"title": "foo"
},
"doc": {} "doc": {}
} }
], ],
...@@ -1463,9 +1539,11 @@ ...@@ -1463,9 +1539,11 @@
result, result,
[{ [{
"title": "bar", "title": "bar",
"otherId": "foo",
"bar": "foo" "bar": "foo"
}, { }, {
"title": "foo", "title": "foo",
"otherId": "bar",
"foo": "bar" "foo": "bar"
}], }],
"bulk test" "bulk test"
......
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