{"id":646,"date":"2012-06-23T22:50:48","date_gmt":"2012-06-24T05:50:48","guid":{"rendered":"http:\/\/h2plus.biz\/hiromitsu\/?p=646"},"modified":"2018-08-06T14:07:39","modified_gmt":"2018-08-06T21:07:39","slug":"node-js%e3%81%a7sequelize%e3%82%92%e4%bd%bf%e3%81%86%e3%81%9f%e3%82%81%e3%81%ae%e6%ba%96%e5%82%99%ef%bc%8f%e4%b8%ad%e7%b7%a8","status":"publish","type":"post","link":"https:\/\/h2plus.biz\/hiromitsu\/entry\/646","title":{"rendered":"node.js\u3067Sequelize\u3092\u4f7f\u3046\u305f\u3081\u306e\u6e96\u5099\uff0f\u4e2d\u7de8"},"content":{"rendered":"<p><a href=\"http:\/\/h2plus.biz\/hiromitsu\/entry\/631\">\u524d\u7de8<\/a>\u3067\u306f\u3001Sequelize\u3092\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u5316\u3059\u308b\u305f\u3081\u306b\u81ea\u524d\u3067<code>dbconn<\/code>\u30e2\u30b8\u30e5\u30fc\u30eb\u3092\u4f5c\u3063\u3066\u3001<code>app.js<\/code>\u306f\u3053\u3093\u306a\u611f\u3058\u306b\u30b7\u30f3\u30d7\u30eb\u306b\u66f8\u3051\u308b\u3088\u3046\u306b\u306a\u3063\u305f\u3002<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nvar express = require('express')\r\n  , resource = require('express-resource')\r\n  , config = require('config')\r\n  , sequelize = require('dbconn')(config)\r\n  , app = module.exports = express.createServer();\r\n\r\napp.resource('users', require('.\/routes\/users'), { id: 'id' });\r\n<\/pre>\n<p>\u5b9f\u969b\u306bSequelize\u3092\u4f7f\u3063\u3066DB\u306b\u5bfe\u3059\u308bCRUD\u64cd\u4f5c\u3092\u884c\u3046API\u306e\u5b9f\u88c5\u3092\u66f8\u304f\u4ee5\u524d\u306b\u3001\u307e\u3060DB\u3059\u3089\u5b58\u5728\u3057\u306a\u3044\u306e\u3067<code>mysql<\/code>\u30b3\u30de\u30f3\u30c9\u3067DB\u3060\u3051\u4f5c\u3063\u3066\u304a\u304f\u3002<\/p>\n<p><!--more--><\/p>\n<pre>\n$ mysql -u root -p\nmysql&gt; create database test;\nmysql&gt; grant all privileges on test.* to 'h2plus'@'%' identified by 'password';\nmysql&gt; \\q\n<\/pre>\n<p>\u6b21\u306b\u30c6\u30fc\u30d6\u30eb\u306e\u5b9a\u7fa9\u3092\u3059\u308b\u308f\u3051\u3060\u304c\u3001\u3082\u3046SQL\u6587\u3092\u76f4\u63a5\u66f8\u304f\u3053\u3068\u306f\u306a\u3044\u3002Sequelize\u306e<a href=\"http:\/\/docs.sequelizejs.com\/#models-import\"><code>import<\/code>\u30e1\u30bd\u30c3\u30c9<\/a>\u3092\u4f7f\u3048\u3070\u3001\u30e2\u30b8\u30e5\u30fc\u30eb\u3068\u3057\u3066\u5b9f\u88c5\u3055\u308c\u305f\u30e2\u30c7\u30eb\u3092\u5b9a\u7fa9\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u3002\u30b5\u30f3\u30d7\u30eb\u3068\u3057\u3066\u3001\u4ee5\u4e0b\u306eSQL\u3067\u5b9a\u7fa9\u3055\u308c\u308b\u30e6\u30fc\u30b6\u30fc\u30c6\u30fc\u30d6\u30eb\u3092\u4f5c\u3063\u3066\u307f\u3088\u3046\u3002<\/p>\n<pre>\ncreate table users\n(\n    username varchar(255),\n    password char(40)\n);\n<\/pre>\n<p>\u5bfe\u5fdc\u3059\u308b\u30e2\u30c7\u30eb\u5b9a\u7fa9\u306e\u30e2\u30b8\u30e5\u30fc\u30eb\u306f\u3053\u3046\u306a\u308b\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>\u3053\u308c\u3092<code>models\/user.js<\/code>\u3068\u3044\u3046\u30d5\u30a1\u30a4\u30eb\u306b\u4fdd\u5b58\u3057\u3066\u304a\u304f\u3002<code>sequelize.define()<\/code>\u306e\u7b2c\u4e00\u5f15\u6570\u306b\u6307\u5b9a\u3057\u3066\u3044\u308b\u30e2\u30c7\u30eb\u540d\u306f\u5358\u6570\u5f62\u306b\u306a\u3063\u3066\u3044\u308b\u304c\u3001Sequelize\u304c\u5b9f\u969b\u306b\u5b9a\u7fa9\u3059\u308b\u30c6\u30fc\u30d6\u30eb\u540d\u306f\u8907\u6570\u5f62\u306b\u306a\u308b\u3002<\/p>\n<p>\u3044\u307e\u5b9a\u7fa9\u3057\u305f<code>User<\/code>\u30e2\u30c7\u30eb\u3092\u52d5\u7684\u306b\u8aad\u307f\u8fbc\u3080<code>models<\/code>\u30e2\u30b8\u30e5\u30fc\u30eb\u3092<code>libs\/models\/index.js<\/code>\u306b\u5b9f\u88c5\u3057\u3066\u3044\u304f\u3002<code>libs<\/code>\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u306f<a href=\"http:\/\/h2plus.biz\/hiromitsu\/entry\/631\">\u524d\u7de8<\/a>\u3067<code>dbconn<\/code>\u30e2\u30b8\u30e5\u30fc\u30eb\u3092\u5b9f\u88c5\u3059\u308b\u3068\u304d\u306b\u4f5c\u3063\u305f\u3082\u306e\u3060\u3002<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nmodule.exports = function(sequelize) {\r\n\r\n  var fs = require('fs')\r\n    , path = require('path')\r\n    , directory = 'models'\r\n    , models = exports;\r\n\r\n  try {\r\n    fs.lstatSync(directory);\r\n  }\r\n  catch(e) {\r\n    fs.mkdirSync(directory);\r\n  }\r\n\r\n  models.__cache = {};\r\n\r\n  fs.readdirSync(directory).forEach(function(fname) {\r\n    if(\/\\.js$\/.test(fname)) {\r\n      var name = path.basename(fname, '.js')\r\n        , realpath = fs.realpathSync(directory+'\/'+fname);\r\n\r\n      models.__defineGetter__(name, function() {\r\n        if(models.__cache[name] === undefined) {\r\n          models.__cache[name] = sequelize.import(realpath);\r\n        }\r\n        return models.__cache[name];\r\n      });\r\n    }\r\n  });\r\n\r\n  return models;\r\n};\r\n<\/pre>\n<p>\u3053\u308c\u3067\u3001<code>models<\/code>\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u306b\u30c6\u30fc\u30d6\u30eb\u5b9a\u7fa9\u30e2\u30b8\u30e5\u30fc\u30eb\u3092\u4f5c\u308b\u3060\u3051\u3067\u3001\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306b\u30e2\u30c7\u30eb\u3092\u8ffd\u52a0\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308b\u3002\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3067\u5b9a\u7fa9\u3057\u305f\u30e2\u30c7\u30eb\u3092DB\u306b\u53cd\u6620\u3055\u305b\u308b\u306b\u306fSequelize\u306e<a href=\"http:\/\/docs.sequelizejs.com\/#models-sync\"><code>sync<\/code>\u30e1\u30bd\u30c3\u30c9<\/a>\u3092\u4f7f\u3046\u3002\u30a2\u30d7\u30ea\u958b\u767a\u4e2d\u306b\u30e2\u30c7\u30eb\u304c\u66f4\u65b0\u3055\u308c\u308b\u3053\u3068\u306f\u591a\u3044\u3060\u308d\u3046\u304b\u3089\u3001<code>sync<\/code>\u30e1\u30bd\u30c3\u30c9\u306e\u547c\u3073\u51fa\u3057\u81ea\u4f53\u3092API\u5316\u3057\u3066\u3057\u3066\u304a\u304f\u3068\u4fbf\u5229\u3060\u3002<\/p>\n<p><code>sync<\/code>\u30e1\u30bd\u30c3\u30c9\u3092\u5b9f\u88c5\u3057\u305fAPI\u3092\u7528\u610f\u3059\u308b\u524d\u306b\u3001\u4eca\u307e\u3067<code>app.js<\/code>\u306b\u66f8\u3044\u3066\u3042\u3063\u305f<code>express-resource<\/code>\u3092\u4f7f\u3063\u305f\u30b3\u30fc\u30c9\u3084\u3001Sequelize\u3092\u4f7f\u3046\u30b3\u30fc\u30c9\u3092<code>routes\/index.js<\/code>\u306b\u30ab\u30d7\u30bb\u30eb\u5316\u3057\u3066\u3057\u307e\u304a\u3046\u3002<code>app.js<\/code>\u306e\u6700\u7d42\u5f62\u306f\u3053\u3046\u306a\u308b\u3002<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nvar express = require('express')\r\n  , resource = require('express-resource')\r\n  , config = require('config')\r\n  , app = module.exports = express.createServer();\r\n\r\n\/\/\r\n\/\/ Configuration\r\n\/\/\r\napp.configure(function() {\r\n  app.set('views', __dirname + '\/views');\r\n  app.set('view engine', 'ejs');\r\n  app.use(express.bodyParser());\r\n  app.use(express.cookieParser());\r\n  app.use(express.methodOverride());\r\n  app.use(app.router);\r\n  app.use(express.static(__dirname + '\/public'));\r\n});\r\n\r\napp.configure('development', function() {\r\n  app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));\r\n});\r\n\r\napp.configure('production', function() {\r\n  app.use(express.errorHandler());\r\n});\r\n\r\n\/\/\r\n\/\/ Routes\r\n\/\/\r\nrequire('.\/routes')(app, config);\r\n\r\n\/\/\r\n\/\/ Launch\r\n\/\/\r\napp.listen(process.env.PORT || 3000, function() {\r\n  console.log(&amp;amp;quot;Express server listening on port %d in %s mode&amp;amp;quot;, app.address().port, app.settings.env);\r\n});\r\n<\/pre>\n<p>\u5192\u982d\u306b\u7d39\u4ecb\u3057\u305f\u30b3\u30fc\u30c9\u3067\u306f\u3001Sequelize\u3092<code>app.js<\/code>\u5185\u3067\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u5316\u3057\u3066\u3044\u305f\u304c\u3001\u5b9f\u969b\u306bSequelize\uff08\u30e2\u30c7\u30eb\uff09\u3092\u4f7f\u3046\u306e\u306f<code>routes<\/code>\u306e\u4e2d\u306b\u306a\u308b\u306e\u3067\u3001Sequelize\u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u5316\u306b\u5fc5\u8981\u306a<code>config<\/code>\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092<code>routes<\/code>\u306b\u6e21\u3059\u3088\u3046\u306b\u3057\u3066\u3044\u308b\u3002<code>config<\/code>\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u81ea\u4f53\u306f\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306e\u8a2d\u5b9a\u3092\u4fdd\u6301\u3059\u308b\u3082\u306e\u3067\u3042\u308a\u3001Sequelize\u4ee5\u5916\u306e\u30e2\u30b8\u30e5\u30fc\u30eb\u304c\u5fc5\u8981\u3068\u3059\u308b\u8a2d\u5b9a\u304c\u542b\u307e\u308c\u308b\u3053\u3068\u3092\u8003\u3048\u308b\u3068\u3001<code>app.js<\/code>\u5185\u3067\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u5316\u3059\u308b\u306e\u304c\u9069\u5207\u3060\u308d\u3046\u3002<\/p>\n<p>30\u884c\u76ee\u3067\u307e\u3060\u5b58\u5728\u3057\u306a\u3044<code>routes<\/code>\u30e2\u30b8\u30e5\u30fc\u30eb\u3092\u547c\u3073\u51fa\u3059\u30b3\u30fc\u30c9\u3092\u5148\u306b\u66f8\u3044\u3066\u3057\u307e\u3063\u305f\u304c\u3001<code>routes<\/code>\u30e2\u30b8\u30e5\u30fc\u30eb\u306f<code>routes\/index.js<\/code>\u3067\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u5b9f\u88c5\u3057\u3066\u3044\u308b\u3002<\/p>\n<pre class=\"brush: jscript; 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  app.resource('users', require('.\/users'), { id: 'id' });\r\n};\r\n<\/pre>\n<p>\u3053\u308c\u3067\u3057\u3070\u3089\u304f<code>app.js<\/code>\u306b\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u3059\u308b\u3053\u3068\u306f\u306a\u304f\u306a\u308b\u3060\u308d\u3046\u3002\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u30ed\u30b8\u30c3\u30af\u306e\u30e1\u30a4\u30f3\u306f<code>routes<\/code>\u30e2\u30b8\u30e5\u30fc\u30eb\u304c\u8acb\u3051\u8ca0\u3046\u3053\u3068\u306b\u306a\u308b\u3002<\/p>\n<p>\u7d50\u5c40\u4e09\u90e8\u4f5c\u306b\uff08\u7b11\uff09<a href=\"http:\/\/h2plus.biz\/hiromitsu\/entry\/659\">\u5f8c\u7de8\u306b\u7d9a\u304f<\/a>&#8230;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u524d\u7de8\u3067\u306f\u3001Sequelize\u3092\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u5316\u3059\u308b\u305f\u3081\u306b\u81ea\u524d\u3067dbconn\u30e2\u30b8\u30e5\u30fc\u30eb\u3092\u4f5c\u3063\u3066\u3001app.js\u306f\u3053\u3093\u306a\u611f\u3058\u306b\u30b7\u30f3\u30d7\u30eb\u306b\u66f8\u3051\u308b\u3088\u3046\u306b\u306a\u3063\u305f\u3002 \u5b9f\u969b\u306bSequelize\u3092\u4f7f\u3063\u3066DB\u306b\u5bfe\u3059\u308bCRUD\u64cd\u4f5c\u3092\u884c\u3046API\u306e <a href='https:\/\/h2plus.biz\/hiromitsu\/entry\/646' 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":[232,225,231,230],"_links":{"self":[{"href":"https:\/\/h2plus.biz\/hiromitsu\/wp-json\/wp\/v2\/posts\/646"}],"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=646"}],"version-history":[{"count":2,"href":"https:\/\/h2plus.biz\/hiromitsu\/wp-json\/wp\/v2\/posts\/646\/revisions"}],"predecessor-version":[{"id":998,"href":"https:\/\/h2plus.biz\/hiromitsu\/wp-json\/wp\/v2\/posts\/646\/revisions\/998"}],"wp:attachment":[{"href":"https:\/\/h2plus.biz\/hiromitsu\/wp-json\/wp\/v2\/media?parent=646"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/h2plus.biz\/hiromitsu\/wp-json\/wp\/v2\/categories?post=646"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/h2plus.biz\/hiromitsu\/wp-json\/wp\/v2\/tags?post=646"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}