Database model
/model/db.js database Operation class, complete the link database and database additions and deletions to check and change
Query table
/* Enquiry */ 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 a record
/*Add*/Add:function(tablename,tabledata,callback) {varSql= "INSERT INTO" + This. C.db_pre+TableName; varClumn= "; varValue= "; for(varKeyinchtabledata) {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); }
Modify a record
/*Modify*/Update:function(tablename,tabledata,where,callback) {varSql= "Update" + This. C.db_pre+tablename+ "Set"; varClumns= ""; for(varKeyinchtabledata) {Clumns+ = "," +key+ "= '" +tabledata[key]+ "'"; } clumns=CLUMNS.SUBSTR (1); SQL+=clumns+ "where" +where; Console.log (SQL); This. Db.query (Sql,callback); }
Deleting records
/* Delete */ Delete:function(tablename,where,callback) { var sql= "Delete from" + this. C.db_pre+tablename+ "where" +where; Console.log (SQL); This . Db.query (Sql,callback); }
Business model
such as the classification model,/model/category.js
/** * Classification model **/Module.exports={getalllist:function() {Db.select ("Category",function(err,list) {console.log (list); }); }, /*Add*/addcate:function(data) {Db.add ("Category", data,function(err,list) {console.log (err); }); }, /*Modify*/savecate:function(data,where) {db.update ("Category", Data,where,function(err,list) {console.log (err); }); }, /*Delete*/delcate:function(where) {db.Delete("category", where,function(err,list) {//Console.log (err); }); }};
Controller
Add a method to call the model in the Public function file first
/* instantiating a model */ Model:function(name) { return require (".. /model/"+name); }
Controller Invoke business model
/** * Home Controller*/varRouter=Express. Router (); Router.get (‘/‘,function(Req,res,next) {F.model ("Category"). Getalllist (); //F.model ("category"). Addcate ({"Name": "Test"}); //F.model ("category"). Savecate ({"Name": "Test 1"}, "id=4"); //F.model ("category"). Delcate ("id=4"); /*Render Template*/Res.render ("Home/index");}); Module.exports=router;
[Nodejs] Nodejs Development Personal Blog (iv) data model