developers.rst 8.68 KB
Newer Older
1 2 3 4 5 6
For developers
==============

Quick start
-----------

Marco Mariani's avatar
Marco Mariani committed
7
To get started with jIO, clone one of the repositories linked in :ref:`Download & Fork <download-fork>`.
8

Marco Mariani's avatar
Marco Mariani committed
9
To build the library you have to:
10 11

* Install `NodeJS <http://nodejs.org/>`_ (including NPM)
Marco Mariani's avatar
Marco Mariani committed
12 13 14 15 16 17 18 19 20 21
* Install Grunt command line with npm.

  ``# npm -g install grunt-cli``

* Install dev dependencies.
  
  ``$ npm install``

* Compile JS/CC parser.
  
Marco Mariani's avatar
Marco Mariani committed
22
  ``$ make`` (until we find how to compile it with grunt)
Marco Mariani's avatar
Marco Mariani committed
23

Marco Mariani's avatar
Marco Mariani committed
24
* Run build.
Marco Mariani's avatar
Marco Mariani committed
25 26
  
  ``$ grunt``
27 28 29 30 31 32 33 34

The repository also includes the built ready-to-use files, so in case you do
not want to build jIO yourself, just use *jio.js* as well as *complex_queries.js*
plus the storages and dependencies you need and you will be good to go.

Naming Conventions
------------------

Marco Mariani's avatar
Marco Mariani committed
35
All the code follows these :ref:`JavaScript Naming Conventions <naming-conventions>`.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76

How to design your own jIO Storage Library
------------------------------------------

Create a constructor:

.. code-block:: javascript

    function MyStorage(storage_description) {
      this._value = storage_description.value;
      if (typeof this._value !== 'string') {
        throw new TypeError("'value' description property is not a string");
      }
    }

Create 10 methods: ``post``, ``put``, ``putAttachment``, ``get``, ``getAttachment``,
``remove``, ``removeAttachment``, ``allDocs``, ``check`` and ``repair``.

.. code-block:: javascript

    MyStorage.prototype.post = function (command, metadata, option) {
      var document_id = metadata._id;
      // [...]
    };

    MyStorage.prototype.get = function (command, param, option) {
      var document_id = param._id;
      // [...]
    };

    MyStorage.prototype.putAttachment = function (command, param, option) {
      var document_id = param._id;
      var attachment_id = param._attachment;
      var attachment_data = param._blob;
      // [...]
    };

    // [...]



Marco Mariani's avatar
Marco Mariani committed
77
(To help you design your methods, some tools are provided by jIO.util.)
78

79
The first parameter command provides some methods to act on the jIO job:
80

81
* ``success``, to tell jIO that the job is successfully terminated
82 83 84 85 86

  ``command.success(status[Text], [{custom key to add to the response}]);``

* ``resolve``, is equal to success

87
* ``error``, to tell jIO that the job cannot be done
88 89 90

  ``command.error(status[Text], [reason], [message], [{custom key to add to the response}])``

91
* ``retry``, to tell jIO that the job cannot be done now, but can be retried later. (same API than error)
92

93
* ``reject``, to tell jIO that the job cannot be done, let jIO to decide whether to retry or not. (same API than error)
94 95


96
The second parameter ``metadata`` or ``param`` is the first parameter provided by the jIO user.
97

98
The third parameter ``option`` is the option parameter provided by the jIO user.
99

Marco Mariani's avatar
Marco Mariani committed
100
Methods should return the following objects:
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125

* post --> success("created", {"id": new_generated_id})

* put, remove, putAttachment or removeAttachment --> success(204)

* get --> success("ok", {"data": document_metadata})

* getAttachment -->

  success("ok", {"data": binary_string, "content_type": content_type})
  // or
  success("ok", {"data": new Blob([data], {"type": content_type})})

* allDocs --> success("ok", {"data": row_object})

* check -->

  .. code-block:: javascript

    // if metadata provides "_id" -> check document state
    // if metadata doesn't promides "_id" -> check storage state
    success("no_content")
    // or
    error("conflict", "corrupted", "incoherent document or storage")

Marco Mariani's avatar
Marco Mariani committed
126
* repair -->
127 128 129 130 131 132 133 134 135 136

  .. code-block:: javascript

    // if metadata provides "_id" -> repair document state
    // if metadata doesn't promides "_id" -> repair storage state
    success("no_content")
    // or
    error("conflict", "corrupted", "impossible to repair document or storage")
    // DON'T DESIGN STORAGES IF THEIR IS NO WAY TO REPAIR INCOHERENT STATES

Marco Mariani's avatar
Marco Mariani committed
137 138 139
After creating all methods, your storage must be added to jIO. This is done
with the ``jIO.addStorage()`` method, which requires two parameters: the storage
type (string) and a constructor (function). It is called like this:
140 141 142 143 144 145 146 147

