nodejs個人博客開發(fā)第四步 數(shù)據(jù)模型
更新時間:2017年04月12日 11:52:47 作者:陶士涵
這篇文章主要為大家詳細介紹了nodejs個人博客開發(fā)的數(shù)據(jù)模型,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文為大家分享了nodejs個人博客開發(fā)的數(shù)據(jù)模型,具體內(nèi)容如下
數(shù)據(jù)庫模型
/model/db.js 數(shù)據(jù)庫操作類,完成鏈接數(shù)據(jù)庫和數(shù)據(jù)庫的增刪查改
查詢表
/*查詢*/
select:function(tableName,callback,where,field){
field=field ? field : '*';
var sql="select "+field+" from "+this.C.DB_PRE+tableName;
if(where){
sql+=" where "+where;
}
this.db.query(sql,callback);
}
添加記錄
/*添加*/
add:function(tableName,tableData,callback){
var sql="insert into "+this.C.DB_PRE+tableName;
var clumn='';
var value='';
for(var key in tableData){
clumn+=","+key;
value+=",'"+tableData[key]+"'";
}
clumns="("+clumn.substr(1)+")";
values="("+value.substr(1)+")";
sql=sql+clumns+"values"+values;
console.log(sql);
this.db.query(sql,callback);
}
修改記錄
/*修改*/
update:function(tableName,tableData,where,callback){
var sql="update "+this.C.DB_PRE+tableName+" set ";
var clumns="";
for(var key in tableData){
clumns+=","+key+"='"+tableData[key]+"'";
}
clumns=clumns.substr(1);
sql+=clumns+" where "+where;
console.log(sql);
this.db.query(sql,callback);
}
刪除記錄
/*刪除*/
delete:function(tableName,where,callback){
var sql="delete from "+this.C.DB_PRE+tableName+" where "+where;
console.log(sql);
this.db.query(sql,callback);
}
業(yè)務模型
例如分類模型,/model/category.js
/**
*分類模型
*
*/
module.exports={
getAllList:function(){
db.select("category",function(err,list){
console.log(list);
});
},
/*添加*/
addCate:function(data){
db.add("category",data,function(err,list){
console.log(err);
});
},
/*修改*/
saveCate:function(data,where){
db.update("category",data,where,function(err,list){
console.log(err);
});
},
/*刪除*/
delCate:function(where){
db.delete("category",where,function(err,list){
//console.log(err);
});
}
};
控制器
先在公共函數(shù)文件增加一個調(diào)用模型的方法
/*實例化模型*/
model:function(name){
return require("../model/"+name);
}
控制器調(diào)用業(yè)務模型
/**
* 首頁控制器
*/
var router=express.Router();
router.get('/',function(req,res,next){
F.model("category").getAllList();
//F.model("category").addCate({"name":"測試"});
//F.model("category").saveCate({"name":"測試1"},"id=4");
//F.model("category").delCate("id=4");
/*渲染模板*/
res.render("home/index");
});
module.exports=router;
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
基于Node.js和Socket.IO實現(xiàn)實時通信功能
在現(xiàn)代網(wǎng)絡應用中,實時通信變得越來越重要,Node.js,作為一個JavaScript運行環(huán)境,而Socket.IO則為Node.js提供了一個強大的實時通信庫,本文將通過一個簡單的示例,展示如何使用Node.js和Socket.IO創(chuàng)建一個能夠?qū)崿F(xiàn)實時通信的服務器,需要的朋友可以參考下2024-11-11
用C/C++來實現(xiàn) Node.js 的模塊(二)
上篇文章的主要內(nèi)容講訴了用C/C++來實現(xiàn) Node.js 的模塊,本文更深一步繼續(xù)探討這個問題,有需要的朋友可以參考下2014-09-09

