UNPKG

lowkie

Version:

My Custom lowkie / Express Applcation

150 lines (129 loc) 4.34 kB
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>JSDoc: Source: schema.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: schema.js</h1> <section> <article> <pre class="prettyprint source linenums"><code>'use strict'; const flatten = require('flat'); const ObjectId = require('./object_id'); /** * proxy for creating new loki documents * * @class lowkieSchema */ class lowkieSchema { constructor(scheme, lowkieSingleton) { this.scheme = scheme; this.flattenedScheme = flatten(scheme); this.validNames = Object.keys(scheme); this.lowkie = lowkieSingleton; this.createDoc = this.createDocument.bind(this); this.validNames.push('_id'); // console.log(this.lowkie.config); // this.properties = properties; return this; } /** * returns validated document for lokijs * * @param {any} doc * @returns object * * @memberOf lowkieSchema */ createDocument(doc) { let newDoc = Object.assign({ _id: ObjectId.createId(), }, doc); let validDoc = this.validNames.reduce((result, key) => { // if (this.lowkie.config.strictSchemas) { // if (typeof newDoc[ key ] === 'string' &amp;&amp; (this.scheme[ key ] !== String || (this.scheme[key].type !== String))) { // throw new Error(`${key} (${newDoc[ key ] } - ${typeof newDoc[ key ]}) must be a valid String`); // } // } if (newDoc[ key ]) { if (typeof newDoc[ key ] === 'string' &amp;&amp; this.scheme[ key ] === String) { result[ key ] = newDoc[ key ].toString(); } else if (this.scheme[ key ] === Boolean) { result[ key ] = (newDoc[ key ]) ? true : false; } else if (this.scheme[ key ] === Number) { result[ key ] = Number(newDoc[ key ]); } else if (typeof newDoc[ key ] === 'object') { result[ key ] = newDoc[ key ]; } else { result[ key ] = newDoc[ key ]; } } return result; }, {}); return validDoc; } /** * overwrites the default insert method * * @param {any} options * @returns Promise * * @memberOf lowkieSchema */ insert(options = {}) { let lokiCollectionInsert = options.target; let lowkieInstance = options.thisArg; let lowkieDocument = options.argumentsList; // let { target, thisArg, argumentsList, } = options; return new Promise((resolve, reject) => { try { let newDoc = (Array.isArray(lowkieDocument)) ? lowkieDocument.map(lowkiedoc=>this.createDoc(lowkiedoc)) : this.createDoc(lowkieDocument); lokiCollectionInsert.call(lowkieInstance, newDoc); this.lowkie.db.saveDatabase((err) => { if (err) reject(err); else return resolve(newDoc); }); } catch (e) { reject(e); } }); } } /** * schema data types */ lowkieSchema.Types = { String, Buffer, Date, Number, ObjectId, Array, Mixed:Object, }; module.exports = lowkieSchema;</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>