parse-native-query-result.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // Dependencies
  2. var _ = require('@sailshq/lodash');
  3. module.exports = {
  4. friendlyName: 'Parse native query result',
  5. description: 'Parse a raw result from a native query and normalize it for the specified query type.',
  6. sideEffects: 'cacheable',
  7. sync: true,
  8. inputs: {
  9. queryType: {
  10. description: 'The type of query operation this raw result came from.',
  11. moreInfoUrl: 'https://github.com/node-machine/waterline-driver-interface#query-results',
  12. extendedDescription:
  13. 'Either "select", "insert", "destroy", "update", "count", "sum", or "avg". ' +
  14. 'This determines how the provided raw result will be parsed/coerced.',
  15. required: true,
  16. example: '==='
  17. // example: 'select'
  18. },
  19. nativeQueryResult: {
  20. description: 'The result data sent back from the the database as a result of a native query.',
  21. extendedDescription: 'Specifically, be sure to use the `result` property of the output report from a successful native query (i.e. don\'t include `meta`!) The data provided will be coerced to a JSON-serializable value if it isn\'t one already (see [rttc.dehydrate()](https://github.com/node-machine/rttc#dehydratevalue-allownullfalse-dontstringifyfunctionsfalse)). That means any Date instances therein will be converted to timezone-agnostic ISO timestamp strings (i.e. JSON timestamps).',
  22. required: true,
  23. example: '==='
  24. },
  25. meta: {
  26. friendlyName: 'Meta (custom)',
  27. description: 'Additional stuff to pass to the driver.',
  28. extendedDescription: 'This is reserved for custom driver-specific extensions. Please refer to the documentation for the driver you are using for more specific information.',
  29. example: '==='
  30. }
  31. },
  32. exits: {
  33. success: {
  34. description: 'The result was successfully normalized.',
  35. outputVariableName: 'report',
  36. outputDescription: 'The `result` property is the normalized version of the raw result originally provided. The `meta` property is reserved for custom driver-specific extensions.',
  37. outputExample: '==='
  38. // example: {
  39. // result: '*',
  40. // meta: '==='
  41. // }
  42. },
  43. },
  44. fn: function parseNativeQueryResult(inputs, exits) {
  45. var normalizedResult;
  46. switch (inputs.queryType) {
  47. case 'select':
  48. normalizedResult = inputs.nativeQueryResult.rows;
  49. break;
  50. case 'insert':
  51. normalizedResult = {
  52. inserted: inputs.nativeQueryResult.insertId
  53. };
  54. break;
  55. case 'update':
  56. normalizedResult = {
  57. numRecordsUpdated: inputs.nativeQueryResult.affectedRows
  58. };
  59. break;
  60. case 'delete':
  61. normalizedResult = {
  62. numRecordsDeleted: inputs.nativeQueryResult.affectedRows
  63. };
  64. break;
  65. case 'avg':
  66. var avgResult = _.first(inputs.nativeQueryResult.rows);
  67. var avgResultKey = _.first(_.keys(avgResult));
  68. var avg = inputs.nativeQueryResult.rows[0][avgResultKey];
  69. normalizedResult = Number(avg);
  70. break;
  71. case 'sum':
  72. var sumResult = _.first(inputs.nativeQueryResult.rows);
  73. var sumResultKey = _.first(_.keys(sumResult));
  74. var sum = inputs.nativeQueryResult.rows[0][sumResultKey];
  75. normalizedResult = Number(sum);
  76. break;
  77. case 'count':
  78. var countResult = _.first(inputs.nativeQueryResult.rows);
  79. var countResultKey = _.first(_.keys(countResult));
  80. var count = inputs.nativeQueryResult.rows[0][countResultKey];
  81. normalizedResult = Number(count);
  82. break;
  83. default:
  84. }
  85. return exits.success({
  86. result: normalizedResult,
  87. meta: inputs.meta
  88. });
  89. }
  90. };