TCG Development Log (4) sequelize

Source: Internet
Author: User

The advantage of using sequelize is that query in Sequelize is a promise that can be called with await without writing a very complex callback structure.

Then the first installation is:

NPM Install Sequelize--save

NPM install MySQL--save

I used Wamp on this machine and I can see the Visual database in Localhost/phpmyadmin.

First create a local database tcgtest, and then connect it on the server:

Import sequelize from ' sequelize ';

Let sequelize = new Sequelize (' Tcgtest ', ' root ', NULL, {
Host: ' 127.0.0.1 ',
Dialect: ' MySQL '
})

where root and null are account names and passwords, which are local default values.

You can then create a new table, which is defined using the Sequelize model:

var User = sequelize.define (' user ', {
nick:{
Type:Sequelize.STRING
},
password:{
Type:Sequelize.STRING
}
});

Sync can synchronize all the defined model to the database, it has many parameters, see http://itbilu.com/nodejs/npm/VkYIaRPz-.html

After that, you can easily insert, UPDATE, select and other basic operations:

Note: In order to invoke promise with await, all of the query must be written in an async function.

(Async () =>{

await Sequelize.sync ();

Await Sequelize.query ("INSERT into users (Nick, password) VALUES (?,?)", {
Type:sequelize. Querytypes.insert,
Replacements: [' foo ', ' Bar ']
})

Let ret = await sequelize.query ("SELECT * from users", {
Type:sequelize. Querytypes.select
});

})();

You can use Console.log (ret) to see the results, here's a strange place, if you don't specify the type of the event in query,

The results returned are rowdatapacket and do not know why the results are returned two times.

TCG Development Log (4) sequelize

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.