YOU'VE MADE A BRAVE DECISION, WELCOME.

每一个不曾起舞的日子都是对生命的辜负。

学习笔记:Migrate-Nodejs

用express框架,node语言和sequelize的ROM操纵MySQL做练习。

前期准备

1.用express搭起框架,在命令行输入$ express demo,利用npm和bower安装所需要的依赖,并管理相关依赖,部分插件如下:

1
2
3
4
5
6
7
8
$npm install
$npm install mysql —save-dev
$npm install sequelize —save-dev
$npm install sequelize-cli —save-dev
$bower init
$bower install bootstrap —save-dev
$bower install jQuery —save-dev

2.添加.gitignore在根目录下,用来忽略bower_components/、node_modules/和bin/里的内容。


用Sequelize进行初始化

在根目录下输入$sequelize init后,会在当前目录下新建models/、seeders/、migrations/和config/文件夹。

可以用$seqlelize help查看帮助命令,显示如下命令:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
Usage
sequelize [task]
Available tasks
db:migrate Run pending migrations.
db:migrate:old_schema Update legacy migration table
db:migrate:undo Revert the last migration run.
db:migrate:undo:all Revert all migrations ran.
db:seed Run specified seeder.
db:seed:all Run every seeder.
db:seed:undo Deletes data from the database.
db:seed:undo:all Deletes data from the database.
help Display this help text. Aliases: h
init Initializes the project.
init:config Initializes the configuration.
init:migrations Initializes the migrations.
init:models Initializes the models.
init:seeders Initializes the seeders.
migration:create Generates a new migration file. Aliases: migration:generate
model:create Generates a model and its migration. Aliases: model:generate
seed:create Generates a new seed file. Aliases: seed:generate
version Prints the version number. Aliases: v
Available manuals
help:db:migrate The documentation for "sequelize db:migrate".
help:db:migrate:old_schema The documentation for "sequelize db:migrate:old_schema".
help:db:migrate:undo The documentation for "sequelize db:migrate:undo".
help:db:migrate:undo:all The documentation for "sequelize db:migrate:undo:all".
help:db:seed The documentation for "sequelize db:seed".
help:db:seed:all The documentation for "sequelize db:seed:all".
help:db:seed:undo The documentation for "sequelize db:seed:undo".
help:db:seed:undo:all The documentation for "sequelize db:seed:undo:all".
help:init The documentation for "sequelize init".
help:init:config The documentation for "sequelize init:config".
help:init:migrations The documentation for "sequelize init:migrations".
help:init:models The documentation for "sequelize init:models".
help:init:seeders The documentation for "sequelize init:seeders".
help:migration:create The documentation for "sequelize migration:create".
help:model:create The documentation for "sequelize model:create".
help:seed:create The documentation for "sequelize seed:create".
help:version The documentation for "sequelize version".

使用Available manuals可以看各种命令的使用方法,建立model可以同时建立model和migration。例如:

$sequelize help:model:create

1
2
3
4
5
The attributes can be specified as in the following (and semantically equal) examples:
sequelize model:create --name User --attributes first_name:string,last_name:string,bio:text
sequelize model:create --name User --attributes 'first_name:string last_name:string bio:text'
sequelize model:create --name User --attributes 'first_name:string, last_name:string, bio:text'

输入命令就可以建立model和migration文件。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// the model file
// located under models/user.js
'use strict';
module.exports = function(sequelize, DataTypes) {
var User = sequelize.define('User', {
first_name: DataTypes.STRING,
last_name: DataTypes.STRING,
bio: DataTypes.TEXT
}, {
classMethods: {
associate: function(models) {
// associations can be defined here
}
}
});
return User;
};
// the migration file
// located under migrations/20160710153131-create-user.js
'use strict';
module.exports = {
up: function(queryInterface, Sequelize) {
return queryInterface.createTable('Users', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
first_name: {
type: Sequelize.STRING
},
last_name: {
type: Sequelize.STRING
},
bio: {
type: Sequelize.TEXT
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
down: function(queryInterface, Sequelize) {
return queryInterface.dropTable('Users');
}
};

使用$sequelize db:migrate,可以执行migration/里的文件,建立MySQL里的表。 根目录下的config/文件夹里的config.json写着连接数据库的信息。 之后在controller/文件夹下调用model里的方法或sequelize里原生的方法使用数据库。


参考目录

更多关于Sequelize的详细用法,请参见其官方文档