var Sequelize = require('sequelize');
var sequelize = new Sequelize('db', 'root', 'root', {
host: 'localhost',
dialect: 'MySQL',
pool: {
max: 5,
min: 0,
idle: 10000
},
Sequelize :Sequelize
});
function ApplicationMysql(){
return sequelize;
}
module.exports = new ApplicationMysql();
2. 生成user表
//user.js
var app = require('../server/applicationMysql.js');
User = app.define('user',{
firstName: {
type: app.Sequelize.STRING,
// field: 'first_name' // Will result in an attribute that is firstName when user facing but first_name in the database
},
lastName: {
type: app.Sequelize.STRING
}
},{
freezeTableName: true // Model tableName will be the same as the model name
});
参考文章
一、利用Node代码,创建MYSQL数据库表
var Sequelize = require('sequelize'); var sequelize = new Sequelize('db', 'root', 'root', { host: 'localhost', dialect: 'MySQL', pool: { max: 5, min: 0, idle: 10000 }, Sequelize :Sequelize });
function ApplicationMysql(){ return sequelize; } module.exports = new ApplicationMysql();
//user.js
var app = require('../server/applicationMysql.js'); User = app.define('user',{ firstName: { type: app.Sequelize.STRING, // field: 'first_name' // Will result in an attribute that is firstName when user facing but first_name in the database }, lastName: { type: app.Sequelize.STRING } },{ freezeTableName: true // Model tableName will be the same as the model name });
app.sync().then(function() { User.create({ firstName: 'qi', lastName: 'shuo' }); }); module.exports = User;
sequelize-auto -o "./mysqltest" -d databaseName -h localhost -u username -p 3306 -x password -e mysql
/ jshint indent: 2 /
module.exports = function(sequelize, DataTypes) { return sequelize.define('user', { id: { type: DataTypes.INTEGER(11), allowNull: false, primaryKey: true, autoIncrement: true }, firstName: { type: DataTypes.STRING(255), allowNull: true }, lastName: { type: DataTypes.STRING(255), allowNull: true }, createdAt: { type: DataTypes.DATE, allowNull: false }, updatedAt: { type: DataTypes.DATE, allowNull: false } }, { tableName: 'user' }); };