Nodeのsequelize追加削除
2792 ワード
一、sequelizeモジュールの導入
var Sequelize = require('sequelize');
二、接続データベースvar sequelize=new Sequelize('sample',//データベース名'root',//ユーザー名'psw',//ユーザーパスワード{'dialect':'mysql',//データベースmysql'host':'localhost',//データベースサーバip'port':3306,//データベースサーバポート'define'://フィールド以下のスクライブ()を選択して分割します(デフォルトはアルパカの名前付きスタイル)'underscored':true}).
三、定義表
該当するフィールドには、typeフィールドデータ型(sequlize....)allowNull(空のtrue,falseを許可するかどうか)autoIncrement(自己増加、true,false)unique(一意性、true,false,string)comment(説明)があります.
primaryKey(プライマリ・キーの設定、true、false)defaultValue(デフォルトの設定)field
var User = sequelize.define( 'user', { userId: { field: 'user_id', primaryKey: true, type: Sequelize.BIGINT, allowNull: false }, userName: { field: 'user_name', type: Sequelize.STRING, allowNull: false }, userIcon: { field: 'user_icon', type: Sequelize.STRING, allowNull: true }, title: { field: 'title', type: Sequelize.STRING, allowNull: true }, gender: { field: 'gender', type: Sequelize.ENUM('MALE','FEMALE'), allowNull: true }, birth: { field: 'birth', type: Sequelize.STRING, allowNull: true }, mail: { field: 'mail', type: Sequelize.STRING, allowNull: true }, tel: { field: 'tel', type: Sequelize.STRING, allowNull: true }, mobile: { field: 'mobile', type: Sequelize.STRING, allowNull: true }, updateTime: { field: 'update_time', type: Sequelize.STRING, allowNull: true } }, { tableName: 'user', timestamps: false, freezeTableName: true } );
四、表にデータを追加する.create({userId:23,userName:'楊',updateTime:'2016-01-22 18:37:22'});
五、表のデータを修正する
六、テーブル内のデータuserを削除するdestroy({
where: {
id:5
}
});//テーブル内のIdが5に等しいタプルを削除
七、テーブル内のデータを検索user.findOne({
where: {
id:5
}
})
var Sequelize = require('sequelize');
二、接続データベースvar sequelize=new Sequelize('sample',//データベース名'root',//ユーザー名'psw',//ユーザーパスワード{'dialect':'mysql',//データベースmysql'host':'localhost',//データベースサーバip'port':3306,//データベースサーバポート'define'://フィールド以下のスクライブ()を選択して分割します(デフォルトはアルパカの名前付きスタイル)'underscored':true}).
三、定義表
該当するフィールドには、typeフィールドデータ型(sequlize....)allowNull(空のtrue,falseを許可するかどうか)autoIncrement(自己増加、true,false)unique(一意性、true,false,string)comment(説明)があります.
primaryKey(プライマリ・キーの設定、true、false)defaultValue(デフォルトの設定)field
var User = sequelize.define( 'user', { userId: { field: 'user_id', primaryKey: true, type: Sequelize.BIGINT, allowNull: false }, userName: { field: 'user_name', type: Sequelize.STRING, allowNull: false }, userIcon: { field: 'user_icon', type: Sequelize.STRING, allowNull: true }, title: { field: 'title', type: Sequelize.STRING, allowNull: true }, gender: { field: 'gender', type: Sequelize.ENUM('MALE','FEMALE'), allowNull: true }, birth: { field: 'birth', type: Sequelize.STRING, allowNull: true }, mail: { field: 'mail', type: Sequelize.STRING, allowNull: true }, tel: { field: 'tel', type: Sequelize.STRING, allowNull: true }, mobile: { field: 'mobile', type: Sequelize.STRING, allowNull: true }, updateTime: { field: 'update_time', type: Sequelize.STRING, allowNull: true } }, { tableName: 'user', timestamps: false, freezeTableName: true } );
四、表にデータを追加する.create({userId:23,userName:'楊',updateTime:'2016-01-22 18:37:22'});
五、表のデータを修正する
// roominfo id 1 room_status 0
roominfo.update(
{
room_status: '0'
}, {
'where': { 'id': 1 }
}
).then(ra => {
res.send({
code: 200,
data: r,
mess: ra
})
六、テーブル内のデータuserを削除するdestroy({
where: {
id:5
}
});//テーブル内のIdが5に等しいタプルを削除
七、テーブル内のデータを検索user.findOne({
where: {
id:5
}
})