send-native-query.test.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. var assert = require('assert');
  2. var _ = require('@sailshq/lodash');
  3. var Pack = require('../../');
  4. describe('Queryable ::', function() {
  5. describe('Send Native Query', function() {
  6. var manager;
  7. var connection;
  8. // Create a manager and connection
  9. before(function(done) {
  10. // Needed to dynamically get the host using the docker container
  11. var host = process.env.MYSQL_PORT_3306_TCP_ADDR || 'localhost';
  12. Pack.createManager({
  13. connectionString: 'mysql://mp:mp@' + host + ':3306/mppg'
  14. })
  15. .exec(function(err, report) {
  16. if (err) {
  17. return done(err);
  18. }
  19. // Store the manager
  20. manager = report.manager;
  21. Pack.getConnection({
  22. manager: manager
  23. })
  24. .exec(function(err, report) {
  25. if (err) {
  26. return done(err);
  27. }
  28. // Store the connection
  29. connection = report.connection;
  30. // Create a table to use for testing
  31. // Uses sendNativeQuery but doesn't get rows or anything.
  32. // TODO: figure out a query that can run with the given permissions
  33. // that doesn't need an additional table
  34. Pack.sendNativeQuery({
  35. connection: connection,
  36. nativeQuery: 'CREATE TABLE IF NOT EXISTS people(name varchar(255));'
  37. })
  38. .exec(function(err) {
  39. if (err) {
  40. return done(err);
  41. }
  42. return done();
  43. });
  44. });
  45. });
  46. });
  47. // Afterwards release the connection
  48. after(function(done) {
  49. Pack.sendNativeQuery({
  50. connection: connection,
  51. nativeQuery: 'DROP TABLE people;'
  52. })
  53. .exec(function(err) {
  54. if (err) {
  55. return done(err);
  56. }
  57. Pack.releaseConnection({
  58. connection: connection
  59. }).exec(done);
  60. });
  61. });
  62. it('should run a native query and return the reports', function(done) {
  63. Pack.sendNativeQuery({
  64. connection: connection,
  65. nativeQuery: 'select * from people;'
  66. })
  67. .exec(function(err, report) {
  68. if (err) {
  69. return done(err);
  70. }
  71. assert(_.isArray(report.result.rows));
  72. return done();
  73. });
  74. });
  75. });
  76. });