标签:product const err title Sequelize SQL Databases sequelize
SQL vs NoSQL
Connect app to SQL database
npm install --save mysql2
Connection setup
const mysql = require('mysql2');
// connection pool
// manage multiple connections (multiple queries) simultaneously.
const pool = mysql.createPool({
host: 'localhost',
user: 'root',
database: 'node-complete',
password:'Meiting5'
});
module.exports = pool.promise();
execute sql queries
db.execute('')
Sequelize
An object-relational mapping library
sequelize works with Promises
it does all the heavy lifting, all the SQL code behind the scenes for us and maps it into javascript objects with convenience methods which we can call to execute that behind the scenes SQL code so that we never have to write SQL code on our own.
npm install --save sequelize
Connection setup
const Sequelize = require("sequelize");
const sequelize = new Sequelize("node-complete", "root", "Meiting5", {
dialect: "mysql",
host: "localhost"
});
module.exports = sequelize;
define model
const Product = sequelize.define('product',{
id: {
type:Sequelize.INTEGER,
autoIncrement: true,
allowNull: false,
primaryKey: true
},
title: Sequelize.STRING,
price:{
type: Sequelize.DOUBLE,
allowNull: false
},
imageUrl: {
type: Sequelize.STRING,
allowNull: false
},
description: {
type: Sequelize.STRING,
allowNull: false
}
});
Create table
sequelize
.sync()
.then(result => {
app.listen(3000);
})
.catch(err => {
console.log(err);
});
Insert Data & create a product
exports.postAddProduct = (req, res, next) => {
const title = req.body.title;
const imageUrl = req.body.imageUrl;
const price = req.body.price;
const description = req.body.description;
Product.create({
title: title,
price: price,
imageUrl: imageUrl,
description: description
})
.then(result => {
console.log("Created Product");
})
.catch(err => {
console.log(err);
});
};
Retrieving data and find products
Product.findAll()
.then(products => {
res.render("shop/index", {
prods: products,
pageTitle: "Shop",
path: "/"
});
})
.catch(err => console.log(err));
// Product.findAll({where: {id: prodId}})
Product.findByPk(prodId)
.then((product) => {
res.render("shop/product-detail", {
product: product,
pageTitle: product.title,
path: "/products"
});
})
.catch(err => console.log(err));
// create if not exist, overwrite if not exist
product.save();
delete
Product.findByPk(prodId)
.then(product => {
return product.destroy();
})
.then(result => {
console.log("DESTROYED");
res.redirect("/admin/products");
})
.catch(err => console.log(err));
Associations
// delete user, all product associated to the user gone
Product.belongsTo(User,{constraints: true, onDelete: 'CASCADE'});
User.hasOne(Cart);
create new associated object
// pass data cannot be inferred by sequelize
req.user.createProduct({
title: title,
price: price,
imageUrl: imageUrl,
description: description
})
.getProducts()
标签:product,const,err,title,Sequelize,SQL,Databases,sequelize 来源: https://blog.csdn.net/weixin_42552135/article/details/100691233
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。