destroy.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // ██████╗ ███████╗███████╗████████╗██████╗ ██████╗ ██╗ ██╗
  2. // ██╔══██╗██╔════╝██╔════╝╚══██╔══╝██╔══██╗██╔═══██╗╚██╗ ██╔╝
  3. // ██║ ██║█████╗ ███████╗ ██║ ██████╔╝██║ ██║ ╚████╔╝
  4. // ██║ ██║██╔══╝ ╚════██║ ██║ ██╔══██╗██║ ██║ ╚██╔╝
  5. // ██████╔╝███████╗███████║ ██║ ██║ ██║╚██████╔╝ ██║
  6. // ╚═════╝ ╚══════╝╚══════╝ ╚═╝ ╚═╝ ╚═╝ ╚═════╝ ╚═╝
  7. //
  8. // Destroy the record(s) and return the values that were destroyed if needed.
  9. // If a fetch was performed, first the records need to be searched for with the
  10. // primary key selected.
  11. var _ = require('@sailshq/lodash');
  12. var runQuery = require('./run-query');
  13. var compileStatement = require('./compile-statement');
  14. module.exports = function insertRecord(options, cb) {
  15. // ╦ ╦╔═╗╦ ╦╔╦╗╔═╗╔╦╗╔═╗ ┌─┐┌─┐┌┬┐┬┌─┐┌┐┌┌─┐
  16. // ╚╗╔╝╠═╣║ ║ ║║╠═╣ ║ ║╣ │ │├─┘ │ ││ ││││└─┐
  17. // ╚╝ ╩ ╩╩═╝╩═╩╝╩ ╩ ╩ ╚═╝ └─┘┴ ┴ ┴└─┘┘└┘└─┘
  18. if (_.isUndefined(options) || !_.isPlainObject(options)) {
  19. throw new Error('Invalid options argument. Options must contain: connection, statement, fetch, and primaryKey.');
  20. }
  21. if (!_.has(options, 'connection') || !_.isObject(options.connection)) {
  22. throw new Error('Invalid option used in options argument. Missing or invalid connection.');
  23. }
  24. if (!_.has(options, 'statement') || !_.isPlainObject(options.statement)) {
  25. throw new Error('Invalid option used in options argument. Missing or invalid statement.');
  26. }
  27. if (!_.has(options, 'primaryKey') || !_.isString(options.primaryKey)) {
  28. throw new Error('Invalid option used in options argument. Missing or invalid primaryKey.');
  29. }
  30. if (!_.has(options, 'fetch') || !_.isBoolean(options.fetch)) {
  31. throw new Error('Invalid option used in options argument. Missing or invalid fetch flag.');
  32. }
  33. // ╔═╗╔═╗╔╦╗ ┬─┐┌─┐┌─┐┌─┐┬─┐┌┬┐┌─┐ ┌┐ ┌─┐┬┌┐┌┌─┐ ┌┬┐┌─┐┌─┐┌┬┐┬─┐┌─┐┬ ┬┌─┐┌┬┐
  34. // ║ ╦║╣ ║ ├┬┘├┤ │ │ │├┬┘ ││└─┐ ├┴┐├┤ │││││ ┬ ││├┤ └─┐ │ ├┬┘│ │└┬┘├┤ ││
  35. // ╚═╝╚═╝ ╩ ┴└─└─┘└─┘└─┘┴└──┴┘└─┘ └─┘└─┘┴┘└┘└─┘ ─┴┘└─┘└─┘ ┴ ┴└─└─┘ ┴ └─┘─┴┘
  36. // If a fetch is used, the records that will be destroyed need to be found first.
  37. // This is because in order to (semi) accurately return the records that were
  38. // destroyed in MySQL first they need to be found, then destroyed.
  39. (function getRecordsToDestroy(proceed) {
  40. // Only look up the records if fetch was used
  41. if (!options.fetch) {
  42. return proceed();
  43. }
  44. // Otherwise build up a select query
  45. var fetchStatement = {
  46. from: options.statement.from,
  47. where: options.statement.where
  48. };
  49. // ╔═╗╔═╗╔╦╗╔═╗╦╦ ╔═╗ ┌─┐ ┬ ┬┌─┐┬─┐┬ ┬
  50. // ║ ║ ║║║║╠═╝║║ ║╣ │─┼┐│ │├┤ ├┬┘└┬┘
  51. // ╚═╝╚═╝╩ ╩╩ ╩╩═╝╚═╝ └─┘└└─┘└─┘┴└─ ┴
  52. // Compile the statement into a native query.
  53. var compiledFetchQuery;
  54. try {
  55. compiledFetchQuery = compileStatement(fetchStatement);
  56. } catch (e) {
  57. // If the statement could not be compiled, return an error.
  58. return proceed(e);
  59. }
  60. // ╦═╗╦ ╦╔╗╔ ┌─┐ ┬ ┬┌─┐┬─┐┬ ┬
  61. // ╠╦╝║ ║║║║ │─┼┐│ │├┤ ├┬┘└┬┘
  62. // ╩╚═╚═╝╝╚╝ └─┘└└─┘└─┘┴└─ ┴
  63. // Run the initial find query
  64. runQuery({
  65. connection: options.connection,
  66. nativeQuery: compiledFetchQuery.nativeQuery,
  67. valuesToEscape: compiledFetchQuery.valuesToEscape,
  68. meta: compiledFetchQuery.meta,
  69. disconnectOnError: false,
  70. queryType: 'select'
  71. },
  72. function runQueryCb(err, report) {
  73. if (err) {
  74. return proceed(err);
  75. }
  76. return proceed(undefined, report);
  77. });
  78. })(function afterInitialFetchCb(err, selectReport) {
  79. if (err) {
  80. return cb(err);
  81. }
  82. // ╔═╗╔═╗╔╦╗╔═╗╦╦ ╔═╗ ┌─┐ ┬ ┬┌─┐┬─┐┬ ┬
  83. // ║ ║ ║║║║╠═╝║║ ║╣ │─┼┐│ │├┤ ├┬┘└┬┘
  84. // ╚═╝╚═╝╩ ╩╩ ╩╩═╝╚═╝ └─┘└└─┘└─┘┴└─ ┴
  85. // Compile the destroy statement into a native query.
  86. var compiledUpdateQuery;
  87. try {
  88. compiledUpdateQuery = compileStatement(options.statement);
  89. } catch (e) {
  90. // If the statement could not be compiled, return an error.
  91. return cb(e);
  92. }
  93. // ╦═╗╦ ╦╔╗╔ ┌─┐ ┬ ┬┌─┐┬─┐┬ ┬
  94. // ╠╦╝║ ║║║║ │─┼┐│ │├┤ ├┬┘└┬┘
  95. // ╩╚═╚═╝╝╚╝ └─┘└└─┘└─┘┴└─ ┴
  96. // Run the destroy query
  97. runQuery({
  98. connection: options.connection,
  99. nativeQuery: compiledUpdateQuery.nativeQuery,
  100. valuesToEscape: compiledUpdateQuery.valuesToEscape,
  101. meta: compiledUpdateQuery.meta,
  102. disconnectOnError: false,
  103. queryType: 'destroy'
  104. },
  105. function runQueryCb(err, report) {
  106. if (err) {
  107. return cb(err);
  108. }
  109. // If no fetch was used, then nothing else needs to be done.
  110. if (!options.fetch) {
  111. return cb(undefined, report.result);
  112. }
  113. // Otherwise, return the selected records
  114. return cb(undefined, selectReport.result);
  115. });
  116. });
  117. };