Commit 7f71dd30 authored by Vincent Bechu's avatar Vincent Bechu Committed by Vincent Bechu

[querystorage] add spec schema to use key_schema

parent fdcb654c
/*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;
}
}
}
}
}
/** /**
* The jIO QueryStorage extension * The jIO QueryStorage extension
* *
...@@ -11,7 +33,8 @@ ...@@ -11,7 +33,8 @@
*/ */
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 () {
...@@ -211,4 +234,4 @@ ...@@ -211,4 +234,4 @@
jIO.addStorage('query', QueryStorage); jIO.addStorage('query', QueryStorage);
}(jIO, RSVP)); }(jIO, RSVP, jiodate));
/*jslint nomen: true*/ /*jslint nomen: true*/
/*global Blob*/ /*global Blob, jiodate*/
(function (jIO, QUnit, Blob) { (function (jIO, QUnit, Blob) {
"use strict"; "use strict";
var test = QUnit.test, var test = QUnit.test,
...@@ -27,6 +27,7 @@ ...@@ -27,6 +27,7 @@
test("create substorage", function () { test("create substorage", function () {
var jio = jIO.createJIO({ var jio = jIO.createJIO({
type: "query", type: "query",
schema: {'date': {type: 'string', format: 'date-time'}},
sub_storage: { sub_storage: {
type: "querystorage200" type: "querystorage200"
} }
...@@ -34,6 +35,13 @@ ...@@ -34,6 +35,13 @@
ok(jio.__storage._sub_storage instanceof jio.constructor); ok(jio.__storage._sub_storage instanceof jio.constructor);
equal(jio.__storage._sub_storage.__type, "querystorage200"); equal(jio.__storage._sub_storage.__type, "querystorage200");
deepEqual(jio.__storage._key_schema.key_set, {
"date": {
"cast_to": "dateType",
"read_from": "date"
}
}, 'check key_schema');
ok(typeof jio.__storage._key_schema.cast_lookup.dateType === 'function');
}); });
...@@ -823,6 +831,99 @@ ...@@ -823,6 +831,99 @@
}); });
}); });
test("manual query used and use schema", function () {
stop();
expect(4);
function StorageSchemaCapacity() {
return this;
}
StorageSchemaCapacity.prototype.get = function (id) {
var doc = {
title: id,
id: "ID " + id,
"another": "property"
};
if (id === "foo") {
equal(id, "foo", "Get foo");
doc.modification_date = "Fri, 08 Sep 2017 07:46:27 +0000";
} else {
equal(id, "bar", "Get bar");
doc.modification_date = "Thu, 07 Sep 2017 18:59:23 +0000";
}
return doc;
};
StorageSchemaCapacity.prototype.hasCapacity = function (capacity) {
if ((capacity === "list")) {
return true;
}
return false;
};
StorageSchemaCapacity.prototype.buildQuery = function (options) {
deepEqual(options, {}, "No query parameter");
var result2 = [{
id: "foo",
value: {}
}, {
id: "bar",
value: {}
}];
return result2;
};
jIO.addStorage(
'querystoragenoschemacapacity',
StorageSchemaCapacity
);
var jio = jIO.createJIO({
type: "query",
schema: {
"modification_date": {
"type": "string",
"format": "date-time"
}
},
sub_storage: {
type: "querystoragenoschemacapacity"
}
});
jio.allDocs({
sort_on: [["modification_date", "descending"]],
limit: [0, 5],
select_list: ['modification_date']
})
.then(function (result) {
deepEqual(result, {
data: {
rows: [
{
id: "foo",
doc: {},
value: {
modification_date: "Fri, 08 Sep 2017 07:46:27 +0000"
}
}, {
id: "bar",
doc: {},
value: {
modification_date: "Thu, 07 Sep 2017 18:59:23 +0000"
}
}
],
total_rows: 2
}
});
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
// queryStorage.repair // queryStorage.repair
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
......
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