[Nodejs] Nodejs Development Personal Blog (iv) data model

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.