Nodejs + sequelize 實(shí)現(xiàn)增刪改查操作
1. 下載資源庫
npm install sequelize --save
npm install mysql2 --save // npm install mysql 提示不完整
2. 創(chuàng)建數(shù)據(jù)庫配置文件 db.js,配置數(shù)據(jù)庫
var Sequelize = require('sequelize');
module.exports = new Sequelize('blog', 'root', '123456', {
host: 'localhost', // 數(shù)據(jù)庫地址
dialect: 'mysql', // 指定連接的數(shù)據(jù)庫類型
operatorsAliases: false,
pool: {
max: 5, // 連接池中最大連接數(shù)量
min: 0, // 連接池中最小連接數(shù)量
idle: 10000 // 如果一個(gè)線程 10 秒鐘內(nèi)沒有被使用過的話,那么就釋放線程
}
});
3. 創(chuàng)建一個(gè)model 文件 user.js
var Sequelize = require('sequelize');
var sequelize = require('./db');
// 創(chuàng)建 model
var User = sequelize.define('user', {
id : {type : Sequelize.INTEGER, autoIncrement : true, primaryKey : true, unique : true},
userName: {
type: Sequelize.STRING, // 指定值的類型
field: 'user_name' // 指定存儲在表中的鍵名稱
},
// 沒有指定 field,表中鍵名稱則與對象鍵名相同,為 email
email: {
type: Sequelize.STRING
}
}, {
// 如果為 true 則表的名稱和 model 相同,即 user
// 為 false MySQL創(chuàng)建的表名稱會是復(fù)數(shù) users
// 如果指定的表名稱本就是復(fù)數(shù)形式則不變
freezeTableName: true
});
/*User.sync({force:false}).then(function(){
console.log("success to start");
}).catch(function(err){
console.log("failed to start ")
})*/
// 創(chuàng)建表
// User.sync() 會創(chuàng)建表并且返回一個(gè)Promise對象
// 如果 force = true 則會把存在的表(如果users表已存在)先銷毀再創(chuàng)建表
// 默認(rèn)情況下 forse = false
//var user = User.sync({ force: false });
// 添加新用戶
exports.addUser = function(userName, email) {
// 向 user 表中插入數(shù)據(jù)
return User.create({
userName: userName,
email: email
}).then(function(result){
console.log("插入操作成功"+result);
}).catch(function(err){
console.log("添加數(shù)據(jù)發(fā)生錯(cuò)誤:"+err)
});
};
exports.findByName = function(userName) {
return User.findOne({where: {user_name:userName
}}).then(function(result){
console.log("成功:" + result.id);
}).catch(function(err){
console.log("發(fā)生錯(cuò)誤:" + err);
});
};
// 通過用戶名查找用戶
exports.update = function(id){
return User.findOne({where: {id:id
}}).then(function(user){
return user.update({
email:'jack3@qq.com'
}).then(function(result){
console.log("update success: "+result);
}).catch(function(err){
console.log("更新操作出錯(cuò):"+err);
});
});
};
exports.destroy = function(id){
return User.destroy({where:{id:id}}).then(function(result){
console.log("delete success");
}).catch(function(err){
console.log("delete data err: "+err);
})
}
4. 測試文件
var user = require('./user');
//查詢操作
//user.findByName("jack");
// 添加用戶
//user.addUser('jack2', 'jack@163.com');
// 更新
//user.update(1001);
//刪除
//user.destroy(1001);
補(bǔ)充知識:nodejs Sequelize 簡單查詢語句和 mysql常用的幾個(gè)查詢命令
我是前端,但總有需求讓做后端的活,所以順帶著熟悉了下簡單的查詢語句
貼出來,如果有需要可以參考下,備注很詳細(xì),就不多解釋了
廢話不多說貼代碼:
#去除unionid 重復(fù)的搜索結(jié)果 #query_resultsign 表名 select *, count(unionid) from query_resultsign where issign='false' group by unionid ; #去除unionid 重復(fù)的搜索結(jié)果 #query_resultsign 表名 select *, count(unionid) from query_resultsign where issign='true' group by unionid ; #求未簽約用戶的平均訪問頻率(即為求搜索結(jié)果列的平均值issign='false' 未簽約) #cuid 是unid的別名 #query_resultsign 表名 select AVG(bs.cuid) as unUserAvg FROM (select *, count(unionid) cuid from query_resultsign where issign='false' group by unionid ) as bs; #求平均值 #(即為求搜索結(jié)果issign='true' count的平均值) #bs為子查詢的別名,不帶別名會報(bào)錯(cuò) #query_resultsign 表名 select AVG(bs.cuid) userAvg FROM (select *, count(unionid) cuid from query_resultsign where issign='true' group by unionid ) as bs; #增加id 列 int #query_resultsign ALTER TABLE query_resultsign add id int; #使表 query_resultsign (上一步)增加的列變?yōu)樽栽隽? alter table query_resultsign change id id int NOT NULL AUTO_INCREMENT primary key; #獲取兩列數(shù)據(jù)中有相同數(shù)據(jù)的列 #query_resultsign 表名 select p1.* from query_resultsign p1,query_resultsign p2 where p1.id<>p2.id and p1.x = p2.x and p1.y = p2.y ; #查找表query_resultsign unionid 相同的用戶 select p1.* from query_resultsign p1,query_resultsign p2 where p1.id<>p2.id and p1.unionid = p2.unionid ;
sequelize 的調(diào)用sql語句的方法順帶提一下,網(wǎng)上大多教程都是用model 查詢的,每次都要建立model。有點(diǎn)麻煩 。配置的教程請參看配置教程。
sequelize調(diào)用sql主要用query(sql,{})方法:
var Sequelize = require('sequelize');//引入sequelize
var sequelize = require('./../../database/dataconfig'); //引入連接配置文件
//查找簽約用戶
exports.selectHeatData = function (req, res) {
return sequelize.query("select * from `query_resultSign` where issign ='true'", { type: sequelize.QueryTypes.SELECT }).then(data => {
// console.log('******', data);
res.send(data);
}).catch(err => {
console.log('錯(cuò)誤', err)
})
}
//其他方法就是換了下sql語句
主要知識點(diǎn)就是query方法內(nèi)傳入查詢出的結(jié)果的類型 { type: sequelize.QueryTypes.SELECT } 這樣就不用手動(dòng)轉(zhuǎn)換成json對象了。
附帶配置文件代碼
dataconfig.js
var Sequelize = require('sequelize');
module.exports = new Sequelize('pingan_scame', 'root', '123456', {
host: 'localhost', // 數(shù)據(jù)庫地址
dialect: 'mysql', // 指定連接的數(shù)據(jù)庫類型
operatorsAliases: false,
pool: {
max: 5, // 連接池中最大連接數(shù)量
min: 0, // 連接池中最小連接數(shù)量
idle: 10000 // 如果一個(gè)線程 10 秒鐘內(nèi)沒有被使用過的話,那么就釋放線程
}
});
以上這篇Nodejs + sequelize 實(shí)現(xiàn)增刪改查操作就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
node封裝一個(gè)控制臺進(jìn)度條插件???????詳情
這篇文章主要介紹了node封裝一個(gè)控制臺進(jìn)度條插件???????詳情,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-08-08
package-lock.json解決依賴的版本管理使用詳解
這篇文章主要為大家介紹了package-lock.json解決依賴的版本管理使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08
node.js中RPC(遠(yuǎn)程過程調(diào)用)的實(shí)現(xiàn)原理介紹
這篇文章主要介紹了node.js中RPC(遠(yuǎn)程過程調(diào)用)的實(shí)現(xiàn)原理介紹,本文基于一個(gè)簡單的RPC庫nodejs light_rpc實(shí)現(xiàn),需要的朋友可以參考下2014-12-12
NPM命令運(yùn)行報(bào)錯(cuò):npm?v10.2.4?is?known?not?to?run?on?Node.js
這篇文章主要給大家介紹了關(guān)于NPM命令運(yùn)行報(bào)錯(cuò):npm?v10.2.4?is?known?not?to?run?on?Node.js?v14.21.1的解決辦法,文中將解決辦法介紹的非常詳細(xì),需要的朋友可以參考下2024-01-01
學(xué)習(xí)使用grunt來打包JavaScript和CSS程序的教程
這篇文章主要介紹了學(xué)習(xí)使用grunt來打包JavaScript和CSS程序的教程,grunt基于node.js和需要的朋友可以參考下2016-01-01
使用Node.js創(chuàng)建HTTP服務(wù)器并實(shí)現(xiàn)公網(wǎng)訪問本地Server的步驟
Node.js含有一系列內(nèi)置模塊,使得程序可以脫離 Apache HTTP Server 或 IIS,作為獨(dú)立服務(wù)器運(yùn),下面將介紹如何簡單幾步實(shí)現(xiàn)遠(yuǎn)程公共網(wǎng)絡(luò)下訪問windwos node.js的服務(wù)端,感興趣的朋友一起看看吧2023-11-11
nodejs 十六進(jìn)制字符串型數(shù)據(jù)與btye型數(shù)據(jù)相互轉(zhuǎn)換
這篇文章主要介紹了nodejs 十六進(jìn)制字符串型數(shù)據(jù)與btye型數(shù)據(jù)相互轉(zhuǎn)換,需要的朋友可以參考下2018-07-07

