{"id":659,"date":"2012-07-01T23:40:48","date_gmt":"2012-07-02T06:40:48","guid":{"rendered":"http:\/\/h2plus.biz\/hiromitsu\/?p=659"},"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%e5%be%8c%e7%b7%a8","status":"publish","type":"post","link":"https:\/\/h2plus.biz\/hiromitsu\/entry\/659","title":{"rendered":"node.js\u3067Sequelize\u3092\u4f7f\u3046\u305f\u3081\u306e\u6e96\u5099\uff0f\u5f8c\u7de8"},"content":{"rendered":"<p>\u305d\u308c\u3067\u306f<a href=\"http:\/\/h2plus.biz\/hiromitsu\/entry\/646\">\u4e2d\u7de8<\/a>\u3067\u68da\u4e0a\u3052\u3057\u3066\u3044\u305f<code>sync<\/code>\u30e1\u30bd\u30c3\u30c9\u3092\u63d0\u4f9b\u3059\u308bAPI\u306e\u5b9f\u88c5\u3092\u66f8\u3044\u3066\u3044\u3053\u3046\u3002<\/p>\n<p><a href=\"http:\/\/docs.sequelizejs.com\/#models-sync\">Sequelize\u306e\u30b5\u30f3\u30d7\u30eb<\/a>\u3092\u898b\u3066\u3082\u308f\u304b\u308b\u3088\u3046\u306b\u3001<code>sync<\/code>\u30e1\u30bd\u30c3\u30c9\u306f<code>import<\/code>\u30e1\u30bd\u30c3\u30c9\u306b\u3088\u3063\u3066\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u5316\u3055\u308c\u305f\u30e2\u30c7\u30eb\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306b\u5bfe\u3059\u308b\u547c\u3073\u51fa\u3057\uff08<code>Project.sync<\/code>\uff09\u3068\u3001<code>Sequelize<\/code>\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306b\u5bfe\u3059\u308b\u547c\u3073\u51fa\u3057\uff08<code>sequelize.sync<\/code>\uff09\u306e2\u30d1\u30bf\u30fc\u30f3\u3042\u308b\u3002\u30e2\u30c7\u30eb\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306b\u5bfe\u3057\u3066<code>sync()<\/code>\u3092\u5b9f\u884c\u3059\u308b\u3068\u3001\u5bfe\u5fdc\u3059\u308b\u30c6\u30fc\u30d6\u30eb\u5b9a\u7fa9\u3060\u3051\u304c\u69cb\u7bc9\u3055\u308c\u308b\u3002 Sequelize\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306b\u5bfe\u3057\u3066<code>sync()<\/code>\u3092\u5b9f\u884c\u3059\u308b\u3068\u3001\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u5168\u4f53\u304c\u69cb\u7bc9\u3055\u308c\u308b\u3002\u307e\u305f\u3001<code>sync()<\/code>\u306e\u5f15\u6570\u306b<code>{ force: true }<\/code>\u3092\u6e21\u3059\u3068\u3001\u65e2\u5b58\u306e\u30c6\u30fc\u30d6\u30eb\u3092\u4e00\u65e6\u524a\u9664\uff08DROP\uff09\u3057\u3066\u30bc\u30ed\u304b\u3089\u30c6\u30fc\u30d6\u30eb\u3092\u4f5c\u308a\u76f4\u3059\uff08\u518d\u69cb\u7bc9\uff09\u3002<\/p>\n<p><!--more--><br \/>\nAPI\u3068\u3057\u3066\u306f\u3001\u500b\u3005\u306e\u30c6\u30fc\u30d6\u30eb\uff08\u30e2\u30c7\u30eb\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\uff09\u306b\u5bfe\u3059\u308b\u547c\u3073\u51fa\u3057\u3068\u3001\u5168\u4f53\uff08Sequelize\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\uff09\u306b\u5bfe\u3059\u308b\u547c\u3073\u51fa\u3057\u3092\u5206\u3051\u3066\u63d0\u4f9b\u3059\u308b\u3002\u305d\u308c\u305e\u308c\u306e\u30e1\u30bd\u30c3\u30c9\u540d\u3092<code>\/devel\/sync\/:table<\/code>\u3001<code>\/devel\/sync<\/code>\u3068\u3057\u3066\u3001<code>routes\/index.js<\/code>\u306b\u4ee5\u4e0b\u306e\u30b3\u30fc\u30c9\u3092\u8ffd\u52a0\u3057\u3088\u3046\u3002<\/p>\n<pre class=\"brush: jscript; highlight: [7,8,9,10,11]; 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  app.resource('users', require('.\/users'), { id: 'id' });\r\n};\r\n<\/pre>\n<p><code>sync<\/code>\u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u3046\u306e\u306f\u3042\u304f\u307e\u3067\u958b\u767a\u904e\u7a0b\u3060\u3051\u3067\u3042\u3063\u3066\u3001\u30ea\u30ea\u30fc\u30b9\u3057\u305f\u3068\u304d\u306bAPI\u3092\u53e9\u304b\u308c\u3066\u3057\u307e\u3063\u3066\u306f\u305f\u307e\u3089\u306a\u3044\u306e\u3067\u3001<code>development<\/code>\u74b0\u5883\u3067\u3060\u3051\u52d5\u4f5c\u3059\u308b\u3088\u3046\u306b\u3057\u3066\u304a\u304f\u3002\u307e\u305f\u3001\u30d6\u30e9\u30a6\u30b6\u304b\u3089\u3046\u3063\u304b\u308aAPI\u3092\u53e9\u304f\u3053\u3068\u304c\u306a\u3044\u3088\u3046\u306b\u3001HTTP\u30e1\u30bd\u30c3\u30c9\u3082POST\u306e\u307f\u53d7\u3051\u4ed8\u3051\u308b\u3088\u3046\u306b\u3057\u3066\u304a\u304f\u3002<\/p>\n<p>\u7d9a\u3044\u3066<code>routes\/sync.js<\/code>\u306b<code>sync<\/code>\u30e2\u30b8\u30e5\u30fc\u30eb\u3092\u5b9f\u88c5\u3057\u3066\u3044\u304f\u3002<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nvar done = function(res) {\r\n      return function() {\r\n        res.send('Done.\\n');\r\n      };\r\n    },\r\n    dberr = function(res) {\r\n      return function(e) {\r\n          res.statusCode = 500;\r\n          res.send(e);\r\n      };\r\n    };\r\n\r\nmodule.exports =\r\n{\r\n  all: function(sequelize) {\r\n    return function(req, res) {\r\n      sequelize.sync(req.body).success(done(res)).error(dberr(res));\r\n    };\r\n  },\r\n\r\n  one: function(models) {\r\n    return function(req, res) {\r\n      var model = models[req.params.table];\r\n      if(model != undefined) {\r\n        model.sync(req.body).success(done(res)).error(dberr(res));\r\n      }\r\n      else {\r\n        res.send(404);\r\n      }\r\n    };\r\n  }\r\n};\r\n<\/pre>\n<p>\u7e70\u308a\u8fd4\u3059\u304c\u3001\u958b\u767a\u904e\u7a0b\u3067\u4f7f\u3046\u3082\u306e\u306a\u306e\u3067\u3001\u6b63\u5e38\u6642\u3001\u30a8\u30e9\u30fc\u6642\u306e\u51fa\u529b\u306f\u81ea\u5206\u304c\u308f\u304b\u308b\u30ec\u30d9\u30eb\u3067\u3088\u3044\u3002<\/p>\n<p>\u3055\u3041\u3001\u3053\u308c\u3067\u6e96\u5099\u304c\u6574\u3063\u305f\u3002<a href=\"http:\/\/h2plus.biz\/hiromitsu\/entry\/646\">\u4e2d\u7de8<\/a>\u3067<code>models\/users.js<\/code>\u3067\u5b9f\u88c5\u3057\u305f<code>User<\/code>\u30e2\u30c7\u30eb\u5b9a\u7fa9\u3092\u601d\u3044\u51fa\u3057\u3066\u307b\u3057\u3044\u3002\u30e2\u30c7\u30eb\u5b9a\u7fa9\u3092\u3057\u305f\u3060\u3051\u3067\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u4e0a\u306b\u306f\u307e\u3060\u30c6\u30fc\u30d6\u30eb\u5b9a\u7fa9\u304c\u306a\u3044\u72b6\u614b\u3067\u3042\u308b\u3002<\/p>\n<p>\u307e\u305a\u306fnode\u3092\u8d77\u3061\u4e0a\u3052\u3066\u304a\u304f\u3002<\/p>\n<pre>\n$ NODE_PATH=libs node app.js\n<\/pre>\n<p>\u5225\u30bf\u30fc\u30df\u30ca\u30eb\u3092\u958b\u3044\u3066\u3001\u3044\u307e\u5b9f\u88c5\u3057\u305f<code>sync<\/code>\u30e2\u30b8\u30e5\u30fc\u30eb\u306eAPI\u3092\u5b9f\u884c\u3057\u3066\u307f\u3088\u3046\u3002<\/p>\n<pre>\n$ curl -X POST localhost:3000\/devel\/sync\nDone.\n<\/pre>\n<p>node\u3092\u8d77\u3061\u4e0a\u3052\u305f\u30bf\u30fc\u30df\u30ca\u30eb\u306b\u306f\u3001\u30c6\u30fc\u30d6\u30eb\u304c\u69cb\u7bc9\u3055\u308c\u305f\u969b\u306b\u5b9f\u884c\u3055\u308c\u305fSQL\u6587\u304c\u30c0\u30f3\u30d7\u3055\u308c\u3066\u3044\u308b\u306f\u305a\u3060\u3002<\/p>\n<pre>\n$ NODE_PATH=libs node app.js\nSequelize connecting to test on localhost as h2plus\nExpress server listening on port 3000 in development mode\nExecuting: CREATE TABLE IF NOT EXISTS `users` (\n `userame` VARCHAR(255),\n `password` char(40),\n `id` INTEGER NOT NULL auto_increment ,\n `createdAt` DATETIME NOT NULL,\n `updatedAt` DATETIME NOT NULL,\n PRIMARY KEY (`id`)\n) ENGINE=InnoDB;\n<\/pre>\n<p><code>models\/users.js<\/code>\u3067\u306f\u3001<code>id<\/code>\u3001<code>createdAt<\/code>\u3001<code>updatedAt<\/code>\u3068\u3044\u3046\u30ab\u30e9\u30e0\u3092\u5b9a\u7fa9\u3057\u3066\u3044\u306a\u3044\u304c\u3001\u3053\u308c\u306fSequelize\u304c\uff08\u52dd\u624b\u306b\uff09\u751f\u6210\u3057\u3066\u3044\u308b\u3082\u306e\u3067\u3042\u308b\u3002<\/p>\n<p><code>mysqlshow<\/code>\u30b3\u30de\u30f3\u30c9\u3092\u5b9f\u884c\u3057\u3066\u3001\u5b9f\u969b\u306b\u30c6\u30fc\u30d6\u30eb\u304c\u3067\u304d\u3066\u3044\u308b\u3053\u3068\u3092\u78ba\u8a8d\u3057\u3066\u307f\u308b\u3068\u3044\u3044\u3060\u308d\u3046\u3002<\/p>\n<pre>\n$ mysqlshow -u h2plus -p test users\n<\/pre>\n<p><code>users<\/code>\u30c6\u30fc\u30d6\u30eb\u3060\u3051\u69cb\u7bc9\u3059\u308b\u306a\u3089\u3070\u3001<\/p>\n<pre>\n$ curl -X POST localhost:3000\/devel\/sync\/users\n<\/pre>\n<p>\u3092\u5b9f\u884c\u3059\u308c\u3070\u3044\u3044\u3002<\/p>\n<p><code>User<\/code>\u30e2\u30c7\u30eb\u306e\u5b9a\u7fa9\u3092\u9014\u4e2d\u3067\u5909\u66f4\u3057\u3066\u3001\u30c6\u30fc\u30d6\u30eb\u3092\u518d\u69cb\u7bc9\u3057\u305f\u3044\u5834\u5408\u306f\u3001<\/p>\n<pre>\n$ curl -X POST -d 'force=1' localhost:3000\/devel\/sync\/users\n<\/pre>\n<p>\u3092\u5b9f\u884c\u3059\u308c\u3070\u3001<code>sequelize.sync({ force: true })<\/code>\u304c\u5b9f\u884c\u3055\u308c\u308b\u3088\u3046\u306b\u306a\u308b\u3002<\/p>\n<p>\u3042\u3068\u306f\u3001\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306e\u30e2\u30c7\u30eb\u5b9a\u7fa9\u3092<code>models<\/code>\u914d\u4e0b\u306b\u8ffd\u52a0\u3057\u3066\u3044\u304d\u3001<code>routes\/index.js<\/code>\u306b\u30ed\u30b8\u30c3\u30af\u3092\u5b9f\u88c5\u3057\u3066\u3044\u304f\u3060\u3051\u3067\u3042\u308b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u305d\u308c\u3067\u306f\u4e2d\u7de8\u3067\u68da\u4e0a\u3052\u3057\u3066\u3044\u305fsync\u30e1\u30bd\u30c3\u30c9\u3092\u63d0\u4f9b\u3059\u308bAPI\u306e\u5b9f\u88c5\u3092\u66f8\u3044\u3066\u3044\u3053\u3046\u3002 Sequelize\u306e\u30b5\u30f3\u30d7\u30eb\u3092\u898b\u3066\u3082\u308f\u304b\u308b\u3088\u3046\u306b\u3001sync\u30e1\u30bd\u30c3\u30c9\u306fimport\u30e1\u30bd\u30c3\u30c9\u306b\u3088\u3063\u3066\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u5316\u3055\u308c\u305f\u30e2\u30c7\u30eb\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306b <a href='https:\/\/h2plus.biz\/hiromitsu\/entry\/659' 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":[234,233,225,231,230],"_links":{"self":[{"href":"https:\/\/h2plus.biz\/hiromitsu\/wp-json\/wp\/v2\/posts\/659"}],"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=659"}],"version-history":[{"count":2,"href":"https:\/\/h2plus.biz\/hiromitsu\/wp-json\/wp\/v2\/posts\/659\/revisions"}],"predecessor-version":[{"id":999,"href":"https:\/\/h2plus.biz\/hiromitsu\/wp-json\/wp\/v2\/posts\/659\/revisions\/999"}],"wp:attachment":[{"href":"https:\/\/h2plus.biz\/hiromitsu\/wp-json\/wp\/v2\/media?parent=659"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/h2plus.biz\/hiromitsu\/wp-json\/wp\/v2\/categories?post=659"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/h2plus.biz\/hiromitsu\/wp-json\/wp\/v2\/tags?post=659"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}