Think.js 配置数据库

Think.js 配置数据库

在 Thinkjs 项目下,配置 src/config/adapter.js 文件

    // adapter.js
    // 引用database.js
    const database = require('./database.js');
    ...
    exports.model = {
      type: 'mysql',
      common: {
      logConnect: isDev,
      logSql: isDev,
      logger: msg => think.logger.info(msg)
     },
     mysql: database
     };

在 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
    const mysql = require('think-model-mysql');
    ...
    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
     }
    };

你可能感兴趣的:(mysql,thinkjs,后端)