You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
72 lines
1.4 KiB
JavaScript
72 lines
1.4 KiB
JavaScript
'use strict';
|
|
|
|
const joi = require('joi');
|
|
const { Sequelize, DataTypes, Op } = require('sequelize');
|
|
|
|
/**
|
|
* Generate a validation schema using joi to check the type of your environment variables
|
|
*/
|
|
const envSchema = joi
|
|
.object({
|
|
DB_USER: joi.string(),
|
|
DB_HOST: joi.string(),
|
|
DB_PASSWORD: joi
|
|
.string()
|
|
.optional()
|
|
.empty(''),
|
|
DB_DATABASE: joi.string(),
|
|
DB_PORT: joi.number(),
|
|
})
|
|
.unknown()
|
|
.required();
|
|
|
|
/**
|
|
* Validate the env variables using joi.validate()
|
|
*/
|
|
const { error, value: envVars } = joi.validate(process.env, envSchema);
|
|
if (error) {
|
|
throw new Error(`Config validation error: ${error.message}`);
|
|
}
|
|
|
|
const config = {
|
|
databaseConfig: {
|
|
user: envVars.DB_USER,
|
|
host: envVars.DB_HOST,
|
|
password: envVars.DB_PASSWORD,
|
|
database: envVars.DB_DATABASE,
|
|
port: envVars.DB_PORT,
|
|
},
|
|
};
|
|
|
|
const { databaseConfig } = config;
|
|
|
|
const DB = new Sequelize(databaseConfig.database, databaseConfig.user, databaseConfig.password, {
|
|
host: databaseConfig.host,
|
|
port: databaseConfig.port,
|
|
dialect: 'mysql',
|
|
// operatorsAliases: false,
|
|
// operatorsAliases: 0,
|
|
dialectOptions: {
|
|
charset: 'utf8mb4',
|
|
// collate: 'utf8mb4_unicode_ci',
|
|
supportBigNumbers: true,
|
|
bigNumberStrings: true,
|
|
},
|
|
|
|
pool: {
|
|
max: 5,
|
|
min: 0,
|
|
acquire: 30000,
|
|
idle: 10000,
|
|
},
|
|
timezone: '+08:00', // 东八时区
|
|
});
|
|
|
|
module.exports = {
|
|
database: {
|
|
DataTypes,
|
|
Op,
|
|
sequelize: DB,
|
|
},
|
|
};
|