25
22

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Node.jsのORマッパー、Sequelizeのサンプル

Posted at

参考

基本編
http://blog.excale.net/index.php/archives/1173

応用編
http://blog.excale.net/index.php/archives/1538

サンプルソース

app.js
var Sequelize = require ('sequelize');
var sequelize = new Sequelize('blogapp', 'postgres', 'root', { host: 'localhost', port: 5433,dialect: 'postgres' });
 
//定義
//データ型参考
//http://sequelizejs.com/docs/latest/models#data-types
var User = sequelize.define('User', {
      name: Sequelize.STRING,
      email: Sequelize.STRING
});
 
//create table
//User.sync();
 
//insert
/*
User.create({ name: 'hoge', email: 'hoge@hoge.com'})
    .error(function(err) {
          //エラー時の処理
          console.log(err);
    })
    .success(function(result) {
          //成功時の処理
          console.log(result);
    });
*/
 
//select
User.find({ where: {name: 'hoge'} })
    .error(function(err) {
          //エラー時の処理
          console.log(err);
    })
    .success(function(result) {
          //成功時の処理
          //console.log(result)
          console.log(result.name);
          console.log(result.email);
    });
    
//update
/*
User.update(
    //更新内容
    {
        email: 'foo@foo.com',
        name: 'foo'
    },
    //対象
    { id: '3'}
    )
    .error(function(err) {
          //エラー時の処理
          console.log(err);
    })
    .success(function(result) {
          //成功時の処理
          console.log(result);
    });
*/
 
 
//delete
/*
User.destroy({name: 'foo'})
    .error(function(err) {
          //エラー時の処理
          console.log(err);
    })
    .success(function(result) {
          //成功時の処理
          console.log(result);
    });
*/
25
22
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
25
22

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?