To set up jIO, include the source file, dependencies and connectors for the storages you plan to use in your page header (dependencies vary depending on the types of storages being used):
```
<!-- jio core-->
<script src="jio.js"></script>
<!-- jio dependencies-->
<script src="md5.js"></script>
<!-- jio storage connectors -->
<script src="localstorage.js">
<script ...>
<!-- jio complex queries -->
<script src="jio.complex_queries.js"></script>
```
Then create your jIO instance like this:
```
var jio = jIO.newJio({
"type": "local",
"username": "your_username",
"application_name": "your_application_name"
});
```
Documents and Methods
-------------------------------------------------
-------------------------------------------------
Documents are JSON strings that contain <em><u>meta-data</u></em>
(properties, e.g. filename) and <em><u>attachments</u></em> (optional content, e.g. a base64-encoded image).
jIO exposes the following methods to <em><u>create</u></em>,
<em><u>read</u></em>, <em><u>update</u></em> and <em><u>delete</u></em> documents:
```
```
// get 'document.txt'
// create and store new document
jio.get('document.txt',function (err, val) {
jio.post({"title":"some title"}, function (err, response) {
console.log (err || val.content);
// console.log(response):
// {"ok":"true", "id":"cabc9...826" }
});
});
// update 'document.txt'
// create or update an existing document
jio.put({_id:'document.txt',content:'newcontent'}, function (err, val) {
jio.put({"_id":"demoID", "title":"new title"}, function (err, response) {
console.log (err || val);
// console.log(response):
// {"ok":"true", "id":"demoID" }
});
});
// get a list of documents
// add an attachement to a document (Note: no underscore on id!)
jio.remove ({_id:'document.txt'}, function (err, val) {
jio.get("demoID/myImg", function (err, response) {
console.log (err || val);
// console.log(response):
// {"data":"abc"}
});
});
```
Documentation Index
===================
+[What is jIO?](#what-is-jio)
+[How does it work?](#how-does-it-work)
+[Getting started](#getting-started)
+[Where can I store documents?](#where-can-i-store-documents)
[**For developers**](#for-developers)
+[Quick start](#quick-start)
+[How to design your own jIO Storage Library](#how-to-design-your-own-jio-storage-library)
+[Error status](#error-status)
+[Job rules](#job-rules)
+[Code sample](#code-sample)
[**Authors**](#authors)
[**Copyright and license**](#copyright-and-license)
What is jIO?
// delete a document or an attachment
------------
jio.remove({"_id":"demoID"}, function (err, response) {
// console.log(response):
jIO is a JavaScript Library that stores/manipulates documents on storage servers over the internet in an asynchronous fashion.
// {"ok":"true", "id":"demoID"}
});
How does it work?
-----------------
jIO is separated in 2 parts, core library and storage library(ies). The core must use some javascript objects (storages) to interact with the associated remote storage servers. jIO uses job management, so every request adds jobs in a queue which is saved on browser local storage in order to be restored later if the browser crashed or else. jIO will invoke all these jobs at the same time. Of course, adding an already ongoing job won't work, so there will be no conflicts.
Getting started
---------------
This short tutorial is designed to help you get started using jIO. First, download the jIO core and the jIO storages scripts (git clone http://git.erp5.org/repos/jio.git) and their dependencies ([jQuery](http://jquery.com), [base64](http://www.webtoolkit.info/javascript-base64.html), [sjcl](http://crypto.stanford.edu/sjcl/), [sha2](http://anmar.eu.org/projects/jssha2/)). Then, add the scripts in your HTML page as following:
// get all documents
jio.allDocs(function(err, response){
// console.log(response):
// {
// "total-rows":1,
// "rows":[{
// "_id":"demoID",
// "title";"new text",
// "_attachments":{
// "myImg": {
// "digest":"md5-nN...",
// "data":"abc",
// "length":6
// }
// }
// }]
// }
});
```
For more information on the methods and additional options (e.g. revision management, please refer to the documentation).
Example
-------------------------------------------------
This is an example of how to store a video file with an attachment in
local storage. Note that attachments should best be added inside one of the available document callback methods (success & error or callback).
```
```
<!-- jIO Core -->
// create a new localStorage
<script src="localorcookiestorage.js"></script>
var jio = JIO.newJio({
<script src="jio.js"></script>
"type":"local",
<!-- Some storage dependencies -->
"username":"user",
<script src="jquery.js"></script>
"application_name":"app"
<script src="base64.js"></script>
});
<script src="sjcl.js"></script>
// post the document
<script src="sha2.js"></script>
jio.post(
<!-- Some storage -->
{
<script src="jio.storage.js"></script>
"_id" : "myVideo",
"title" : "My Video",
"videoCodec" : "vorbis",
"language" : "en",
"description" : "Images Compilation"
},
function (err, response) {
if (err) {
alert('Error when posting the document description');
} else {
// if successful, add video attachment (base64 encoded)
jio.putAttachment(
{
"id": "myVideo/video",
"data": Base64(my_video),
"mimetype":"video/ogg"
},
function (err, response) {
if (err) {
alert('Error when attaching the video');
} else {
alert('Video Stored');
}
}
);
}
}
);
```
```
Storage Locations
-------------------------------------------------
jIO allows to build "storage trees" consisting of connectors to multiple storages (webDav, xWiki, S3, localStorage) and use storage handlers to add features like revision management or indices to a child storage (sub_storage).
+ jquery.js - see http://jquery.com
The following storages are currently supported:
+ localorcookiestorage.js - is a small library that stores persistent data on local storage even if the browser does not support HTML 5.
+ base64.js - is a small library to encrypt data into base64.
+ sjcl.js - is a powerful library to encrypt/decrypt data.
+ sha2.js - is a small library to hash data.
+ jio.js - is the jIO core.
+ jio.storage.js - is a jIO Storage library that can interact with some remote storage servers.
The jIO API provides 5 main methods:
+`post` - Creates a new file in the storage
+`get` - Reads a file from the storage.
+`put` - Updates a file in the storage.
+`remove` - Deletes a file from the storage.
+`allDocs` - Gets a list of existant files.
**DummyStorage (custom storage prototype)**
```
```
var my_jio_instance = jIO.newJio(storagedescription);
+**Tests** - Go to `${repo}/tests/`, and you can open `jiotests_withoutrequirejs.html` from localhost. (Tests with requireJS are not available yet.)
How to design your own jIO Storage Library
-----------------------------------------
jIO basicStorage interface must be inherited by all the new storages. Seven methods must be redesigned: `post, get, put, remove, allDocs, serialized, validateState`
Except 'serialized' and 'validateState', the above methods must end with 'success','retry' or 'error' inherited methods, which can have only one parameter.
This parameter is the job return value, it is very important.
The return value must seams like PouchDB return values.
The method 'serialized' is used by jIO to store a serialized version of the storage inside the localStorage in order to be restored by jIO later.
```
```
var super_serialized = that.serialized;
// initialize a connection to S3 storage
serialized = function () {
coming soon
var o = super_serialized();
o.important_info1 = '...';
o.important_info2 = '...';
return o;
};
```
```
When jIO try to restore this storage, 'important_info1' and 2 will be given in the storage spec.
**IndexStorage (maintains indices of documents in a substorage)**
**CAUTION**: Don't store confidential informations like passwords!
The method 'validateState' is used by jIO to validate the storage state. For example, if the storage specifications are not correct, this method must return a non-empty string. If all is ok, it must return an empty string.
```
```
validate = function () {
// initialize an indexStorage (for a local storage)
if (spec.important_info1) {
var jio = JIO.newJio({
return '';
"type": "indexed",
}
"sub_storage": {
return 'Where is my important information ??';
"type": "local",
};
"username": <string>,
"application_name": <string>
},
// create two indices for the substorage with fields A and A,B
"indices": [
{"name":<string>, "fields":[<string A>]},
{"name":<string>, "fields":[<string A>, <string B>]}
The storage created, you must add the storage type to jIO.
jIO.addStorageType() require two parameters: the storage type (string) add a constructor (function). `jIO.addStorageType('mystoragetype', myConstructor);`
To see what this can look like, see [Code sample](#code-sample).
Error status
------------
* 0: Unknown Error
* >9 & <20: Job Errors
* 10: Stopped, The Job has been stopped by adding another one
* 11: Not Accepted, The added Job cannot be accepted
* 12: Replaced, The Job has been replaced by another one
* >19 & <30: Command Errors
* 20: Id Required, The Command needs a document id
* 21: Content Required, The Command needs a document content
* >29: Storage Errors
* >99: HTTP Errors
Job rules
---------
jIO job manager will follow several rules set at the creation of a new jIO instance. When you try to do a command, jIO will create a job and will make sure the job is really necessary. Thanks to job rules, jIO knows what to do with the new job before adding it to the queue.
You can add your own rules like this:
```
```
var jio = jIO.newJio(<storagedescription>);
// initialize a cryptStorage (to encrypt data on a storage)
var jio_rules = jio.getJobRules();
coming soon
// When a 'put' job is on going (true), and we add a 'get' job,
// then the 'get' job must wait for the end of the 'put' job.
jio_rules.addActionRule('put', true /* on going */, 'get', jio_rules.wait);
```
```
**Revision Storage (add revision management to a substorage)**
+`wait` - wait until the end of the current job
+`update` - replace the current job by this one
+`eliminate` - eliminate the current job, and add the new one
+`dontAccept` - the new job cannot be accepted
+`none` - simply add the new job to the job queue
You can make special rules like this:
```
```
var putput = function(job1,job2){
// initialize a revison storage on a local storage
For more information on the specific storages including guidelines on how to create your own connector, please refer to the documentation.
Code sample
Complex Queries
-----------
-------------------------------------------------
jIO includes a complex-queries manager, which can be run on top of the
<em>allDocs()</em> method to query documents in the storage tree. A
sample query would look like this (note, that <em>allDocs</em> and complex queries cannot be run on every storage and that pre-querying of documents on distant storages should best be done server-side):
Storage example:
```
```
(function () {
// run allDocs with query option on an existing jIO
var newLittleStorage = function ( spec, my ) {
jio.allDocs({
var that = my.basicStorage ( spec, my );
"query":{
"query":'(fieldX: >= <string> AND fieldY: < <string>)',
var super_serialized = that.serialized;
"filter": {
that.serialized = function () {
// records to display ("from to")
var o = super_serialized();
"limit":[0,5],
o.firstname = spec.firstname;
// sort by
o.lastname = spec.lastname;
"sort_on":[[<string A>,'descending']],
return o;
// fields to return in response
};
"select_list":[<string A>;,<string B>]
},
that.validateState = function () {
"wildcard_character":'%'
if (spec.firstname && spec.lastname) {
}
return '';
},function(err, response){
}
// console.log(response):
return 'This storage needs your firstname and your lastname.';
// [{
};
// "id": <string>,
// <string A>: <string>,
// <string B>: <string>
// }]
}
});
```
Task Management
-------------------------------------------------
jIO is running a task queue manager in the background which processes
incoming tasks according to set of defined rules. To find out
more and including how to define your own execution rules, please refer to the documentation.
that.post = function (command) {
Conflict Management
// [code]
-------------------------------------------------
that.success({ok:true,id:command.getDocId()});
As jIO allows to manage and share documents across multiple storage
};
locactions it is likely for conflicts to occur (= multiple versions of a single document existing in the storage tree). jIO manages conflicts by ensuring that every version of a document is available on every storage and that conflicts are accessible (and solvable) using the <em><u>conflicts:true</u></em> option when using the respective jIO methods. For more info on conflicts and available options, please refer to the documentation.
that.put = function (command) {
Crash-Proof
// [code]
-------------------------------------------------
that.success({ok:true,id:command.getDocId()});
All tasks are managed inside the browser local storage so no data is lost, as the task manager queue will persist through browser crashes and continues to run when the page is reloaded after a browser crash.
};
that.get = function (command) {
Authors
// example with jQuery
-------------------------------------------------
jQuery.ajax ( {
+ Francois Billioud
url: 'www.google.com',
+ Tristan Cavelier
type: "GET",
async: true,
success: function (content) {
that.success({
_id:command.getDocId(),content:content,
_last_modified:123,_creation_date:12
});
},
error: function (type) {
type.reason = 'error occured';
type.message = type.reason + '.';
type.error = type.statusText;
that.error(type);
}
} );
};
that.allDocs = function (command) {
Copyright and license
// [code]
-------------------------------------------------
if (!can_I_reach_the_internet) {
jIO is an open-source library and is licensed under the LGPL license. More information on LGPL can be found <ahref="http://en.wikipedia.org/wiki/GNU_Lesser_General_Public_License">here</a>.
// Oh, I can't reach the internet...
that.retry({
status:0,error:'unknown_error',
statusText:'Unknown Error',
reason:'network_not_reachable',
message:'Impossible to reach the internet.'
});
} else {
// Oh, I can't retreive any files..
that.error({
status:403,error:'forbidden',
statusText:'Forbidden',
reason:'unable to get all docs',
message:'This storage is not able to retreive anything.'
});
}
};
that.remove = function (command) {
Documentation
// [code]
=============
that.success({ok:true,id:command.getDocId()});
You can find the full documentation here: [jIO Documentation](http://www.j-io.org/documentation).
};
return that;
For developers
};
==============
Quick start
-------------------------------------------------
To get started with jIO, clone one of the repositories on
jIO uses a <ahref="http://git.erp5.org/gitweb/jio.git/blob_plain/HEAD:/Makefile?js=1">makeFile</a> to build and compress the necessary files (jIO.js and complex-queries.js). To run the makeFile you will require the following:
The repository also includes the built ready-to-use files, so in case you do not want to build jIO yourself, just use <em>jio.min.js</em> as well as <em>complex-queries.min.js</em> plus the storages and dependencies you need and you will be good to go.
+ Tristan Cavelier
Copyright and license
You can also check out the jIO QUnit tests (jiotests_withoutrequirejs.html) for an overview on how to create a page
=====================
[setup](http://git.erp5.org/gitweb/jio.git/blob/HEAD:/test/jiotests_withoutrequirejs.html?js=1) with required scripts as well as setting up jIO itself
[setup](http://git.erp5.org/gitweb/jio.git/blob/HEAD:/test/jiotests.js?js=1). If you want to run the QUnit tests
locally after cloning the repo, please make sure you have the above minified files in your repository.