BVB Source Codes

waterline Show get-attribute.js Source code

Return Download waterline: download get-attribute.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. var getModel = require('./get-model');
  10.  
  11.  
  12. /**
  13.  * Module constants
  14.  */
  15.  
  16. var KNOWN_ATTR_TYPES = ['string', 'number', 'boolean', 'json', 'ref'];
  17.  
  18.  
  19. /**
  20.  * getAttribute()
  21.  *
  22.  * Look up an attribute definition (by name) from the specified model.
  23.  * Usable with normal attributes AND with associations.
  24.  *
  25.  * > Note that we do a few quick assertions in the process, purely as sanity checks
  26.  * > and to help prevent bugs.  If any of these fail, then it means there is some
  27.  * > unhandled usage error, or a bug going on elsewhere in Waterline.
  28.  *
  29.  * ------------------------------------------------------------------------------------------
  30.  * @param {String} attrName
  31.  *        The name of the attribute (e.g. "id" or "favoriteBrands")
  32.  *        > Useful for looking up the Waterline model and accessing its attribute definitions.
  33.  *
  34.  * @param {String} modelIdentity
  35.  *        The identity of the model this is referring to (e.g. "pet" or "user")
  36.  *        > Useful for looking up the Waterline model and accessing its attribute definitions.
  37.  *
  38.  * @param {Ref} orm
  39.  *        The Waterline ORM instance.
  40.  * ------------------------------------------------------------------------------------------
  41.  * @returns {Ref}  [the attribute definition (a direct reference to it, so be careful!!)]
  42.  * ------------------------------------------------------------------------------------------
  43.  * @throws {Error} If no such model exists.
  44.  *         E_MODEL_NOT_REGISTERED
  45.  *
  46.  * @throws {Error} If no such attribute exists.
  47.  *         E_ATTR_NOT_REGISTERED
  48.  *
  49.  * @throws {Error} If anything else goes wrong.
  50.  * ------------------------------------------------------------------------------------------
  51.  */
  52.  
  53. module.exports = function getAttribute(attrName, modelIdentity, orm) {
  54.  
  55.   // ================================================================================================
  56.   // Check that the provided `attrName` is valid.
  57.   // (`modelIdentity` and `orm` will be automatically checked by calling `getModel()`)
  58.   assert(_.isString(attrName) && attrName !== '', '`attrName` must be a non-empty string.');
  59.   // ================================================================================================
  60.  
  61.  
  62.   // Try to look up the Waterline model.
  63.   //
  64.   // > Note that, in addition to being the model definition, this
  65.   // > "WLModel" is actually the hydrated model object (fka a "Waterline collection")
  66.   // > which has methods like `find`, `create`, etc.
  67.   var WLModel = getModel(modelIdentity, orm);
  68.  
  69.   // Try to look up the attribute definition.
  70.   var attrDef = WLModel.attributes[attrName];
  71.   if (_.isUndefined(attrDef)) {
  72.     throw flaverr('E_ATTR_NOT_REGISTERED', new Error('No such attribute (`'+attrName+'`) exists in model (`'+modelIdentity+'`).'));
  73.   }
  74.  
  75.   // ================================================================================================
  76.   // This section consists of more sanity checks for the attribute definition:
  77.  
  78.   assert(_.isObject(attrDef) && !_.isArray(attrDef) && !_.isFunction(attrDef), 'The referenced attribute (`'+attrName+'`, from model `'+modelIdentity+'`) corresponds with a CORRUPTED attribute definition: '+util.inspect(attrDef, {depth:5})+'');
  79.  
  80.   // Some basic sanity checks that this is a valid model association.
  81.   // (note that we don't get too deep here-- though we could)
  82.   if (!_.isUndefined(attrDef.model)) {
  83.     assert(_.isString(attrDef.model) && attrDef.model !== '', 'The referenced attribute (`'+attrName+'`, from model `'+modelIdentity+'`) has an invalid `model` property.  If specified, `model` should be a non-empty string.  But instead, got: '+util.inspect(attrDef.model, {depth:5})+'');
  84.     assert(_.isUndefined(attrDef.via), 'The referenced attribute (`'+attrName+'`, from model `'+modelIdentity+'`) is an association, because it declares a `model`.  But with a "model" association, the `via` property should always be undefined.  But instead, it is: '+util.inspect(attrDef.via, {depth:5})+'');
  85.     assert(_.isUndefined(attrDef.dominant), 'The referenced attribute (`'+attrName+'`, from model `'+modelIdentity+'`) is an association, because it declares a `model`.  But with a "model" association, the `dominant` property should always be undefined.  But instead, it is: '+util.inspect(attrDef.dominant, {depth:5})+'');
  86.     try {
  87.       getModel(attrDef.model, orm);
  88.     } catch (e){ throw new Error('Consistency violation: The referenced attribute (`'+attrName+'`, from model `'+modelIdentity+'`) is an association, because it declares a `model`.  But the other model it references (`'+attrDef.model+'`) is missing or invalid.  Details: '+e.stack); }
  89.   }
  90.   // Some basic sanity checks that this is a valid collection association.
  91.   // (note that we don't get too deep here-- though we could)
  92.   else if (!_.isUndefined(attrDef.collection)) {
  93.     assert(_.isString(attrDef.collection) && attrDef.collection !== '', 'The referenced attribute (`'+attrName+'`, from model `'+modelIdentity+'`) has an invalid `collection` property.  If specified, `collection` should be a non-empty string.  But instead, got: '+util.inspect(attrDef.collection, {depth:5})+'');
  94.     var OtherWLModel;
  95.     try {
  96.       OtherWLModel = getModel(attrDef.collection, orm);
  97.     } catch (e){ throw new Error('Consistency violation: The referenced attribute (`'+attrName+'`, from model `'+modelIdentity+'`) is an association, because it declares a `collection`.  But the other model it references (`'+attrDef.collection+'`) is missing or invalid.  Details: '+e.stack); }
  98.  
  99.     if (!_.isUndefined(attrDef.via)) {
  100.       assert(_.isString(attrDef.via) && attrDef.via !== '', 'The referenced attribute (`'+attrName+'`, from model `'+modelIdentity+'`) has an invalid `via` property.  If specified, `via` should be a non-empty string.  But instead, got: '+util.inspect(attrDef.via, {depth:5})+'');
  101.  
  102.       // Note that we don't call getAttribute recursively.  (That would be madness.)
  103.       // We also don't check for reciprocity on the other side.
  104.       // Instead, we simply do a watered down check.
  105.       // > waterline-schema goes much deeper here.
  106.       // > Remember, these are just sanity checks for development.
  107.       if (!_.isUndefined(attrDef.through)) {
  108.  
  109.         var ThroughWLModel;
  110.         try {
  111.           ThroughWLModel = getModel(attrDef.through, orm);
  112.         } catch (e){ throw new Error('Consistency violation: The referenced attribute (`'+attrName+'`, from model `'+modelIdentity+'`) is a "through" association, because it declares a `through`.  But the junction model it references as "through" (`'+attrDef.through+'`) is missing or invalid.  Details: '+e.stack); }
  113.  
  114.         assert(ThroughWLModel.attributes[attrDef.via], 'The referenced attribute (`'+attrName+'`, from model `'+modelIdentity+'`) is a "through" association, because it declares a `through`.  But the association\'s specified `via` ('+attrDef.via+'`) does not correspond with a recognized attribute on the junction model (`'+attrDef.through+'`)');
  115.         assert(ThroughWLModel.attributes[attrDef.via].model, 'The referenced attribute (`'+attrName+'`, from model `'+modelIdentity+'`) is a "through" association, but its specified `via` ('+attrDef.via+'`) corresponds with an unexpected attribute on the junction model (`'+attrDef.through+'`).  The attribute referenced by `via` should be a singular ("model") association, but instead, got: '+util.inspect(ThroughWLModel.attributes[attrDef.via],{depth: 5})+'');
  116.  
  117.       }
  118.       else {
  119.         assert(OtherWLModel.attributes[attrDef.via], 'The referenced attribute (`'+attrName+'`, from model `'+modelIdentity+'`) is an association, because it declares a `collection`.  But that association also specifies a `via` ('+attrDef.via+'`) which does not correspond with a recognized attribute on the other model (`'+attrDef.collection+'`)');
  120.       }
  121.     }
  122.   }
  123.   // Check that this is a valid, miscellaneous attribute.
  124.   else {
  125.     assert(_.isString(attrDef.type) && attrDef.type !== '', 'The referenced attribute (`'+attrName+'`, from model `'+modelIdentity+'`) has an invalid `type` property.  If specified, `type` should be a non-empty string.  But instead, got: '+util.inspect(attrDef.type, {depth:5})+'');
  126.     assert(_.contains(KNOWN_ATTR_TYPES, attrDef.type), 'The referenced attribute (`'+attrName+'`, from model `'+modelIdentity+'`) has an unrecognized `type`: `'+attrDef.type+'`.');
  127.     assert(attrDef.required === true || attrDef.required === false, 'The referenced attribute (`'+attrName+'`, from model `'+modelIdentity+'`) has an unrecognized `required` property in its definition.  By this time, it should always be true or false.  But instead, got: '+util.inspect(attrDef.required, {depth:5})+'');
  128.     if (attrDef.required) {
  129.       assert(_.isUndefined(attrDef.defaultsTo), 'The referenced attribute (`'+attrName+'`, from model `'+modelIdentity+'`) has `required: true`, but it also specifies a `defaultsTo`.  This should never have been allowed-- defaultsTo should be undefined!  But instead, got: '+util.inspect(attrDef.defaultsTo, {depth:5})+'');
  130.     }
  131.   }
  132.   // ================================================================================================
  133.  
  134.   //-鈥
  135.   // Send back a reference to this attribute definition.
  136.   return attrDef;
  137.  
  138. };
  139.  
downloadget-attribute.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