@zhin.js/database 1.0.76 → 1.0.77

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.
@@ -1,12 +1,14 @@
1
1
  import * as QueryClasses from './query-classes.js';
2
2
  import { TransactionContextImpl } from './transaction.js';
3
+ import { getLogger } from '@zhin.js/logger';
4
+ const logger = getLogger('database');
3
5
  /**
4
6
  * 默认查询日志处理器
5
7
  */
6
8
  const defaultLogger = ({ sql, params, duration, error }) => {
7
9
  const paramStr = params?.length ? ` [${JSON.stringify(params)}]` : '';
8
10
  const status = error ? `❌ ERROR: ${error instanceof Error ? error.message : String(error)}` : `✅ ${duration}ms`;
9
- console.log(`[SQL] ${sql}${paramStr} → ${status}`);
11
+ logger.debug(`[SQL] ${sql}${paramStr} → ${status}`);
10
12
  };
11
13
  /**
12
14
  * 基础数据库抽象类
@@ -78,7 +80,7 @@ export class Database {
78
80
  async stop() {
79
81
  await this.dialect.disconnect();
80
82
  this.hasStarted = false;
81
- console.log(`Database disconnected from dialect: ${this.dialect.name}`);
83
+ logger.info(`Database disconnected from dialect: ${this.dialect.name}`);
82
84
  }
83
85
  /**
84
86
  * 健康检查
@@ -1,6 +1,8 @@
1
1
  import { Dialect } from '../base/index.js';
2
2
  import { DocumentDatabase } from "../type/document/database.js";
3
3
  import { Registry } from "../registry.js";
4
+ import { getLogger } from '@zhin.js/logger';
5
+ const logger = getLogger('database');
4
6
  export class MongoDBDialect extends Dialect {
5
7
  client = null;
6
8
  db = null;
@@ -52,7 +54,7 @@ export class MongoDBDialect extends Dialect {
52
54
  this.db = this.client.db(this.config.dbName);
53
55
  }
54
56
  catch (error) {
55
- console.error('forgot install mongodb ?');
57
+ logger.error('forgot install mongodb ?');
56
58
  throw new Error(`MongoDB 连接失败: ${error}`);
57
59
  }
58
60
  }
@@ -64,7 +66,7 @@ export class MongoDBDialect extends Dialect {
64
66
  await this.client.close();
65
67
  this.client = null;
66
68
  this.db = null;
67
- console.log('MongoDB 连接已关闭');
69
+ logger.info('MongoDB 连接已关闭');
68
70
  }
69
71
  }
70
72
  /**
@@ -1,6 +1,8 @@
1
1
  import { Dialect } from '../base/index.js';
2
2
  import { RelatedDatabase } from "../type/related/database.js";
3
3
  import { Registry } from "../registry.js";
4
+ import { getLogger } from '@zhin.js/logger';
5
+ const logger = getLogger('database');
4
6
  export class MySQLDialect extends Dialect {
5
7
  connection = null;
6
8
  pool = null;
@@ -25,7 +27,7 @@ export class MySQLDialect extends Dialect {
25
27
  idleTimeout: this.config.pool?.idleTimeoutMillis ?? 60000,
26
28
  };
27
29
  this.pool = createPool(poolConfig);
28
- console.log(`MySQL 连接池已创建 (max: ${poolConfig.connectionLimit})`);
30
+ logger.info(`MySQL 连接池已创建 (max: ${poolConfig.connectionLimit})`);
29
31
  }
30
32
  else {
31
33
  const { createConnection } = await import('mysql2/promise');
@@ -33,7 +35,7 @@ export class MySQLDialect extends Dialect {
33
35
  }
34
36
  }
35
37
  catch (error) {
36
- console.error('forgot install mysql2 ?');
38
+ logger.error('forgot install mysql2 ?');
37
39
  throw new Error(`MySQL 连接失败: ${error}`);
38
40
  }
39
41
  }
@@ -41,7 +43,7 @@ export class MySQLDialect extends Dialect {
41
43
  if (this.usePool && this.pool) {
42
44
  await this.pool.end();
43
45
  this.pool = null;
44
- console.log('MySQL 连接池已关闭');
46
+ logger.info('MySQL 连接池已关闭');
45
47
  }
46
48
  else if (this.connection) {
47
49
  await this.connection.end();
@@ -1,6 +1,8 @@
1
1
  import { Dialect } from '../base/index.js';
2
2
  import { RelatedDatabase } from "../type/related/database.js";
3
3
  import { Registry } from "../registry.js";
4
+ import { getLogger } from '@zhin.js/logger';
5
+ const logger = getLogger('database');
4
6
  export class PostgreSQLDialect extends Dialect {
5
7
  connection = null;
6
8
  pool = null;
@@ -25,7 +27,7 @@ export class PostgreSQLDialect extends Dialect {
25
27
  connectionTimeoutMillis: this.config.pool?.acquireTimeoutMillis ?? 10000,
26
28
  };
27
29
  this.pool = new Pool(poolConfig);
28
- console.log(`PostgreSQL 连接池已创建 (max: ${poolConfig.max})`);
30
+ logger.info(`PostgreSQL 连接池已创建 (max: ${poolConfig.max})`);
29
31
  }
30
32
  else {
31
33
  const { Client } = await import('pg');
@@ -34,7 +36,7 @@ export class PostgreSQLDialect extends Dialect {
34
36
  }
35
37
  }
36
38
  catch (error) {
37
- console.error('forgot install pg ?');
39
+ logger.error('forgot install pg ?');
38
40
  throw new Error(`PostgreSQL 连接失败: ${error}`);
39
41
  }
40
42
  }
@@ -42,7 +44,7 @@ export class PostgreSQLDialect extends Dialect {
42
44
  if (this.usePool && this.pool) {
43
45
  await this.pool.end();
44
46
  this.pool = null;
45
- console.log('PostgreSQL 连接池已关闭');
47
+ logger.info('PostgreSQL 连接池已关闭');
46
48
  }
47
49
  else if (this.connection) {
48
50
  await this.connection.end();
@@ -1,6 +1,8 @@
1
1
  import { Dialect } from '../base/index.js';
2
2
  import { KeyValueDatabase } from "../type/keyvalue/database.js";
3
3
  import { Registry } from "../registry.js";
4
+ import { getLogger } from '@zhin.js/logger';
5
+ const logger = getLogger('database');
4
6
  export class RedisDialect extends Dialect {
5
7
  client = null;
6
8
  constructor(config) {
@@ -48,21 +50,21 @@ export class RedisDialect extends Dialect {
48
50
  const { createClient } = await import('redis');
49
51
  this.client = createClient(this.config);
50
52
  this.client.on('error', (err) => {
51
- console.error('Redis 客户端错误:', err);
53
+ logger.error('Redis 客户端错误:', err);
52
54
  });
53
55
  this.client.on('connect', () => {
54
- console.log('Redis 连接已建立');
56
+ logger.info('Redis 连接已建立');
55
57
  });
56
58
  this.client.on('ready', () => {
57
- console.log('Redis 客户端已准备就绪');
59
+ logger.info('Redis 客户端已准备就绪');
58
60
  });
59
61
  this.client.on('end', () => {
60
- console.log('Redis 连接已关闭');
62
+ logger.info('Redis 连接已关闭');
61
63
  });
62
64
  await this.client.connect();
63
65
  }
64
66
  catch (error) {
65
- console.error('forgot install redis ?');
67
+ logger.error('forgot install redis ?');
66
68
  throw new Error(`Redis 连接失败: ${error}`);
67
69
  }
68
70
  }
@@ -73,7 +75,7 @@ export class RedisDialect extends Dialect {
73
75
  if (this.client) {
74
76
  await this.client.quit();
75
77
  this.client = null;
76
- console.log('Redis 连接已关闭');
78
+ logger.info('Redis 连接已关闭');
77
79
  }
78
80
  }
79
81
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zhin.js/database",
3
- "version": "1.0.76",
3
+ "version": "1.0.77",
4
4
  "description": "Universal database abstraction layer for Zhin.js - supports SQLite, MySQL, PostgreSQL, MongoDB, Redis",
5
5
  "type": "module",
6
6
  "main": "./lib/index.js",
@@ -19,6 +19,9 @@
19
19
  "engines": {
20
20
  "node": ">=22.5.0"
21
21
  },
22
+ "dependencies": {
23
+ "@zhin.js/logger": "1.0.75"
24
+ },
22
25
  "peerDependencies": {
23
26
  "mongodb": ">=3.1.13",
24
27
  "mysql2": ">=3.9.8",