.. code-block:: javascript

    // add custom storage to jIO
    jIO.addStorage('mystoragetype', MyStorage);


Please refer to *localstorage.js* implementation for a good example on how to
Marco Mariani's avatar
Marco Mariani committed
148 149 150 151
setup a storage and what methods are required.

Also keep in mind that jIO is a job-based library: whenever you trigger a method,
a job is created, which will later return a response, after being processed.
152 153 154 155

Job rules
---------

Marco Mariani's avatar
Marco Mariani committed
156
The jIO job manager follows several rules set at the creation of a new jIO
157 158 159
instance. When you try to call a method, jIO will create a job and will make
sure the job is really necessary and will be executed. Thanks to these job
rules, jIO knows what to do with the new job before adding it to the queue. You
Marco Mariani's avatar
Marco Mariani committed
160
can also add your own rules, as we're going to see now.
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215

These are the jIO **default rules**:

.. code-block:: javascript

    var jio_instance = jIO.createJIO(storage_description, {
      "job_rules": [{
        "code_name": "readers update",
        "conditions": [
          "sameStorageDescription",
          "areReaders",
          "sameMethod",
          "sameParameters",
          "sameOptions"
        ],
        "action": "update"
      }, {
        "code_name": "metadata writers update",
        "conditions": [
          "sameStorageDescription",
          "areWriters",
          "useMetadataOnly",
          "sameMethod",
          "haveDocumentIds",
          "sameParameters"
        ],
        "action": "update"
      }, {
        "code_name": "writers wait",
        "conditions": [
          "sameStorageDescription",
          "areWriters",
          "haveDocumentIds",
          "sameDocumentId"
        ],
        "action": "wait"
      }]
    });


The following actions can be used:

* ``ok`` - accept the job
* ``wait`` - wait until the end of the selected job
* ``update`` - bind the selected job to this one
* ``deny`` - reject the job

The following condition function can be used:

* ``sameStorageDescription`` - check if the storage descriptions are different.
* ``areWriters`` - check if the commands are ``post``, ``put``, ``putAttachment``, ``remove``, ``removeAttachment``, or ``repair``.
* ``areReaders`` - check if the commands are ``get``, ``getAttachment``, ``allDocs`` or ``check``.
* ``useMetadataOnly`` - check if the commands are ``post``, ``put``, ``get``, ``remove`` or ``allDocs``.
* ``sameMethod`` - check if the commands are equal.
* ``sameDocumentId`` - check if the document ids are equal.
Marco Mariani's avatar
Marco Mariani committed
216
* ``sameParameters`` - check if the metadata or param are equal (deep comparison).
217
* ``sameOptions`` - check if the command options are equal.
Marco Mariani's avatar
Marco Mariani committed
218
* ``haveDocumentIds`` - test if the two commands contain document ids.
219 220 221 222

Create Job Condition
--------------------

Marco Mariani's avatar
Marco Mariani committed
223
You can create two types of function: job condition, and job comparison.
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270

.. code-block:: javascript

    // Job Condition
    // Check if the job is a get command
    jIO.addJobRuleCondition("isGetMethod", function (job) {
      return job.method === 'get';
    });

    // Job Comparison
    // Check if the jobs have the same 'title' property only if they are strings
    jIO.addJobRuleCondition("sameTitleIfString", function (job, selected_job) {
      if (typeof job.kwargs.title === 'string' &&
          typeof selected_job.kwargs.title === 'string') {
        return job.kwargs.title === selected_job.kwargs.title;
      }
      return false;
    });


Add job rules
-------------

You just have to define job rules in the jIO options:

.. code-block:: javascript

    // Do not accept to post or put a document which title is equal to another
    // already running post or put document title
    var jio_instance = jIO.createJIO(storage_description, {
      "job_rules": [{
        "code_name": "avoid similar title",
        "conditions": [
          "sameStorageDescription",
          "areWriters",
          "sameTitleIfString"
        ],
        "action": "deny",
        "before": "writers update" // optional
        // "after": also exists
      }]
    });


Clear/Replace default job rules
-------------------------------

Marco Mariani's avatar
Marco Mariani committed
271
If a job's ``code_name`` is equal to ``readers update``, then adding this rule will replace it:
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289

.. code-block:: javascript

    var jio_instance = jIO.createJIO(storage_description, {
      "job_rules": [{
        "code_name": "readers update",
        "conditions": [
          "sameStorageDescription",
          "areReaders",
          "sameMethod",
          "haveDocumentIds"
          "sameParameters"
          // sameOptions is removed
        ],
        "action": "update"
      }]
    });

Marco Mariani's avatar
Marco Mariani committed
290
Or you can just clear all rules before adding new ones:
291 292 293 294 295 296 297 298 299 300

.. code-block:: javascript

    var jio_instance = jIO.createJIO(storage_description, {
      "clear_job_rules": true,
      "job_rules": [{
        // ...
      }]
    });