@axiosleo/orm-mysql 0.11.6 → 0.11.7

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.
package/bin/orm-mysql.js CHANGED
@@ -9,7 +9,7 @@ const app = new App({
9
9
  name: 'MySQL ORM CLI',
10
10
  desc: 'migrate, model, seed, etc.',
11
11
  bin: 'orm-mysql',
12
- version: '0.11.6',
12
+ version: '0.11.7',
13
13
  commands_dir: path.join(__dirname, '../commands'),
14
14
  });
15
15
 
package/index.d.ts CHANGED
@@ -537,4 +537,6 @@ export declare class MigrationInterface {
537
537
  dropIndex(indexName: string): void;
538
538
 
539
539
  dropForeignKey(foreign_key: string, tableName: string): void;
540
+
541
+ insertData(table: string, data: any[]): void;
540
542
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@axiosleo/orm-mysql",
3
- "version": "0.11.6",
3
+ "version": "0.11.7",
4
4
  "description": "MySQL ORM tool",
5
5
  "keywords": [
6
6
  "mysql",
package/src/builder.js CHANGED
@@ -274,7 +274,23 @@ class Builder {
274
274
  _buildValues(value) {
275
275
  let fields = [];
276
276
  if (is.array(value)) {
277
- [fields] = value.map((v) => this._buildValue(v));
277
+ fields = this._buildValue(value[0]);
278
+
279
+ this.values = this.values.slice(0, -fields.length);
280
+
281
+ value.forEach((obj) => {
282
+ fields.forEach((field) => {
283
+ const val = obj[field];
284
+ if (val instanceof Date) {
285
+ this.values.push(val);
286
+ } else if (Array.isArray(val) || is.object(val)) {
287
+ this.values.push(JSON.stringify(val));
288
+ } else {
289
+ this.values.push(val);
290
+ }
291
+ });
292
+ });
293
+
278
294
  let item = '(' + fields.map(f => '?').join(',') + ')';
279
295
  return { fields, sqlStr: new Array(value.length).fill(item).join(',') };
280
296
  }
package/src/client.js CHANGED
@@ -25,7 +25,17 @@ const createClient = (options, name = null) => {
25
25
  const key = name ? name :
26
26
  `${options.host}:${options.port}:${options.user}:${options.password}:${options.database}`;
27
27
  if (clients[key]) {
28
- return clients[key];
28
+ // 检查现有连接的连通性
29
+ const existingConnection = clients[key];
30
+
31
+ // 检查连接是否已关闭或销毁
32
+ if (existingConnection._closing || existingConnection._closed || existingConnection.destroyed) {
33
+ // 连接已关闭,清除并重新创建
34
+ delete clients[key];
35
+ } else {
36
+ // 如果连接看起来正常,返回现有的连接
37
+ return existingConnection;
38
+ }
29
39
  }
30
40
  clients[key] = mysql.createConnection(options);
31
41
  clients[key].connect();
@@ -48,7 +58,17 @@ const createPromiseClient = async (options, name = null) => {
48
58
  const key = name ? name :
49
59
  `${options.host}:${options.port}:${options.user}:${options.password}:${options.database}`;
50
60
  if (clients[key]) {
51
- return clients[key];
61
+ // 检查现有连接的连通性
62
+ const existingConnection = clients[key];
63
+
64
+ // 检查连接是否已关闭或销毁
65
+ if (existingConnection._closing || existingConnection._closed || existingConnection.destroyed) {
66
+ // 连接已关闭,清除并重新创建
67
+ delete clients[key];
68
+ } else {
69
+ // 如果连接看起来正常,返回现有的连接
70
+ return existingConnection;
71
+ }
52
72
  }
53
73
  clients[key] = await mysqlPromise.createConnection(options);
54
74
  return clients[key];
@@ -66,6 +86,7 @@ const createPool = (options, name = null) => {
66
86
  password: 'required|string',
67
87
  port: 'required|integer',
68
88
  database: 'required|string',
89
+ enableKeepAlive: 'boolean',
69
90
  });
70
91
  const key = name ? name :
71
92
  `${options.host}:${options.port}:${options.user}:${options.password}:${options.database}`;
@@ -150,5 +171,8 @@ module.exports = {
150
171
  getClient,
151
172
  createPool,
152
173
  createClient,
153
- createPromiseClient
174
+ createPromiseClient,
175
+
176
+ // 仅用于测试
177
+ _clients: clients
154
178
  };
package/src/migration.js CHANGED
@@ -10,7 +10,7 @@ const { _render } = require('@axiosleo/cli-tool/src/helper/str');
10
10
  const { _foreach } = require('@axiosleo/cli-tool/src/helper/cmd');
11
11
  const { _assign } = require('@axiosleo/cli-tool/src/helper/obj');
12
12
  const { TransactionHandler } = require('..');
13
- const { ManageSQLBuilder } = require('./builder');
13
+ const { ManageSQLBuilder, Builder } = require('./builder');
14
14
 
15
15
  const migrationColumns = [
16
16
  {
@@ -268,6 +268,17 @@ function _initMigration(file, queries = {}) {
268
268
  }, ...baseAttr
269
269
  });
270
270
 
271
+ Object.defineProperty(migration, 'insertData', {
272
+ value: function (table, data) {
273
+ const builder = new Builder({
274
+ operator: 'insert',
275
+ tables: [{ table }],
276
+ data
277
+ });
278
+ queries[file].push({ sql: builder.sql, values: builder.values });
279
+ }, ...baseAttr
280
+ });
281
+
271
282
  return migration;
272
283
  }
273
284