gpt4 book ai didi

mysql - Node.js MySQL 模型设计

转载 作者:IT老高 更新时间:2023-10-28 23:26:37 26 4
gpt4 key购买 nike

我正在使用 MySQL 数据库开发一个 node.js 应用程序,但我一直坚持在我的应用程序的 node.js 端制作模型。我以前使用 Mongoose 来生成模式并使用模型来执行数据库功能,但我找不到对 MySQL 的这种支持。任何人都可以建议一种正确的方法来隔离我在 node.js 中的数据库功能,就像我对 Mongoose 所做的那样。这是我现在正在使用的 app.js 和用户模型。

app.js

var express= require("express");
var bodyParser = require("body-parser");
var mysql = require("mysql");
var UserModel= require("./models/User.js")
var app=express();

var sql = mysql.createConnection({
host: "localhost",
user: "root",
password: "1234",
database: "dricm"
});

sql.connect(function (err) {
if(err){
console.log("error");
}else{
console.log("connected");
}
});

app.set("views", "./views");

app.use(express.static("node_modules/bootstrap/dist"));
app.use(express.static("public"));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false}));

app.get('/', function (req, res) {
res.render("signup.jade");
});

app.post('/signup', function (req, res) {
var obj= {
username: req.body.username,
password: req.body.password
};
UserModel.createUser(obj);
res.redirect("/");
});

app.listen(3000, function () {
console.log("server running at 3000");
});

User.js(可能的模型)

var mysql= require("mysql");
var bcrypt = require("bcryptjs");

var sql = mysql.createConnection({
host: "localhost",
user: "root",
password: "1234",
database: "dricm"
});

sql.connect(function (err) {
if(err){
console.log("error");
}else{
console.log("connected");
}
});

var User= {

}

User.createUser = function createUser(newUser) {
bcrypt.genSalt(10, function(err, salt){
bcrypt.hash(newUser.password,salt, function (err, hash) {
newUser.password = hash;
var query = sql.query("INSERT INTO USERS set ?", newUser, function (err, res) {
console.log(query);
if(err) {
console.log("error");
}
else{

console.log(res.insertId);
}
});
});
});

}

module.exports= User;

最佳答案

您要查找的内容称为 ORM(对象关系映射)Mongoose是 MongoDB 的一个(这是一个面向 NOSQL 文档的数据库)

还有其他适用于 Node.js 的关系数据库 ORM,目前最流行的是 Sequelize我个人使用并推荐的。

使用 Sequelize,您可以像 Mongoose 一样将模型放在不同的文件中,但是为了加载它们,您需要在 index.js 中使用简单的脚本添加它们

想象以下工作区:

--models/
----User.js
----Permissions.js
--index.js

你的模型定义应该是这样的:

用户.js

const UserModel = (sequelize, Sequelize) => {
const {INTEGER, STRING, FLOAT, BOOLEAN, DATE} = Sequelize
const User = sequelize.define('User', {
UserId: {type: INTEGER, primaryKey: true, autoIncrement: true},
Username: {type: STRING, primaryKey: true, allowNull: false},
Password: STRING
})
return User
}

module.exports = UserModel

Permissions.js

const PermissionsModel = (sequelize, Sequelize) => {
const {INTEGER, STRING, FLOAT, BOOLEAN, DATE} = Sequelize
const Permissions = sequelize.define('Permissions', {
Role: {type: STRING, allowNull: false},
ControllerAddress: {type: STRING, allowNull: false}
})
return Permissions
}

module.exports = PermissionsModel

现在您需要使用以下脚本在 index.js 中使用它们

let normalizedPath = require('path').join(__dirname, "models")
require('fs').readdirSync(normalizedPath).forEach((file) => {
sequelize.import('./models/' + file)
})
let {User, Permissions} = sequelize.models

现在您可以使用 User 和 Permissions 实例来控制它们并调用如下函数:

User.create({Username, Password})

关于mysql - Node.js MySQL 模型设计,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38980031/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com