Commit 522f08a4 authored by Vincent Bechu's avatar Vincent Bechu

Release 3.25.0

Query extend key_schema to sort_on
querystorage: add schema to use key_schema
parent 42a5a033
...@@ -6312,8 +6312,8 @@ return new Parser; ...@@ -6312,8 +6312,8 @@ return new Parser;
* @param {String} [way="ascending"] 'ascending' or 'descending' * @param {String} [way="ascending"] 'ascending' or 'descending'
* @return {Function} The sort function * @return {Function} The sort function
*/ */
function sortFunction(key, way) { function sortFunction(key, way, key_schema) {
var result; var result, cast_to;
if (way === 'descending') { if (way === 'descending') {
result = 1; result = 1;
} else if (way === 'ascending') { } else if (way === 'ascending') {
...@@ -6322,6 +6322,29 @@ return new Parser; ...@@ -6322,6 +6322,29 @@ return new Parser;
throw new TypeError("Query.sortFunction(): " + throw new TypeError("Query.sortFunction(): " +
"Argument 2 must be 'ascending' or 'descending'"); "Argument 2 must be 'ascending' or 'descending'");
} }
if (key_schema !== undefined &&
key_schema.key_set !== undefined &&
key_schema.key_set[key] !== undefined &&
key_schema.key_set[key].cast_to !== undefined) {
if (typeof key_schema.key_set[key].cast_to === "string") {
cast_to = key_schema.cast_lookup[key_schema.key_set[key].cast_to];
} else {
cast_to = key_schema.key_set[key].cast_to;
}
return function (a, b) {
var f_a = cast_to(a[key]), f_b = cast_to(b[key]);
if (typeof f_b.cmp === 'function') {
return result * f_b.cmp(f_a);
}
if (f_a > f_b) {
return -result;
}
if (f_a < f_b) {
return result;
}
return 0;
};
}
return function (a, b) { return function (a, b) {
// this comparison is 5 times faster than json comparison // this comparison is 5 times faster than json comparison
var i, l; var i, l;
...@@ -6346,6 +6369,7 @@ return new Parser; ...@@ -6346,6 +6369,7 @@ return new Parser;
}; };
} }
/** /**
* Sort a list of items, according to keys and directions. * Sort a list of items, according to keys and directions.
* *
...@@ -6353,7 +6377,7 @@ return new Parser; ...@@ -6353,7 +6377,7 @@ return new Parser;
* @param {Array} list The item list to sort * @param {Array} list The item list to sort
* @return {Array} The filtered list * @return {Array} The filtered list
*/ */
function sortOn(sort_on_option, list) { function sortOn(sort_on_option, list, key_schema) {
var sort_index; var sort_index;
if (!Array.isArray(sort_on_option)) { if (!Array.isArray(sort_on_option)) {
throw new TypeError("jioquery.sortOn(): " + throw new TypeError("jioquery.sortOn(): " +
...@@ -6363,7 +6387,8 @@ return new Parser; ...@@ -6363,7 +6387,8 @@ return new Parser;
sort_index -= 1) { sort_index -= 1) {
list.sort(sortFunction( list.sort(sortFunction(
sort_on_option[sort_index][0], sort_on_option[sort_index][0],
sort_on_option[sort_index][1] sort_on_option[sort_index][1],
key_schema
)); ));
} }
return list; return list;
...@@ -6426,6 +6451,35 @@ return new Parser; ...@@ -6426,6 +6451,35 @@ return new Parser;
return list; return list;
} }
function checkKeySchema(key_schema) {
var prop;
if (key_schema !== undefined) {
if (typeof key_schema !== 'object') {
throw new TypeError("Query().create(): " +
"key_schema is not of type 'object'");
}
// key_set is mandatory
if (key_schema.key_set === undefined) {
throw new TypeError("Query().create(): " +
"key_schema has no 'key_set' property");
}
for (prop in key_schema) {
if (key_schema.hasOwnProperty(prop)) {
switch (prop) {
case 'key_set':
case 'cast_lookup':
case 'match_lookup':
break;
default:
throw new TypeError("Query().create(): " +
"key_schema has unknown property '" + prop + "'");
}
}
}
}
}
/** /**
* The query to use to filter a list of objects. * The query to use to filter a list of objects.
* This is an abstract class. * This is an abstract class.
...@@ -6433,7 +6487,10 @@ return new Parser; ...@@ -6433,7 +6487,10 @@ return new Parser;
* @class Query * @class Query
* @constructor * @constructor
*/ */
function Query() { function Query(key_schema) {
checkKeySchema(key_schema);
this._key_schema = key_schema || {};
/** /**
* Called before parsing the query. Must be overridden! * Called before parsing the query. Must be overridden!
...@@ -6506,7 +6563,7 @@ return new Parser; ...@@ -6506,7 +6563,7 @@ return new Parser;
} }
if (option.sort_on) { if (option.sort_on) {
sortOn(option.sort_on, item_list); sortOn(option.sort_on, item_list, this._key_schema);
} }
if (option.limit) { if (option.limit) {
...@@ -6845,7 +6902,7 @@ return new Parser; ...@@ -6845,7 +6902,7 @@ return new Parser;
*/ */
QueryFactory.create = function (object, key_schema) { QueryFactory.create = function (object, key_schema) {
if (object === "") { if (object === "") {
return new Query(); return new Query(key_schema);
} }
if (typeof object === "string") { if (typeof object === "string") {
object = parseStringToObject(object); object = parseStringToObject(object);
...@@ -6877,35 +6934,6 @@ return new Parser; ...@@ -6877,35 +6934,6 @@ return new Parser;
throw new TypeError("This object is not a query"); throw new TypeError("This object is not a query");
} }
function checkKeySchema(key_schema) {
var prop;
if (key_schema !== undefined) {
if (typeof key_schema !== 'object') {
throw new TypeError("SimpleQuery().create(): " +
"key_schema is not of type 'object'");
}
// key_set is mandatory
if (key_schema.key_set === undefined) {
throw new TypeError("SimpleQuery().create(): " +
"key_schema has no 'key_set' property");
}
for (prop in key_schema) {
if (key_schema.hasOwnProperty(prop)) {
switch (prop) {
case 'key_set':
case 'cast_lookup':
case 'match_lookup':
break;
default:
throw new TypeError("SimpleQuery().create(): " +
"key_schema has unknown property '" + prop + "'");
}
}
}
}
}
/** /**
* The SimpleQuery inherits from Query, and compares one metadata value * The SimpleQuery inherits from Query, and compares one metadata value
* *
...@@ -6917,11 +6945,7 @@ return new Parser; ...@@ -6917,11 +6945,7 @@ return new Parser;
* @param {String} spec.value The value of the metadata to compare * @param {String} spec.value The value of the metadata to compare
*/ */
function SimpleQuery(spec, key_schema) { function SimpleQuery(spec, key_schema) {
Query.call(this); Query.call(this, key_schema);
checkKeySchema(key_schema);
this._key_schema = key_schema || {};
/** /**
* Operator to use to compare object values * Operator to use to compare object values
...@@ -11956,10 +11980,37 @@ return new Parser; ...@@ -11956,10 +11980,37 @@ return new Parser;
}(jIO, UriTemplate, FormData, RSVP, URI, Blob, }(jIO, UriTemplate, FormData, RSVP, URI, Blob,
SimpleQuery, ComplexQuery)); SimpleQuery, ComplexQuery));
;/*jslint nomen: true*/ ;/*jslint nomen: true*/
/*global RSVP*/ /*global RSVP, jiodate*/
(function (jIO, RSVP) { (function (jIO, RSVP, jiodate) {
"use strict"; "use strict";
function dateType(str) {
return jiodate.JIODate(new Date(str).toISOString());
}
function initKeySchema(storage, spec) {
var property;
for (property in spec.schema) {
if (spec.schema.hasOwnProperty(property)) {
if (spec.schema[property].type === "string" &&
spec.schema[property].format === "date-time") {
storage._key_schema.key_set[property] = {
read_from: property,
cast_to: "dateType"
};
if (storage._key_schema.cast_lookup.dateType === undefined) {
storage._key_schema.cast_lookup.dateType = dateType;
}
} else {
throw new jIO.util.jIOError(
"Wrong schema for property: " + property,
400
);
}
}
}
}
/** /**
* The jIO QueryStorage extension * The jIO QueryStorage extension
* *
...@@ -11968,7 +12019,8 @@ return new Parser; ...@@ -11968,7 +12019,8 @@ return new Parser;
*/ */
function QueryStorage(spec) { function QueryStorage(spec) {
this._sub_storage = jIO.createJIO(spec.sub_storage); this._sub_storage = jIO.createJIO(spec.sub_storage);
this._key_schema = spec.key_schema; this._key_schema = {key_set: {}, cast_lookup: {}};
initKeySchema(this, spec);
} }
QueryStorage.prototype.get = function () { QueryStorage.prototype.get = function () {
...@@ -12168,7 +12220,7 @@ return new Parser; ...@@ -12168,7 +12220,7 @@ return new Parser;
jIO.addStorage('query', QueryStorage); jIO.addStorage('query', QueryStorage);
}(jIO, RSVP)); }(jIO, RSVP, jiodate));
;/*jslint nomen: true*/ ;/*jslint nomen: true*/
/*global RSVP, Blob*/ /*global RSVP, Blob*/
(function (jIO, RSVP, Blob) { (function (jIO, RSVP, Blob) {
......
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
{ {
"name": "jio", "name": "jio",
"version": "v3.24.0", "version": "v3.25.0",
"license": "LGPLv3", "license": "LGPLv3",
"author": "Nexedi SA", "author": "Nexedi SA",
"contributors": [ "contributors": [
......
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