drop.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // ██████╗ ██████╗ ██████╗ ██████╗
  2. // ██╔══██╗██╔══██╗██╔═══██╗██╔══██╗
  3. // ██║ ██║██████╔╝██║ ██║██████╔╝
  4. // ██║ ██║██╔══██╗██║ ██║██╔═══╝
  5. // ██████╔╝██║ ██║╚██████╔╝██║
  6. // ╚═════╝ ╚═╝ ╚═╝ ╚═════╝ ╚═╝
  7. //
  8. module.exports = require('machine').build({
  9. friendlyName: 'Drop',
  10. description: 'Remove a table from the database.',
  11. inputs: {
  12. datastore: {
  13. description: 'The datastore to use for connections.',
  14. extendedDescription: 'Datastores represent the config and manager required to obtain an active database connection.',
  15. required: true,
  16. example: '==='
  17. },
  18. tableName: {
  19. description: 'The name of the table to destroy.',
  20. required: true,
  21. example: 'users'
  22. },
  23. meta: {
  24. friendlyName: 'Meta (custom)',
  25. description: 'Additional stuff to pass to the driver.',
  26. extendedDescription: 'This is reserved for custom driver-specific extensions.',
  27. example: '==='
  28. }
  29. },
  30. exits: {
  31. success: {
  32. description: 'The table was destroyed successfully.'
  33. },
  34. badConnection: {
  35. friendlyName: 'Bad connection',
  36. description: 'A connection either could not be obtained or there was an error using the connection.'
  37. }
  38. },
  39. fn: function drop(inputs, exits) {
  40. // Dependencies
  41. var _ = require('@sailshq/lodash');
  42. var Helpers = require('./private');
  43. // Set a flag if a leased connection from outside the adapter was used or not.
  44. var leased = _.has(inputs.meta, 'leasedConnection');
  45. // ╔═╗╔═╗╔═╗╦ ╦╔╗╔ ┌─┐┌─┐┌┐┌┌┐┌┌─┐┌─┐┌┬┐┬┌─┐┌┐┌
  46. // ╚═╗╠═╝╠═╣║║║║║║ │ │ │││││││├┤ │ │ ││ ││││
  47. // ╚═╝╩ ╩ ╩╚╩╝╝╚╝ └─┘└─┘┘└┘┘└┘└─┘└─┘ ┴ ┴└─┘┘└┘
  48. // Spawn a new connection to run the queries on.
  49. Helpers.connection.spawnOrLeaseConnection(inputs.datastore, inputs.meta, function spawnConnectionCb(err, connection) {
  50. if (err) {
  51. return exits.badConnection(err);
  52. }
  53. // ╔═╗╔═╗╔═╗╔═╗╔═╗╔═╗ ┌┬┐┌─┐┌┐ ┬ ┌─┐ ┌┐┌┌─┐┌┬┐┌─┐
  54. // ║╣ ╚═╗║ ╠═╣╠═╝║╣ │ ├─┤├┴┐│ ├┤ │││├─┤│││├┤
  55. // ╚═╝╚═╝╚═╝╩ ╩╩ ╚═╝ ┴ ┴ ┴└─┘┴─┘└─┘ ┘└┘┴ ┴┴ ┴└─┘
  56. var tableName;
  57. try {
  58. tableName = Helpers.schema.escapeTableName(inputs.tableName);
  59. } catch (e) {
  60. // Release the connection on error
  61. Helpers.connection.releaseConnection(connection, leased, function releaseConnectionCb() {
  62. return exits.error(e);
  63. });
  64. return;
  65. }
  66. // Build native query
  67. var query = 'DROP TABLE IF EXISTS ' + tableName + ';';
  68. // ╦═╗╦ ╦╔╗╔ ┌┬┐┬─┐┌─┐┌─┐ ┌─┐ ┬ ┬┌─┐┬─┐┬ ┬
  69. // ╠╦╝║ ║║║║ ││├┬┘│ │├─┘ │─┼┐│ │├┤ ├┬┘└┬┘
  70. // ╩╚═╚═╝╝╚╝ ─┴┘┴└─└─┘┴ └─┘└└─┘└─┘┴└─ ┴
  71. Helpers.query.runNativeQuery(connection, query, [], undefined, function runNativeQueryCb(err) {
  72. // Always release the connection back to the pool
  73. Helpers.connection.releaseConnection(connection, leased, function releaseConnectionCb() {
  74. if (err) {
  75. return exits.error(err);
  76. }
  77. return exits.success();
  78. }); // </ releaseConnection >
  79. }); // </ runNativeQuery >
  80. }); // </ spawnConnection >
  81. }
  82. });