BVB Source Codes

waterline Show is-capable-of-optimized-populate.js Source code

Return Download waterline: download is-capable-of-optimized-populate.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 getModel = require('./get-model');
  9. var getAttribute = require('./get-attribute');
  10.  
  11.  
  12. /**
  13.  * isCapableOfOptimizedPopulate()
  14.  *
  15.  * Determine whether this association fully supports optimized populate.
  16.  *
  17.  * > Note that, if this is a plural association (a `collection` assoc. that is pointed at
  18.  * > by `via` on the other side, or for which there IS no "other side"), then there will be
  19.  * > a junction model in play.  For this utility to return `true`, that junction model must
  20.  * > also be on the same datastore!
  21.  *
  22.  * ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  23.  * @param {String} attrName        [the name of the association in question]
  24.  * @param {String} modelIdentity   [the identity of the model this association belongs to]
  25.  * @param {Ref} orm                [the Waterline ORM instance]
  26.  * ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  27.  * @returns {Boolean}
  28.  */
  29.  
  30. module.exports = function isCapableOfOptimizedPopulate(attrName, modelIdentity, orm) {
  31.  
  32.   assert(_.isString(attrName), 'Must specify `attrName` as a string.  But instead, got: '+util.inspect(attrName, {depth:5})+'');
  33.   assert(_.isString(modelIdentity), 'Must specify `modelIdentity` as a string.  But instead, got: '+util.inspect(modelIdentity, {depth:5})+'');
  34.   assert(!_.isUndefined(orm), 'Must pass in `orm` (a reference to the Waterline ORM instance).  But instead, got: '+util.inspect(orm, {depth:5})+'');
  35.  
  36.  
  37.   //  鈺  鈺斺晲鈺椻晹鈺愨晽鈺︹晹鈺  鈺 鈺︹晹鈺愨晽  鈹屸攢鈹愨攲鈹€鈹愨攲鈹€鈹愨攲鈹€鈹愨攲鈹€鈹愨敩鈹屸攢鈹愨攲鈹攼鈹攲鈹€鈹愨攲鈹愨攲   鈹   鈹屸敩鈹愨攲鈹€鈹愨攲鈹攼鈹屸攢鈹愨敩  鈹屸攢鈹
  38.   //  鈺  鈺 鈺戔晳 鈺戔暊鈺┾晽  鈺 鈺戔暊鈺愨暆  鈹溾攢鈹も敂鈹€鈹愨敂鈹€鈹愨攤 鈹傗攤  鈹傗敎鈹€鈹 鈹 鈹傗攤 鈹傗攤鈹傗攤  鈹屸敿鈹€  鈹傗攤鈹傗攤 鈹 鈹傗攤鈹溾敜 鈹  鈹斺攢鈹
  39.   //  鈺┾晲鈺濃暁鈺愨暆鈺氣晲鈺濃暕 鈺  鈺氣晲鈺濃暕    鈹 鈹粹敂鈹€鈹樷敂鈹€鈹樷敂鈹€鈹樷敂鈹€鈹樷敶鈹 鈹 鈹 鈹粹敂鈹€鈹樷敇鈹斺敇  鈹斺敇   鈹 鈹粹敂鈹€鈹樷攢鈹粹敇鈹斺攢鈹樷敶鈹€鈹樷敂鈹€鈹
  40.  
  41.   // Look up the containing model for this association, and the attribute definition itself.
  42.   var PrimaryWLModel = getModel(modelIdentity, orm);
  43.   var attrDef = getAttribute(attrName, modelIdentity, orm);
  44.  
  45.   assert(attrDef.model || attrDef.collection, 'Attempting to check whether attribute `'+attrName+'` of model `'+modelIdentity+'` is capable of optimized populate, but it\'s not even an association!');
  46.  
  47.   // Look up the other, associated model.
  48.   var otherModelIdentity = attrDef.model ? attrDef.model : attrDef.collection;
  49.   var OtherWLModel = getModel(otherModelIdentity, orm);
  50.  
  51.  
  52.  
  53.   //  鈹屸攢鈹愨敩 鈹攲鈹€鈹愨攲鈹€鈹愨敩鈹屸攢  鈹 鈹敩 鈹攲鈹€鈹愨攲鈹攼鈹 鈹攲鈹€鈹愨敩鈹€鈹  鈺斺晲鈺椻暒  鈺    鈹屸敩鈹愨攲鈹€鈹愨攲鈹攼鈹屸攢鈹愨敩  鈹屸攢鈹
  54.   //  鈹  鈹溾攢鈹も敎鈹 鈹  鈹溾敶鈹  鈹傗攤鈹傗敎鈹€鈹も敎鈹  鈹 鈹溾攢鈹も敎鈹 鈹溾敩鈹  鈺犫晲鈺b晳  鈺    鈹傗攤鈹傗攤 鈹 鈹傗攤鈹溾敜 鈹  鈹斺攢鈹
  55.   //  鈹斺攢鈹樷敶 鈹粹敂鈹€鈹樷敂鈹€鈹樷敶 鈹  鈹斺敶鈹樷敶 鈹粹敂鈹€鈹 鈹 鈹 鈹粹敂鈹€鈹樷敶鈹斺攢  鈺 鈺┾暕鈺愨暆鈺┾晲鈺  鈹 鈹粹敂鈹€鈹樷攢鈹粹敇鈹斺攢鈹樷敶鈹€鈹樷敂鈹€鈹
  56.   //  鈹屸攢鈹愨敩鈹€鈹愨攲鈹€鈹  鈹 鈹攲鈹€鈹愨敩鈹屸攼鈹屸攲鈹€鈹  鈹屸敩鈹愨敩 鈹攲鈹€鈹  鈺斺晲鈺椻晹鈺愨晽鈺斺暒鈺椻晹鈺愨晽  鈺斺暒鈺椻晹鈺愨晽鈺斺暒鈺椻晹鈺愨晽鈺斺晲鈺椻晹鈺︹晽鈺斺晲鈺椻暒鈺愨晽鈺斺晲鈺
  57.   //  鈹溾攢鈹も敎鈹敇鈹溾敜   鈹 鈹傗敂鈹€鈹愨攤鈹傗攤鈹傗攤 鈹   鈹 鈹溾攢鈹も敎鈹   鈺氣晲鈺椻暊鈺愨暎鈺戔晳鈺戔晳鈺    鈺戔晳鈺犫晲鈺 鈺 鈺犫晲鈺b暁鈺愨晽 鈺 鈺 鈺戔暊鈺︹暆鈺戔暎
  58.   //  鈹 鈹粹敶鈹斺攢鈹斺攢鈹  鈹斺攢鈹樷敂鈹€鈹樷敶鈹樷敂鈹樷敂鈹€鈹   鈹 鈹 鈹粹敂鈹€鈹  鈺氣晲鈺濃暕 鈺┾暕 鈺┾暁鈺愨暆  鈺愨暕鈺濃暕 鈺 鈺 鈺 鈺┾暁鈺愨暆 鈺 鈺氣晲鈺濃暕鈺氣晲鈺氣晲鈺
  59.  
  60.   // Determine if the two models are using the same datastore.
  61.   var isUsingSameDatastore = (PrimaryWLModel.datastore === OtherWLModel.datastore);
  62.  
  63.   if (!_.isString(PrimaryWLModel.datastore) || !_.isString(OtherWLModel.datastore)) {
  64.     console.warn('TODO: Fix outdated semantics (see https://github.com/balderdashy/waterline/commit/ecd3e1c8f05e27a3b0c1ea4f08a73a0b4ad83c07#commitcomment-20271012)  The `datastore` property should be a string, not an array.');
  65.     // ^^^TODO: instead of the above lines (^^^) replace it with the following lines:
  66.     // ```
  67.     // assert(_.isString(PrimaryWLModel.datastore));
  68.     // assert(_.isString(OtherWLModel.datastore));
  69.     // ```
  70.   }
  71.  
  72.  
  73.  
  74.   // Now figure out if this association is using a junction (aka "many to many"),
  75.   // and if so, which model it is.
  76.   // > If it is not using a junction, we'll leave `JunctionWLModel` as undefined.
  77.   // ------
  78.   var JunctionWLModel;
  79.   // To accomplish this, we'll grab the already-mapped relationship info (attached by wl-schema
  80.   // to models, as the `schema` property).  If our directly-related model (as mapped by WL-schema
  81.   // has a `junctionTable` flag or a `throughTable` dictionary, then we can safely say this association
  82.   // is using a junction, and that this directly-related model is indeed that junction.
  83.   var junctionOrOtherModelIdentity = PrimaryWLModel.schema[attrName].referenceIdentity;
  84.   var JunctionOrOtherWLModel = getModel(junctionOrOtherModelIdentity, orm);
  85.   var arcaneProto = Object.getPrototypeOf(JunctionOrOtherWLModel);
  86.   if (_.isBoolean(arcaneProto.junctionTable) || _.isPlainObject(arcaneProto.throughTable)) {
  87.     JunctionWLModel = JunctionOrOtherWLModel;
  88.   }//>-
  89.   // -----
  90.  
  91.   // If there is a junction, make sure to factor that in too.
  92.   // (It has to be using the same datastore as the other two for it to count.)
  93.   if (JunctionWLModel) {
  94.     isUsingSameDatastore = isUsingSameDatastore && (JunctionWLModel.datastore === PrimaryWLModel.datastore);
  95.  
  96.     if (!_.isString(JunctionWLModel.datastore)) {
  97.       console.warn('TODO: outdated semantics (see https://github.com/balderdashy/waterline/commit/ecd3e1c8f05e27a3b0c1ea4f08a73a0b4ad83c07#commitcomment-20271012)  The `datastore` property should be a string, not an array.');
  98.       // ^^^TODO: instead of the above lines (^^^) replace it with the following lines:
  99.       // ```
  100.       // assert(_.isString(JunctionWLModel.datastore));
  101.       // ```
  102.     }
  103.  
  104.   }//>-
  105.  
  106.   // Now, if any of the models involved is using a different datastore, then bail.
  107.   if (!isUsingSameDatastore) {
  108.     return false;
  109.   }//-鈥
  110.  
  111.  
  112.   // --鈥
  113.   // IWMIH, we know that this association is using exactly ONE datastore.
  114.   // And we even know that datastore's name.
  115.   //
  116.   // (remember, we just checked to verify that they're exactly the same above-- so we could have grabbed
  117.   // this datastore name from ANY of the involved models)
  118.   var relevantDatastoreName = PrimaryWLModel.datastore;
  119.   if (!_.isString(PrimaryWLModel.datastore)) {
  120.     console.warn('TODO: outdated semantics (see https://github.com/balderdashy/waterline/commit/ecd3e1c8f05e27a3b0c1ea4f08a73a0b4ad83c07#commitcomment-20271012)  The `datastore` property should be a string, not an array.');
  121.     relevantDatastoreName = _.first(PrimaryWLModel.datastore);
  122.     // ^^^TODO: instead of the above two lines (^^^) replace it with the following lines:
  123.     // ```
  124.     // assert(_.isString(PrimaryWLModel.datastore));
  125.     // ```
  126.   }
  127.  
  128.   assert(_.isString(relevantDatastoreName));
  129.  
  130.  
  131.   // Finally, now that we know which datastore we're dealing with, check to see if that datastore's
  132.   // configured adapter supports optimized populates.
  133.   //
  134.   // If not, then we're done.
  135.   if (!PrimaryWLModel._adapter.join) {
  136.     return false;
  137.   }//-鈥
  138.  
  139.   // IWMIH, then we know that all involved models in this query share a datastore, and that the datastore's
  140.   // adapter supports optimized populates.  So we return true!
  141.   return true;
  142.  
  143. };
  144.  
  145.  
  146. // Quick test:
  147. /*```
  148. require('./lib/waterline/utils/ontology/is-capable-of-optimized-populate')('pets', 'user', { collections: { user: { attributes: { id: { type: 'string', required: true, unique: true }, age: { type: 'number', required: false }, foo: { type: 'string', required: true }, pets: { collection: 'pet' } }, primaryKey: 'id', hasSchema: true}, pet: { attributes: { id: { type:'number', required: true, unique: true } }, primaryKey: 'id', hasSchema: true } } });
  149. ```*/
  150.  
downloadis-capable-of-optimized-populate.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
discover-flask - Full Stack Web Development with Flask. ... 2017-01-12
spring-mvc-showcase - Demonstrates the features of the Spring MVC web fr... 2017-01-12
tushare - TuShare is a utility for crawling historical data ... 2017-01-12
raml-spec - RAML Specification http://raml.... 2017-01-12
android-stackblur - Android StackBlur is a library that can perform a ... 2017-01-12
sound-redux - A Soundcloud client built with React / Redux ... 2017-01-12
httpstat - curl statistics made simple 2017-01-12
vim - Vim configuration file and plug-ins 2017-01-12
appframework - The definitive HTML5 mobile javascript framework ... 2017-01-12
BaiduExporter - Assistant for Baidu to export download links to ar... 2017-01-11

 Back to top