欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Nodejs + sequelize 實(shí)現(xiàn)增刪改查操作

 更新時(shí)間:2020年11月07日 10:51:29   作者:sorrycx  
這篇文章主要介紹了Nodejs + sequelize 實(shí)現(xiàn)增刪改查操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧

1. 下載資源庫(kù)

npm install sequelize --save

npm install mysql2 --save // npm install mysql 提示不完整

2. 創(chuàng)建數(shù)據(jù)庫(kù)配置文件 db.js,配置數(shù)據(jù)庫(kù)

var Sequelize = require('sequelize'); 
module.exports = new Sequelize('blog', 'root', '123456', {
  host: 'localhost', // 數(shù)據(jù)庫(kù)地址
  dialect: 'mysql', // 指定連接的數(shù)據(jù)庫(kù)類(lèi)型
  operatorsAliases: false,
  pool: {
    max: 5, // 連接池中最大連接數(shù)量
    min: 0, // 連接池中最小連接數(shù)量
    idle: 10000 // 如果一個(gè)線程 10 秒鐘內(nèi)沒(méi)有被使用過(guò)的話,那么就釋放線程
  }
});

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, // 指定值的類(lèi)型
    field: 'user_name' // 指定存儲(chǔ)在表中的鍵名稱(chēng)
  },
  // 沒(méi)有指定 field,表中鍵名稱(chēng)則與對(duì)象鍵名相同,為 email
  email: {
    type: Sequelize.STRING
  }
}, {
  // 如果為 true 則表的名稱(chēng)和 model 相同,即 user
  // 為 false MySQL創(chuàng)建的表名稱(chēng)會(huì)是復(fù)數(shù) users
  // 如果指定的表名稱(chēng)本就是復(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() 會(huì)創(chuàng)建表并且返回一個(gè)Promise對(duì)象
// 如果 force = true 則會(huì)把存在的表(如果users表已存在)先銷(xiāo)毀再創(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);
    });
};

// 通過(guò)用戶名查找用戶
 
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. 測(cè)試文件

var user = require('./user');
//查詢操作
//user.findByName("jack");
// 添加用戶
//user.addUser('jack2', 'jack@163.com');
// 更新
//user.update(1001);
//刪除
//user.destroy(1001);

補(bǔ)充知識(shí):nodejs Sequelize 簡(jiǎn)單查詢語(yǔ)句和 mysql常用的幾個(gè)查詢命令

我是前端,但總有需求讓做后端的活,所以順帶著熟悉了下簡(jiǎn)單的查詢語(yǔ)句

貼出來(lái),如果有需要可以參考下,備注很詳細(xì),就不多解釋了

廢話不多說(shuō)貼代碼:

#去除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 ;
 
#求未簽約用戶的平均訪問(wèn)頻率(即為求搜索結(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為子查詢的別名,不帶別名會(huì)報(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語(yǔ)句的方法順帶提一下,網(wǎng)上大多教程都是用model 查詢的,每次都要建立model。有點(diǎn)麻煩 。配置的教程請(qǐng)參看配置教程。

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語(yǔ)句

主要知識(shí)點(diǎn)就是query方法內(nèi)傳入查詢出的結(jié)果的類(lèi)型 { type: sequelize.QueryTypes.SELECT } 這樣就不用手動(dòng)轉(zhuǎn)換成json對(duì)象了。

附帶配置文件代碼

dataconfig.js

var Sequelize = require('sequelize'); 
module.exports = new Sequelize('pingan_scame', 'root', '123456', {
  host: 'localhost', // 數(shù)據(jù)庫(kù)地址
  dialect: 'mysql', // 指定連接的數(shù)據(jù)庫(kù)類(lèi)型
  operatorsAliases: false,
  pool: {
    max: 5, // 連接池中最大連接數(shù)量
    min: 0, // 連接池中最小連接數(shù)量
    idle: 10000 // 如果一個(gè)線程 10 秒鐘內(nèi)沒(méi)有被使用過(guò)的話,那么就釋放線程
  }
 
});

以上這篇Nodejs + sequelize 實(shí)現(xiàn)增刪改查操作就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論