UNPKG

forerunnerdb

Version:

A NoSQL document store database for browsers and Node.js.

168 lines (131 loc) 5.63 kB
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>JSDoc: Source: Rest.js</title> <script src="scripts/prettify/prettify.js"> </script> <script src="scripts/prettify/lang-css.js"> </script> <!--[if lt IE 9]> <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script> <![endif]--> <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css"> <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css"> </head> <body> <div id="main"> <h1 class="page-title">Source: Rest.js</h1> <section> <article> <pre class="prettyprint source linenums"><code>"use strict"; var Shared = require('./Shared'), RestClient = require('rest'), mime = require('rest/interceptor/mime'), Db, Collection, CollectionDrop, CollectionGroup, CollectionInit, DbInit, Overload; var Rest = function () { this.init.apply(this, arguments); }; Rest.prototype.init = function (db) { this._endPoint = ''; this._client = RestClient.wrap(mime); }; /** * Convert a JSON object to url query parameter format. * @param {Object} obj The object to convert. * @returns {String} * @private */ Rest.prototype._params = function (obj) { var parts = []; for (var key in obj) { if (obj.hasOwnProperty(key)) { parts.push(encodeURIComponent(key) + '=' + encodeURIComponent(obj[key])); } } return parts.join('&amp;'); }; Rest.prototype.get = function (path, data, callback) { var self= this, coll; path = path !== undefined ? path : ""; //console.log('Getting: ', this.endPoint() + path + '?' + this._params(data)); this._client({ method: 'get', path: this.endPoint() + path, params: data }).then(function (response) { if (response.entity &amp;&amp; response.entity.error) { if (callback) { callback(response.entity.error, response.entity, response); } } else { // Check if we have a collection coll = self.collection(); if (coll) { // Upsert the records into the collection coll.upsert(response.entity); } if (callback) { callback(false, response.entity, response); } } }, function(response) { if (callback) { callback(true, response.entity, response); } }); }; Rest.prototype.post = function (path, data, callback) { this._client({ method: 'post', path: this.endPoint() + path, entity: data, headers: { 'Content-Type': 'application/json' } }).then(function (response) { if (response.entity &amp;&amp; response.entity.error) { if (callback) { callback(response.entity.error, response.entity, response); } } else { if (callback) { callback(false, response.entity, response); } } }, function(response) { if (callback) { callback(true, response); } }); }; Shared.synthesize(Rest.prototype, 'sessionData'); Shared.synthesize(Rest.prototype, 'endPoint'); Shared.synthesize(Rest.prototype, 'collection'); Shared.addModule('Rest', Rest); Shared.mixin(Rest.prototype, 'Mixin.ChainReactor'); Db = Shared.modules.Db; Collection = require('./Collection'); CollectionDrop = Collection.prototype.drop; CollectionGroup = require('./CollectionGroup'); CollectionInit = Collection.prototype.init; DbInit = Db.prototype.init; Overload = Shared.overload; Collection.prototype.init = function () { this.rest = new Rest(); this.rest.collection(this); CollectionInit.apply(this, arguments); }; Db.prototype.init = function () { this.rest = new Rest(); DbInit.apply(this, arguments); }; Shared.finishModule('Rest'); module.exports = Rest;</code></pre> </article> </section> </div> <nav> <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="ActiveBucket.html">ActiveBucket</a></li><li><a href="Angular.html">Angular</a></li><li><a href="AutoBind.html">AutoBind</a></li><li><a href="BinaryTree.html">BinaryTree</a></li><li><a href="Collection.html">Collection</a></li><li><a href="CollectionGroup.html">CollectionGroup</a></li><li><a href="Core.html">Core</a></li><li><a href="Db.html">Db</a></li><li><a href="Document.html">Document</a></li><li><a href="Grid.html">Grid</a></li><li><a href="Highchart.html">Highchart</a></li><li><a href="IndexBinaryTree.html">IndexBinaryTree</a></li><li><a href="IndexHashMap.html">IndexHashMap</a></li><li><a href="Infinilist.html">Infinilist</a></li><li><a href="KeyValueStore.html">KeyValueStore</a></li><li><a href="Metrics.html">Metrics</a></li><li><a href="OldView.html">OldView</a></li><li><a href="Operation.html">Operation</a></li><li><a href="Overload.html">Overload</a></li><li><a href="Path.html">Path</a></li><li><a href="Persist.html">Persist</a></li><li><a href="ReactorIO.html">ReactorIO</a></li><li><a href="Serialiser.html">Serialiser</a></li><li><a href="Shared.overload.html">overload</a></li><li><a href="View.html">View</a></li></ul><h3>Mixins</h3><ul><li><a href="ChainReactor.html">ChainReactor</a></li><li><a href="crcTable.html">crcTable</a></li><li><a href="Shared.html">Shared</a></li></ul><h3>Global</h3><ul><li><a href="global.html#%2522boolean,function%2522">"boolean, function"</a></li><li><a href="global.html#%2522string,*,function%2522">"string, *, function"</a></li><li><a href="global.html#%2522string,function%2522">"string, function"</a></li><li><a href="global.html#boolean">boolean</a></li><li><a href="global.html#function">function</a></li></ul> </nav> <br class="clear"> <footer> Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.3.3</a> on Thu Nov 19 2015 13:31:32 GMT+0000 (GMT) </footer> <script> prettyPrint(); </script> <script src="scripts/linenumber.js"> </script> </body> </html>