Help us understand the problem. What is going on with this article?

Node.js + Sequelize で複数レコードの更新

More than 5 years have passed since last update.

備忘録です.
Node.js の ORM である Sequelize を使って検索した複数レコードに対して更新をかける処理を書きます.
Document( http://sequelizejs.com/docs/1.7.8/installation )では詳しく触れてなかったような。。。

環境

  • MacOSX (10.9)
  • Node.js (v0.10.29)
  • npm (1.4.14)
  • Squelize (1.7.9)
  • mysql (2.4.1)
  • Underscore.js (1.6.0)

対象レコード

サンプルなので適当です.
id: 1, title: 'sample', description: 'aiueo',
id: 2, title: 'sample', description: 'waaai'
とか適当に用意してみた.

サンプルコード

sample.js
// Sequelize の準備
var Sequelize = require('sequelize');
var sequelize = new Sequelize('sample', 'user', 'password', {
  host: 'localhost',
  port: 3306
});

// Underscore.js 使いたいひと
var _us = require('underscore');

// テーブル定義,作成
var Sample = sequelize(define('Sample', {
  title: Sequelize.STRING,
  description: Sequelize.TEXT
});
Sample.sync();

// 検索,更新
Sample.findAll({where: {title: 'sample'} })
  .success(function(sample) {
    sample.forEach(function(element) {
      element.updateAttributes({
        description: 'pure'
      }).success(function() {
        console.log('success.');
      });
    });

    // Undersocre.js 使いたいひと
    _us.each(sample, function(element, index, array) {
      element.updateAttributes({
        description: 'underscore'
      }).success(function() {
        console.log('success.(underscore)');
      });
    });
  });
});

変わったかな?

ひとこと

今日は目が痛い日でつらたん.コンタクトしたまま寝てはいけない…

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away