Node sequelize ORM关系库

14 篇文章 0 订阅
10 篇文章 0 订阅
  • sequelize star:18.2k

https://github.com/demopark/sequelize-docs-Zh-CN

安装

yarn add sequelize mysql mysql2 -s

连接

  const sequelize = new Sequelize("test_orm", "root", "root", {
    host: "localhost",
    port: "3306",
    dialect: "mysql"
  });

定义模型

  • 手动定义
 const Author = sequelize.define(
    "author",
    {
      // id: Sequelize.INTEGER(15),
      age: Sequelize.STRING(11),
      name: Sequelize.STRING(20)
    },
    {
      tableName: "author"
    }
  );
  • 根据MySQL库自动生成
 npm i -g sequelize-auto

sequelize-auto -d database -u user -x pwd -h host -p port
sequelize-auto -d test_orm -u root -x root -p 3306 -h localhost
// 自动生成到命令行文件夹下 /models/

MySQL表关系定义

// 一对一

//每一篇 文章根据 author_id 对应一个作者

// 作者关联到文章  通过作者查文章
Author.hasOne(Article, { foreignKey: 'author_id' })
// 文章关联到文章 ,通过文章查作者
Article.belongsTo(Author, { foreignKey: 'author_id' })


// 一对多
// 每一个作者有多个文章
  Author.hasMany(Article, { foreignKey: "author_id" });
// 多对多

查询

  • find - 搜索数据库中的一个特定元素
// 搜索已知的ids
Project.findById(123).then(project => {
  // project 将是 Project的一个实例,并具有在表中存为 id 123 条目的内容。
  // 如果没有定义这样的条目,你将获得null
})


// 搜索属性
Project.findOne({ where: {title: 'aProject'} }).then(project => {
  // project 将是 Projects 表中 title 为 'aProject'  的第一个条目 || null
})
// 查找一条
Project.findOne({
  where: {title: 'aProject'},
  attributes: ['id', ['name', 'title']]
}).then(project => {
  // project 将是 Projects 表中 title 为 'aProject'  的第一个条目 || null
  // project.title 将包含 project 的 name
})


// findAll - 搜索数据库中的多个元素
Author.findAll()
    .then(result => {
      console.log(result);
      // res.send("查询成功");
      res.type("json");
      res.json(result);
    })
    .catch(err => {
      res.status(500).end(`查询出错::${err.message}`);
    });

// 连表查询
Article.findAll({
    // where:{id:12},
    include: [Author]
  })
    .then(result => {
      res.type("json");
      res.json({ status: "ok", result });
    }) //删除成功的回调
    .catch(err => {
      res.json({ message: err.message });
    });

更新

// values  options
  Author.update(
    {
      name: "update_fdsafsdafdasfdsa"
    },
    {
      where: { id } //where是指定查询条件
    }
  )
    .then(result => {
      res.type("json");
      res.json({ status: "ok" });
    })
    .catch(err => {
      res.json({ message: "更新错误" });
    });

插入

//插入一条
  Author.create({ name: "name_add", age: 20 })
    .then(result => {
      console.log(result);
      res.json({ status: "ok" });
    })
    .catch(e => {
      res.json({ status: "error", message: e.message });
    }); //异常捕获

// 批量插入
//itemsList:Array[Object]
 Article.bulkCreate(itemsList)
        .then(result => {
          res.json({ message: "插入成功",result:itemsList });
        })
        .catch(err => {
          res.status(500).json({ message: "插入失败", errMsg: err.message });
        });

删除

 Author.destroy({
    where: { id } //where是指定查询条件
  })
    .then(result => {
      res.type("json");
      res.json({ status: "ok" });
    }) //删除成功的回调
    .catch(err => {
      res.json({ message: "删除错误" });
    });
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值