UNPKG

lowkie

Version:

My Custom lowkie / Express Applcation

96 lines (73 loc) 3.03 kB
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>JSDoc: Source: model.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: model.js</h1> <section> <article> <pre class="prettyprint source linenums"><code>'use strict'; const pluralize = require('pluralize'); const lowkieSchema = require('./schema'); /** * lowkie model proxy for lokijs collection * * @param {string} model name of lowkie model * @param {class} Schema instance of lokieSchema * @returns Proxy */ function model(model, Schema, collectionOptions = {}) { try { // console.log('this.connections.size', this.connections.size); if (!this.connections.size) throw new Error('There has to be an active lowkie connection before creating models, lowkie.connect is asynchronous'); if (!model || typeof model !=='string') throw new Error('model name must be a valid string'); if (!(Schema instanceof lowkieSchema)) throw new Error(`${model} must be an instance of a lowkieSchema`); let modelName = pluralize(model); let existingCollection = this.db.getCollection(modelName); let modelProxy = (existingCollection) ? existingCollection : this.db.addCollection(modelName, collectionOptions); let modelHandler = { /*get: function (target, name) {return target[ name ];},*/ }; modelProxy.insert = new Proxy(modelProxy.insert, { apply: function (target, thisArg, argumentsList) { return Schema.insert({ target, thisArg, argumentsList, }); }, }); let newModelProxy = new Proxy(modelProxy, modelHandler); this.models[ modelName ] = newModelProxy; return newModelProxy; } catch (e) { if (this.debug) { console.error(e); } throw e; } } module.exports = model; </code></pre> </article> </section> </div> <nav> <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="lowkie.html">lowkie</a></li><li><a href="lowkie.lowkie.html">lowkie</a></li><li><a href="lowkieSchema.html">lowkieSchema</a></li><li><a href="ObjectId.html">ObjectId</a></li></ul><h3>Global</h3><ul><li><a href="global.html#connect">connect</a></li><li><a href="global.html#handler">handler</a></li><li><a href="global.html#model">model</a></li></ul> </nav> <br class="clear"> <footer> Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.3</a> on Mon Mar 27 2017 21:39:17 GMT-0400 (EDT) </footer> <script> prettyPrint(); </script> <script src="scripts/linenumber.js"> </script> </body> </html>