periodicjs.core.data
Version:
Core data is the ORM wrapping component of periodicjs.core.controller that provides database adapters for commonly used databases (ie. mongo, sql, postgres). Adapters provide a standard set of methods and options regardless of the type of database and so
132 lines (109 loc) • 5.95 kB
HTML
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Source: utility/cursor.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: utility/cursor.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>'use strict';
const TransformStream = require('stream').Transform;
const Promisie = require('promisie');
/**
* A default on success function for each iteration of cursor
* @param {*} data Generally a single document be pushed by stream but can be any data type or a Buffer if cursor is not instantiated in objectMode
* @return {*} Simply returns whatever data is passed as data arugment
*/
var defaultSuccess = function (data) {
return data;
};
/**
* A default on error function for each iteration of cursor
* @param {Object} e An instance of Error
* @return {Object} Returns a rejected Promise
*/
var defaultError = function (e) {
return Promisie.reject(e);
};
/**
* A simple cursor-like implementation that combines generator and stream functionality. Cursor exposes all the normal stream methods such as pipe, unpipe, on, once etc as well as an initialize method that returns a generator and allows for documents to be iteratively resolved. Using both the generator based cursor interface and stream interface concurrently is not recommended as both utilize the TransformStream on data, on error and on finish events and as such will likely cause unpredictable behavior
* @type {Cursor}
* @extends {TranformStream}
*/
const CURSOR = class Cursor extends TransformStream {
/**
* Constructor for Cursor class
* @param {Object} [options={}] Configurable options for cursor (see TransformStream documentation for more details)
* @param {Boolean} options.objectMode Unlike a normal transform stream the cursor class is meant for documents and as such objectMode will always be true unless explicitly declared as false at initialization
*/
constructor (options = {}) {
options.objectMode = (options.objectMode === false) ? false : true;
super(options);
this._transform = function (data, enc, next) {
this.push(data);
next();
};
this._isInitialized = false;
this._isDone = false;
}
/**
* Internally used _next method (this should never be accessed directly). Resumes the transform stream and resolves the next document before pausing the stream
* @return {Object} Returns a Promise which resolves with the next document written to the stream
*/
_next () {
this.resume();
return new Promisie((resolve, reject) => {
this.once('data', data => {
this.pause();
resolve(data);
})
.once('error', reject);
});
}
/**
* Initializes the cursor interface. Once initialized the generator will yield a single document with each next call. The behavior can be modified by passing an onSuccess function as the first argument with each next call. Once cursor is initialized the streams finish, data and error events will have registered listeners
* @param {Function} [onSuccess=defaultSuccess] A default function to be called on each successful .next call. This function can be temporarily overridden by passing a function in the .next call
* @param {Function} [onError=defaultError] A default function to be called when a .next call results in an rejection
* @return {Function} Returns a generator that iteratively resolves the documents written to the stream
*/
initialize (onSuccess = defaultSuccess, onError = defaultError) {
this._isInitialized = true;
this.on('finish', () => {
this._isDone = true;
});
return function* () {
let value;
while (!this._isDone) {
value = yield this._next()
.try((typeof value === 'function') ? value : onSuccess)
.catch(onError);
}
}.bind(this);
}
};
module.exports = CURSOR;
</code></pre>
</article>
</section>
</div>
<nav>
<h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="CURSOR.html">CURSOR</a></li><li><a href="DB_ADAPTER_INTERFACE.html">DB_ADAPTER_INTERFACE</a></li><li><a href="LOKI_ADAPTER.html">LOKI_ADAPTER</a></li><li><a href="MONGO_ADAPTER.html">MONGO_ADAPTER</a></li><li><a href="SQL_ADAPTER.html">SQL_ADAPTER</a></li></ul><h3>Global</h3><ul><li><a href="global.html#_CREATE">_CREATE</a></li><li><a href="global.html#_DELETE">_DELETE</a></li><li><a href="global.html#_DELETED">_DELETED</a></li><li><a href="global.html#_LOAD">_LOAD</a></li><li><a href="global.html#_QUERY">_QUERY</a></li><li><a href="global.html#_QUERY_WITH_PAGINATION">_QUERY_WITH_PAGINATION</a></li><li><a href="global.html#_RAW">_RAW</a></li><li><a href="global.html#_SEARCH">_SEARCH</a></li><li><a href="global.html#_STREAM">_STREAM</a></li><li><a href="global.html#_UPDATE">_UPDATE</a></li><li><a href="global.html#_UPDATE_ALL">_UPDATE_ALL</a></li><li><a href="global.html#_UPDATED">_UPDATED</a></li><li><a href="global.html#defaultError">defaultError</a></li><li><a href="global.html#defaultSuccess">defaultSuccess</a></li><li><a href="global.html#EXAMPLE">EXAMPLE</a></li><li><a href="global.html#GENERATE_PATCH">GENERATE_PATCH</a></li><li><a href="global.html#GENERATE_PUT">GENERATE_PUT</a></li><li><a href="global.html#GENERATE_SELECT">GENERATE_SELECT</a></li></ul>
</nav>
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.3</a> on Fri Apr 07 2017 09:04:28 GMT-0400 (EDT)
</footer>
<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>