码迷,mamicode.com
首页 > 其他好文 > 详细

(三)sequelize postgres增删改

时间:2018-06-06 21:35:46      阅读:341      评论:0      收藏:0      [点我收藏+]

标签:tutorial   save   OLE   val   gre   对象   now()   更新   seq   

build创建实例

let account = Account.build({
    id: Date.now(),
    account: ‘4619test‘,
    password: ‘4619test‘
});

console.log(account);   //account为对象实例

create保存数据到数据库

Account.create({
    id: Date.now(),
    account: ‘4619test‘,
    password: ‘4619test‘
}).then(account => {
    console.log(account);   //保存成功后返回对象实例
}, err => {
    console.log(err);
});

//等同于
 Account.build({
    id: Date.now(),
    account: ‘4619test‘,
    password: ‘4619test‘
  }).save().then(account => {
    console.log(account);
  }, err => {
    console.log(err);
  });

bulkCreate批量创建数据到数据库

Executing (default): INSERT INTO "account" ("id","account","password") VALUES (1528276108795,‘4619test‘,‘4619test‘),(1528276108895,‘4619test1‘,‘4619test1‘);

Account.bulkCreate([{
    id: Date.now() - 100,
    account: ‘4619test‘,
    password: ‘4619test‘
}, {
    id: Date.now(),
    account: ‘4619test1‘,
    password: ‘4619test1‘
}]).then(res => {
    console.log(res);  //返回值为实例数组
}, err => {
    console.log(err);
});

destory删除数据

//实例对象删除
Account.findById(‘1528273928488‘).then(row => {
    return row.destroy(); //row为实例,要先判断是否为null
}).then(() => {
    //删除成功,返回空数组,不接收
});

//模型根据条件删除(多条处理)
Account.destroy({
    where: {           //各种条件,符合就删除
        id: ‘test7‘
    }
}).then(res => {
    console.log(res);  //返回影响条数
}, err => {
    console.log(err);
});

update更新数据

Executing (default): UPDATE "account" SET "account"=‘update‘ WHERE "id" = ‘1528276108795‘

//修改数据放在方法中,实例对象更新
Account.findById(‘1528276108795‘).then(row => {
    return row.update({
        account: ‘update‘
    });
}).then(res => {
    console.log(res); //返回修改后的实例对象
}, err => {
    console.log(err);
});

Executing (default): UPDATE "account" SET "account"=‘461912‘ WHERE "password" IN (‘4619test‘, ‘4619test1‘)

//模型根据条件更新(多条处理)
Account.update({
    account: ‘461912‘
}, {
    where: {
        password: [‘4619test‘, ‘4619test1‘]
    }
}).then(res => {
    console.log(res);  //[2]
}, err => {
    console.log(err);
});

save更新数据

//需要先赋值后修改
Account.findById(‘1528276108795‘).then(row => {
    row.account = ‘save‘;
    return row.save();
}).then(res => {
    console.log(res); //返回修改后的实例对象
}, err => {
    console.log(err);
});

sequelize Instances

(三)sequelize postgres增删改

标签:tutorial   save   OLE   val   gre   对象   now()   更新   seq   

原文地址:https://www.cnblogs.com/chenmengyuan/p/9083032.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!