Node.js - Sequelize.js |创建动态数据库连接
Node.js - Sequelize.js | Create Dynamic Database Connection
我正在用 Node.js 和 Sequelize.js 编写一个简单的 CMS。我想从 Wordpress 等用户那里获取数据库信息并创建数据库连接。我为此编写了下面的代码,但我的问题是应用程序未在数据库连接之前启动。
控制器
// Get Database Setup Page
exports.getSetup = (req, res) => {
return res.render("pages/account/setup");
};
// Post Database Information
exports.postSetup = (req, res) => {
// Set posted values as ENV Variable
setEnvValue("DB_NAME", req.body.name);
setEnvValue("DB_USERNAME", req.body.username);
setEnvValue("DB_PASSWORD", req.body.password);
setEnvValue("DB_HOST", req.body.host);
return res.redirect("/login");
};
// Error occurs here. Application don't start before database connection.
exports.getLogin = async (req, res) => {
const general = await Models.General.findByPk(1);
// Check Authenticate
if (req.session.isAuth) return res.redirect("/admin");
// Render Page
return res.render("pages/account/login", {
title: "Login",
general: general,
});
};
.env 文件在 Post
之后
DB_NAME="db-name"
DB_USERNAME="root"
DB_PASSWORD="password"
DB_HOST="localhost"
数据库连接文件
我在几个地方使用它,比如定义模型。
const Sequelize = require("sequelize");
const sequelize = new Sequelize(
process.env.DB_NAME,
process.env.DB_USERNAME,
process.env.DB_PASSWORD,
{
dialect: "mysql",
host: process.env.DB_HOST,
}
);
module.exports = sequelize;
您可以将创建的 Sequelize 实例包装到一个函数中并将其导出以在 login
路由中使用,如下所示:
const Sequelize = require("sequelize");
let sequelize = null;
function initializeConnection() {
sequelize = new Sequelize(
process.env.DB_NAME,
process.env.DB_USERNAME,
process.env.DB_PASSWORD,
{
dialect: "MySQL",
host: process.env.DB_HOST,
}
);
// here should be model and their associations' registration
...
}
function getConnection() {
return sequelize;
}
module.exports = {
initializeConnection,
getConnection
}
// Error Occurs here. Application don't start before database connection.
exports.getLogin = async (req, res) => {
if (req.session.isAuth) {
return res.redirect("/admin");
}
try {
await intializeConnection();
catch (err) {
res.status(500).send('Internal server error')
return;
}
const general = await Models.General.findByPk(1)
res.render("pages/account/login", {
title: "Login",
general: general,
});
};
注销时不要忘记与数据库断开连接
我正在用 Node.js 和 Sequelize.js 编写一个简单的 CMS。我想从 Wordpress 等用户那里获取数据库信息并创建数据库连接。我为此编写了下面的代码,但我的问题是应用程序未在数据库连接之前启动。
控制器
// Get Database Setup Page
exports.getSetup = (req, res) => {
return res.render("pages/account/setup");
};
// Post Database Information
exports.postSetup = (req, res) => {
// Set posted values as ENV Variable
setEnvValue("DB_NAME", req.body.name);
setEnvValue("DB_USERNAME", req.body.username);
setEnvValue("DB_PASSWORD", req.body.password);
setEnvValue("DB_HOST", req.body.host);
return res.redirect("/login");
};
// Error occurs here. Application don't start before database connection.
exports.getLogin = async (req, res) => {
const general = await Models.General.findByPk(1);
// Check Authenticate
if (req.session.isAuth) return res.redirect("/admin");
// Render Page
return res.render("pages/account/login", {
title: "Login",
general: general,
});
};
.env 文件在 Post
之后DB_NAME="db-name"
DB_USERNAME="root"
DB_PASSWORD="password"
DB_HOST="localhost"
数据库连接文件
我在几个地方使用它,比如定义模型。
const Sequelize = require("sequelize");
const sequelize = new Sequelize(
process.env.DB_NAME,
process.env.DB_USERNAME,
process.env.DB_PASSWORD,
{
dialect: "mysql",
host: process.env.DB_HOST,
}
);
module.exports = sequelize;
您可以将创建的 Sequelize 实例包装到一个函数中并将其导出以在 login
路由中使用,如下所示:
const Sequelize = require("sequelize");
let sequelize = null;
function initializeConnection() {
sequelize = new Sequelize(
process.env.DB_NAME,
process.env.DB_USERNAME,
process.env.DB_PASSWORD,
{
dialect: "MySQL",
host: process.env.DB_HOST,
}
);
// here should be model and their associations' registration
...
}
function getConnection() {
return sequelize;
}
module.exports = {
initializeConnection,
getConnection
}
// Error Occurs here. Application don't start before database connection.
exports.getLogin = async (req, res) => {
if (req.session.isAuth) {
return res.redirect("/admin");
}
try {
await intializeConnection();
catch (err) {
res.status(500).send('Internal server error')
return;
}
const general = await Models.General.findByPk(1)
res.render("pages/account/login", {
title: "Login",
general: general,
});
};
注销时不要忘记与数据库断开连接