BVB Source Codes

waterline Show forge-stage-three-query.js Source code

Return Download waterline: download forge-stage-three-query.js Source code - Download waterline Source code - Type:.js
  1. /**
  2.  * Module dependencies
  3.  */
  4.  
  5. var util = require('util');
  6. var _ = require('@sailshq/lodash');
  7. var flaverr = require('flaverr');
  8.  
  9. //  鈻堚枅鈻堚枅鈻堚枅鈻堚晽 鈻堚枅鈻堚枅鈻堚枅鈺 鈻堚枅鈻堚枅鈻堚枅鈺  鈻堚枅鈻堚枅鈻堚枅鈺 鈻堚枅鈻堚枅鈻堚枅鈻堚晽    鈻堚枅鈻堚枅鈻堚枅鈻堚晽鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈺 鈻堚枅鈻堚枅鈻堚晽  鈻堚枅鈻堚枅鈻堚枅鈺 鈻堚枅鈻堚枅鈻堚枅鈻堚晽
  10. //  鈻堚枅鈺斺晲鈺愨晲鈺愨暆鈻堚枅鈺斺晲鈺愨晲鈻堚枅鈺椻枅鈻堚晹鈺愨晲鈻堚枅鈺椻枅鈻堚晹鈺愨晲鈺愨晲鈺 鈻堚枅鈺斺晲鈺愨晲鈺愨暆    鈻堚枅鈺斺晲鈺愨晲鈺愨暆鈺氣晲鈺愨枅鈻堚晹鈺愨晲鈺濃枅鈻堚晹鈺愨晲鈻堚枅鈺椻枅鈻堚晹鈺愨晲鈺愨晲鈺 鈻堚枅鈺斺晲鈺愨晲鈺愨暆
  11. //  鈻堚枅鈻堚枅鈻堚晽  鈻堚枅鈺   鈻堚枅鈺戔枅鈻堚枅鈻堚枅鈻堚晹鈺濃枅鈻堚晳  鈻堚枅鈻堚晽鈻堚枅鈻堚枅鈻堚晽      鈻堚枅鈻堚枅鈻堚枅鈻堚晽   鈻堚枅鈺   鈻堚枅鈻堚枅鈻堚枅鈻堚晳鈻堚枅鈺  鈻堚枅鈻堚晽鈻堚枅鈻堚枅鈻堚晽
  12. //  鈻堚枅鈺斺晲鈺愨暆  鈻堚枅鈺   鈻堚枅鈺戔枅鈻堚晹鈺愨晲鈻堚枅鈺椻枅鈻堚晳   鈻堚枅鈺戔枅鈻堚晹鈺愨晲鈺      鈺氣晲鈺愨晲鈺愨枅鈻堚晳   鈻堚枅鈺   鈻堚枅鈺斺晲鈺愨枅鈻堚晳鈻堚枅鈺   鈻堚枅鈺戔枅鈻堚晹鈺愨晲鈺
  13. //  鈻堚枅鈺     鈺氣枅鈻堚枅鈻堚枅鈻堚晹鈺濃枅鈻堚晳  鈻堚枅鈺戔暁鈻堚枅鈻堚枅鈻堚枅鈺斺暆鈻堚枅鈻堚枅鈻堚枅鈻堚晽    鈻堚枅鈻堚枅鈻堚枅鈻堚晳   鈻堚枅鈺   鈻堚枅鈺  鈻堚枅鈺戔暁鈻堚枅鈻堚枅鈻堚枅鈺斺暆鈻堚枅鈻堚枅鈻堚枅鈻堚晽
  14. //  鈺氣晲鈺      鈺氣晲鈺愨晲鈺愨晲鈺 鈺氣晲鈺  鈺氣晲鈺 鈺氣晲鈺愨晲鈺愨晲鈺 鈺氣晲鈺愨晲鈺愨晲鈺愨暆    鈺氣晲鈺愨晲鈺愨晲鈺愨暆   鈺氣晲鈺   鈺氣晲鈺  鈺氣晲鈺 鈺氣晲鈺愨晲鈺愨晲鈺 鈺氣晲鈺愨晲鈺愨晲鈺愨暆
  15. //
  16. //  鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈺椻枅鈻堚晽  鈻堚枅鈺椻枅鈻堚枅鈻堚枅鈻堚晽 鈻堚枅鈻堚枅鈻堚枅鈻堚晽鈻堚枅鈻堚枅鈻堚枅鈻堚晽     鈻堚枅鈻堚枅鈻堚枅鈺 鈻堚枅鈺   鈻堚枅鈺椻枅鈻堚枅鈻堚枅鈻堚枅鈺椻枅鈻堚枅鈻堚枅鈻堚晽 鈻堚枅鈺   鈻堚枅鈺
  17. //  鈺氣晲鈺愨枅鈻堚晹鈺愨晲鈺濃枅鈻堚晳  鈻堚枅鈺戔枅鈻堚晹鈺愨晲鈻堚枅鈺椻枅鈻堚晹鈺愨晲鈺愨晲鈺濃枅鈻堚晹鈺愨晲鈺愨晲鈺    鈻堚枅鈺斺晲鈺愨晲鈻堚枅鈺椻枅鈻堚晳   鈻堚枅鈺戔枅鈻堚晹鈺愨晲鈺愨晲鈺濃枅鈻堚晹鈺愨晲鈻堚枅鈺椻暁鈻堚枅鈺 鈻堚枅鈺斺暆
  18. //     鈻堚枅鈺   鈻堚枅鈻堚枅鈻堚枅鈻堚晳鈻堚枅鈻堚枅鈻堚枅鈺斺暆鈻堚枅鈻堚枅鈻堚晽  鈻堚枅鈻堚枅鈻堚晽      鈻堚枅鈺   鈻堚枅鈺戔枅鈻堚晳   鈻堚枅鈺戔枅鈻堚枅鈻堚枅鈺  鈻堚枅鈻堚枅鈻堚枅鈺斺暆 鈺氣枅鈻堚枅鈻堚晹鈺
  19. //     鈻堚枅鈺   鈻堚枅鈺斺晲鈺愨枅鈻堚晳鈻堚枅鈺斺晲鈺愨枅鈻堚晽鈻堚枅鈺斺晲鈺愨暆  鈻堚枅鈺斺晲鈺愨暆      鈻堚枅鈺戔杽鈻 鈻堚枅鈺戔枅鈻堚晳   鈻堚枅鈺戔枅鈻堚晹鈺愨晲鈺  鈻堚枅鈺斺晲鈺愨枅鈻堚晽  鈺氣枅鈻堚晹鈺
  20. //     鈻堚枅鈺   鈻堚枅鈺  鈻堚枅鈺戔枅鈻堚晳  鈻堚枅鈺戔枅鈻堚枅鈻堚枅鈻堚枅鈺椻枅鈻堚枅鈻堚枅鈻堚枅鈺    鈺氣枅鈻堚枅鈻堚枅鈻堚晹鈺濃暁鈻堚枅鈻堚枅鈻堚枅鈺斺暆鈻堚枅鈻堚枅鈻堚枅鈻堚晽鈻堚枅鈺  鈻堚枅鈺   鈻堚枅鈺
  21. //     鈺氣晲鈺   鈺氣晲鈺  鈺氣晲鈺濃暁鈺愨暆  鈺氣晲鈺濃暁鈺愨晲鈺愨晲鈺愨晲鈺濃暁鈺愨晲鈺愨晲鈺愨晲鈺     鈺氣晲鈺愨杸鈻€鈺愨暆  鈺氣晲鈺愨晲鈺愨晲鈺 鈺氣晲鈺愨晲鈺愨晲鈺愨暆鈺氣晲鈺  鈺氣晲鈺   鈺氣晲鈺
  22. //
  23.  
  24.  
  25. /**
  26.  * forgeStageThreeQuery()
  27.  *
  28.  * @required {Dictionary} stageTwoQuery
  29.  * TODO: document the rest of the options
  30.  *
  31.  * @return {Dictionary}         [the stage 3 query]
  32.  */
  33. module.exports = function forgeStageThreeQuery(options) {
  34.   //  鈺  鈺︹晹鈺愨晽鈺  鈺︹晹鈺︹晽鈺斺晲鈺椻晹鈺︹晽鈺斺晲鈺  鈹屸攢鈹愨攲鈹€鈹愨攲鈹攼鈹攲鈹€鈹愨攲鈹愨攲鈹屸攢鈹
  35.   //  鈺氣晽鈺斺暆鈺犫晲鈺b晳  鈺 鈺戔晳鈺犫晲鈺 鈺 鈺戔暎   鈹 鈹傗敎鈹€鈹 鈹 鈹傗攤 鈹傗攤鈹傗攤鈹斺攢鈹
  36.   //   鈺氣暆 鈺 鈺┾暕鈺愨暆鈺┾晲鈺┾暆鈺 鈺 鈺 鈺氣晲鈺  鈹斺攢鈹樷敶   鈹 鈹粹敂鈹€鈹樷敇鈹斺敇鈹斺攢鈹
  37.   if (!_.has(options, 'stageTwoQuery') || !_.isPlainObject(options.stageTwoQuery)) {
  38.     throw new Error('Invalid options passed to `.buildStageThreeQuery()`. Missing or invalud `stageTwoQuery` option.');
  39.   }
  40.  
  41.   if (!_.has(options, 'identity') || !_.isString(options.identity)) {
  42.     throw new Error('Invalid options passed to `.buildStageThreeQuery()`. Missing or invalud `identity` option.');
  43.   }
  44.  
  45.   if (!_.has(options, 'transformer') || !_.isObject(options.transformer)) {
  46.     throw new Error('Invalid options passed to `.buildStageThreeQuery()`. Missing or invalud `transformer` option.');
  47.   }
  48.  
  49.   if (!_.has(options, 'originalModels') || !_.isPlainObject(options.originalModels)) {
  50.     throw new Error('Invalid options passed to `.buildStageThreeQuery()`. Missing or invalud `originalModels` option.');
  51.   }
  52.  
  53.  
  54.   // Store the options to prevent typing so much
  55.   var s3Q = options.stageTwoQuery;
  56.   var identity = options.identity;
  57.   var transformer = options.transformer;
  58.   var originalModels = options.originalModels;
  59.  
  60.  
  61.   //  鈺斺晲鈺椻暒鈺斺晽鈺斺晹鈺︹晽  鈹屸敩鈹愨攲鈹€鈹愨攲鈹攼鈹屸攢鈹愨敩
  62.   //  鈺犫暎 鈺戔晳鈺戔晳 鈺戔晳  鈹傗攤鈹傗攤 鈹 鈹傗攤鈹溾敜 鈹
  63.   //  鈺  鈺┾暆鈺氣暆鈺愨暕鈺  鈹 鈹粹敂鈹€鈹樷攢鈹粹敇鈹斺攢鈹樷敶鈹€鈹
  64.   // Grab the current model definition. It will be used in all sorts of ways.
  65.   var model;
  66.   try {
  67.     model = originalModels[identity];
  68.   } catch (e) {
  69.     throw new Error('A model with the identity ' + identity + ' could not be found in the schema. Perhaps the wrong schema was used?');
  70.   }
  71.  
  72.  
  73.   //  鈺斺晲鈺椻暒鈺斺晽鈺斺晹鈺︹晽  鈹屸攢鈹愨敩鈹€鈹愨敩鈹屸敩鈹愨攲鈹€鈹愨敩鈹€鈹愨敩 鈹  鈹攲鈹€鈹屸攢鈹愨敩 鈹
  74.   //  鈺犫暎 鈺戔晳鈺戔晳 鈺戔晳  鈹溾攢鈹樷敎鈹敇鈹傗攤鈹傗攤鈹溾攢鈹も敎鈹敇鈹斺敩鈹  鈹溾敶鈹愨敎鈹 鈹斺敩鈹
  75.   //  鈺  鈺┾暆鈺氣暆鈺愨暕鈺  鈹  鈹粹敂鈹€鈹粹敶 鈹粹敶 鈹粹敶鈹斺攢 鈹   鈹 鈹粹敂鈹€鈹 鈹
  76.   // Get the current model's primary key attribute
  77.   var modelPrimaryKey = model.primaryKey;
  78.  
  79.  
  80.   //  鈺斺暒鈺椻暒鈺愨晽鈺斺晲鈺椻晹鈺椻晹鈺斺晲鈺椻晹鈺愨晽鈺斺晲鈺椻暒鈺愨晽鈺斺暒鈺  鈹 鈹攲鈹€鈹愨敩鈹屸攼鈹屸攲鈹€鈹
  81.   //   鈺 鈺犫暒鈺濃暊鈺愨暎鈺戔晳鈺戔暁鈺愨晽鈺犫暎 鈺 鈺戔暊鈺︹暆鈺戔晳鈺  鈹 鈹傗敂鈹€鈹愨攤鈹傗攤鈹傗攤 鈹
  82.   //   鈺 鈺┾暁鈺愨暕 鈺┾暆鈺氣暆鈺氣晲鈺濃暁  鈺氣晲鈺濃暕鈺氣晲鈺 鈺  鈹斺攢鈹樷敂鈹€鈹樷敶鈹樷敂鈹樷敂鈹€鈹
  83.   s3Q.using = model.tableName;
  84.  
  85.  
  86.   //   鈻堚枅鈻堚枅鈻堚枅鈺椻枅鈻堚枅鈻堚枅鈻堚晽 鈻堚枅鈻堚枅鈻堚枅鈻堚晽 鈻堚枅鈻堚枅鈻堚晽 鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈺椻枅鈻堚枅鈻堚枅鈻堚枅鈺
  87.   //  鈻堚枅鈺斺晲鈺愨晲鈺愨暆鈻堚枅鈺斺晲鈺愨枅鈻堚晽鈻堚枅鈺斺晲鈺愨晲鈺愨暆鈻堚枅鈺斺晲鈺愨枅鈻堚晽鈺氣晲鈺愨枅鈻堚晹鈺愨晲鈺濃枅鈻堚晹鈺愨晲鈺愨晲鈺
  88.   //  鈻堚枅鈺     鈻堚枅鈻堚枅鈻堚枅鈺斺暆鈻堚枅鈻堚枅鈻堚晽  鈻堚枅鈻堚枅鈻堚枅鈻堚晳   鈻堚枅鈺   鈻堚枅鈻堚枅鈻堚晽
  89.   //  鈻堚枅鈺     鈻堚枅鈺斺晲鈺愨枅鈻堚晽鈻堚枅鈺斺晲鈺愨暆  鈻堚枅鈺斺晲鈺愨枅鈻堚晳   鈻堚枅鈺   鈻堚枅鈺斺晲鈺愨暆
  90.   //  鈺氣枅鈻堚枅鈻堚枅鈻堚晽鈻堚枅鈺  鈻堚枅鈺戔枅鈻堚枅鈻堚枅鈻堚枅鈺椻枅鈻堚晳  鈻堚枅鈺   鈻堚枅鈺   鈻堚枅鈻堚枅鈻堚枅鈻堚晽
  91.   //   鈺氣晲鈺愨晲鈺愨晲鈺濃暁鈺愨暆  鈺氣晲鈺濃暁鈺愨晲鈺愨晲鈺愨晲鈺濃暁鈺愨暆  鈺氣晲鈺   鈺氣晲鈺   鈺氣晲鈺愨晲鈺愨晲鈺愨暆
  92.   //
  93.   // For `create` queries, the values need to be run through the transformer.
  94.   if (s3Q.method === 'create') {
  95.     // Validate that there is a `newRecord` key on the object
  96.     if (!_.has(s3Q, 'newRecord') || !_.isPlainObject(s3Q.newRecord)) {
  97.       throw flaverr('E_INVALID_RECORD', new Error(
  98.         'Failed process the values set for the record.'
  99.       ));
  100.     }
  101.  
  102.     try {
  103.       s3Q.newRecord = transformer.serialize(s3Q.newRecord);
  104.     } catch (e) {
  105.       throw flaverr('E_INVALID_RECORD', new Error(
  106.         'Failed process the values set for the record.\n'+
  107.         'Details:\n'+
  108.         e.message
  109.       ));
  110.     }
  111.  
  112.     return s3Q;
  113.   }
  114.  
  115.  
  116.   //   鈻堚枅鈻堚枅鈻堚枅鈺椻枅鈻堚枅鈻堚枅鈻堚晽 鈻堚枅鈻堚枅鈻堚枅鈻堚晽 鈻堚枅鈻堚枅鈻堚晽 鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈺椻枅鈻堚枅鈻堚枅鈻堚枅鈺    鈻堚枅鈻堚枅鈻堚枅鈻堚晽 鈻堚枅鈻堚枅鈻堚晽  鈻堚枅鈻堚枅鈻堚枅鈺椻枅鈻堚晽  鈻堚枅鈺
  117.   //  鈻堚枅鈺斺晲鈺愨晲鈺愨暆鈻堚枅鈺斺晲鈺愨枅鈻堚晽鈻堚枅鈺斺晲鈺愨晲鈺愨暆鈻堚枅鈺斺晲鈺愨枅鈻堚晽鈺氣晲鈺愨枅鈻堚晹鈺愨晲鈺濃枅鈻堚晹鈺愨晲鈺愨晲鈺    鈻堚枅鈺斺晲鈺愨晲鈺愨暆鈻堚枅鈺斺晲鈺愨枅鈻堚晽鈻堚枅鈺斺晲鈺愨晲鈺愨暆鈻堚枅鈺  鈻堚枅鈺
  118.   //  鈻堚枅鈺     鈻堚枅鈻堚枅鈻堚枅鈺斺暆鈻堚枅鈻堚枅鈻堚晽  鈻堚枅鈻堚枅鈻堚枅鈻堚晳   鈻堚枅鈺   鈻堚枅鈻堚枅鈻堚晽      鈻堚枅鈻堚枅鈻堚晽  鈻堚枅鈻堚枅鈻堚枅鈻堚晳鈻堚枅鈺     鈻堚枅鈻堚枅鈻堚枅鈻堚晳
  119.   //  鈻堚枅鈺     鈻堚枅鈺斺晲鈺愨枅鈻堚晽鈻堚枅鈺斺晲鈺愨暆  鈻堚枅鈺斺晲鈺愨枅鈻堚晳   鈻堚枅鈺   鈻堚枅鈺斺晲鈺愨暆      鈻堚枅鈺斺晲鈺愨暆  鈻堚枅鈺斺晲鈺愨枅鈻堚晳鈻堚枅鈺     鈻堚枅鈺斺晲鈺愨枅鈻堚晳
  120.   //  鈺氣枅鈻堚枅鈻堚枅鈻堚晽鈻堚枅鈺  鈻堚枅鈺戔枅鈻堚枅鈻堚枅鈻堚枅鈺椻枅鈻堚晳  鈻堚枅鈺   鈻堚枅鈺   鈻堚枅鈻堚枅鈻堚枅鈻堚晽    鈻堚枅鈻堚枅鈻堚枅鈻堚晽鈻堚枅鈺  鈻堚枅鈺戔暁鈻堚枅鈻堚枅鈻堚枅鈺椻枅鈻堚晳  鈻堚枅鈺
  121.   //   鈺氣晲鈺愨晲鈺愨晲鈺濃暁鈺愨暆  鈺氣晲鈺濃暁鈺愨晲鈺愨晲鈺愨晲鈺濃暁鈺愨暆  鈺氣晲鈺   鈺氣晲鈺   鈺氣晲鈺愨晲鈺愨晲鈺愨暆    鈺氣晲鈺愨晲鈺愨晲鈺愨暆鈺氣晲鈺  鈺氣晲鈺 鈺氣晲鈺愨晲鈺愨晲鈺濃暁鈺愨暆  鈺氣晲鈺
  122.   //
  123.   // For `createEach` queries, the values of each record need to be run through the transformer.
  124.   if (s3Q.method === 'createEach') {
  125.     // Validate that there is a `newRecord` key on the object
  126.     if (!_.has(s3Q, 'newRecords') || !_.isArray(s3Q.newRecords)) {
  127.       throw flaverr('E_INVALID_RECORDS', new Error(
  128.         'Failed process the values set for the record.'
  129.       ));
  130.     }
  131.  
  132.     _.each(s3Q.newRecords, function(record) {
  133.       try {
  134.         record = transformer.serialize(record);
  135.       } catch (e) {
  136.         throw flaverr('E_INVALID_RECORD', new Error(
  137.           'Failed process the values set for the record.\n'+
  138.           'Details:\n'+
  139.           e.message
  140.         ));
  141.       }
  142.     });
  143.  
  144.     return s3Q;
  145.   }
  146.  
  147.  
  148.   //  鈻堚枅鈺   鈻堚枅鈺椻枅鈻堚枅鈻堚枅鈻堚晽 鈻堚枅鈻堚枅鈻堚枅鈺  鈻堚枅鈻堚枅鈻堚晽 鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈺椻枅鈻堚枅鈻堚枅鈻堚枅鈺
  149.   //  鈻堚枅鈺   鈻堚枅鈺戔枅鈻堚晹鈺愨晲鈻堚枅鈺椻枅鈻堚晹鈺愨晲鈻堚枅鈺椻枅鈻堚晹鈺愨晲鈻堚枅鈺椻暁鈺愨晲鈻堚枅鈺斺晲鈺愨暆鈻堚枅鈺斺晲鈺愨晲鈺愨暆
  150.   //  鈻堚枅鈺   鈻堚枅鈺戔枅鈻堚枅鈻堚枅鈻堚晹鈺濃枅鈻堚晳  鈻堚枅鈺戔枅鈻堚枅鈻堚枅鈻堚枅鈺   鈻堚枅鈺   鈻堚枅鈻堚枅鈻堚晽
  151.   //  鈻堚枅鈺   鈻堚枅鈺戔枅鈻堚晹鈺愨晲鈺愨暆 鈻堚枅鈺  鈻堚枅鈺戔枅鈻堚晹鈺愨晲鈻堚枅鈺   鈻堚枅鈺   鈻堚枅鈺斺晲鈺愨暆
  152.   //  鈺氣枅鈻堚枅鈻堚枅鈻堚晹鈺濃枅鈻堚晳     鈻堚枅鈻堚枅鈻堚枅鈺斺暆鈻堚枅鈺  鈻堚枅鈺   鈻堚枅鈺   鈻堚枅鈻堚枅鈻堚枅鈻堚晽
  153.   //   鈺氣晲鈺愨晲鈺愨晲鈺 鈺氣晲鈺     鈺氣晲鈺愨晲鈺愨晲鈺 鈺氣晲鈺  鈺氣晲鈺   鈺氣晲鈺   鈺氣晲鈺愨晲鈺愨晲鈺愨暆
  154.   //
  155.   // For `update` queries, both the values and the criteria need to be run
  156.   // through the transformer.
  157.   if (s3Q.method === 'update') {
  158.     // Validate that there is a `valuesToSet` key on the object
  159.     if (!_.has(s3Q, 'valuesToSet') || !_.isPlainObject(s3Q.valuesToSet)) {
  160.       throw flaverr('E_INVALID_RECORD', new Error(
  161.         'Failed process the values set for the record.'
  162.       ));
  163.     }
  164.  
  165.     // Validate that there is a `criteria` key on the object
  166.     if (!_.has(s3Q, 'criteria') || !_.isPlainObject(s3Q.criteria)) {
  167.       throw flaverr('E_INVALID_RECORD', new Error(
  168.         'Failed process the criteria for the record.'
  169.       ));
  170.     }
  171.  
  172.     // Transform the values into column names
  173.     try {
  174.       s3Q.valuesToSet = transformer.serialize(s3Q.valuesToSet);
  175.     } catch (e) {
  176.       throw flaverr('E_INVALID_RECORD', new Error(
  177.         'Failed process the values set for the record.\n'+
  178.         'Details:\n'+
  179.         e.message
  180.       ));
  181.     }
  182.  
  183.     // Transform the criteria into column names
  184.     try {
  185.       s3Q.criteria.where = transformer.serialize(s3Q.criteria.where);
  186.     } catch (e) {
  187.       throw flaverr('E_INVALID_RECORD', new Error(
  188.         'Failed process the criteria for the record.\n'+
  189.         'Details:\n'+
  190.         e.message
  191.       ));
  192.     }
  193.  
  194.     // Transform sort clauses into column names
  195.     if (!_.isUndefined(s3Q.criteria.sort) && s3Q.criteria.sort.length) {
  196.       s3Q.criteria.sort = _.map(s3Q.criteria.sort, function(sortClause) {
  197.         var sort = {};
  198.         var attrName = _.first(_.keys(sortClause));
  199.         var sortDirection = sortClause[attrName];
  200.         var columnName = model.schema[attrName].columnName;
  201.         sort[columnName] = sortDirection;
  202.         return sort;
  203.       });
  204.     }
  205.  
  206.     // Remove any invalid properties
  207.     delete s3Q.criteria.omit;
  208.     delete s3Q.criteria.select;
  209.  
  210.     return s3Q;
  211.   }
  212.  
  213.  
  214.   //  鈻堚枅鈻堚枅鈻堚枅鈺 鈻堚枅鈻堚枅鈻堚枅鈻堚晽鈻堚枅鈻堚枅鈻堚枅鈻堚晽鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈺椻枅鈻堚枅鈻堚枅鈻堚晽  鈻堚枅鈻堚枅鈻堚枅鈺 鈻堚枅鈺   鈻堚枅鈺
  215.   //  鈻堚枅鈺斺晲鈺愨枅鈻堚晽鈻堚枅鈺斺晲鈺愨晲鈺愨暆鈻堚枅鈺斺晲鈺愨晲鈺愨暆鈺氣晲鈺愨枅鈻堚晹鈺愨晲鈺濃枅鈻堚晹鈺愨晲鈻堚枅鈺椻枅鈻堚晹鈺愨晲鈺愨枅鈻堚晽鈺氣枅鈻堚晽 鈻堚枅鈺斺暆
  216.   //  鈻堚枅鈺  鈻堚枅鈺戔枅鈻堚枅鈻堚枅鈺  鈻堚枅鈻堚枅鈻堚枅鈻堚晽   鈻堚枅鈺   鈻堚枅鈻堚枅鈻堚枅鈺斺暆鈻堚枅鈺   鈻堚枅鈺 鈺氣枅鈻堚枅鈻堚晹鈺
  217.   //  鈻堚枅鈺  鈻堚枅鈺戔枅鈻堚晹鈺愨晲鈺  鈺氣晲鈺愨晲鈺愨枅鈻堚晳   鈻堚枅鈺   鈻堚枅鈺斺晲鈺愨枅鈻堚晽鈻堚枅鈺   鈻堚枅鈺  鈺氣枅鈻堚晹鈺
  218.   //  鈻堚枅鈻堚枅鈻堚枅鈺斺暆鈻堚枅鈻堚枅鈻堚枅鈻堚晽鈻堚枅鈻堚枅鈻堚枅鈻堚晳   鈻堚枅鈺   鈻堚枅鈺  鈻堚枅鈺戔暁鈻堚枅鈻堚枅鈻堚枅鈺斺暆   鈻堚枅鈺
  219.   //  鈺氣晲鈺愨晲鈺愨晲鈺 鈺氣晲鈺愨晲鈺愨晲鈺愨暆鈺氣晲鈺愨晲鈺愨晲鈺愨暆   鈺氣晲鈺   鈺氣晲鈺  鈺氣晲鈺 鈺氣晲鈺愨晲鈺愨晲鈺    鈺氣晲鈺
  220.   //
  221.   // For `destroy` queries, the criteria needs to be run through the transformer.
  222.   if (s3Q.method === 'destroy') {
  223.     // Validate that there is a `criteria` key on the object
  224.     if (!_.has(s3Q, 'criteria') || !_.isPlainObject(s3Q.criteria)) {
  225.       throw flaverr('E_INVALID_RECORD', new Error(
  226.         'Failed process the criteria for the record.'
  227.       ));
  228.     }
  229.  
  230.     // Transform the criteria into column names
  231.     try {
  232.       s3Q.criteria.where = transformer.serialize(s3Q.criteria.where);
  233.     } catch (e) {
  234.       throw flaverr('E_INVALID_RECORD', new Error(
  235.         'Failed process the criteria for the record.\n'+
  236.         'Details:\n'+
  237.         e.message
  238.       ));
  239.     }
  240.  
  241.     // Transform sort clauses into column names
  242.     if (!_.isUndefined(s3Q.criteria.sort) && s3Q.criteria.sort.length) {
  243.       s3Q.criteria.sort = _.map(s3Q.criteria.sort, function(sortClause) {
  244.         var sort = {};
  245.         var attrName = _.first(_.keys(sortClause));
  246.         var sortDirection = sortClause[attrName];
  247.         var columnName = model.schema[attrName].columnName;
  248.         sort[columnName] = sortDirection;
  249.         return sort;
  250.       });
  251.     }
  252.  
  253.     // Remove any invalid properties
  254.     delete s3Q.criteria.omit;
  255.     delete s3Q.criteria.select;
  256.  
  257.     return s3Q;
  258.   }
  259.  
  260.  
  261.   //  鈻堚枅鈻堚枅鈻堚枅鈻堚晽鈻堚枅鈺椻枅鈻堚枅鈺   鈻堚枅鈺椻枅鈻堚枅鈻堚枅鈻堚晽
  262.   //  鈻堚枅鈺斺晲鈺愨晲鈺愨暆鈻堚枅鈺戔枅鈻堚枅鈻堚晽  鈻堚枅鈺戔枅鈻堚晹鈺愨晲鈻堚枅鈺
  263.   //  鈻堚枅鈻堚枅鈻堚晽  鈻堚枅鈺戔枅鈻堚晹鈻堚枅鈺 鈻堚枅鈺戔枅鈻堚晳  鈻堚枅鈺
  264.   //  鈻堚枅鈺斺晲鈺愨暆  鈻堚枅鈺戔枅鈻堚晳鈺氣枅鈻堚晽鈻堚枅鈺戔枅鈻堚晳  鈻堚枅鈺
  265.   //  鈻堚枅鈺     鈻堚枅鈺戔枅鈻堚晳 鈺氣枅鈻堚枅鈻堚晳鈻堚枅鈻堚枅鈻堚枅鈺斺暆
  266.   //  鈺氣晲鈺     鈺氣晲鈺濃暁鈺愨暆  鈺氣晲鈺愨晲鈺濃暁鈺愨晲鈺愨晲鈺愨暆
  267.   //
  268.   // Build join instructions and transform criteria to column names.
  269.   if (s3Q.method === 'find' || s3Q.method === 'findOne') {
  270.     s3Q.method = 'find';
  271.  
  272.     //  鈺斺晽 鈺 鈺︹暒鈺  鈺斺暒鈺   鈹攲鈹€鈹愨敩鈹屸攼鈹  鈹攲鈹愨攲鈹屸攢鈹愨攲鈹攼鈹攢鈹愨敩 鈹攲鈹€鈹愨攲鈹攼鈹攲鈹€鈹愨攲鈹愨攲鈹屸攢鈹
  273.     //  鈺犫暕鈺椻晳 鈺戔晳鈺   鈺戔晳   鈹傗攤 鈹傗攤鈹傗攤鈹  鈹傗攤鈹傗攤鈹斺攢鈹 鈹 鈹溾敩鈹樷攤 鈹傗攤   鈹 鈹傗攤 鈹傗攤鈹傗攤鈹斺攢鈹
  274.     //  鈺氣晲鈺濃暁鈺愨暆鈺┾暕鈺愨暆鈺愨暕鈺  鈹斺敇鈹斺攢鈹樷敶鈹樷敂鈹  鈹粹敇鈹斺敇鈹斺攢鈹 鈹 鈹粹敂鈹€鈹斺攢鈹樷敂鈹€鈹 鈹 鈹粹敂鈹€鈹樷敇鈹斺敇鈹斺攢鈹
  275.     // Build the JOIN logic for the population
  276.     var joins = [];
  277.     _.each(s3Q.populates, function(populateCriteria, populateAttribute) {
  278.       // If the populationCriteria is a boolean, make sure it's not a falsy value.
  279.       if (!populateCriteria) {
  280.         return;
  281.       }
  282.  
  283.       if (_.isPlainObject(populateCriteria) && !_.keys(populateCriteria).length) {
  284.         return;
  285.       }
  286.  
  287.       // If the populate criteria is a truthy boolean, expand it out to {}
  288.       if (_.isBoolean(populateCriteria)) {
  289.         populateCriteria = {};
  290.       }
  291.  
  292.       try {
  293.         // Find the normalized schema value for the populated attribute
  294.         var attribute = model.attributes[populateAttribute];
  295.         var schemaAttribute = model.schema[populateAttribute];
  296.  
  297.         var attributeName = populateAttribute;
  298.         if (!attribute) {
  299.           throw new Error('In ' + util.format('`.populate("%s")`', populateAttribute) + ', attempting to populate an attribute that doesn\'t exist');
  300.         }
  301.  
  302.         if (_.has(attribute, 'columnName')) {
  303.           attributeName = attribute.columnName;
  304.         }
  305.  
  306.         // Grab the key being populated from the original model definition to check
  307.         // if it is a has many or belongs to. If it's a belongs_to the adapter needs
  308.         // to know that it should replace the foreign key with the associated value.
  309.         var parentAttr = originalModels[identity].schema[attributeName];
  310.  
  311.         // Build the initial join object that will link this collection to either another collection
  312.         // or to a junction table.
  313.         var join = {
  314.           parentCollectionIdentity: identity,
  315.           parent: s3Q.using,
  316.           parentAlias: s3Q.using + '__' + populateAttribute,
  317.           // For singular associations, the populated attribute will have a schema (since it represents
  318.           // a real column).  For plural associations, we'll use the primary key column of the parent table.
  319.           parentKey: schemaAttribute.columnName || model.schema[modelPrimaryKey].columnName,
  320.           childCollectionIdentity: parentAttr.referenceIdentity,
  321.           child: parentAttr.references,
  322.           childAlias: parentAttr.references + '__' + populateAttribute,
  323.           childKey: parentAttr.on,
  324.           alias: populateAttribute,
  325.           removeParentKey: !!parentAttr.foreignKey,
  326.           model: !!_.has(parentAttr, 'model'),
  327.           collection: !!_.has(parentAttr, 'collection'),
  328.           criteria: _.clone(populateCriteria)
  329.         };
  330.  
  331.         // Build select object to use in the integrator
  332.         var select = [];
  333.         var customSelect = populateCriteria.select && _.isArray(populateCriteria.select);
  334.  
  335.         // Expand out the `*` criteria
  336.         if (customSelect && populateCriteria.select.length === 1 && _.first(populateCriteria.select) === '*') {
  337.           customSelect = false;
  338.         }
  339.  
  340.         _.each(originalModels[parentAttr.referenceIdentity].schema, function(val, key) {
  341.           // Ignore virtual attributes
  342.           if(_.has(val, 'collection')) {
  343.             return;
  344.           }
  345.  
  346.           // Check if the user has defined a custom select
  347.           if(customSelect && !_.includes(populateCriteria.select, key)) {
  348.             return;
  349.           }
  350.  
  351.           // Add the key to the select
  352.           select.push(key);
  353.         });
  354.  
  355.         // Ensure the primary key and foreign key on the child are always selected.
  356.         // otherwise things like the integrator won't work correctly
  357.         var childPk = originalModels[parentAttr.referenceIdentity].primaryKey;
  358.         select.push(childPk);
  359.  
  360.         // Add the foreign key for collections so records can be turned into nested
  361.         // objects.
  362.         if (join.collection) {
  363.           select.push(parentAttr.on);
  364.         }
  365.  
  366.         // Make sure the join's select is unique
  367.         join.criteria.select = _.uniq(select);
  368.  
  369.         // Apply any omits to the selected attributes
  370.         if (populateCriteria.omit && _.isArray(populateCriteria.omit) && populateCriteria.omit.length) {
  371.           _.each(populateCriteria.omit, function(omitValue) {
  372.             _.pull(join.criteria.select, omitValue);
  373.           });
  374.         }
  375.  
  376.         // Remove omit from populate criteria
  377.         delete populateCriteria.omit;
  378.  
  379.         // Find the schema of the model the attribute references
  380.         var referencedSchema = originalModels[parentAttr.referenceIdentity];
  381.         var reference = null;
  382.  
  383.         // If linking to a junction table, the attributes shouldn't be included in the return value
  384.         if (referencedSchema.junctionTable) {
  385.           join.select = false;
  386.           reference = _.find(referencedSchema.schema, function(referencedAttribute) {
  387.             return referencedAttribute.references && referencedAttribute.columnName !== schemaAttribute.on;
  388.           });
  389.         }
  390.         // If it's a through table, treat it the same way as a junction table for now
  391.         else if (referencedSchema.throughTable && referencedSchema.throughTable[identity + '.' + populateAttribute]) {
  392.           join.select = false;
  393.           reference = referencedSchema.schema[referencedSchema.throughTable[identity + '.' + populateAttribute]];
  394.         }
  395.  
  396.         // Add the first join
  397.         joins.push(join);
  398.  
  399.         // If a junction table is used, add an additional join to get the data
  400.         if (reference && _.has(schemaAttribute, 'on')) {
  401.           var selects = [];
  402.           _.each(originalModels[reference.referenceIdentity].schema, function(val, key) {
  403.             // Ignore virtual attributes
  404.             if(_.has(val, 'collection')) {
  405.               return;
  406.             }
  407.  
  408.             // Check if the user has defined a custom select and if so normalize it
  409.             if(customSelect && !_.includes(populateCriteria.select, key)) {
  410.               return;
  411.             }
  412.  
  413.             // Add the value to the select
  414.             selects.push(key);
  415.           });
  416.  
  417.           // Apply any omits to the selected attributes
  418.           if (populateCriteria.omit && populateCriteria.omit.length) {
  419.             _.each(populateCriteria.omit, function(omitValue) {
  420.               _.pull(selects, omitValue);
  421.             });
  422.           }
  423.  
  424.           // Ensure the primary key and foreign are always selected. Otherwise things like the
  425.           // integrator won't work correctly
  426.           childPk = originalModels[reference.referenceIdentity].primaryKey;
  427.           selects.push(childPk);
  428.  
  429.           join = {
  430.             parentCollectionIdentity: schemaAttribute.referenceIdentity,
  431.             parent: schemaAttribute.references,
  432.             parentAlias: schemaAttribute.references + '__' + populateAttribute,
  433.             parentKey: reference.columnName,
  434.             childCollectionIdentity: reference.referenceIdentity,
  435.             child: reference.references,
  436.             childAlias: reference.references + '__' + populateAttribute,
  437.             childKey: reference.on,
  438.             alias: populateAttribute,
  439.             junctionTable: true,
  440.             removeParentKey: !!parentAttr.foreignKey,
  441.             model: false,
  442.             collection: true,
  443.             criteria: _.clone(populateCriteria)
  444.           };
  445.  
  446.           join.criteria.select = _.uniq(selects);
  447.  
  448.           joins.push(join);
  449.         }
  450.  
  451.         // Append the criteria to the correct join if available
  452.         if (populateCriteria && joins.length > 1) {
  453.           joins[1].criteria = _.extend({}, joins[1].criteria);
  454.           delete joins[0].criteria;
  455.         } else if (populateCriteria) {
  456.           joins[0].criteria = _.extend({}, joins[0].criteria);
  457.         }
  458.  
  459.         // Set the criteria joins
  460.         s3Q.joins = s3Q.joins || [];
  461.         s3Q.joins = s3Q.joins.concat(joins);
  462.  
  463.         // Clear out the joins
  464.         joins = [];
  465.  
  466.       } catch (e) {
  467.         throw new Error(
  468.           'Encountered unexpected error while building join instructions for ' +
  469.           util.format('`.populate("%s")`', populateAttribute) +
  470.           '\nDetails:\n' +
  471.           util.inspect(e, false, null)
  472.         );
  473.       }
  474.     }); // </ .each loop >
  475.  
  476.     // Replace populates on the s3Q with joins
  477.     delete s3Q.populates;
  478.  
  479.     // Ensure a joins array exists
  480.     if (!_.has(s3Q, 'joins')) {
  481.       s3Q.joins = [];
  482.     }
  483.  
  484.  
  485.     //  鈺斺晲鈺椻晹鈺愨晽鈺斺暒鈺椻暒 鈺︹晹鈺愨晽  鈹屸攢鈹愨敩鈹€鈹愨攲鈹€鈹 鈹攲鈹€鈹愨攲鈹€鈹愨攲鈹攼鈹攲鈹€鈹愨攲鈹愨攲鈹屸攢鈹
  486.     //  鈺氣晲鈺椻晳鈺  鈺 鈺 鈺戔暊鈺愨暆  鈹溾攢鈹樷敎鈹敇鈹 鈹 鈹傗敎鈹 鈹   鈹 鈹傗攤 鈹傗攤鈹傗攤鈹斺攢鈹
  487.     //  鈺氣晲鈺濃暁鈺愨暆 鈺 鈺氣晲鈺濃暕    鈹  鈹粹敂鈹€鈹斺攢鈹樷敂鈹樷敂鈹€鈹樷敂鈹€鈹 鈹 鈹粹敂鈹€鈹樷敇鈹斺敇鈹斺攢鈹
  488.  
  489.     // If the model's hasSchema value is set to false, remove the select
  490.     if (model.hasSchema === false) {
  491.       s3Q.criteria.select = undefined;
  492.     }
  493.  
  494.     // If a select clause is being used, ensure that the primary key of the model
  495.     // is included. The primary key is always required in Waterline for further
  496.     // processing needs.
  497.     if (s3Q.criteria.select && _.indexOf(s3Q.criteria.select, '*') < 0) {
  498.       s3Q.criteria.select.push(model.primaryKey);
  499.  
  500.       // Just an additional check after modifying the select to ensure it only
  501.       // contains unique values
  502.       s3Q.criteria.select = _.uniq(s3Q.criteria.select);
  503.     }
  504.  
  505.     // If no criteria is selected, expand out the SELECT statement for adapters. This
  506.     // makes it much easier to work with and to dynamically modify the select statement
  507.     // to alias values as needed when working with populates.
  508.     if (s3Q.criteria.select && _.indexOf(s3Q.criteria.select, '*') > -1) {
  509.       var selectedKeys = [];
  510.       _.each(model.attributes, function(val, key) {
  511.         if (!_.has(val, 'collection')) {
  512.           selectedKeys.push(key);
  513.         }
  514.       });
  515.  
  516.       s3Q.criteria.select = _.uniq(selectedKeys);
  517.     }
  518.  
  519.     // Apply any omits to the selected attributes
  520.     if (s3Q.criteria.select && s3Q.criteria.omit.length && s3Q.criteria.select.length) {
  521.       _.each(s3Q.criteria.omit, function(omitValue) {
  522.         _.pull(s3Q.criteria.select, omitValue);
  523.       });
  524.     }
  525.  
  526.     // Transform projections into column names
  527.     if (s3Q.criteria.select) {
  528.       s3Q.criteria.select = _.map(s3Q.criteria.select, function(attrName) {
  529.         return model.schema[attrName].columnName;
  530.       });
  531.     }
  532.  
  533.     // Transform sort clauses into column names
  534.     if (!_.isUndefined(s3Q.criteria.sort) && s3Q.criteria.sort.length) {
  535.       s3Q.criteria.sort = _.map(s3Q.criteria.sort, function(sortClause) {
  536.         var sort = {};
  537.         var attrName = _.first(_.keys(sortClause));
  538.         var sortDirection = sortClause[attrName];
  539.         var columnName = model.schema[attrName].columnName;
  540.         sort[columnName] = sortDirection;
  541.         return sort;
  542.       });
  543.     }
  544.  
  545.     // Transform the criteria into column names
  546.     try {
  547.       s3Q.criteria.where = transformer.serialize(s3Q.criteria.where);
  548.     } catch (e) {
  549.       throw flaverr('E_INVALID_RECORD', new Error(
  550.         'Failed process the criteria for the record.\n'+
  551.         'Details:\n'+
  552.         e.message
  553.       ));
  554.     }
  555.  
  556.     // Transform any populate where clauses to use the correct columnName values
  557.     if (s3Q.joins.length) {
  558.       var lastJoin = _.last(s3Q.joins);
  559.       var joinCollection = originalModels[lastJoin.childCollectionIdentity];
  560.  
  561.       // Ensure a join criteria exists
  562.       lastJoin.criteria = lastJoin.criteria || {};
  563.       lastJoin.criteria = joinCollection._transformer.serialize(lastJoin.criteria);
  564.  
  565.       // Ensure the join select doesn't contain duplicates
  566.       lastJoin.criteria.select = _.uniq(lastJoin.criteria.select);
  567.       delete lastJoin.select;
  568.     }
  569.  
  570.     // Remove any invalid properties
  571.     delete s3Q.criteria.omit;
  572.  
  573.     return s3Q;
  574.   }
  575.  
  576.  
  577.   //   鈻堚枅鈻堚枅鈻堚晽  鈻堚枅鈻堚枅鈻堚枅鈺  鈻堚枅鈻堚枅鈻堚枅鈺 鈻堚枅鈻堚枅鈻堚枅鈺 鈻堚枅鈻堚枅鈻堚枅鈻堚晽 鈻堚枅鈻堚枅鈻堚枅鈺  鈻堚枅鈻堚枅鈻堚晽 鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈺椻枅鈻堚晽 鈻堚枅鈻堚枅鈻堚枅鈺 鈻堚枅鈻堚晽   鈻堚枅鈺椻枅鈻堚枅鈻堚枅鈻堚枅鈺
  578.   //  鈻堚枅鈺斺晲鈺愨枅鈻堚晽鈻堚枅鈺斺晲鈺愨晲鈺愨暆 鈻堚枅鈺斺晲鈺愨晲鈺愨暆 鈻堚枅鈺斺晲鈺愨枅鈻堚晽鈻堚枅鈺斺晲鈺愨晲鈺愨暆鈻堚枅鈺斺晲鈺愨晲鈺愨暆 鈻堚枅鈺斺晲鈺愨枅鈻堚晽鈺氣晲鈺愨枅鈻堚晹鈺愨晲鈺濃枅鈻堚晳鈻堚枅鈺斺晲鈺愨晲鈻堚枅鈺椻枅鈻堚枅鈻堚晽  鈻堚枅鈺戔枅鈻堚晹鈺愨晲鈺愨晲鈺
  579.   //  鈻堚枅鈻堚枅鈻堚枅鈻堚晳鈻堚枅鈺  鈻堚枅鈻堚晽鈻堚枅鈺  鈻堚枅鈻堚晽鈻堚枅鈻堚枅鈻堚枅鈺斺暆鈻堚枅鈻堚枅鈻堚晽  鈻堚枅鈺  鈻堚枅鈻堚晽鈻堚枅鈻堚枅鈻堚枅鈻堚晳   鈻堚枅鈺   鈻堚枅鈺戔枅鈻堚晳   鈻堚枅鈺戔枅鈻堚晹鈻堚枅鈺 鈻堚枅鈺戔枅鈻堚枅鈻堚枅鈻堚枅鈺
  580.   //  鈻堚枅鈺斺晲鈺愨枅鈻堚晳鈻堚枅鈺   鈻堚枅鈺戔枅鈻堚晳   鈻堚枅鈺戔枅鈻堚晹鈺愨晲鈻堚枅鈺椻枅鈻堚晹鈺愨晲鈺  鈻堚枅鈺   鈻堚枅鈺戔枅鈻堚晹鈺愨晲鈻堚枅鈺   鈻堚枅鈺   鈻堚枅鈺戔枅鈻堚晳   鈻堚枅鈺戔枅鈻堚晳鈺氣枅鈻堚晽鈻堚枅鈺戔暁鈺愨晲鈺愨晲鈻堚枅鈺
  581.   //  鈻堚枅鈺  鈻堚枅鈺戔暁鈻堚枅鈻堚枅鈻堚枅鈺斺暆鈺氣枅鈻堚枅鈻堚枅鈻堚晹鈺濃枅鈻堚晳  鈻堚枅鈺戔枅鈻堚枅鈻堚枅鈻堚枅鈺椻暁鈻堚枅鈻堚枅鈻堚枅鈺斺暆鈻堚枅鈺  鈻堚枅鈺   鈻堚枅鈺   鈻堚枅鈺戔暁鈻堚枅鈻堚枅鈻堚枅鈺斺暆鈻堚枅鈺 鈺氣枅鈻堚枅鈻堚晳鈻堚枅鈻堚枅鈻堚枅鈻堚晳
  582.   //  鈺氣晲鈺  鈺氣晲鈺 鈺氣晲鈺愨晲鈺愨晲鈺  鈺氣晲鈺愨晲鈺愨晲鈺 鈺氣晲鈺  鈺氣晲鈺濃暁鈺愨晲鈺愨晲鈺愨晲鈺 鈺氣晲鈺愨晲鈺愨晲鈺 鈺氣晲鈺  鈺氣晲鈺   鈺氣晲鈺   鈺氣晲鈺 鈺氣晲鈺愨晲鈺愨晲鈺 鈺氣晲鈺  鈺氣晲鈺愨晲鈺濃暁鈺愨晲鈺愨晲鈺愨晲鈺
  583.   //
  584.   // For `avg` and `sum` queries, the criteria needs to be run through the transformer.
  585.   if (s3Q.method === 'avg' || s3Q.method === 'sum' || s3Q.method === 'count') {
  586.     // Validate that there is a `criteria` key on the object
  587.     if (!_.has(s3Q, 'criteria') || !_.isPlainObject(s3Q.criteria)) {
  588.       throw flaverr('E_INVALID_RECORD', new Error(
  589.         'Failed process the criteria for the record.'
  590.       ));
  591.     }
  592.  
  593.     // Transform the criteria into column names
  594.     try {
  595.       s3Q.criteria = transformer.serialize(s3Q.criteria);
  596.     } catch (e) {
  597.       throw flaverr('E_INVALID_RECORD', new Error(
  598.         'Failed process the criteria for the record.\n'+
  599.         'Details:\n'+
  600.         e.message
  601.       ));
  602.     }
  603.  
  604.     // Transform the numericAttrName into column names using a nasty hack.
  605.     try {
  606.       var _tmpNumbericAttr = {};
  607.       _tmpNumbericAttr[s3Q.numericAttrName] = '';
  608.       var processedNumericAttrName = transformer.serialize(_tmpNumbericAttr);
  609.       s3Q.numericAttrName = _.first(_.keys(processedNumericAttrName));
  610.     } catch (e) {
  611.       throw flaverr('E_INVALID_RECORD', new Error(
  612.         'Failed process the criteria for the record.\n'+
  613.         'Details:\n'+
  614.         e.message
  615.       ));
  616.     }
  617.  
  618.     // Remove any invalid properties
  619.     delete s3Q.criteria.omit;
  620.     delete s3Q.criteria.select;
  621.     delete s3Q.criteria.where.populates;
  622.  
  623.     if (s3Q.method === 'count') {
  624.       delete s3Q.criteria.skip;
  625.       delete s3Q.criteria.sort;
  626.       delete s3Q.criteria.limit;
  627.     }
  628.  
  629.     return s3Q;
  630.   }
  631.  
  632.  
  633.   // If the method wasn't recognized, throw an error
  634.   throw flaverr('E_INVALID_QUERY', new Error(
  635.     'Invalid query method set - `' + s3Q.method + '`.'
  636.   ));
  637. };
  638.  
downloadforge-stage-three-query.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
GoSublime - A Golang plugin collection for SublimeText **3**, ... 2017-02-19
awesome-emacs - A community driven list of useful Emacs packages, ... 2017-02-19
RKNotificationHub - Make any UIView a full fledged notification center 2017-02-19
vimr - Project VimR - Refined Neovim experience for macOS... 2017-02-19
vue-admin - Vue Admin Panel Framework, Powered by Vue 2.0 and ... 2017-02-19
dev-setup - Mac OS X development environment setup: Easy-to-un... 2017-02-19
CMPopTipView - Custom UIView for iOS that pops up an animated "bu... 2017-02-19
git-recipes - Git for recipes in Chinese. The high quality of Gi... 2017-02-19
CLNDR - 2017-02-19
OptiKey - OptiKey - Full computer control and speech with yo... 2017-02-19

 Back to top