OujMySql.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. "use strict";
  2. var Mysql = require('mysql');
  3. var configs = require('../../conf/config.inc.js');
  4. var AppErrors = require('./AppErrors.js');
  5. var objPools = {};
  6. /**
  7. * 基本类,提供增删改查
  8. * @param {string} dbKey
  9. */
  10. class OujMySql {
  11. constructor(dbKey) {
  12. this.dbKey = dbKey || 'default';
  13. }
  14. getPool() {
  15. if (!objPools[this.dbKey]) {
  16. var config = configs.dbInfo[this.dbKey];
  17. if (config) {
  18. objPools[this.dbKey] = Mysql.createPool(config);
  19. } else {
  20. throw new AppErrors.DbError("数据库:" + this.dbKey + "没有配置");
  21. }
  22. }
  23. return objPools[this.dbKey];
  24. }
  25. query(sql, values) {
  26. let that = this;
  27. return new Promise(function(resolve, reject) {
  28. that.getPool().query(sql, values, function(err, result) {
  29. if (err) {
  30. reject(err);
  31. throw new AppErrors.DbError(err);
  32. } else {
  33. resolve(result);
  34. }
  35. });
  36. });
  37. };
  38. /**
  39. * 更新数据
  40. * @param {string} sql
  41. */
  42. update(sql) {
  43. return this.query(sql, []);
  44. };
  45. /**
  46. * 获取一个值
  47. * @param {string} sql
  48. * @return Promise
  49. */
  50. getOne(sql) {
  51. var that = this;
  52. return new Promise(function(resolve, reject) {
  53. var p1 = that.getAll(sql, 1);
  54. p1.then(function(rows) {
  55. var row = rows[0];
  56. var val = null;
  57. if (row) {
  58. var keys = Object.keys(row);
  59. val = keys && row[keys[0]];
  60. }
  61. resolve(val);
  62. }, reject);
  63. });
  64. };
  65. /**
  66. * 获取一列数据
  67. * @param sql
  68. * @param limit 行数
  69. */
  70. getCol(sql, limit) {
  71. let that = this;
  72. return new Promise(function(resolve, reject) {
  73. var p1 = that.getAll(sql, limit);
  74. p1.then(function(rows) {
  75. var cols = [];
  76. if (rows[0] && rows.length) {
  77. var keys = Object.keys(rows[0]);
  78. let key = keys[0];
  79. for (var i = 0; i < rows.length; i++) {
  80. cols.push(rows[i][key]);
  81. }
  82. }
  83. resolve(cols);
  84. }, reject);
  85. });
  86. };
  87. /**
  88. * 获取一行数据
  89. * @param sql
  90. */
  91. getRow(sql) {
  92. let that = this;
  93. return new Promise(function(resolve, reject) {
  94. var p1 = that.getAll(sql, 1);
  95. p1.then(function(rows) {
  96. resolve(rows[0] || null);
  97. }, reject);
  98. });
  99. };
  100. /**
  101. * 获取所有行数据
  102. * @param sql
  103. * @param limit 行数
  104. */
  105. getAll(sql, limit) {
  106. if (typeof limit == 'number' && limit > 0) {
  107. sql += " LIMIT " + limit;
  108. }
  109. return this.query(sql, []);
  110. };
  111. /**
  112. * 关闭连接池
  113. */
  114. close() {
  115. return new Promise(function(resolve, reject) {
  116. this.getPool().end(function(err) {
  117. if (err) {
  118. reject(err);
  119. } else {
  120. resolve();
  121. }
  122. });
  123. });
  124. };
  125. }
  126. module.exports = OujMySql;