@ -8,17 +8,17 @@ const config = require('../../../shared/config');
const errors = require ( '@tryghost/errors' ) ;
/** @type {knex.Knex} */
let knexInstance ;
let knexInstance ; //执行的时候才会被赋予一个Knex实例
// @TODO:
// - if you require this file before config file was loaded,
// - then this file is cached and you have no chance to connect to the db anymore
// - bring dynamic into this file (db.connect())
function configure ( dbConfig ) {
const client = dbConfig . client ;
const client = dbConfig . client ; //获取但概念的客户端
if ( client === 'sqlite3' ) {
// Backwards compatibility with old knex behaviour
if ( client === 'sqlite3' ) { //向后兼容性, 如果使用的是sqlite3客户端
// Backwards compatibility with old knex behaviour
dbConfig . useNullAsDefault = Object . prototype . hasOwnProperty . call ( dbConfig , 'useNullAsDefault' ) ? dbConfig . useNullAsDefault : true ;
// Enables foreign key checks and delete on cascade
@ -38,6 +38,7 @@ function configure(dbConfig) {
// In the default SQLite test config we set the path to /tmp/ghost-test.db,
// but this won't work on Windows, so we need to replace the /tmp bit with
// the Windows temp folder
// 在windows系统下的兼容性处理
const filename = dbConfig . connection . filename ;
if ( process . platform === 'win32' && _ . isString ( filename ) && filename . match ( /^\/tmp/ ) ) {
dbConfig . connection . filename = filename . replace ( /^\/tmp/ , os . tmpdir ( ) ) ;
@ -47,18 +48,19 @@ function configure(dbConfig) {
if ( client === 'mysql2' ) {
dbConfig . connection . timezone = 'Z' ;
dbConfig . connection . charset = 'utf8mb4' ;
dbConfig . connection . decimalNumbers = true ;
dbConfig . connection . charset = 'utf8mb4' ; //编码方式的设置
dbConfig . connection . decimalNumbers = true ; //是否将MySQL的DECIMAL类型转换为JavaScript的Number类型
if ( process . env . REQUIRE _INFILE _STREAM ) {
if ( process . env . NODE _ENV === 'development' || process . env . ALLOW _INFILE _STREAM ) {
if ( process . env . REQUIRE _INFILE _STREAM ) { //是否要求启用infile流
if ( process . env . NODE _ENV === 'development' || process . env . ALLOW _INFILE _STREAM ) { //如果是在开发环境下, 或者允许启用infile流
dbConfig . connection . infileStreamFactory = path => fs . createReadStream ( path ) ;
} else {
} else { //如果不是在开发环境下, 并且不允许启用infile流
throw new errors . InternalServerError ( { message : 'MySQL infile streaming is required to run the current process, but is not allowed. Run the script in development mode or set ALLOW_INFILE_STREAM=1.' } ) ;
}
}
}
//如果前两个if都没成功的话, 就会返回原始的dbConfig对象
//返回数据库配置对象
return dbConfig ;
}