Recently started to contact Sequelize, which encountered a lot of pits, so want to write an introduction to sequelize and everyone to share, to avoid the same friends and I climb the pit (Tengyun technology ty300.com). Learning Sequelize's original intention is to solve SQL injection, it supports MySQL, SQLite, MariaDB and MSSQL, the article with MySQL example (introductory tutorial qkxue.net).
First step: Instantiate the Sequelize database connection
var sequelize = require (' sequelize ');
var sequelize = new Sequelize (' database ', ' user ', ' password ', {host: ' xxx ', port: ' xxx ', dialect: ' MySQL '});
Of course, I just use the simplest connection method, the specific parameters see http://www.nodeclass.com/api/sequelize.html
The second step: defining the model, in layman terms, is to define your table structure.
Copy Code
var a= sequelize.define (' a ', {
ID: {Type:Sequelize.STRING, max:20},
Typess: {Type:Sequelize.STRING, max:20},
Startstation: {Type:Sequelize.STRING, max:20},
Endstation: {Type:Sequelize.STRING, max:20},
R_date: {Type:Sequelize.STRING, max:20},
Distance: {Type:Sequelize.STRING, max:20}
},{Freezetablename:true,
Timestamps:false})
Copy Code
Define inside the first parameter is the table name, the second parameter is the definition of the field, here I just the simplest definition of type, length, class such as whether to allow null, default value and so on you need to reference the document; The third parameter is provided to the Sequelize constructor using the DEFAULT definition option if it is not filled in. Here I climbed the pit for a long time ...
A brief introduction to the two parameters:
Freezetablename: The default is False, when false, if you already have table A in the database, Sequelize will modify your table name, the result of my test is to help my table name "A" added a s, resulting in my query table A's data, always reported that the table does not exist.
Timestamps: The default is true, which adds two additional fields "Createdat", "Updatedat" to your table.
Sequelize v3.24.3 and V4.0.0-2 released, Sequelize.js provides simple access to the Mysql,mariadb,sqlite and PostgreSQL databases by mapping database entries to objects, or object-to-database entries. In short, it is ORM (object-relational-mapper). Sequelize.js is completely written in JavaScript and works for the Environment of node. js.
Sequelize v4.0.0-2 Update content:
[ADDED] include now supports string as a argument (on top of model/association), string would expand into an association m Atched literally from Model.associations
[FIXED] Accept dates As String while using Typevalidation #6453
[FIXED]-ORDER clause is not included in subquery if ORDER option value is provided as plain string (not as an array VA Lue
[FIXED] Support-CLS with Cls-bluebird module
Sequelize Chinese Course