Nodejs connecting MySQL Database

Source: Internet
Author: User

Search for Orm2 https://github.com/dresende/node-orm2 on GitHub;

Download it in the project folder using NPM install ORM and then write the configuration file

varORM = require ("ORM"); Orm.connect ("Mysql://username:[email Protected]/database",function(err, db) {if(ERR)Throwerr; varperson = db.define (' person ', {name:string, surname:string, Age:number,//FLOATMale:boolean, continent: ["Europe", "America", "Asia", "Africa", "Australia", "Antartica"],//ENUM TypePhoto:buffer,//blob/binaryData:object//JSON encoded}, {methods: {fullName:function () {                return  This. Name + "+ This. Surname; }}, validations: {Age:orm.enforce.ranges.number (Undefined, "under-age")        }    }); //Add the table to the databaseDb.sync (function(err) {if(ERR)Throwerr; //add a row to the person tablePerson.create ({id:1, Name: "John", Surname: "Doe", age:27},function(err) {if(ERR)Throwerr; //query the person table by surnamePerson.find ({surname: "Doe"},function(err, people) {//SQL: "SELECT * from the person WHERE surname = ' Doe '"                    if(ERR)Throwerr; Console.log ("People found:%d", people.length); Console.log ("First Person:%s, age%d", People[0].fullname (), people[0].age); people[0].age = 16; people[0].save (function(err) {//err.msg = "under-age";                });        });    }); });});

Specific configuration View ORM documentation on GitHub

Nodejs connecting MySQL Database

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.