forerunnerdb
Version:
A NoSQL document store database for browsers and Node.js.
219 lines (163 loc) • 6.24 kB
HTML
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Source: Odm.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: Odm.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>"use strict";
// Import external names locally
var Shared,
Collection;
Shared = require('./Shared');
var Odm = function () {
this.init.apply(this, arguments);
};
Odm.prototype.init = function (from, name) {
var self = this;
self.name(name);
self._collectionDroppedWrap = function () {
self._collectionDropped.apply(self, arguments);
};
self.from(from);
};
Shared.addModule('Odm', Odm);
Shared.mixin(Odm.prototype, 'Mixin.Common');
Shared.mixin(Odm.prototype, 'Mixin.ChainReactor');
Shared.mixin(Odm.prototype, 'Mixin.Constants');
Shared.mixin(Odm.prototype, 'Mixin.Events');
Collection = require('./Collection');
Shared.synthesize(Odm.prototype, 'name');
Shared.synthesize(Odm.prototype, 'state');
Shared.synthesize(Odm.prototype, 'parent');
Shared.synthesize(Odm.prototype, 'query');
Shared.synthesize(Odm.prototype, 'from', function (val) {
if (val !== undefined) {
val.chain(this);
val.on('drop', this._collectionDroppedWrap);
}
return this.$super(val);
});
Odm.prototype._collectionDropped = function (collection) {
this.drop();
};
Odm.prototype._chainHandler = function (chainPacket) {
switch (chainPacket.type) {
case 'setData':
case 'insert':
case 'update':
case 'remove':
//this._refresh();
break;
default:
break;
}
};
Odm.prototype.drop = function () {
if (!this.isDropped()) {
this.state('dropped');
this.emit('drop', this);
if (this._from) {
delete this._from._odm;
}
delete this._name;
}
return true;
};
/**
* Queries the current object and returns a result that can
* also be queried in the same way.
* @param {String} prop The property to delve into.
* @param {Object=} query Optional query that limits the returned documents.
* @returns {Odm}
*/
Odm.prototype.$ = function (prop, query) {
var data,
tmpQuery,
tmpColl,
tmpOdm;
if (prop === this._from.primaryKey()) {
// Query is against a specific PK id
tmpQuery = {};
tmpQuery[prop] = query;
data = this._from.find(tmpQuery, {$decouple: false});
tmpColl = new Collection();
tmpColl.setData(data, {$decouple: false});
tmpColl._linked = this._from._linked;
} else {
// Query is against an array of sub-documents
tmpColl = new Collection();
data = this._from.find({}, {$decouple: false});
if (data[0] && data[0][prop]) {
// Set the temp collection data to the array property
tmpColl.setData(data[0][prop], {$decouple: false});
// Check if we need to filter this array further
if (query) {
data = tmpColl.find(query, {$decouple: false});
tmpColl.setData(data, {$decouple: false});
}
}
tmpColl._linked = this._from._linked;
}
tmpOdm = new Odm(tmpColl);
tmpOdm.parent(this);
tmpOdm.query(query);
return tmpOdm;
};
/**
* Gets / sets a property on the current ODM document.
* @param {String} prop The name of the property.
* @param {*} val Optional value to set.
* @returns {*}
*/
Odm.prototype.prop = function (prop, val) {
var tmpQuery;
if (prop !== undefined) {
if (val !== undefined) {
tmpQuery = {};
tmpQuery[prop] = val;
return this._from.update({}, tmpQuery);
}
if (this._from._data[0]) {
return this._from._data[0][prop];
}
}
return undefined;
};
/**
* Get the ODM instance for this collection.
* @returns {Odm}
*/
Collection.prototype.odm = function (name) {
if (!this._odm) {
this._odm = new Odm(this, name);
}
return this._odm;
};
Shared.finishModule('Odm');
module.exports = Odm;</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>