MP new version [2 MySQL sequelize build database]

Source: Internet
Author: User
Tags lua

Two Msqul sequelize Building database

is in the middle of a rip

Build the site back-end database part, originally thought is uses XAMPP and Ecshop.
But then said, our website is node with sequelize to build the database.
Okay,lets do it right now;

Degree Niang tell me about Sequelize:

1 http://www.cnblogs.com/showtime813/p/4512699.html
2 HTTP://CNODEJS.ORG/TOPIC/5201C94144E76D216A39C4DC
3 http://blog.csdn.net/jimscx/article/details/45701921
4 http://my.oschina.net/zj0303/blog/305384
5 http://www.phperz.com/article/15/1113/169037.html
6 Official API in this area

About MySQL

1 http://blog.chinaunix.net/uid-12707183-id-2918849.html
2 Http://www.cnblogs.com/mr-wid/archive/2013/05/09/3068229.html#d22
3 MySQL Common commands

3 node MySQL
1 http://yh.512876.com/diannao/479.html
2 http://www.jb51.net/article/80393.htm

A bit of gibberish in the process of groping, improper

1 Creating new databases creat database Mindpush;

2 access to the database use Mindpush;
3 read out all tables of the database show tables;
4 read out specific data in a table select * Form Thisname;
5 Delete One of the tables drop table thisname;

Create a database called Mindpush with MySQL cmd, and then use node for all of the data table operations.

Create a data table:
Create node. js in your project

//Instantiate Sequelize database connectionvarSequelize= require(' Sequelize '); sequelize= NewSequelize (' Your database name ',' Root ',' Password ', {host:"localhost", Port:3306, dialect:' MySQL '});//Instantiate Sequelize database connection--end//Create a tablevarSafety=Sequelize.Define(' safety ', {//Auto increment, PrimaryKey, uniqueID: {type: sequelize.INTEGER, AutoIncrement:true, PrimaryKey:true, Unique:true},//In this case, the data ID in the table will increase itself without writing    //CommentTitle: {type: sequelize.STRING, Comment:' Task title '},//Allow nullDescription: {type: sequelize.TEXT, Allownull:true},//Default valueDeadline: {type: sequelize.DATE, defaultvalue:sequelize.Now}}); safety.Sync ({force:true}).Then (function (res) {Console.Log("Res is" +RES); })//CREATE TABLE--end

Run for a minute

OK, this creates a data table and then enters the controller of MQL
Show tables; You can read a few data sheets.

Add data to the table and continue adding to node. js

//Insert data  task. Create ({ Description:  ' John Doe ' , title:  ' senior Engineer '  })  . then  (function   (employee)  { console.  Log (employee.    Get ( ' description ' )); }) //find Data  task. FindOne ({where: {title:  ' senior Engineer ' }}). then  (function   { console.  Log (data.  description);})  

The following are the console output results:

When looking for data, it is also possible to

safety.findById(1).then(function(employee) {   console.log(employee.get(‘id‘)); })

Create using Find or

safety  ‘sdepold‘},                 ‘Technical Lead JavaScript‘}}) .spread(function(user, created) {    console.log(user.get({      true    }))    console.log(created)    /* 即相当于创建数据如下      {        ‘sdepold‘,        ‘Technical Lead JavaScript‘,        1,      }      true    */  })

Delete data from a table

//  删除age 属性值为 34的数据safety.destroy({  where: {    ‘34‘  }});

Change the data in a table

//将 age: ‘23‘ 的 title:更改为 what you mnjopsafety.update({  title: ‘what you mnjop‘,}, {  where: {    age: ‘23‘  }});

At this point, the most basic data table additions and deletions to check the function over again.

Then, after the basic function is tried, continue to learn.

Import

You can use the import to write the definition file of the model independently in a file

//in your server file-e.g. App.jsvarProject=Sequelize.Import(__dirname+ "/path/to/models/project")//The model definition is done in/path/to/models/project.js//As you might notice, the datatypes is the very same as explained aboveModule.Exports=function (Sequelize, datatypes) {returnSequelize.Define("Project", {name:datatypes.STRING, Description:datatypes.TEXT})}theImportMethod can also accept a callback as an argument.Sequelize.Import(' Project ', function (Sequelize, datatypes) {returnSequelize.Define("Project", {name:datatypes.STRING, Description:datatypes.TEXT})})

Here's a look at how to create a data table in a modular format;
Separates the introduction of the Sequelize module from the definition table structure
Db.js

var Sequelize=require(‘sequelize‘function () {    var sequelize=new Sequelize(config.db.database,config.db.user, config.db.password,{        host:config.db.host,        port:config.db.port,logging: console.log});    return sequelize;}

Pro.js

varrequire(‘path‘);module.exports.db = {    host"localhost",    port3306,    user"root",    database"mindpush",    password:"111111"}

Unify the creation of data tables
Run.js

var  config=require  (/pro.js ' ); config.db = config.db; global . config = config; var  sequelize=require  ( ". Sequelize (); var  Sequelize=require  ( ' sequelize ' ); //bulk create data table  Sequelize.import ("/security ' ); //create data Table security  sequelize. Sync (). then  (function   (result )  {console.log (' Success ')} );  

Security.js

Module. Exports = function (sequelize, datatypes) {returnSequelize.define (' security ', {ID: {type: Datatypes.bigint ( One), AutoIncrement:true, PrimaryKey:true, Unique:true, Comment:' attribute ID '}, Name: {type: Datatypes.string, Allownull:false, Comment:' attribute name '}}, {underscore:false, Timestamps:false, Freezetablename:true});}

Run the Run.js in node and create a security data table.

It has been about half a month now, to make a company profile.

The idea is still a little messy, a simple summary.

MP new version [2 MySQL sequelize build database]

Related Article

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.