Sequelize(3) CRUD

3469 단어

첨삭


모델 정의
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'
    } );
} );

좋은 웹페이지 즐겨찾기