Commit 76ddf154 authored by Aurélien Vermylen's avatar Aurélien Vermylen

Merge branch 'master' of https://lab.nexedi.com/nexedi/jio into HEAD

Conflicts:
	Gruntfile.js
	dist/jio-latest.js
	dist/jio-latest.min.js
parents 21704054 18cf665b
## Javascript Input/Output
# jIO
**jIO is a client-side JavaScript library to manage documents across multiple
storages.**
jIO is a promised-based JavaScript library that offers connectors to many storages (Dropbox, webdav, IndexedDB, GDrive, ...) using a single API. jIO supports offline use, replication, encryption and synchronization as well as querying of stored documents and attachments allowing to build complex client-side centric web applications.
### Getting Started
### jIO Documentation
To set up jIO you should include jio.js, dependencies and the connectors for the storages
you want to use in the HTML page header (note that more dependencies may be required
depending on type of storages being used):
The documentation can be found on [https://jio.nexedi.com/](https://jio.nexedi.com/)
```html
<script src="RSVP.js"></script>
<script src="jio-latest.js"></script>
```
Then create your jIO instance like this:
```javascript
// create a new jio
var jio_instance = jIO.createJIO({
type: "query",
sub_storage: {
type: "uuid",
sub_storage: {
"type": "indexeddb",
"database": "test"
}
}
});
```
### Documents and Methods
Documents are JSON strings that contain *metadata* (properties, like a filename)
and *attachments* (optional content, for example *image.jpg*).
jIO exposes the following methods to *create*, *read*, *update* and *delete* documents
(for more information, including revision management and available options for
each method, please refer to the documentation):
```javascript
// create and store new document
jio_instance.post({"title": "some title"})
.then(function (new_id) {
...
});
// create or update an existing document
jio_instance.put(new_id, {"title": "New Title"})
.then(function ()
// console.log("Document stored");
});
// add an attachement to a document
jio_instance.putAttachment(document_id, attachment_id, new Blob())
.then(function () {
// console.log("Blob stored");
});
// read a document
jio_instance.get(document_id)
.then(function (document) {
// console.log(document);
// {
// "title": "New Title",
// }
});
// read an attachement
jio_instance.getAttachment(document_id, attachment_id)
.then(function (blob) {
// console.log(blob);
});
// delete a document and its attachment(s)
jio_instance.remove(document_id)
.then(function () {
// console.log("Document deleted");
});
// delete an attachement
jio_instance.removeAttachment(document_id, attachment_id)
.then(function () {
// console.log("Attachment deleted");
});
// get all documents
jio_instance.allDocs().then(function (response) {
// console.log(response);
// {
// "data": {
// "total_rows": 1,
// "rows": [{
// "id": "my_document",
// "value": {}
// }]
// }
// }
});
```
### Example
This is an example of how to store a video file with one attachment in local
storage. Note that attachments should be added after document creation.
```javascript
// create a new localStorage
var jio_instance = jIO.createJIO({
"type": "local",
});
var my_video_blob = new Blob([my_video_binary_string], {
"type": "video/ogg"
});
// post the document
jio_instance.put("myVideo", {
"title" : "My Video",
"format" : ["video/ogg", "vorbis", "HD"],
"language" : "en",
"description" : "Images Compilation"
}).then(function (response) {
// add video attachment
return jio_instance.putAttachment(
"myVideo",
"video.ogv",
my_video_blob
});
}).then(function (response) {
alert('Video Stored');
}, function (err) {
alert('Error when attaching the video');
}, function (progression) {
console.log(progression);
});
```
### Storage Locations
jIO allows to build "storage trees" consisting of connectors to multiple
storages (webDav, xWiki, S3, localStorage) and use type-storages to add features
like revision management or indices to a child storage (sub_storage).
The following storages are currently supported:
- LocalStorage (browser local storage)
- IndexedDB
- ERP5Storage
- DAVStorage (connect to webDAV, more information on the
[documentation](https://www.j-io.org/documentation/jio-documentation/))
For more information on the specific storages including guidelines on how to
create your own connector, please also refer to the [documentation](https://www.j-io.org/documentation/jio-documentation).
### jIO Query
jIO can use queries, which can be run in the allDocs() method to query document
lists. A sample query would look like this (note that not all storages support
allDocs and jio queries, and that pre-querying of documents on distant storages
should best be done server-side):
```javascript
// run allDocs with query option on an existing jIO
jio_instance.allDocs({
"query": '(fieldX: >= "string" AND fieldY: < "string")',
// records to display ("from to")
"limit": [0, 5],
// sort by
"sort_on": [[<string A>, 'descending']],
// fields to return in response
"select_list": [<string A>, <string B>]
}).then(function (response) {
// console.log(response);
// {
// "total_rows": 1,
// "rows": [{
// "id": <string>,
// "value": {
// <string A>: <string>,
// <string B>: <string>
// }
// }, { .. }]
// }
});
```
To find out more about queries, please refer to the documentation.
### Authors
- Francois Billioud
- Tristan Cavelier
- Sven Franck
- Romain Courteaud
### Copyright and license
jIO is an open-source library and is licensed under the LGPL license. More
information on LGPL can be found
[here](http://en.wikipedia.org/wiki/GNU_Lesser_General_Public_License).
### Contribute
Get development environment:
git clone https://lab.nexedi.com/nexedi/jio.git jio.git
cd jio.git
### jIO Quickstart
git clone https://lab.nexedi.com/nexedi/jio.git
npm install
alias grunt="./node_modules/grunt-cli/bin/grunt"
grunt
Run tests:
grunt server
and open http://127.0.0.1:9000/test/tests.html
### jIO Code
RenderJS source code is hosted on Gitlab at [https://lab.nexedi.com/nexedi/jio](https://lab.nexedi.com/nexedi/jio) (Github [mirror](https://github.com/nexedi/jio/) - please use the issue tracker on Gitlab)
Submit merge requests on lab.nexedi.com.
### jIO Test
You can run tests after installing and building jIO by opening the */test/* folder
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.
......@@ -7011,7 +7011,7 @@ return new Parser;
value = '%' + this.value + '%';
for (k in item) {
if (item.hasOwnProperty(k)) {
if (k !== '__id') {
if (k !== '__id' && item[k]) {
if (matchMethod(item[k], value) === true) {
return true;
}
......@@ -12366,10 +12366,9 @@ return new Parser;
}(jIO, RSVP, Blob));
;/*jslint nomen: true*/
/*global Blob, atob, btoa, RSVP*/
(function (jIO, Blob, atob, btoa, RSVP) {
/*global Blob, RSVP, unescape, escape*/
(function (jIO, Blob, RSVP, unescape, escape) {
"use strict";
/**
* The jIO DocumentStorage extension
*
......@@ -12385,7 +12384,13 @@ return new Parser;
var DOCUMENT_EXTENSION = ".json",
DOCUMENT_REGEXP = new RegExp("^jio_document/([\\w=]+)" +
DOCUMENT_EXTENSION + "$"),
ATTACHMENT_REGEXP = new RegExp("^jio_attachment/([\\w=]+)/([\\w=]+)$");
ATTACHMENT_REGEXP = new RegExp("^jio_attachment/([\\w=]+)/([\\w=]+)$"),
btoa = function (str) {
return window.btoa(unescape(encodeURIComponent(str)));
},
atob = function (str) {
return decodeURIComponent(escape(window.atob(str)));
};
function getSubAttachmentIdFromParam(id, name) {
if (name === undefined) {
......@@ -12592,7 +12597,7 @@ return new Parser;
jIO.addStorage('document', DocumentStorage);
}(jIO, Blob, atob, btoa, RSVP));
}(jIO, Blob, RSVP, unescape, escape));
;/*
* Copyright 2013, Nexedi SA
* Released under the LGPL license.
......@@ -13789,648 +13794,95 @@ return new Parser;
jIO.addStorage('websql', WebSQLStorage);
}(jIO, RSVP, Blob, openDatabase));
;/*jslint indent:2, maxlen: 80, nomen: true */
/*global jIO, RSVP, UriTemplate, SimpleQuery, ComplexQuery, QueryFactory,
Query, FormData*/
(function (jIO, RSVP, UriTemplate, SimpleQuery, ComplexQuery, QueryFactory,
Query, FormData) {
;/*jslint indent:2,maxlen:80,nomen:true*/
/*global jIO, RSVP*/
(function (jIO, RSVP) {
"use strict";
function getSubIdEqualSubProperty(storage, value, key) {
var query;
if (storage._no_sub_query_id) {
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({
key: key,
value: value,
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,
"sort_on": storage._query.sort_on,
"select_list": storage._query.select_list,
"limit": storage._query.limit
})
.push(function (data) {
if (data.data.total_rows === 0) {
throw new jIO.util.jIOError(
"Can not find document with (" + key + ", " + value + ")",
404
);
}
if (data.data.total_rows > 1) {
throw new TypeError("id must be unique field: " + key
+ ", result:" + data.data.rows.toString());
}
storage._mapping_id_memory_dict[value] = data.data.rows[0].id;
return data.data.rows[0].id;
});
}
/*jslint unparam: true*/
var mapping_function = {
"equalSubProperty": {
"mapToSubProperty": function (property, sub_doc, doc, args, id) {
sub_doc[args] = doc[property];
return args;
},
"mapToMainProperty": function (property, sub_doc, doc, args, sub_id) {
if (sub_doc.hasOwnProperty(args)) {
doc[property] = sub_doc[args];
}
return args;
},
"mapToSubId": function (storage, doc, id, args) {
if (doc !== undefined) {
if (storage._property_for_sub_id &&
doc.hasOwnProperty(storage._property_for_sub_id)) {
return doc[storage._property_for_sub_id];
}
}
return getSubIdEqualSubProperty(storage, id, storage._map_id[1]);
},
"mapToId": function (storage, sub_doc, sub_id, args) {
return sub_doc[args];
}
},
"equalValue": {
"mapToSubProperty": function (property, sub_doc, doc, args) {
sub_doc[property] = args;
return property;
},
"mapToMainProperty": function (property) {
return property;
}
},
"ignore": {
"mapToSubProperty": function () {
return false;
},
"mapToMainProperty": function (property) {
return property;
}
},
"equalSubId": {
"mapToSubProperty": function (property, sub_doc, doc) {
sub_doc[property] = doc[property];
return property;
},
"mapToMainProperty": function (property, sub_doc, doc, args, sub_id) {
if (sub_id === undefined && sub_doc.hasOwnProperty(property)) {
doc[property] = sub_doc[property];
} else {
doc[property] = sub_id;
}
return property;
},
"mapToSubId": function (storage, doc, id, args) {
return id;
},
"mapToId": function (storage, sub_doc, sub_id) {
return sub_id;
}
},
"keep": {
"mapToSubProperty": function (property, sub_doc, doc) {
sub_doc[property] = doc[property];
return property;
},
"mapToMainProperty": function (property, sub_doc, doc) {
doc[property] = sub_doc[property];
return property;
}
},
"switchPropertyValue": {
"mapToSubProperty": function (property, sub_doc, doc, args) {
sub_doc[args[0]] = args[1][doc[property]];
return args[0];
},
"mapToMainProperty": function (property, sub_doc, doc, args) {
var subvalue, value = sub_doc[args[0]];
for (subvalue in args[1]) {
if (args[1].hasOwnProperty(subvalue)) {
if (value === args[1][subvalue]) {
doc[property] = subvalue;
return property;
}
}
}
}
}
};
/*jslint unparam: false*/
/**
* The jIO SafeRepairStorage extension
*
* @class SafeRepairStorage
* @constructor
*/
function initializeQueryAndDefaultMapping(storage) {
var property, query_list = [];
for (property in storage._mapping_dict) {
if (storage._mapping_dict.hasOwnProperty(property)) {
if (storage._mapping_dict[property][0] === "equalValue") {
if (storage._mapping_dict[property][1] === undefined) {
throw new jIO.util.jIOError("equalValue has not parameter", 400);
}
storage._default_mapping[property] =
storage._mapping_dict[property][1];
query_list.push(new SimpleQuery({
key: property,
value: storage._mapping_dict[property][1],
type: "simple"
}));
}
if (storage._mapping_dict[property][0] === "equalSubId") {
if (storage._property_for_sub_id !== undefined) {
throw new jIO.util.jIOError(
"equalSubId can be defined one time",
400
);
}
storage._property_for_sub_id = property;
}
}
}
if (storage._map_id[0] === "equalSubProperty") {
storage._mapping_dict[storage._map_id[1]] = ["keep"];
}
if (storage._query.query !== undefined) {
query_list.push(QueryFactory.create(storage._query.query));
}
if (query_list.length > 1) {
storage._query.query = new ComplexQuery({
type: "complex",
query_list: query_list,
operator: "AND"
});
} else if (query_list.length === 1) {
storage._query.query = query_list[0];
}
}
function MappingStorage(spec) {
this._mapping_dict = spec.property || {};
function SafeRepairStorage(spec) {
this._sub_storage = jIO.createJIO(spec.sub_storage);
this._map_all_property = spec.map_all_property !== undefined ?
spec.map_all_property : true;
this._no_sub_query_id = spec.no_sub_query_id;
this._attachment_mapping_dict = spec.attachment || {};
this._query = spec.query || {};
this._map_id = spec.id || ["equalSubId"];
this._id_mapped = (spec.id !== undefined) ? spec.id[1] : false;
if (this._query.query !== undefined) {
this._query.query = QueryFactory.create(this._query.query);
}
this._default_mapping = {};
this._mapping_id_memory_dict = {};
this._attachment_list = spec.attachment_list || [];
initializeQueryAndDefaultMapping(this);
}
function getAttachmentId(storage, sub_id, attachment_id, method) {
var mapping_dict = storage._attachment_mapping_dict;
if (mapping_dict !== undefined
&& mapping_dict[attachment_id] !== undefined
&& mapping_dict[attachment_id][method] !== undefined
&& mapping_dict[attachment_id][method].uri_template !== undefined) {
return UriTemplate.parse(
mapping_dict[attachment_id][method].uri_template
).expand({id: sub_id});
}
return attachment_id;
}
function getSubStorageId(storage, id, doc) {
return new RSVP.Queue()
.push(function () {
var map_info = storage._map_id || ["equalSubId"];
if (storage._property_for_sub_id && doc !== undefined &&
doc.hasOwnProperty(storage._property_for_sub_id)) {
return doc[storage._property_for_sub_id];
}
return mapping_function[map_info[0]].mapToSubId(
storage,
doc,
id,
map_info[1]
);
});
this._id_dict = {};
}
function mapToSubProperty(storage, property, sub_doc, doc, id) {
var mapping_info = storage._mapping_dict[property] || ["keep"];
return mapping_function[mapping_info[0]].mapToSubProperty(
property,
sub_doc,
doc,
mapping_info[1],
id
);
}
function mapToMainProperty(storage, property, sub_doc, doc, sub_id) {
var mapping_info = storage._mapping_dict[property] || ["keep"];
return mapping_function[mapping_info[0]].mapToMainProperty(
property,
sub_doc,
doc,
mapping_info[1],
sub_id
);
}
function mapToMainDocument(storage, sub_doc, sub_id) {
var doc = {},
property,
property_list = [storage._id_mapped];
for (property in storage._mapping_dict) {
if (storage._mapping_dict.hasOwnProperty(property)) {
property_list.push(mapToMainProperty(
storage,
property,
sub_doc,
doc,
sub_id
));
}
}
if (storage._map_all_property) {
for (property in sub_doc) {
if (sub_doc.hasOwnProperty(property)) {
if (property_list.indexOf(property) < 0) {
doc[property] = sub_doc[property];
}
}
}
}
if (storage._map_for_sub_storage_id !== undefined) {
doc[storage._map_for_sub_storage_id] = sub_id;
}
return doc;
}
function mapToSubstorageDocument(storage, doc, id) {
var sub_doc = {}, property;
for (property in doc) {
if (doc.hasOwnProperty(property)) {
mapToSubProperty(storage, property, sub_doc, doc, id);
}
}
for (property in storage._default_mapping) {
if (storage._default_mapping.hasOwnProperty(property)) {
sub_doc[property] = storage._default_mapping[property];
}
}
if (storage._map_id[0] === "equalSubProperty" && id !== undefined) {
sub_doc[storage._map_id[1]] = id;
}
return sub_doc;
}
function handleAttachment(storage, argument_list, method) {
return getSubStorageId(storage, argument_list[0])
.push(function (sub_id) {
argument_list[0] = sub_id;
var old_id = argument_list[1];
argument_list[1] = getAttachmentId(
storage,
argument_list[0],
argument_list[1],
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(
storage._sub_storage,
argument_list
);
});
}
MappingStorage.prototype.get = function (id) {
var storage = this;
return getSubStorageId(this, id)
.push(function (sub_id) {
return storage._sub_storage.get(sub_id)
.push(function (sub_doc) {
return mapToMainDocument(storage, sub_doc, sub_id);
});
});
SafeRepairStorage.prototype.get = function () {
return this._sub_storage.get.apply(this._sub_storage, arguments);
};
MappingStorage.prototype.post = function (doc) {
var sub_doc = mapToSubstorageDocument(
this,
doc
),
id = doc[this._property_for_sub_id],
storage = this;
if (this._property_for_sub_id && id !== undefined) {
return this._sub_storage.put(id, sub_doc);
}
if (this._id_mapped && doc[this._id_mapped] !== undefined) {
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(
"post is not supported with id mapped",
400
);
SafeRepairStorage.prototype.allAttachments = function () {
return this._sub_storage.allAttachments.apply(this._sub_storage, arguments);
};
MappingStorage.prototype.put = function (id, doc) {
var storage = this,
sub_doc = mapToSubstorageDocument(this, doc, id);
return getSubStorageId(this, id, doc)
.push(function (sub_id) {
return storage._sub_storage.put(sub_id, sub_doc);
})
.push(undefined, function (error) {
if (error instanceof jIO.util.jIOError && error.status_code === 404) {
return storage._sub_storage.post(sub_doc);
}
throw error;
})
.push(function () {
return id;
});
SafeRepairStorage.prototype.post = function () {
return this._sub_storage.post.apply(this._sub_storage, arguments);
};
MappingStorage.prototype.remove = function (id) {
SafeRepairStorage.prototype.put = function (id, doc) {
var storage = this;
return getSubStorageId(this, id)
.push(function (sub_id) {
return storage._sub_storage.remove(sub_id);
})
.push(function () {
return id;
return this._sub_storage.put.apply(this._sub_storage, arguments)
.push(undefined, function (error) {
if (error instanceof jIO.util.jIOError &&
error.status_code === 403) {
if (storage._id_dict[id]) {
return storage._sub_storage.put(storage._id_dict[id], doc);
}
return storage._sub_storage.post(doc)
.push(function (sub_id) {
storage._id_dict[id] = sub_id;
return sub_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)
.push(function () {
return attachment_id;
});
SafeRepairStorage.prototype.remove = function () {
return;
};
MappingStorage.prototype.removeAttachment = function (id, attachment_id) {
return handleAttachment(this, arguments, "remove", id)
.push(function () {
return attachment_id;
});
SafeRepairStorage.prototype.getAttachment = function () {
return this._sub_storage.getAttachment.apply(this._sub_storage, arguments);
};
MappingStorage.prototype.allAttachments = function (id) {
var storage = this, sub_id;
return getSubStorageId(storage, id)
.push(function (sub_id_result) {
sub_id = sub_id_result;
return storage._sub_storage.allAttachments(sub_id);
})
.push(function (result) {
var attachment_id,
attachments = {},
mapping_dict = {},
i;
for (attachment_id in storage._attachment_mapping_dict) {
if (storage._attachment_mapping_dict.hasOwnProperty(attachment_id)) {
mapping_dict[getAttachmentId(storage, sub_id, attachment_id, "get")]
= attachment_id;
}
}
for (attachment_id in result) {
if (result.hasOwnProperty(attachment_id)) {
if (!(storage._attachment_list.length > 0
&& storage._attachment_list.indexOf(attachment_id) < 0)) {
if (mapping_dict.hasOwnProperty(attachment_id)) {
attachments[mapping_dict[attachment_id]] = {};
} else {
attachments[attachment_id] = {};
SafeRepairStorage.prototype.putAttachment = function (id, attachment_id,
attachment) {
var storage = this;
return this._sub_storage.putAttachment.apply(this._sub_storage, arguments)
.push(undefined, function (error) {
if (error instanceof jIO.util.jIOError &&
error.status_code === 403) {
return new RSVP.Queue()
.push(function () {
if (storage._id_dict[id]) {
return storage._id_dict[id];
}
}
}
}
for (i = 0; i < storage._attachment_list.length; i += 1) {
if (!attachments.hasOwnProperty(storage._attachment_list[i])) {
attachments[storage._attachment_list[i]] = {};
}
return storage._sub_storage.get(id)
.push(function (doc) {
return storage._sub_storage.post(doc);
});
})
.push(function (sub_id) {
storage._id_dict[id] = sub_id;
return storage._sub_storage.putAttachment(sub_id, attachment_id,
attachment);
});
}
return attachments;
});
};
MappingStorage.prototype.hasCapacity = function (name) {
return this._sub_storage.hasCapacity(name);
SafeRepairStorage.prototype.removeAttachment = function () {
return;
};
MappingStorage.prototype.repair = function () {
SafeRepairStorage.prototype.repair = function () {
return this._sub_storage.repair.apply(this._sub_storage, arguments);
};
MappingStorage.prototype.bulk = function (id_list) {
var storage = this;
function mapId(parameter) {
return getSubStorageId(storage, parameter.parameter_list[0])
.push(function (id) {
return {"method": parameter.method, "parameter_list": [id]};
});
}
return new RSVP.Queue()
.push(function () {
var promise_list = id_list.map(mapId);
return RSVP.all(promise_list);
})
.push(function (id_list_mapped) {
return storage._sub_storage.bulk(id_list_mapped);
})
.push(function (result) {
var mapped_result = [], i;
for (i = 0; i < result.length; i += 1) {
mapped_result.push(mapToMainDocument(
storage,
result[i]
));
}
return mapped_result;
});
SafeRepairStorage.prototype.hasCapacity = function (name) {
return this._sub_storage.hasCapacity(name);
};
MappingStorage.prototype.buildQuery = function (option) {
var storage = this,
i,
query,
property,
select_list = [],
sort_on = [];
function mapQuery(one_query) {
var j, query_list = [], key, sub_query;
if (one_query.type === "complex") {
for (j = 0; j < one_query.query_list.length; j += 1) {
sub_query = mapQuery(one_query.query_list[j]);
if (sub_query) {
query_list.push(sub_query);
}
}
one_query.query_list = query_list;
return one_query;
}
key = mapToMainProperty(storage, one_query.key, {}, {});
if (key !== undefined) {
one_query.key = key;
return one_query;
}
return false;
}
if (option.sort_on !== undefined) {
for (i = 0; i < option.sort_on.length; i += 1) {
property = mapToMainProperty(this, option.sort_on[i][0], {}, {});
if (property && sort_on.indexOf(property) < 0) {
sort_on.push([property, option.sort_on[i][1]]);
}
}
}
if (this._query.sort_on !== undefined) {
for (i = 0; i < this._query.sort_on.length; i += 1) {
property = mapToMainProperty(this, this._query.sort_on[i], {}, {});
if (sort_on.indexOf(property) < 0) {
sort_on.push([property, option.sort_on[i][1]]);
}
}
}
if (option.select_list !== undefined) {
for (i = 0; i < option.select_list.length; i += 1) {
property = mapToMainProperty(this, option.select_list[i], {}, {});
if (property && select_list.indexOf(property) < 0) {
select_list.push(property);
}
}
}
if (this._query.select_list !== undefined) {
for (i = 0; i < this._query.select_list; i += 1) {
property = this._query.select_list[i];
if (select_list.indexOf(property) < 0) {
select_list.push(property);
}
}
}
if (this._id_mapped) {
// modify here for future way to map id
select_list.push(this._id_mapped);
}
if (option.query !== undefined) {
query = mapQuery(QueryFactory.create(option.query));
}
if (this._query.query !== undefined) {
if (query === undefined) {
query = this._query.query;
}
query = new ComplexQuery({
operator: "AND",
query_list: [query, this._query.query],
type: "complex"
});
}
if (query !== undefined) {
query = Query.objectToSearchText(query);
}
return this._sub_storage.allDocs(
{
query: query,
select_list: select_list,
sort_on: sort_on,
limit: option.limit
}
)
.push(function (result) {
var sub_doc, map_info = storage._map_id || ["equalSubId"];
for (i = 0; i < result.data.total_rows; i += 1) {
sub_doc = result.data.rows[i].value;
result.data.rows[i].id =
mapping_function[map_info[0]].mapToId(
storage,
sub_doc,
result.data.rows[i].id,
map_info[1]
);
result.data.rows[i].value =
mapToMainDocument(
storage,
sub_doc
);
}
return result.data.rows;
});
SafeRepairStorage.prototype.buildQuery = function () {
return this._sub_storage.buildQuery.apply(this._sub_storage,
arguments);
};
jIO.addStorage('mapping', MappingStorage);
}(jIO, RSVP, UriTemplate, SimpleQuery, ComplexQuery, QueryFactory, Query,
FormData));
\ No newline at end of file
jIO.addStorage('saferepair', SafeRepairStorage);
}(jIO, RSVP));
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.
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",
"version": "v3.21.0",
"version": "v3.23.1",
"license": "LGPLv3",
"author": "Nexedi SA",
"contributors": [
......
/*jslint nomen: true*/
/*global Blob, atob, btoa, RSVP*/
(function (jIO, Blob, atob, btoa, RSVP) {
/*global Blob, RSVP, unescape, escape*/
(function (jIO, Blob, RSVP, unescape, escape) {
"use strict";
/**
* The jIO DocumentStorage extension
*
......@@ -18,7 +17,13 @@
var DOCUMENT_EXTENSION = ".json",
DOCUMENT_REGEXP = new RegExp("^jio_document/([\\w=]+)" +
DOCUMENT_EXTENSION + "$"),
ATTACHMENT_REGEXP = new RegExp("^jio_attachment/([\\w=]+)/([\\w=]+)$");
ATTACHMENT_REGEXP = new RegExp("^jio_attachment/([\\w=]+)/([\\w=]+)$"),
btoa = function (str) {
return window.btoa(unescape(encodeURIComponent(str)));
},
atob = function (str) {
return decodeURIComponent(escape(window.atob(str)));
};
function getSubAttachmentIdFromParam(id, name) {
if (name === undefined) {
......@@ -225,4 +230,4 @@
jIO.addStorage('document', DocumentStorage);
}(jIO, Blob, atob, btoa, RSVP));
}(jIO, Blob, RSVP, unescape, escape));
/*jslint nomen: true */
/*global RSVP, UriTemplate*/
(function (jIO, RSVP, UriTemplate) {
"use strict";
var GET_POST_URL = "https://graph.facebook.com/v2.9/{+post_id}" +
"?fields={+fields}&access_token={+access_token}",
get_post_template = UriTemplate.parse(GET_POST_URL),
GET_FEED_URL = "https://graph.facebook.com/v2.9/{+user_id}/feed" +
"?fields={+fields}&limit={+limit}&since={+since}&access_token=" +
"{+access_token}",
get_feed_template = UriTemplate.parse(GET_FEED_URL);
function FBStorage(spec) {
if (typeof spec.access_token !== 'string' || !spec.access_token) {
throw new TypeError("Access Token must be a string " +
"which contains more than one character.");
}
if (typeof spec.user_id !== 'string' || !spec.user_id) {
throw new TypeError("User ID must be a string " +
"which contains more than one character.");
}
this._access_token = spec.access_token;
this._user_id = spec.user_id;
this._default_field_list = spec.default_field_list || [];
this._default_limit = spec.default_limit || 500;
}
FBStorage.prototype.get = function (id) {
var that = this;
return new RSVP.Queue()
.push(function () {
return jIO.util.ajax({
type: "GET",
url: get_post_template.expand({post_id: id,
fields: that._default_field_list, access_token: that._access_token})
});
})
.push(function (result) {
return JSON.parse(result.target.responseText);
});
};
function paginateResult(url, result, select_list) {
return new RSVP.Queue()
.push(function () {
return jIO.util.ajax({
type: "GET",
url: url
});
})
.push(function (response) {
return JSON.parse(response.target.responseText);
},
function (err) {
throw new jIO.util.jIOError("Getting feed failed " + err.toString(),
err.target.status);
})
.push(function (response) {
if (response.data.length === 0) {
return result;
}
var i, j, obj = {};
for (i = 0; i < response.data.length; i += 1) {
obj.id = response.data[i].id;
obj.value = {};
for (j = 0; j < select_list.length; j += 1) {
obj.value[select_list[j]] = response.data[i][select_list[j]];
}
result.push(obj);
obj = {};
}
return paginateResult(response.paging.next, result, select_list);
});
}
FBStorage.prototype.buildQuery = function (query) {
var that = this, fields = [], limit = this._default_limit,
template_argument = {
user_id: this._user_id,
limit: limit,
access_token: this._access_token
};
if (query.include_docs) {
fields = fields.concat(that._default_field_list);
}
if (query.select_list) {
fields = fields.concat(query.select_list);
}
if (query.limit) {
limit = query.limit[1];
}
template_argument.fields = fields;
template_argument.limit = limit;
return paginateResult(get_feed_template.expand(template_argument), [],
fields)
.push(function (result) {
if (!query.limit) {
return result;
}
return result.slice(query.limit[0], query.limit[1]);
});
};
FBStorage.prototype.hasCapacity = function (name) {
var this_storage_capacity_list = ["list", "select", "include", "limit"];
if (this_storage_capacity_list.indexOf(name) !== -1) {
return true;
}
};
jIO.addStorage('facebook', FBStorage);
}(jIO, RSVP, UriTemplate));
\ No newline at end of file
......@@ -385,7 +385,9 @@
end_index -= 1;
}
function resolver(result) {
result_list.push(result);
if (result.blob !== undefined) {
result_list.push(result);
}
resolve(result_list);
}
function getPart(i) {
......
......@@ -416,7 +416,7 @@
}
return new RegExp("^" + stringEscapeRegexpCharacters(string)
.replace(regexp_percent, '[\\s\\S]*')
.replace(regexp_underscore, '.') + "$");
.replace(regexp_underscore, '.') + "$", "i");
}
/**
......@@ -743,7 +743,7 @@
value = '%' + this.value + '%';
for (k in item) {
if (item.hasOwnProperty(k)) {
if (k !== '__id') {
if (k !== '__id' && item[k]) {
if (matchMethod(item[k], value) === true) {
return true;
}
......
......@@ -236,6 +236,46 @@
});
});
test("with special utf-8 char", function () {
stop();
expect(5);
Storage200.prototype.putAttachment = function (id, name, blob) {
equal(blob.type, "application/json", "Blob type is OK");
equal(id, "foo", "putAttachment 200 called");
equal(
name,
"jio_document/Zm9vw6kKYmFy5rWL6K+V5Zub8J+YiA==.json",
"putAttachment 200 called"
);
return jIO.util.readBlobAsText(blob)
.then(function (result) {
deepEqual(JSON.parse(result.target.result),
{"title": "fooé\nbar测试四😈"},
"JSON is in blob");
return id;
});
};
var jio = jIO.createJIO({
type: "document",
document_id: "foo",
sub_storage: {
type: "documentstorage200"
}
});
jio.put("fooé\nbar测试四😈", {"title": "fooé\nbar测试四😈"})
.then(function (result) {
equal(result, "fooé\nbar测试四😈");
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
/////////////////////////////////////////////////////////////////
// documentStorage.remove
/////////////////////////////////////////////////////////////////
......
/*jslint nomen: true */
/*global Blob, sinon*/
(function (jIO, QUnit, sinon) {
"use strict";
var test = QUnit.test,
stop = QUnit.stop,
start = QUnit.start,
ok = QUnit.ok,
expect = QUnit.expect,
deepEqual = QUnit.deepEqual,
equal = QUnit.equal,
module = QUnit.module,
throws = QUnit.throws,
token = "sample_token",
user_id = "sample_user_id";
/////////////////////////////////////////////////////////////////
// Facebook Storage constructor
/////////////////////////////////////////////////////////////////
module("Facebook Drive Storage.constructor");
test("create storage", function () {
var jio = jIO.createJIO({
type: "facebook",
access_token: token,
user_id: user_id
});
equal(jio.__type, "facebook");
deepEqual(jio.__storage._access_token, token);
deepEqual(jio.__storage._user_id, user_id);
});
test("reject non string token", function () {
throws(
function () {
jIO.createJIO({
type: "facebook",
access_token: 42,
user_id: user_id,
default_field_list: ['id', 'message', 'created_time']
});
},
function (error) {
ok(error instanceof TypeError);
equal(error.message,
"Access Token must be a string which contains more than " +
"one character.");
return true;
}
);
});
test("reject non string user_id", function () {
throws(
function () {
jIO.createJIO({
type: "facebook",
access_token: '42',
user_id: 1
});
},
function (error) {
ok(error instanceof TypeError);
equal(error.message,
"User ID must be a string which contains more than one " +
"character.");
return true;
}
);
});
/////////////////////////////////////////////////////////////////
// Facebook Storage.get
/////////////////////////////////////////////////////////////////
module("Facebook Storage.get", {
setup: function () {
this.server = sinon.fakeServer.create();
this.server.autoRespond = true;
this.server.autoRespondAfter = 5;
this.jio = jIO.createJIO({
type: "facebook",
access_token: token,
user_id: user_id,
default_field_list: ['id', 'created_time', 'message', 'story']
});
},
teardown: function () {
this.server.restore();
delete this.server;
}
});
test("get post", function () {
var url = "https://graph.facebook.com/v2.9/sampleID" +
"?fields=id,created_time,message,story&access_token=sample_token",
body = '{"id": "sampleID",' +
'"created_time": "2017-07-13T09:37:13+0000",' +
'"message": "Test post", "story": "test"}';
this.server.respondWith("GET", url, [200, {
"Content-Type": "text/xml"
}, body
]);
stop();
expect(1);
this.jio.get("sampleID")
.then(function (result) {
deepEqual(result,
{"id": "sampleID",
"created_time": "2017-07-13T09:37:13+0000",
"message": "Test post",
"story": "test"}, "Check document");
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
/////////////////////////////////////////////////////////////////
// Facebook Storage.allDocs
/////////////////////////////////////////////////////////////////
module("Facebook Storage.allDocs", {
setup: function () {
this.server = sinon.fakeServer.create();
this.server.autoRespond = true;
this.server.autoRespondAfter = 5;
this.jio = jIO.createJIO({
type: "facebook",
access_token: token,
user_id: user_id,
default_field_list: ["id", "message", "created_time"]
});
},
teardown: function () {
this.server.restore();
delete this.server;
}
});
test("get all posts with single page returned", function () {
var url1 = 'https://graph.facebook.com/v2.9/sample_user_id/feed?fields' +
'=created_time,id,message,link&limit=500&since=&access_token=' +
'sample_token',
url2 = 'https://graph.facebook.com/v2.9/sample_user_id/feed?fields=' +
'created_time,id,message,link&limit=500&since=&access_token=' +
'sample_token&__paging_token=sample_paging_token',
body1 = '{"data": [{"created_time": "2016", "id": "1", ' +
'"message": "Test 1", "link": "https://test.com", ' +
'"story": "Test story"}, {"created_time": "2016", "id": "2", ' +
'"message": "Test 2", "link": "https://jio.com", "story": "Test post"}' +
', {"created_time": "2016", "id":"3", "message": "Test 3", ' +
'"link": "https://renderjs.com", "story": "Test story"}], ' +
'"paging": {"next": "' + url2 + '", "previous": null}}',
body2 = '{"data": [], "paging": {"next": null, "previous": null}}',
server = this.server,
return_object = {"data": {
"rows": [
{
"id": "1",
"value": {
"created_time": "2016",
"id": "1",
"message": "Test 1",
"link": "https://test.com"
}
},
{
"id": "2",
"value": {
"created_time": "2016",
"id": "2",
"message": "Test 2",
"link": "https://jio.com"
}
},
{
"id": "3",
"value": {
"created_time": "2016",
"id": "3",
"message": "Test 3",
"link": "https://renderjs.com"
}
}
],
"total_rows": 3
}
};
this.server.respondWith("GET", url1, [200, {
"Content-Type": "text/xml"
}, body1
]);
this.server.respondWith("GET", url2, [200, {
"Content-Type": "text/xml"
}, body2
]);
stop();
expect(10);
this.jio.allDocs({select_list: ["created_time", "id", "message",
"link"]})
.then(function (result) {
equal(server.requests.length, 2);
equal(server.requests[0].method, "GET");
equal(server.requests[1].method, "GET");
equal(server.requests[0].url, url1);
equal(server.requests[1].url, url2);
equal(server.requests[0].status, 200);
equal(server.requests[1].status, 200);
equal(server.requests[0].responseText, body1);
equal(server.requests[1].responseText, body2);
deepEqual(result, return_object);
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
test("get all posts with multiple paged result", function () {
var url1 = 'https://graph.facebook.com/v2.9/sample_user_id/feed?fields' +
'=created_time,id,message,link&limit=500&since=&access_token=' +
'sample_token',
url2 = 'https://graph.facebook.com/v2.9/sample_user_id/feed?fields=' +
'created_time,id,message,link&limit=500&since=&access_token=' +
'sample_token&__paging_token=sample_paging_token1',
url3 = 'https://graph.facebook.com/v2.9/sample_user_id/feed?fields=' +
'created_time,id,message,link&limit=500&since=&access_token=' +
'sample_token&__paging_token=sample_paging_token2',
body1 = '{"data": [{"created_time": "2016", "id": "1", ' +
'"message": "Test 1", "link": "https://test.com"}, {"created_time": ' +
'"2016", "id": "2", "message": "Test 2", "link": ' +
'"https://jio.com"}, {"created_time": "2016", "id": "3", ' +
'"message": "Test 3", "link": "https://renderjs.com"}], "paging": ' +
'{"next": "' + url2 + '", "previous": null}}',
body2 = '{"data": [{"created_time": "2016", "id": "4", "message": "Test' +
' 4", "link": null}], "paging": {"next": "' + url3 + '", "previous": ' +
'null}}',
body3 = '{"data": [], "paging": {"next": null, "previous": null}}',
server = this.server,
return_object = {"data": {
"rows": [
{
"id": "1",
"value": {
"created_time": "2016",
"id": "1",
"link": "https://test.com",
"message": "Test 1"
}
},
{
"id": "2",
"value": {
"created_time": "2016",
"id": "2",
"link": "https://jio.com",
"message": "Test 2"
}
},
{
"id": "3",
"value": {
"created_time": "2016",
"id": "3",
"link": "https://renderjs.com",
"message": "Test 3"
}
},
{
"id": "4",
"value": {
"created_time": "2016",
"id": "4",
"link": null,
"message": "Test 4"
}
}
],
"total_rows": 4
}
};
this.server.respondWith("GET", url1, [200, {
"Content-Type": "text/xml"
}, body1
]);
this.server.respondWith("GET", url2, [200, {
"Content-Type": "text/xml"
}, body2
]);
this.server.respondWith("GET", url3, [200, {
"Content-Type": "text/xml"
}, body3
]);
stop();
expect(8);
this.jio.allDocs({select_list: ["created_time", "id", "message",
"link"]})
.then(function (result) {
equal(server.requests.length, 3);
equal(server.requests[0].url, url1);
equal(server.requests[1].url, url2);
equal(server.requests[2].url, url3);
equal(server.requests[0].responseText, body1);
equal(server.requests[1].responseText, body2);
equal(server.requests[2].responseText, body3);
deepEqual(result, return_object);
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
test("get all posts without parameter", function () {
var url1 = 'https://graph.facebook.com/v2.9/sample_user_id/feed?fields' +
'=&limit=500&since=&access_token=' +
'sample_token',
url2 = 'https://graph.facebook.com/v2.9/sample_user_id/feed?fields' +
'=&limit=500&since=&access_token=' +
'sample_token&__paging_token=sample_paging_token',
body1 = '{"data": [{"created_time": "2016", "id": "1", ' +
'"message": "Test 1"}, {"created_time": "2016", "id": "2", "message": ' +
'"Test 2"}, {"created_time": "2016", "id": "3", "message": "Test 3"}]' +
', "paging": {"next": "' + url2 + '", "previous": null}}',
body2 = '{"data": [], "paging": {"next": null, "previous": null}}',
server = this.server,
return_object = {"data": {
"rows": [
{
"id": "1",
"value": {}
},
{
"id": "2",
"value": {}
},
{
"id": "3",
"value": {}
}
],
"total_rows": 3
}
};
this.server.respondWith("GET", url1, [200, {
"Content-Type": "text/xml"
}, body1
]);
this.server.respondWith("GET", url2, [200, {
"Content-Type": "text/xml"
}, body2
]);
stop();
expect(10);
this.jio.allDocs()
.then(function (result) {
equal(server.requests.length, 2);
equal(server.requests[0].method, "GET");
equal(server.requests[1].method, "GET");
equal(server.requests[0].url, url1);
equal(server.requests[1].url, url2);
equal(server.requests[0].status, 200);
equal(server.requests[1].status, 200);
equal(server.requests[0].responseText, body1);
equal(server.requests[1].responseText, body2);
deepEqual(result, return_object);
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
test("get all posts with include_docs", function () {
var url1 = 'https://graph.facebook.com/v2.9/sample_user_id/feed?fields' +
'=id,message,created_time&limit=500&since=&access_token=' +
'sample_token',
url2 = 'https://graph.facebook.com/v2.9/sample_user_id/feed?fields=' +
'id,message,created_time&limit=500&since=&access_token=' +
'sample_token&__paging_token=sample_paging_token',
body1 = '{"data": [{"created_time": "2016", "id": "1", "message": "Test' +
' 1"}, {"created_time": "2016", "id": "2", "message": "Test 2"}, ' +
'{"created_time": "2016", "id": "3", "message": "Test 3"}], "paging": ' +
'{"next": "' + url2 + '", "previous": null}}',
body2 = '{"data": [], "paging": {"next": null, "previous": null}}',
server = this.server,
return_object = {"data": {
"rows": [
{
"id": "1",
"value": {
"created_time": "2016",
"id": "1",
"message": "Test 1"
}
},
{
"id": "2",
"value": {
"created_time": "2016",
"id": "2",
"message": "Test 2"
}
},
{
"id": "3",
"value": {
"created_time": "2016",
"id": "3",
"message": "Test 3"
}
}
],
"total_rows": 3
}
};
this.server.respondWith("GET", url1, [200, {
"Content-Type": "text/xml"
}, body1
]);
this.server.respondWith("GET", url2, [200, {
"Content-Type": "text/xml"
}, body2
]);
stop();
expect(10);
this.jio.allDocs({include_docs: true})
.then(function (result) {
equal(server.requests.length, 2);
equal(server.requests[0].method, "GET");
equal(server.requests[1].method, "GET");
equal(server.requests[0].url, url1);
equal(server.requests[1].url, url2);
equal(server.requests[0].status, 200);
equal(server.requests[1].status, 200);
equal(server.requests[0].responseText, body1);
equal(server.requests[1].responseText, body2);
deepEqual(result, return_object);
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
test("get all posts with include_docs and select_list", function () {
var url1 = 'https://graph.facebook.com/v2.9/sample_user_id/feed?fields=id' +
',message,created_time,story&limit=500&since=&acces' +
's_token=sample_token',
url2 = 'https://graph.facebook.com/v2.9/sample_user_id/feed?fields=' +
'id,message,story,created_time&limit=500&since=&acc' +
'ess_token=sample_token&__paging_token=sample_paging_token',
body1 = '{"data": [{"created_time": "2016", "id": "1", ' +
'"message": "Test 1", "story": "Test story"}, {"created_time": "2016"' +
', "id": "2", "message": "Test 2", "story": "Test story"}, ' +
'{"created_time": "2016", "id": "3", "message": "Test 3", ' +
'"story": "Test story"}], "paging": {"next": "' + url2 + '",' +
'"previous": null}}',
body2 = '{"data": [], "paging": {"next": null, "previous": null}}',
server = this.server,
return_object = {"data": {
"rows": [
{
"id": "1",
"value": {
"created_time": "2016",
"id": "1",
"message": "Test 1",
"story": "Test story"
}
},
{
"id": "2",
"value": {
"created_time": "2016",
"id": "2",
"message": "Test 2",
"story": "Test story"
}
},
{
"id": "3",
"value": {
"created_time": "2016",
"id": "3",
"message": "Test 3",
"story": "Test story"
}
}
],
"total_rows": 3
}
};
this.server.respondWith("GET", url1, [200, {
"Content-Type": "text/xml"
}, body1
]);
this.server.respondWith("GET", url2, [200, {
"Content-Type": "text/xml"
}, body2
]);
stop();
expect(10);
this.jio.allDocs({select_list: ['story'], include_docs: true})
.then(function (result) {
equal(server.requests.length, 2);
equal(server.requests[0].method, "GET");
equal(server.requests[1].method, "GET");
equal(server.requests[0].url, url1);
equal(server.requests[1].url, url2);
equal(server.requests[0].status, 200);
equal(server.requests[1].status, 200);
equal(server.requests[0].responseText, body1);
equal(server.requests[1].responseText, body2);
deepEqual(result, return_object);
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
test("get all posts with limit", function () {
var url1 = 'https://graph.facebook.com/v2.9/sample_user_id/feed?fields' +
'=&limit=5&since=&access_token=' +
'sample_token',
url2 = 'https://graph.facebook.com/v2.9/sample_user_id/feed?fields' +
'=&limit=5&since=&access_token=' +
'sample_token&__paging_token=sample_paging_token',
body1 = '{"data": [{"created_time": "2016", "id": "1", ' +
'"message": "Test 1"}, {"created_time": "2016", "id": "2", ' +
'"message": "Test 2"}, {"created_time": "2016", "id": "3", ' +
'"message": "Test 3"}, {"created_time": "2016", "id": "4", ' +
'"message": "Test 4"}, {"created_time": "2016", "id": "5", ' +
'"message": "Test 5"}, {"created_time": "2016", "id": "6", ' +
'"message": "Test 6"}, {"created_time": "2016", "id": "7", ' +
'"message": "Test 7"}], "paging": {"next":"' + url2 + '", ' +
'"previous": null}}',
body2 = '{"data": [], "paging": {"next": null, "previous": null}}',
server = this.server,
return_object = {"data": {
"rows": [
{
"id": "3",
"value": {}
},
{
"id": "4",
"value": {}
},
{
"id": "5",
"value": {}
}
],
"total_rows": 3
}
};
this.server.respondWith("GET", url1, [200, {
"Content-Type": "text/xml"
}, body1
]);
this.server.respondWith("GET", url2, [200, {
"Content-Type": "text/xml"
}, body2
]);
stop();
expect(10);
this.jio.allDocs({'limit': [2, 5]})
.then(function (result) {
equal(server.requests.length, 2);
equal(server.requests[0].method, "GET");
equal(server.requests[1].method, "GET");
equal(server.requests[0].url, url1);
equal(server.requests[1].url, url2);
equal(server.requests[0].status, 200);
equal(server.requests[1].status, 200);
equal(server.requests[0].responseText, body1);
equal(server.requests[1].responseText, body2);
deepEqual(result, return_object);
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
});
}(jIO, QUnit, sinon));
\ No newline at end of file
......@@ -1357,6 +1357,33 @@
});
});
test("retrieve empty blob", function () {
var context = this,
attachment = "attachment",
blob = new Blob();
stop();
expect(1);
deleteIndexedDB(context.jio)
.then(function () {
return context.jio.put("foo", {"title": "bar"});
})
.then(function () {
return context.jio.putAttachment("foo", attachment, blob);
})
.then(function () {
return context.jio.getAttachment("foo", attachment);
})
.then(function (result) {
deepEqual(result, blob, "check empty blob");
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
/////////////////////////////////////////////////////////////////
// indexeddbStorage.removeAttachment
/////////////////////////////////////////////////////////////////
......
......@@ -601,7 +601,8 @@
then(function (dl) {
deepEqual(dl, [
{'identifier': 'àéîöùç'},
{'identifier': 'âèî ôùc'}
{'identifier': 'âèî ôùc'},
{'identifier': 'ÀÉÎÖÙÇ'}
], 'It should be possible to query regardless of accents');
})
);
......
......@@ -440,7 +440,72 @@
});
});
// Asterisk wildcard is not supported yet.
// Test queries which have components with key and without it.
// Default operator used if not present is AND.
test('Mixed query', function () {
var doc_list = [
{"identifier": "a", "value": "test1", "time": "2016"},
{"identifier": "b", "value": "test2", "time": "2017"},
{"identifier": "c", "value": "test3", "time": "2017"}
];
stop();
expect(2);
jIO.QueryFactory.create('test2 time:%2017%').exec(doc_list).
then(function (doc_list) {
deepEqual(doc_list, [
{"identifier": "b", "value": "test2", "time": "2017"}
], 'Document with test2 in any column and 2017 in time is matched');
doc_list = [
{"identifier": "a", "value": "test post 1", "time": "2016"},
{"identifier": "b", "value": "test post 2", "time": "2017"},
{"identifier": "c", "value": "test post 3", "time": "2018"}
];
return jIO.QueryFactory.create('value:"%test post 2%" OR c OR ' +
'2016').exec(doc_list).
then(function (doc_list) {
deepEqual(doc_list, [
{"identifier": "a", "value": "test post 1", "time": "2016"},
{"identifier": "b", "value": "test post 2", "time": "2017"},
{"identifier": "c", "value": "test post 3", "time": "2018"}
], 'Documents with "test post 2" in value or "c" or "2016" ' +
'anywhere are matched');
}).always(start);
});
});
test('Case insensitive queries', function () {
var doc_list = [
{"identifier": "a", "value": "Test Post", "time": "2016"},
{"identifier": "b", "value": "test post", "time": "2017"},
{"identifier": "c", "value": "test3", "time": "2018"}
];
stop();
expect(2);
jIO.QueryFactory.create('test post').exec(doc_list).
then(function (doc_list) {
deepEqual(doc_list, [
{"identifier": "a", "value": "Test Post", "time": "2016"},
{"identifier": "b", "value": "test post", "time": "2017"}
], 'Documunts with the value irrespective of case are matched');
doc_list = [
{"identifier": "a", "value": "Test Post", "time": "2016"},
{"identifier": "b", "value": "test post", "time": "2017"},
{"identifier": "c", "value": "test3", "time": "2018"}
];
return jIO.QueryFactory.create('value:"test post"').exec(doc_list).
then(function (doc_list) {
deepEqual(doc_list, [
{"identifier": "b", "value": "test post", "time": "2017"}
], 'If value is in quotes, only match if exactly same');
}).always(start);
});
});
// Asterisk wildcard is not supported yet.
/* test('Full text query with asterisk', function () {
var doc_list = [
{"identifier": "abc"},
......
......@@ -52,6 +52,7 @@
<script src="jio.storage/zipstorage.tests.js"></script>
<script src="jio.storage/gdrivestorage.tests.js"></script>
<script src="jio.storage/websqlstorage.tests.js"></script>
<script src="jio.storage/fbstorage.tests.js"></script>
<!--script src="../lib/jquery/jquery.min.js"></script>
<script src="../src/jio.storage/xwikistorage.js"></script>
<script src="jio.storage/xwikistorage.tests.js"></script-->
......
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