Commit dd1743bb authored by Boris Kocherov's avatar Boris Kocherov

mappingstorage: add default_property feature

parent 4c7d58a7
...@@ -95,8 +95,48 @@ ...@@ -95,8 +95,48 @@
return storage._sub_storage.allDocs({ return storage._sub_storage.allDocs({
"query": query, "query": query,
"sort_on": storage._query.sort_on, "sort_on": storage._query.sort_on,
"select_list": storage._query.select_list,
"limit": storage._query.limit "limit": storage._query.limit
})
.push(function (data) {
if (data.data.rows.length === 0 &&
storage._mapping_dict.id.default_property) {
query = new SimpleQuery({
key: storage._mapping_dict.id.default_property,
value: id,
type: "simple"
});
if (storage._query.query !== undefined) {
query = new ComplexQuery({
operator: "AND",
query_list: [query, storage._query.query],
type: "complex"
});
}
query = Query.objectToSearchText(query);
return storage._sub_storage.allDocs({
"query": query,
"select_list": [storage._mapping_dict.id.equal],
"sort_on": storage._query.sort_on,
"limit": storage._query.limit
})
.push(function (data) {
var i,
rows = [],
orig_rows = data.data.rows;
if (orig_rows.length > 1) {
for (i = 0; i < orig_rows.length; i += 1) {
if (!orig_rows[i].value[
storage._mapping_dict.id.equal
]) {
rows.push(orig_rows[i]);
}
}
data.data.rows = rows;
}
return data;
});
}
return data;
}) })
.push(function (data) { .push(function (data) {
if (data.data.rows.length === 0) { if (data.data.rows.length === 0) {
...@@ -120,13 +160,14 @@ ...@@ -120,13 +160,14 @@
} }
function mapToSubProperty(storage, property, sub_doc, doc) { function mapToSubProperty(storage, property, sub_doc, doc) {
if (storage._mapping_dict[property] !== undefined) { var sub_property = storage._mapping_dict[property];
if (storage._mapping_dict[property].equal !== undefined) { if (sub_property !== undefined) {
sub_doc[storage._mapping_dict[property].equal] = doc[property]; if (sub_property.equal !== undefined) {
return storage._mapping_dict[property].equal; sub_doc[sub_property.equal] = doc[property];
} return sub_property.equal;
if (storage._mapping_dict[property].default_value !== undefined) { }
sub_doc[property] = storage._mapping_dict[property].default_value; if (sub_property.default_value !== undefined) {
sub_doc[property] = sub_property.default_value;
return property; return property;
} }
} }
...@@ -145,14 +186,21 @@ ...@@ -145,14 +186,21 @@
} }
function mapToMainProperty(storage, property, sub_doc, doc) { function mapToMainProperty(storage, property, sub_doc, doc) {
if (storage._mapping_dict[property] !== undefined) { var main_property = storage._mapping_dict[property];
if (storage._mapping_dict[property].equal !== undefined) { if (main_property !== undefined) {
if (sub_doc.hasOwnProperty(storage._mapping_dict[property].equal)) { if (main_property.equal !== undefined) {
doc[property] = sub_doc[storage._mapping_dict[property].equal]; if (sub_doc.hasOwnProperty(main_property.equal)) {
doc[property] = sub_doc[main_property.equal];
return main_property.equal;
}
if (main_property.default_property &&
sub_doc.hasOwnProperty(main_property.default_property)) {
doc[property] = sub_doc[main_property.default_property];
return main_property.default_property;
} }
return storage._mapping_dict[property].equal; return main_property.equal;
} }
if (storage._mapping_dict[property].default_value !== undefined) { if (main_property.default_value !== undefined) {
return property; return property;
} }
} }
...@@ -433,6 +481,9 @@ ...@@ -433,6 +481,9 @@
} }
if (this._id_is_mapped) { if (this._id_is_mapped) {
select_list.push(this._mapping_dict.id.equal); select_list.push(this._mapping_dict.id.equal);
if (this._mapping_dict.id.default_property) {
select_list.push(this._mapping_dict.id.default_property);
}
} }
if (option.query !== undefined) { if (option.query !== undefined) {
query = mapQuery(QueryFactory.create(option.query)); query = mapQuery(QueryFactory.create(option.query));
......
...@@ -9,7 +9,8 @@ ...@@ -9,7 +9,8 @@
expect = QUnit.expect, expect = QUnit.expect,
deepEqual = QUnit.deepEqual, deepEqual = QUnit.deepEqual,
equal = QUnit.equal, equal = QUnit.equal,
module = QUnit.module; module = QUnit.module,
count_of_run;
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
// Custom test substorage definition // Custom test substorage definition
...@@ -169,6 +170,60 @@ ...@@ -169,6 +170,60 @@
}); });
}); });
test("get with id and props mapped (id use default_property)", function () {
stop();
expect(4);
count_of_run = 1;
var jio = jIO.createJIO({
type: "mapping",
sub_storage: {
type: "mappingstorage2713"
},
mapping_dict: {
"title": {"equal": "otherTitle"},
"id": {
"equal": "otherId",
"default_property": "otherOtherId"
}
}
});
Storage2713.prototype.hasCapacity = function () {
return true;
};
Storage2713.prototype.buildQuery = function (options) {
if (count_of_run === 1) {
count_of_run += 1;
equal(options.query, 'otherId: "42"', "allDoc 2713 called firstly");
return [];
}
if (count_of_run === 2) {
equal(options.query, 'otherOtherId: "42"',
"allDoc 2713 called secondary");
return [{id: "2713"}];
}
};
Storage2713.prototype.get = function (id) {
equal(id, "2713", "get 2713 called");
return {"otherTitle": "foo"};
};
jio.get("42")
.push(function (result) {
deepEqual(result, {
"title": "foo"
});
}).push(undefined, function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
test("get with id mapped and query", function () { test("get with id mapped and query", function () {
stop(); stop();
expect(3); expect(3);
...@@ -1302,6 +1357,99 @@ ...@@ -1302,6 +1357,99 @@
}); });
}); });
test("allDocs id and prop mapped and query and default_property used",
function () {
stop();
expect(2);
var jio = jIO.createJIO({
type: "mapping",
mapping_dict: {
"id": {
"equal": "otherId",
"default_property": "otherOtherId"
},
"title": {"equal": "otherTitle"}
},
sub_storage: {
type: "query",
sub_storage: {
type: "uuid",
sub_storage: {
type: "memory"
}
}
}
});
jio.put("42",
{
"title": "foo",
"smth": "bar",
"otherOtherId": "43"
})
.push(function () {
//return jio.put(43,)
return jio.__storage
._sub_storage.__storage._sub_storage.__storage
._sub_storage.put('zzz', {
"otherOtherId": "43",
"otherTitle": "shrek"
});
})
.push(function () {
return jio.allDocs({
query: 'title: "foo"',
select_list: ["title"]
});
})
.push(function (result) {
deepEqual(result,
{
"data": {
"rows": [
{
"id": "42",
"value": {
"otherOtherId": "43",
"title": "foo"
},
"doc": {}
}
],
"total_rows": 1
}
}, "allDocs check");
})
.push(function () {
return jio.allDocs({
query: 'title: "shrek"',
select_list: ["title"]
});
})
.push(function (result) {
deepEqual(result,
{
"data": {
"rows": [
{
"id": "43",
"value": {"title": "shrek"},
"doc": {}
}
],
"total_rows": 1
}
}, "allDocs check default_property");
})
.push(undefined, function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
// mappingStorage.bulk // mappingStorage.bulk
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
......
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