Nodejs + sequelize 實現增刪改查操作
阿新 • • 發佈:2020-11-09
1. 下載資源庫
npm install sequelize --save
npm install mysql2 --save // npm install mysql 提示不完整
2. 建立資料庫配置檔案 db.js,配置資料庫
var Sequelize = require('sequelize'); module.exports = new Sequelize('blog','root','123456',{ host: 'localhost',// 資料庫地址 dialect: 'mysql',// 指定連線的資料庫型別 operatorsAliases: false,pool: { max: 5,// 連線池中最大連線數量 min: 0,// 連線池中最小連線數量 idle: 10000 // 如果一個執行緒 10 秒鐘內沒有被使用過的話,那麼就釋放執行緒 } });
3. 建立一個model 檔案 user.js
var Sequelize = require('sequelize'); var sequelize = require('./db'); // 建立 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建立的表名稱會是複數 users // 如果指定的表名稱本就是複數形式則不變 freezeTableName: true }); /*User.sync({force:false}).then(function(){ console.log("success to start"); }).catch(function(err){ console.log("failed to start ") })*/ // 建立表 // User.sync() 會建立表並且返回一個Promise物件 // 如果 force = true 則會把存在的表(如果users表已存在)先銷燬再建立表 // 預設情況下 forse = false //var user = User.sync({ force: false }); // 新增新使用者 exports.addUser = function(userName,email) { // 向 user 表中插入資料 return User.create({ userName: userName,email: email }).then(function(result){ console.log("插入操作成功"+result); }).catch(function(err){ console.log("新增資料發生錯誤:"+err) }); }; exports.findByName = function(userName) { return User.findOne({where: {user_name:userName }}).then(function(result){ console.log("成功:" + result.id); }).catch(function(err){ console.log("發生錯誤:" + err); }); }; // 通過使用者名稱查詢使用者 exports.update = function(id){ return User.findOne({where: {id:id }}).then(function(user){ return user.update({ email:'[email protected]' }).then(function(result){ console.log("update success: "+result); }).catch(function(err){ console.log("更新操作出錯:"+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','[email protected]'); // 更新 //user.update(1001); //刪除 //user.destroy(1001);
補充知識:nodejs Sequelize 簡單查詢語句和 mysql常用的幾個查詢命令
我是前端,但總有需求讓做後端的活,所以順帶著熟悉了下簡單的查詢語句
貼出來,如果有需要可以參考下,備註很詳細,就不多解釋了
廢話不多說貼程式碼:
#去除unionid 重複的搜尋結果 #query_resultsign 表名 select *,count(unionid) from query_resultsign where issign='false' group by unionid ; #去除unionid 重複的搜尋結果 #query_resultsign 表名 select *,count(unionid) from query_resultsign where issign='true' group by unionid ; #求未簽約使用者的平均訪問頻率(即為求搜尋結果列的平均值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; #求平均值 #(即為求搜尋結果issign='true' count的平均值) #bs為子查詢的別名,不帶別名會報錯 #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 (上一步)增加的列變為自增列 alter table query_resultsign change id id int NOT NULL AUTO_INCREMENT primary key; #獲取兩列資料中有相同資料的列 #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 的呼叫sql語句的方法順帶提一下,網上大多教程都是用model 查詢的,每次都要建立model。有點麻煩 。配置的教程請參看配置教程。
sequelize呼叫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('錯誤',err) }) } //其他方法就是換了下sql語句
主要知識點就是query方法內傳入查詢出的結果的型別 { type: sequelize.QueryTypes.SELECT } 這樣就不用手動轉換成json物件了。
附帶配置檔案程式碼
dataconfig.js
var Sequelize = require('sequelize'); module.exports = new Sequelize('pingan_scame',// 連線池中最小連線數量 idle: 10000 // 如果一個執行緒 10 秒鐘內沒有被使用過的話,那麼就釋放執行緒 } });
以上這篇Nodejs + sequelize 實現增刪改查操作就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支援我們。