{"id":694,"date":"2012-07-28T22:10:41","date_gmt":"2012-07-29T05:10:41","guid":{"rendered":"http:\/\/h2plus.biz\/hiromitsu\/?p=694"},"modified":"2012-08-26T00:24:49","modified_gmt":"2012-08-26T07:24:49","slug":"sequelize%e3%82%92%e4%bd%bf%e3%81%a3%e3%81%a6%e3%83%a6%e3%83%bc%e3%82%b6%e3%83%bc%e8%aa%8d%e8%a8%bc%ef%bc%8f%e8%a8%ad%e8%a8%88%e7%b7%a8","status":"publish","type":"post","link":"https:\/\/h2plus.biz\/hiromitsu\/entry\/694","title":{"rendered":"Sequelize\u3092\u4f7f\u3063\u3066\u30e6\u30fc\u30b6\u30fc\u8a8d\u8a3c\uff0f\u8a2d\u8a08\u7de8"},"content":{"rendered":"<p> <a href=\"http:\/\/h2plus.biz\/hiromitsu\/entry\/659\">Sequelize\u3092\u4f7f\u3046\u6e96\u5099<\/a>\u304c\u6574\u3063\u305f\u3068\u3053\u308d\u3067\u3001\u3069\u3093\u306aWeb\u30a2\u30d7\u30ea\u3092\u4f5c\u308b\u306b\u3057\u3066\u3082\u907f\u3051\u3066\u901a\u308c\u306a\u3044\u3067\u3042\u308d\u3046\u30e6\u30fc\u30b6\u30fc\u8a8d\u8a3c\u306e\u4ed5\u7d44\u307f\u3092\u5b9f\u88c5\u3057\u3066\u3044\u304f\u3002<\/p>\n<p>\u5927\u307e\u304b\u306a\u6d41\u308c\u306f\u3053\u3093\u306a\u611f\u3058\u3002<\/p>\n<ol>\n<li><code>User<\/code>\u30e2\u30c7\u30eb\u3092\u5b9a\u7fa9\u3059\u308b<\/li>\n<li>\u8a8d\u8a3c\u30e2\u30b8\u30e5\u30fc\u30eb\u3092\u8a2d\u8a08\u3059\u308b<\/li>\n<li>\u30ea\u30af\u30a8\u30b9\u30c8\u306e\u30eb\u30fc\u30c6\u30a3\u30f3\u30b0\u3092\u8ffd\u52a0\u3059\u308b<\/li>\n<li>\u8a8d\u8a3c\u30e2\u30b8\u30e5\u30fc\u30eb\u3092\u5b9f\u88c5\u3059\u308b<\/li>\n<\/ol>\n<p><!--more--><br \/>\n\u306f\u3058\u3081\u306b\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306e\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u69cb\u9020\u304c\u3069\u3046\u306a\u3063\u3066\u3044\u308b\u304b\u304a\u3055\u3089\u3044\u3002<\/p>\n<pre>\r\nmyrest\/\r\n\u251c\u2500\u2500 app.js\r\n\u251c\u2500\u2500 config\/\r\n\u2502\u00a0\u00a0 \u2514\u2500\u2500 development.json\r\n\u251c\u2500\u2500 libs\/\r\n\u2502\u00a0\u00a0 \u251c\u2500\u2500 dbconn\/\r\n\u2502\u00a0\u00a0 \u2514\u2500\u2500 models\/\r\n\u251c\u2500\u2500 models\/\r\n\u2502\u00a0\u00a0 \u2514\u2500\u2500 user.js\r\n\u251c\u2500\u2500 node_modules\/\r\n\u251c\u2500\u2500 package.json\r\n\u2514\u2500\u2500 routes\/\r\n \u00a0\u00a0 \u251c\u2500\u2500 index.js\r\n \u00a0\u00a0 \u2514\u2500\u2500 sync.js\r\n<\/pre>\n<h4>User\u30e2\u30c7\u30eb\u3092\u5b9a\u7fa9\u3059\u308b<\/h4>\n<p><code>User<\/code>\u30e2\u30c7\u30eb\u306f<code>models\/user.js<\/code>\u3067\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u5b9a\u7fa9\u3057\u3066\u3044\u305f\u3002<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nmodule.exports = function(sequelize, DataTypes) {\r\n    return sequelize.define('user', {\r\n        username: DataTypes.STRING\r\n      , password: 'char(40)'\r\n    });\r\n};\r\n<\/pre>\n<p>\u30e6\u30fc\u30b6\u30fc\u3092\u8a8d\u8a3c\u3059\u308b\u969b\u306f<code>username<\/code>\u30ab\u30e9\u30e0\u304c\u30ad\u30fc\u306b\u306a\u308b\u306e\u3067\u3001UNIQUE\u5236\u7d04\u3092\u8ffd\u52a0\u3057\u3066\u304a\u304f\u5fc5\u8981\u304c\u3042\u308b\u3002<br \/>\n<code>password<\/code>\u30ab\u30e9\u30e0\u306f40\u30d0\u30a4\u30c8\u306e\u56fa\u5b9a\u9577\u3068\u3057\u3066\u3044\u308b\u306e\u306f\u3001SHA-1\u306e\u30cf\u30c3\u30b7\u30e5\u5024\uff08160bit\uff09\u309216\u9032\u6570\u3067\u683c\u7d0d\u3059\u308b\u305f\u3081\u3060\u3002\u5e73\u6587\u306e\u30d1\u30b9\u30ef\u30fc\u30c9\u304b\u3089\u30cf\u30c3\u30b7\u30e5\u5024\u3092\u8a08\u7b97\u3059\u308b\u95a2\u6570\u306f\u3044\u305a\u308c\u5fc5\u8981\u306b\u306a\u308b\u306e\u3067\u3001<code>User<\/code>\u30e2\u30c7\u30eb\u306e\u30af\u30e9\u30b9\u30e1\u30bd\u30c3\u30c9\u3068\u3057\u3066\u5b9f\u88c5\u3057\u3066\u304a\u304f\u3002<\/p>\n<pre class=\"brush: jscript; highlight: [3,4,5,6,10,11,12,13,14,15,16]; title: ; notranslate\" title=\"\">\r\nmodule.exports = function(sequelize, DataTypes) {\r\n    return sequelize.define('user', {\r\n        username: {\r\n            type: DataTypes.STRING\r\n          , unique: true\r\n        }\r\n      , password: 'char(40)'\r\n    }, \/\/ End of table definition\r\n    {\r\n        classMethods: {\r\n            hashPassword: function(pass) {\r\n                var sha1 = require('crypto').createHash('sha1');\r\n                sha1.update(pass, 'utf8');\r\n                return sha1.digest('hex');\r\n            }\r\n        }\r\n    });\r\n};\r\n<\/pre>\n<p>\u3053\u3046\u3057\u3066\u304a\u304f\u3053\u3068\u3067\u3001<code>User<\/code>\u30e2\u30c7\u30eb\u3092\u53c2\u7167\u3067\u304d\u308b\u3068\u3053\u308d\u3067\u306f\u3001<code>User.hashPassword()<\/code>\u3092\u547c\u3073\u51fa\u305b\u308b\u3088\u3046\u306b\u306a\u308b\u3002<\/p>\n<h4>\u8a8d\u8a3c\u30e2\u30b8\u30e5\u30fc\u30eb\u3092\u8a2d\u8a08\u3059\u308b<\/h4>\n<p>\u5b9f\u88c5\u3059\u308b\u8a8d\u8a3c\u6a5f\u80fd\u306f\u30b5\u30a4\u30f3\u30a2\u30c3\u30d7\u3001\u30ed\u30b0\u30a4\u30f3\u3001\u30ed\u30b0\u30a2\u30a6\u30c8\u306e\u57fa\u672c\u7684\u306a\u3082\u306e\u3060\u3051\u306b\u3057\u3066\u304a\u304f\u3002facebook\u9023\u643a\u3068\u304b\u30d1\u30b9\u30ef\u30fc\u30c9\u30ea\u30de\u30a4\u30f3\u30c0\u30fc\u3068\u304b\u3001\u8a8d\u8a3c\u30e2\u30b8\u30e5\u30fc\u30eb\u306e\u4ed5\u4e8b\u306f\u305f\u304f\u3055\u3093\u3042\u308b\u3051\u3069\u306d\u3002<\/p>\n<p>\u307e\u305a<code>routes\/auth.js<\/code>\u306b\u8a8d\u8a3c\u30e2\u30b8\u30e5\u30fc\u30eb\u306e\u67a0\u7d44\u307f\u3060\u3051\u4f5c\u3063\u3066\u3057\u307e\u304a\u3046\u3002<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nmodule.exports = function(models) {\r\n  var User = models.user;\r\n\r\n  return {\r\n      signup: function(req, res) {\r\n      }\r\n\r\n    , login: function(req, res) {\r\n      }\r\n\r\n    , logout: function(req, res) {\r\n      }\r\n  };\r\n};\r\n<\/pre>\n<p><code>routes\/auth<\/code>\u30e2\u30b8\u30e5\u30fc\u30eb\u306f\u3001<code>models<\/code>\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u5f15\u6570\u306b\u53d7\u3051\u53d6\u308a\u3001\u8a8d\u8a3c\u6a5f\u80fd\u3092\u63d0\u4f9b\u3059\u308b\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u8fd4\u3059\u95a2\u6570\u3068\u3057\u3066\u5b9f\u88c5\u3059\u308b\u3002<\/p>\n<p><code>signup<\/code>\u30e1\u30bd\u30c3\u30c9\u3067<code>users<\/code>\u30c6\u30fc\u30d6\u30eb\u306b\u65b0\u898f\u30c7\u30fc\u30bf\u3092\u767b\u9332\u3002<code>login<\/code>\u30e1\u30bd\u30c3\u30c9\u3067\u8a8d\u8a3c\u51e6\u7406\u3092\u884c\u3044\u30bb\u30c3\u30b7\u30e7\u30f3\u3092\u4f7f\u3063\u3066\u30ed\u30b0\u30a4\u30f3\u72b6\u614b\u3092\u4fdd\u5b58\u3059\u308b\u3002<code>logout<\/code>\u30e1\u30bd\u30c3\u30c9\u306f\u30bb\u30c3\u30b7\u30e7\u30f3\u306e\u7834\u68c4\u3092\u884c\u3046\u3060\u3051\u3002<\/p>\n<p>\u5404\u30e1\u30bd\u30c3\u30c9\u306e\u4e2d\u8eab\u306f\u5b9f\u88c5\u7de8\u306b\u3066\u3002<\/p>\n<h4>\u30ea\u30af\u30a8\u30b9\u30c8\u306e\u30eb\u30fc\u30c6\u30a3\u30f3\u30b0\u3092\u8ffd\u52a0\u3059\u308b<\/h4>\n<p><code>routes\/index.js<\/code>\u3067<code>routes\/auth<\/code>\u30e2\u30b8\u30e5\u30fc\u30eb\u306e\u5404\u30e1\u30bd\u30c3\u30c9\u306b\u30eb\u30fc\u30c6\u30a3\u30f3\u30b0\u3059\u308b\u30b3\u30fc\u30c9\u3092\u8ffd\u52a0\u3059\u308b\u3002<\/p>\n<pre class=\"brush: jscript; highlight: [13,14,15,16,17]; title: ; notranslate\" title=\"\">\r\nmodule.exports = function(app, config) {\r\n\r\n  var sequelize = require('dbconn')(config)\r\n    , models = require('models')(sequelize);\r\n\r\n  \/\/ Database migration\r\n  app.configure('development', function() {\r\n    var sync = require('.\/sync');\r\n    app.post('\/devel\/sync\/:table', sync.one(models));\r\n    app.post('\/devel\/sync', sync.all(sequelize));\r\n  });\r\n\r\n  \/\/ Authentication\r\n  var auth = require('.\/auth')(models);\r\n  app.post('\/auth\/signup', auth.signup);\r\n  app.post('\/auth\/login', auth.login);\r\n  app.post('\/auth\/logout', auth.logout);\r\n\r\n};\r\n<\/pre>\n<p>\u3059\u3079\u3066POST\u30e1\u30bd\u30c3\u30c9\u306e\u307f\u30eb\u30fc\u30c6\u30a3\u30f3\u30b0\u3057\u3066\u3001\u5165\u529b\u3068\u306a\u308b\u30c7\u30fc\u30bf\u306fexpress\u304c\u63d0\u4f9b\u3059\u308b<code>request.body<\/code>\u7d4c\u7531\u3067\u53d6\u5f97\u3059\u308b\u3088\u3046\u306b\u306a\u308b\u3002<\/p>\n<p><a href=\"http:\/\/h2plus.biz\/hiromitsu\/entry\/746\">\u5b9f\u88c5\u7de8<\/a>\u3078\u7d9a\u304f&#8230;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sequelize\u3092\u4f7f\u3046\u6e96\u5099\u304c\u6574\u3063\u305f\u3068\u3053\u308d\u3067\u3001\u3069\u3093\u306aWeb\u30a2\u30d7\u30ea\u3092\u4f5c\u308b\u306b\u3057\u3066\u3082\u907f\u3051\u3066\u901a\u308c\u306a\u3044\u3067\u3042\u308d\u3046\u30e6\u30fc\u30b6\u30fc\u8a8d\u8a3c\u306e\u4ed5\u7d44\u307f\u3092\u5b9f\u88c5\u3057\u3066\u3044\u304f\u3002 \u5927\u307e\u304b\u306a\u6d41\u308c\u306f\u3053\u3093\u306a\u611f\u3058\u3002 User\u30e2\u30c7\u30eb\u3092\u5b9a\u7fa9\u3059\u308b \u8a8d\u8a3c\u30e2\u30b8\u30e5\u30fc\u30eb\u3092\u8a2d\u8a08\u3059\u308b \u30ea <a href='https:\/\/h2plus.biz\/hiromitsu\/entry\/694' class='excerpt-more'>[&#8230;]<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[77],"tags":[225,245,241,244,243,242],"_links":{"self":[{"href":"https:\/\/h2plus.biz\/hiromitsu\/wp-json\/wp\/v2\/posts\/694"}],"collection":[{"href":"https:\/\/h2plus.biz\/hiromitsu\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/h2plus.biz\/hiromitsu\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/h2plus.biz\/hiromitsu\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/h2plus.biz\/hiromitsu\/wp-json\/wp\/v2\/comments?post=694"}],"version-history":[{"count":0,"href":"https:\/\/h2plus.biz\/hiromitsu\/wp-json\/wp\/v2\/posts\/694\/revisions"}],"wp:attachment":[{"href":"https:\/\/h2plus.biz\/hiromitsu\/wp-json\/wp\/v2\/media?parent=694"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/h2plus.biz\/hiromitsu\/wp-json\/wp\/v2\/categories?post=694"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/h2plus.biz\/hiromitsu\/wp-json\/wp\/v2\/tags?post=694"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}