BVB Source Codes

waterline Show get-model.js Source code

Return Download waterline: download get-model.js Source code - Download waterline Source code - Type:.js
  1. /**
  2.  * Module dependencies
  3.  */
  4.  
  5. var util = require('util');
  6. var assert = require('assert');
  7. var _ = require('@sailshq/lodash');
  8. var flaverr = require('flaverr');
  9.  
  10.  
  11. /**
  12.  * getModel()
  13.  *
  14.  * Look up a Waterline model by identity.
  15.  *
  16.  * > Note that we do a few quick assertions in the process, purely as sanity checks
  17.  * > and to help prevent bugs.  If any of these fail, then it means there is some
  18.  * > unhandled usage error, or a bug going on elsewhere in Waterline.
  19.  *
  20.  * ------------------------------------------------------------------------------------------
  21.  * @param {String} modelIdentity
  22.  *        The identity of the model this is referring to (e.g. "pet" or "user")
  23.  *        > Useful for looking up the Waterline model and accessing its attribute definitions.
  24.  *
  25.  * @param {Ref} orm
  26.  *        The Waterline ORM instance.
  27.  * ------------------------------------------------------------------------------------------
  28.  * @returns {Ref}  [the Waterline model]
  29.  * ------------------------------------------------------------------------------------------
  30.  * @throws {Error} If no such model exists.
  31.  *         E_MODEL_NOT_REGISTERED
  32.  *
  33.  * @throws {Error} If anything else goes wrong.
  34.  * ------------------------------------------------------------------------------------------
  35.  */
  36.  
  37. module.exports = function getModel(modelIdentity, orm) {
  38.  
  39.   // ================================================================================================
  40.   // Check that this utility function is being used properly, and that the provided `modelIdentity` and `orm` are valid.
  41.   assert(_.isString(modelIdentity), '`modelIdentity` must be a non-empty string.  Instead got: '+modelIdentity);
  42.   assert(modelIdentity !== '', '`modelIdentity` must be a non-empty string.  Instead got :'+modelIdentity);
  43.   assert(_.isObject(orm) && !_.isArray(orm) && !_.isFunction(orm), '`orm` must be a valid Waterline ORM instance (must be a dictionary)');
  44.   assert(_.isObject(orm.collections) && !_.isArray(orm.collections) && !_.isFunction(orm.collections), '`orm` must be a valid Waterline ORM instance (must have a dictionary of "collections")');
  45.   // ================================================================================================
  46.  
  47.  
  48.   // Try to look up the Waterline model.
  49.   //
  50.   // > Note that, in addition to being the model definition, this
  51.   // > "WLModel" is actually the hydrated model object (fka a "Waterline collection")
  52.   // > which has methods like `find`, `create`, etc.
  53.   var WLModel = orm.collections[modelIdentity];
  54.   if (_.isUndefined(WLModel)) {
  55.     throw flaverr('E_MODEL_NOT_REGISTERED', new Error('The provided `modelIdentity` references a model (`'+modelIdentity+'`) which is not registered in this `orm`.'));
  56.   }
  57.  
  58.   // ================================================================================================
  59.   // Finally, do a couple of quick sanity checks on the registered
  60.   // Waterline model, such as verifying that it declares an extant,
  61.   // valid primary key attribute.
  62.   assert(_.isObject(WLModel) && !_.isArray(WLModel) && !_.isFunction(WLModel), 'All model definitions must be dictionaries, but somehow, the referenced Waterline model (`'+modelIdentity+'`) seems to have become corrupted.  Here it is: '+util.inspect(WLModel, {depth: 1}));
  63.   assert(_.isObject(WLModel.attributes) && !_.isArray(WLModel.attributes) && !_.isFunction(WLModel.attributes), 'All model definitions must have a dictionary of `attributes`.  But somehow, the referenced Waterline model (`'+modelIdentity+'`) seems to have become corrupted and has a missing or invalid `attributes` property.  Here is the Waterline model: '+util.inspect(WLModel, {depth: 1}));
  64.   assert(_.isString(WLModel.primaryKey), 'The referenced Waterline model (`'+modelIdentity+'`) defines an invalid `primaryKey` setting.  Should be a string (the name of the primary key attribute), but instead, it is: '+util.inspect(WLModel.primaryKey, {depth:5}));
  65.   var pkAttrDef = WLModel.attributes[WLModel.primaryKey];
  66.   assert(!_.isUndefined(pkAttrDef), 'The referenced Waterline model (`'+modelIdentity+'`) declares `primaryKey: \''+WLModel.primaryKey+'\'`, yet there is no `'+WLModel.primaryKey+'` attribute defined in the model!');
  67.   assert(_.isObject(pkAttrDef) && !_.isArray(pkAttrDef) && !_.isFunction(pkAttrDef), 'The `primaryKey` (`'+WLModel.primaryKey+'`) in the referenced Waterline model (`'+modelIdentity+'`) corresponds with a CORRUPTED attribute definition: '+util.inspect(pkAttrDef, {depth:5})+'\n(^^this should have been caught already!)');
  68.   assert(pkAttrDef.required === true || pkAttrDef.required === false, 'The `primaryKey` (`'+WLModel.primaryKey+'`) in the referenced Waterline model (`'+modelIdentity+'`) corresponds with a CORRUPTED attribute definition '+util.inspect(pkAttrDef, {depth:5})+'\n(^^this should have been caught already!  `required` must be either true or false!)');
  69.   assert(pkAttrDef.type === 'number' || pkAttrDef.type === 'string', 'The `primaryKey` (`'+WLModel.primaryKey+'`) in the referenced Waterline model (`'+modelIdentity+'`) corresponds with an INCOMPATIBLE attribute definition.  In order to be used as the logical primary key, the referenced attribute should declare itself `type: \'string\'` or `type: \'number\'`...but instead its `type` is: '+util.inspect(pkAttrDef.type, {depth:5})+'\n(^^this should have been caught already!)');
  70.   // ================================================================================================
  71.  
  72.  
  73.   // Send back a reference to this Waterline model.
  74.   return WLModel;
  75.  
  76. };
  77.  
downloadget-model.js Source code - Download waterline Source code
Related Source Codes/Software:
notepad-plus-plus - Notepad++ official repository h... 2017-01-10
che - Eclipse Che: Next-generation Eclipse IDE. Open sou... 2017-01-10
Gource - oftware version control visualization ... 2017-01-10
FDFullscreenPopGesture - A UINavigationController's category to enable full... 2017-01-10
node-style-guide - A guide for styling your node.js / JavaScript code... 2017-01-09
Workerman - An asynchronous event driven PHP framework for eas... 2017-01-10
structor - An advanced visual editor for React components ... 2017-01-10
golearn - Machine Learning for Go 2017-01-10
poisontap - Exploits locked/password protected computers over ... 2017-01-10
kcptun - A Simple UDP Tunnel Based On KCP 2017-01-11
CRYENGINE - CRYENGINE is a powerful real-time game development... 2017-06-11
postal - 2017-06-11
reactide - Reactide is the first dedicated IDE for React web ... 2017-06-11
rkt - rkt is a pod-native container engine for Linux. It... 2017-06-11
uWebSockets - Tiny WebSockets https://for... 2017-06-11
realworld - TodoMVC for the RealWorld - Exemplary fullstack Me... 2017-06-11
goreplay - GoReplay is an open-source tool for capturing and ... 2017-06-10
pyenv - Simple Python version management 2017-06-10
redux-saga - An alternative side effect model for Redux apps ... 2017-06-10
angular-starter - 2017-06-10

 Back to top