Think.js 配置数据库

Think.js 配置数据库

方法
  1. 在 Thinkjs 项目下,配置 src/config/adapter.js 文件
    // adapter.js
    // 引用database
    const database = require('./database.js');
    ...
    /**
    * model adapter config
    * @type {Object}
    */
    exports.model = {
      type: 'mysql',
      common: {
      logConnect: isDev,
      logSql: isDev,
      logger: msg => think.logger.info(msg)
     },
     mysql: database
     };
  1. 在 Thinkjs 项目下,配置 src/config/database.js 文件
    // database.js
    module.exports = {
      handle: mysql,
      database: 'think', // database_name
      prefix: 'think_', // database_table 前缀
      encoding: 'utf8mb4',
      host: '127.0.0.1', // your MySQL host
      port: '3306', // your MySQL port
      user: '',
      password: '',
      dateStrings: true
    };

或者直接在 adapter.js 内配置

    // adapter.js
    // 引用database
    const database = require('./database.js');
    ...
    /**
    * model adapter config
    * @type {Object}
    */
    exports.model = {
      type: 'mysql',
      common: {
      logConnect: isDev,
      logSql: isDev,
      logger: msg => think.logger.info(msg)
     },
     mysql: {
     handle: mysql,
      database: 'think', // database_name
      prefix: 'think_', // database_table 前缀
      encoding: 'utf8mb4',
      host: '127.0.0.1', // your MySQL host
      port: '3306', // your MySQL port
      user: '',
      password: '',
      dateStrings: true
     }
    };
点赞