Commit 514ca23a authored by Vincent Bechu's avatar Vincent Bechu

mappingstorage: add reversed mapping id and test

parent b968fe14
...@@ -64,7 +64,7 @@ ...@@ -64,7 +64,7 @@
return attachment_id; return attachment_id;
} }
function getSubStorageId(storage, id) { function getSubStorageId(storage, id, sub_doc) {
var query; var query;
return new RSVP.Queue() return new RSVP.Queue()
.push(function () { .push(function () {
...@@ -72,6 +72,17 @@ ...@@ -72,6 +72,17 @@
return id; return id;
} }
if (storage._mapping_dict.id.equal !== undefined) { if (storage._mapping_dict.id.equal !== undefined) {
if (JSON.stringify(storage._mapping_dict)
.indexOf('{"equal":"id"}') >= 0
&& sub_doc !== undefined) {
if (sub_doc.id !== undefined) {
return sub_doc.id;
}
throw new jIO.util.jIOError(
"Cannot find id field related",
400
);
}
query = new SimpleQuery({ query = new SimpleQuery({
key: storage._mapping_dict.id.equal, key: storage._mapping_dict.id.equal,
value: id, value: id,
...@@ -154,10 +165,13 @@ ...@@ -154,10 +165,13 @@
return false; return false;
} }
function mapToMainDocument(storage, sub_doc, delete_id_from_doc) { function mapToMainDocument(storage, sub_doc, sub_id, delete_id_from_doc) {
var doc = {}, var doc = {},
property, property,
property_list = []; property_list = [];
if (sub_id) {
sub_doc.id = sub_id;
}
for (property in storage._mapping_dict) { for (property in storage._mapping_dict) {
if (storage._mapping_dict.hasOwnProperty(property)) { if (storage._mapping_dict.hasOwnProperty(property)) {
property_list.push(mapToMainProperty(storage, property, sub_doc, doc)); property_list.push(mapToMainProperty(storage, property, sub_doc, doc));
...@@ -178,8 +192,9 @@ ...@@ -178,8 +192,9 @@
return doc; return doc;
} }
function mapToSubstorageDocument(storage, doc) { function mapToSubstorageDocument(storage, doc, id, delete_id_from_doc) {
var sub_doc = {}, property; var sub_doc = {}, property;
doc.id = id;
for (property in doc) { for (property in doc) {
if (doc.hasOwnProperty(property)) { if (doc.hasOwnProperty(property)) {
...@@ -191,7 +206,9 @@ ...@@ -191,7 +206,9 @@
sub_doc[property] = storage._default_mapping[property]; sub_doc[property] = storage._default_mapping[property];
} }
} }
delete sub_doc.id; if (delete_id_from_doc) {
delete sub_doc.id;
}
return sub_doc; return sub_doc;
} }
...@@ -216,16 +233,21 @@ ...@@ -216,16 +233,21 @@
var context = this; var context = this;
return getSubStorageId(this, id) return getSubStorageId(this, id)
.push(function (sub_id) { .push(function (sub_id) {
return context._sub_storage.get(sub_id); return context._sub_storage.get(sub_id)
}) .push(function (sub_doc) {
.push(function (sub_doc) { return mapToMainDocument(context, sub_doc, sub_id, true);
return mapToMainDocument(context, sub_doc, true); });
}); });
}; };
MappingStorage.prototype.post = function (doc) { MappingStorage.prototype.post = function (doc) {
if (!this._id_is_mapped) { if (!this._id_is_mapped) {
return this._sub_storage.post(mapToSubstorageDocument(this, doc)); return this._sub_storage.post(mapToSubstorageDocument(
this,
doc,
true,
true
));
} }
throw new jIO.util.jIOError( throw new jIO.util.jIOError(
"post is not supported with id mapped", "post is not supported with id mapped",
...@@ -234,15 +256,16 @@ ...@@ -234,15 +256,16 @@
}; };
MappingStorage.prototype.put = function (id, doc) { MappingStorage.prototype.put = function (id, doc) {
doc.id = id;
var context = this, var context = this,
sub_doc = mapToSubstorageDocument(this, doc); sub_doc = mapToSubstorageDocument(this, doc, id, false);
return getSubStorageId(this, id) return getSubStorageId(this, id, sub_doc)
.push(function (sub_id) { .push(function (sub_id) {
delete sub_doc.id;
return context._sub_storage.put(sub_id, sub_doc); return context._sub_storage.put(sub_id, sub_doc);
}) })
.push(undefined, function (error) { .push(undefined, function (error) {
if (error instanceof jIO.util.jIOError && error.status_code === 404) { if (error instanceof jIO.util.jIOError && error.status_code === 404) {
delete sub_doc.id;
return context._sub_storage.post(sub_doc); return context._sub_storage.post(sub_doc);
} }
throw error; throw error;
...@@ -340,7 +363,12 @@ ...@@ -340,7 +363,12 @@
.push(function (result) { .push(function (result) {
var mapped_result = [], i; var mapped_result = [], i;
for (i = 0; i < result.length; i += 1) { for (i = 0; i < result.length; i += 1) {
mapped_result.push(mapToMainDocument(context, result[i], false)); mapped_result.push(mapToMainDocument(
context,
result[i],
false,
true
));
} }
return mapped_result; return mapped_result;
}); });
...@@ -431,12 +459,17 @@ ...@@ -431,12 +459,17 @@
.push(function (result) { .push(function (result) {
for (i = 0; i < result.data.total_rows; i += 1) { for (i = 0; i < result.data.total_rows; i += 1) {
result.data.rows[i].value = result.data.rows[i].value =
mapToMainDocument(context, result.data.rows[i].value, false); mapToMainDocument(
if (result.data.rows[i].id !== undefined && context._id_is_mapped) { context,
result.data.rows[i].value,
false,
false
);
if (context._id_is_mapped) {
result.data.rows[i].id = result.data.rows[i].id =
result.data.rows[i].value.id; result.data.rows[i].value.id;
delete result.data.rows[i].value.id;
} }
delete result.data.rows[i].value.id;
} }
return result.data.rows; return result.data.rows;
}); });
......
...@@ -127,6 +127,36 @@ ...@@ -127,6 +127,36 @@
}); });
}); });
test("get with id mapped", function () {
stop();
expect(2);
var jio = jIO.createJIO({
type: "mapping",
sub_storage: {
type: "mappingstorage2713"
},
mapping_dict: {"title": {"equal": "id"}}
});
Storage2713.prototype.get = function (id) {
equal(id, "bar", "get 2713 called");
return {};
};
jio.get("bar")
.push(function (result) {
deepEqual(result, {
"title": "bar"
});
}).push(undefined, function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
test("get with id and props mapped", function () { test("get with id and props mapped", function () {
stop(); stop();
expect(3); expect(3);
...@@ -368,6 +398,40 @@ ...@@ -368,6 +398,40 @@
}); });
}); });
test("put with id mapped", function () {
stop();
expect(3);
var jio = jIO.createJIO({
type: "mapping",
sub_storage: {
type: "mappingstorage2713"
},
mapping_dict: {
"id": {"equal": "otherId"},
"title": {"equal": "id"}
}
});
Storage2713.prototype.put = function (id, doc) {
deepEqual(doc,
{"otherId": "42"}, "post 2713 called");
equal(id, "bar");
return "bar";
};
jio.put("42", {"title": "bar"})
.push(function (result) {
equal(result, "42");
})
.push(undefined, function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
test("put with map_all_property", function () { test("put with map_all_property", function () {
stop(); stop();
expect(3); expect(3);
......
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