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