mm_mysql 2.2.1 → 2.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +4 -4
  2. package/index.js +8 -16
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -213,10 +213,10 @@ const config = {
213
213
  ### Mysql管理器
214
214
 
215
215
  #### mysqlAdmin(scope, config)
216
- 获取Mysql管理器实例(线程安全版)
216
+ 获取Mysql管理器实例
217
217
  - `scope` {String} 作用域名称(可选,默认为'sys')
218
218
  - `config` {Object} 配置参数
219
- - 返回:{Promise<Mysql>} Mysql实例
219
+ - 返回:{Mysql} Mysql实例
220
220
 
221
221
  使用示例:
222
222
  ```javascript
@@ -224,7 +224,7 @@ const { mysqlAdmin } = require('mm_mysql');
224
224
 
225
225
  async function example() {
226
226
  // 获取默认作用域的Mysql实例
227
- const mysql1 = await mysqlAdmin('default', {
227
+ const mysql1 = mysqlAdmin('default', {
228
228
  host: 'localhost',
229
229
  user: 'root',
230
230
  password: '123456',
@@ -232,7 +232,7 @@ async function example() {
232
232
  });
233
233
 
234
234
  // 获取另一个作用域的Mysql实例
235
- const mysql2 = await mysqlAdmin('backup', {
235
+ const mysql2 = mysqlAdmin('backup', {
236
236
  host: 'localhost',
237
237
  user: 'root',
238
238
  password: '123456',
package/index.js CHANGED
@@ -194,7 +194,7 @@ Mysql.prototype._safeReleaseConnection = async function (conn, is_pool_conn) {
194
194
  Mysql.prototype.getConn = async function () {
195
195
  // 参数校验
196
196
  if (!this._pool && !this._connection) {
197
- throw new Error('数据库连接未初始化');
197
+ await this.open();
198
198
  }
199
199
 
200
200
  try {
@@ -602,9 +602,9 @@ function _acquireLock(key) {
602
602
  * @description Mysql管理器,用于创建缓存(线程安全版)
603
603
  * @param {String} scope 作用域
604
604
  * @param {Object} config 配置参数
605
- * @return {Promise<Object>} 返回一个Mysql类实例
605
+ * @return {Object} 返回一个Mysql类实例
606
606
  */
607
- async function mysqlAdmin(scope, config) {
607
+ function mysqlAdmin(scope, config) {
608
608
  if (!scope) {
609
609
  scope = 'sys';
610
610
  }
@@ -614,20 +614,12 @@ async function mysqlAdmin(scope, config) {
614
614
  return $.pool.mysql[scope];
615
615
  }
616
616
 
617
- // 获取锁,确保线程安全
618
- const release = await _acquireLock(scope);
619
-
620
- try {
621
- // 双重检查,防止在等待锁期间其他线程已创建实例
622
- if (!$.pool.mysql[scope]) {
623
- $.pool.mysql[scope] = new Mysql(config);
624
- }
625
-
626
- return $.pool.mysql[scope];
627
- } finally {
628
- // 释放锁
629
- release();
617
+ // 同步方式创建实例(无需锁,因为Node.js是单线程的)
618
+ if (!$.pool.mysql[scope]) {
619
+ $.pool.mysql[scope] = new Mysql(config);
630
620
  }
621
+
622
+ return $.pool.mysql[scope];
631
623
  }
632
624
 
633
625
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mm_mysql",
3
- "version": "2.2.1",
3
+ "version": "2.2.3",
4
4
  "description": "这是超级美眉mysql帮助函数模块,用于便捷操作mysql,使用await方式,可以避免嵌套函数",
5
5
  "main": "index.js",
6
6
  "dependencies": {