美文网首页
Sequelize 的增删查改

Sequelize 的增删查改

作者: RoyChina | 来源:发表于2019-04-22 22:58 被阅读0次

文章转载自:https://www.jianshu.com/p/8242b841de9d

模型定义

var Task = sequelize.define( 'task', {
   title: Sequelize.STRING,
   rank: { type: Sequelize.STRING, defaultValue: 3 }
} );

Create

// 创建数据库中对象
Task.create( { title: 'foo' } );

// 创建临时变量
var task = Task.build( { title: 'very important task' } );

// 将临时变量存入数据库内, 只保存 title 属性
task.save( { fields: [ 'title' ] } );

Update

// 在数据库中更新 task 的 title
task.update( { title: 'a very different title now' } );


// 更新 排名小于1000或者无排名的 post 的 updateAt 为 null
Post.update( {
    updatedAt: null,
}, {
    where: {
        rank: {
            $or: {
                $lt: 100,
                $eq: null
            }
        }
    }
    //// rank < 1000 OR rank IS NULL
} );

Delete

// 删除 不活跃的 post
Post.destroy( {
    where: {
        status: 'inactive'
    }
} );

Retrieve

// 只选择相应属性
Model.findAll( {
    attributes: [ 'foo', 'bar' ]
} );
// 做聚类
Model.findAll( {
    attributes: { include: [ [ sequelize.fn( 'COUNT', sequelize.col( 'hats' ) ), 'no_hats' ] ] }
} );
// 除开某些属性
Model.findAll( {
    attributes: { exclude: [ 'baz' ] }
} );

// id 查找
Project.findById( 123 ).then();

// 属性查找
Project.findOne( { where: { title: 'aProject' } } ).then();
// ?
Project.findOne( { where: { title: 'aProject' }, attributes: [ 'id', [ 'name', 'title' ] ] } ).then();

// 找,无则创建, created 为 boolean
User.findOrCreate( { where: { username: 'kayor' } } ).spread( function ( user, created ) {} );

// 找并且计数 count 为计数, rows 为对象数组
Project.findAndCountAll( {
    where: { title: { $like: 'foo%' } },
    offset: 10,
    limit: 2
} ).then( function ( result ) {
    console.log( result.count );
    console.log( result.rows );
} );

// 找出拥有 active profile 的用户
User.findAndCountAll( {
    include: [
        { model: Profile, where: { active: true } }
  ],
    limit: 3
} );


Project.findAll();
Project.all();
Project.findAll( { where: { name: "a Project" } } );
Project.findAll( { where: [ "id>?", 25 ] } );
Project.findAll( { where: { id: [ 1, 2, 3 ] } } );
Project.findAll( {
    where: {
        id: {
            $and: { a: 5 },
            $or: [ { a: 5 }, { a: 6 } ],
            $gt: 6,
            $gte: 6,
            $lt: 10,
            $lte: 10,
            $ne: 20,
            $between: [ 6, 10 ],
            $notBetween: [ 6, 10 ],
            $in: [ 1, 2 ],
            $notIn: [ 1, 2 ],
            $like: '%hat'
        },
        status: { $not: false }
    }
} );

//只找十个
Project.findAll( { limit: 10 } );
// 跳过前10个
Project.findAll( { offset: 10 } );
// 跳过前10,取两个
Project.findAll( { offset: 10, limit: 2 } );

// 递减
Project.findAll( { order: 'title DESC' } );
// 分组
Project.findAll( { group: 'name' } );
// 计数
Project.count( { where: [ "id>?", 25 ] } );
// 找最大
Project.max( 'age' );

// 内连接找到拥有符合条件工具的用户
Post.findAll( {
    include: [ {
        model: Comment,
        as: 'comment_my',
        where: { name: { $like: '%ooth%' } }
    } ]
} );

// 获得原数据
Post.findOne( { where: { title: 'scut' } } ).then( function ( post ) {
    post.title = 'south china university of tecknology';
    console.log( post.title ); // 'south china university of tecknology'

    post.reload().then( function () {
        console.log( post.title ); // 'scut'
    } );
} );

相关文章

  • Sequelize 的增删查改

    文章转载自:https://www.jianshu.com/p/8242b841de9d 模型定义 Create ...

  • node的前后链条思路

    curd || resetfo(增删改查)增删改查就是调用数据库-mysql-sequelize 接口 $ npm...

  • configparser配置文件模块

    Menu configparser定义 创建configparser文件 增删改查 - 查 增删改查 - 改 增删...

  • Spring Boot——增删查改(1)

    Spring Boot增删查改 标签: SpringBoot 增删查改是最为基础的功能,在很多场景下都有运用。本文...

  • Arraylist和Linkedlist

    Arraylist:底层是数组结构,增删慢改查快. Linkedlist:底层是链表结构,增删快 改查慢. Arr...

  • Java集合之ArrayList - 吃透增删查改

    Java集合之ArrayList - 吃透增删查改 从源码看初始化以及增删查改,学习ArrayList。 先来看下...

  • ElasticSearch常用的增删查改操作

    使用Java对ElasticSearch增删查改操作,分为两个步骤:1.拼接sql语句2.执行增删查改操作以下提供...

  • NSQL增删查改

    无论前端还是后台,都离不开对数据库的一些基本操作,我们现在分享一下数据库的一些基本语法: 修改表 修改表 语法: ...

  • mongoDB增删查改

    一、 数据库使用开启 mongodb 服务:要管理数据库,必须先开启服务,开启服务使用 mongod --dbpa...

  • 一个小小的留言本

    需求分析:用户可以在留言本留言,并对内容进行增删查改。 涉及知识点: PHP语言 数据库的增删查改 留言页面 wr...

网友评论

      本文标题:Sequelize 的增删查改

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