標籤:
二 msqul sequelize 搭建資料庫
正在發懵中
搭建網站後台資料庫部分,本來想的是使用XAMPP 和ecshop .
但是之後說,我們的網站是node 配合 sequelize 進行資料庫搭建。
okay,lets do it right now;
度娘告訴我關於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 官方API在這裡面
關於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常用指令
3 node mysql
1 http://yh.512876.com/diannao/479.html
2 http://www.jb51.net/article/80393.htm
一下是在摸索過程中的胡言亂語,不成體統
1 建立新的資料庫 creat database mindpush;
2 進入該資料庫 use mindpush;
3 讀出資料庫的所有表 show tables;
4 讀出某一個表中具體資料 select * form thisname;
5 刪除其中某個表 drop table thisname;
通過mysql的cmd 建立一個名為mindpush的資料庫,之後關於資料表的操作全部使用 node來進行。
建立一個資料表:
在項目中建立node.js
//執行個體化sequelize 資料庫連接var Sequelize = require(‘sequelize‘);sequelize = new Sequelize(‘your database name ‘,‘root‘,‘password‘,{ host: "localhost", port: 3306, dialect: ‘mysql‘});//執行個體化sequelize 資料庫連接 --end//建立表var safety = sequelize.define(‘safety‘, { // auto increment, primaryKey, unique id : {type : Sequelize.INTEGER, autoIncrement : true, primaryKey : true, unique : true}, // 這樣的話,表中資料ID 會自己增加 ,不用自己寫 // comment title : {type : Sequelize.STRING, comment : ‘Task title‘}, // allow null description : {type : Sequelize.TEXT, allowNull : true}, // default value deadline : {type : Sequelize.DATE, defaultValue : Sequelize.NOW}});safety .sync({force: true}) .then(function(res){ console.log("res is" + res); })//建立表 --end
運行一下
ok,這樣建立了一個資料表,然後再mql的控制器中輸入
show tables; 就可以讀出有幾個資料表了。
向表中增加資料,繼續向node.js中添加
//插入資料task .create({ description: ‘John Doe‘, title: ‘senior engineer‘ }) .then(function(employee) { console.log(employee.get(‘description‘)); })//尋找資料task .findOne({ where: {title: ‘senior engineer‘} }).then(function(data) { console.log(data.description);})
以下是控制台輸出結果:
在尋找資料的時候,也可以根據ID
safety.findById(1).then(function(employee) { console.log(employee.get(‘id‘)); })
使用尋找or建立
safety .findOrCreate({where: {title: ‘sdepold‘}, defaults: {description: ‘Technical Lead JavaScript‘}}) .spread(function(user, created) { console.log(user.get({ plain: true })) console.log(created) /* 即相當於建立資料如下 { title: ‘sdepold‘, description: ‘Technical Lead JavaScript‘, id: 1, } created: true */ })
從表中刪除資料
// 刪除age 屬性值為 34的資料safety.destroy({ where: { age: ‘34‘ }});
更改表中資料
//將 age: ‘23‘ 的 title:更改為 what you mnjopsafety.update({ title: ‘what you mnjop‘,}, { where: { age: ‘23‘ }});
至此,最最基礎的資料表增刪改查功能過了一遍。
Then , 基本的功能嘗試之後,繼續學習。
Import
可以使用匯入,將模型的定義檔案獨立寫在一個檔案中
// in your server file - e.g. app.jsvar Project = sequelize.import(__dirname + "/path/to/models/project")// The model definition is done in /path/to/models/project.js// As you might notice, the DataTypes are the very same as explained abovemodule.exports = function(sequelize, DataTypes) { return sequelize.define("project", { name: DataTypes.STRING, description: DataTypes.TEXT })}The import method can also accept a callback as an argument.sequelize.import(‘project‘, function(sequelize, DataTypes) { return sequelize.define("project", { name: DataTypes.STRING, description: DataTypes.TEXT })})
下面來看看怎麼模組化建立一個資料表;
將引入sequelize模組 和定義表結構分開
db.js
var Sequelize=require(‘sequelize‘);exports.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
var path = require(‘path‘);module.exports.db = { host: "localhost", port: 3306, user: "root", database: "mindpush", password:"111111"}
統一建立資料表
run.js
var config=require(‘./pro.js‘);config.db = config.db;global.config = config;var sequelize=require(‘./db‘).sequelize();var Sequelize=require(‘sequelize‘);//大量建立資料表sequelize.import(‘./security‘); //建立資料表securitysequelize .sync() .then(function(result){ console.log(‘success‘) });
security.js
module.exports = function (sequelize, DataTypes) { return sequelize.define(‘security‘, { id: { type: DataTypes.BIGINT(11), autoIncrement: true, primaryKey: true, unique: true, comment:‘屬性Id‘ }, name: { type: DataTypes.STRING, allowNull: false, comment:‘屬性名稱‘ } }, { underscore: false, timestamps: false, freezeTableName: true });}
在node 中運行一下run.js ,然後就建立出一個security的資料表。
到今天已經大約半個月的時間了,做出來一個公司簡介。
思路還是有點亂,簡單總結一下。
MP 新版本 [2 mysql sequelize 搭建資料庫]