美文网首页
关系模型的操作

关系模型的操作

作者: royluck | 来源:发表于2020-02-24 09:13 被阅读0次

    Sequelize 系列教程之一对一模型关系
    Sequelize 系列教程之一对多模型关系
    Sequelize 系列教程之多对多模型关系
    Node.js Sequelize 模型(表)之间的关联及关系模型的操作
    Sequelize 操作 MySQL数据库方法
    Sequelize 多对多的创建、查询、更新

    • 一对一:
    // User的实例对象将拥有getAccount、setAccount、createAccount方法
    User.hasOne(Account); // 在target模型中插入关联键
    // Account的实例对象将拥有getUser、setUser、createUser方法
    Account.belongsTo(User); // 将关联键插入到source模型中
    

    新增:

    const user = await User.create({ empId: '1' }); // (1)
    const account = await user.createAccount({ email: 'semlinker@gmail.com' }); // (2)
    

    修改:

    const user = await User.findById(1); // (1)
    const newAccount = await Account.create({ email: 'sequelize@gmail.com' }); // (2)
    user.setAccount(newAccount); // (3)
    

    删除:

    const user = await User.findById(1); // (1)
    user.setAccount(null); // (2)
    

    查询:

    const user = await User.findById(1); // (1)
    user.getAccount(); // (2)
    

    在应用层要保证数据一致性,我们就需要遵循良好的编码约定。新增用户账号时使用 user.createAccount 方法,更新用户账号时就使用 user.setAccount 方法。


    • 一对多:
    // User的实例对象将拥有:getNotes、setNotes、addNote、createNote、removeNote、hasNote方法
    User.hasMany(Note);
    // Note的实例对象将拥有getUser、setUser、createUser方法
    Note.belongsTo(User);
    

    新增:

    const user = await User.create({ empId: '1' }); // (1)
    const note = await user.createNote({ title: 'learn sequelize' }); // (2)
    
    const user = await User.create({ empId: '1' }); // (1)
    const note = await Note.create({ title: 'learn sequelize' }); // (2)
    await user.addNote(note); // (3)
    

    修改:

    const user = await User.create({ empId: '1' }); // (1)
    const note1 = await user.createNote({ title: 'learn node.js' }); // (2)
    const note2 = await user.createNote({ title: 'learn rx.js' }); // (3)
    
    const note3 = await Note.create({ title: 'learn angular.js' }); // (4)
    const note4 = await Note.create({ title: 'learn typescript.js' }); // (5)
    await user.setNotes([note3, note4]); // (6)
    

    删除:

    const user = await User.create({ empId: '1' }); // (1)
    const note1 = await user.createNote({ title: 'learn node.js' }); // (2)
    const note2 = await user.createNote({ title: 'learn rx.js' }); // (3)
    await user.setNotes([]); // (4)
    
    const user = await User.create({ empId: '1' }); // (1)
    const note1 = await user.createNote({ title: 'learn node.js' }); // (2)
    const note2 = await user.createNote({ title: 'learn rx.js' }); // (3)
    user.removeNote(note2);
    

    查询:

    // 查询当前用户下所有满足条件的 note 数据:
    const Op = Sequelize.Op
    const user = await User.findById(1); // (1)
    const notes = await user.getNotes({ // (2)
      where: {
        title: {
          [Op.like]: '%node%'
        }
      }
    });
    
    // 查询所有满足条件的 note,同时获取 note 所属的 user:
    const Op = Sequelize.Op
    const notes = await Note.findAll({
      include: [User],
      where: {
        title: {
          [Op.like]: '%node%'
        }
     }
    });
    
    // 查询所有满足条件的 user,同时获取该 user 所有满足条件的 note:
    const Op = Sequelize.Op
    const users = await User.findAll({
      include: [Note],
      where: {
        createdAt: {
          [Op.lt]: new Date()
        }
     }
    });
    
    • 多对多:
    // Note的实例拥有getTags、setTags、addTag、addTags、createTag、 removeTag、hasTag方法
    Note.belongsToMany(Tag, { through: Tagging });
    // Tag的实例拥有getNotes、setNotes、addNote、addNotes、createNote、removeNote、hasNote方法
    Tag.belongsToMany(Note, { through: Tagging });
    

    新增:

    const note = await Note.create({ title: 'note' }); // (1)
    await note.createTag({ name: 'tag' }, { through: { type: 0 }}); // (2)
    
    const note = await Note.create({ title: 'note' });
    const tag = await Tag.create({ name: 'tag' });
    await note.addTag(tag, { through: { type: 0 } });
    
    const note = await Note.create({ title: 'note' }); // (1)
    const tag1 = await Tag.create({ name: 'tag1' }); // (2)
    const tag2 = await Tag.create({ name: 'tag2' }); // (3)
    await note.addTags([tag1, tag2], { through: { type: 2 }}); // (4)
    

    修改:

    const note = await Note.create({ title: 'note' });
    const tag1 = await Tag.create({ name: 'tag1'});
    const tag2 = await Tag.create({ name: 'tag2'});
    await note.addTags([tag1, tag2], { through: { type: 2 }});
    
    const tag3 = await Tag.create({ name: 'tag3'}); // (1)
    const tag4 = await Tag.create({ name: 'tag4'}); // (2)
    await note.setTags([tag3, tag4], { through: { type: 3 }}); // (3)
    

    删除:

    // 删除单条记录
    const note = await Note.create({ title: 'note' });
    const tag1 = await Tag.create({ name: 'tag1' });
    const tag2 = await Tag.create({ name: 'tag2' });
    await note.addTags([tag1, tag2], { through: { type: 2 }});
    
    await note.removeTag(tag1); // (1)
    
    // 全部删除
    const note = await Note.create({ title: 'note' });
    const tag1 = await Tag.create({ name: 'tag1' });
    const tag2 = await Tag.create({ name: 'tag2' });
    await note.addTags([tag1, tag2], { through: { type: 2 }});
    
    await note.setTags([]); // (1)
    

    查询:

    // 查询当前 note 中所有满足条件的 tag:
    const Op = Sequelize.Op
    const tags = await note.getTags({
      where: {
        name: {
          [Op.like]: 'tag%'
        }
      }
    });
    
    // 询所有满足条件的 tag,同时获取每个 tag 所在的 note:
    const tags = await Tag.findAll({
      include: {
        model: Note
      }
    });
    
    // 查询所有满足条件的 note,同时获取每个 note 相关联的 tag:
    const notes = await Note.findAll({
      include: [
        {
           model: Tag // 支持tags设置查询条件
        }
      ]
    });
    

    相关文章

      网友评论

          本文标题:关系模型的操作

          本文链接:https://www.haomeiwen.com/subject/cndjqhtx.html