{"id":5546,"date":"2020-01-10T01:17:37","date_gmt":"2020-01-09T16:17:37","guid":{"rendered":"https:\/\/blog.capilano-fw.com\/?p=5546"},"modified":"2020-01-25T05:37:41","modified_gmt":"2020-01-24T20:37:41","slug":"%e4%be%bf%e5%88%a9%ef%bc%81node-js%e3%81%abdb%e3%83%9e%e3%82%a4%e3%82%b0%e3%83%ac%e3%83%bc%e3%82%b7%e3%83%a7%e3%83%b3%e3%80%81seed%e3%80%81%e3%83%a2%e3%83%87%e3%83%ab%e3%82%92%e7%94%a8%e6%84%8f","status":"publish","type":"post","link":"https:\/\/blog.capilano-fw.com\/?p=5546","title":{"rendered":"\u4fbf\u5229\uff01Node.js\u306bDB\u30de\u30a4\u30b0\u30ec\u30fc\u30b7\u30e7\u30f3\u3001Seed\u3001\u30e2\u30c7\u30eb\u3092\u7528\u610f\u3059\u308b\u300cSequelize\u300d"},"content":{"rendered":"<p>\u3055\u3066\u3055\u3066\u3001\u524d\u3005\u56de\u306e\u300c<a href=\"https:\/\/blog.capilano-fw.com\/?p=5485\">\u5165\u9580\uff01Express\u306e\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3068\u57fa\u672c\u306e\u307e\u3068\u3081<\/a>\u300d\u3067\u3082\u5c11\u3057\u66f8\u3044\u305f\u306e\u3067\u3059\u304c\u3001<code>Node.js<\/code>\u306e\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u300c<strong>Express<\/strong>\u300d\u3092\u672c\u683c\u7684\u306b\u4f7f\u3063\u3066\u307f\u305f\u5b9f\u611f\u3068\u3057\u3066\u3001<code>Laravel<\/code>\u306e\u3088\u3046\u306a\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u5468\u308a\u306e\u6a5f\u80fd\u304c\u306a\u304f\u3001\u3061\u3087\u3063\u3068\u6b8b\u5ff5\u306a\u6c17\u6301\u3061\u306b\u306a\u3063\u3066\u3044\u307e\u3057\u305f\u3002<\/p>\n<p>\u3068\u3053\u308d\u304c\u3001\u3084\u306f\u308a\u4e16\u754c\u4e2d\u306b\u306f\u5929\u624d\u30d7\u30ed\u30b0\u30e9\u30de\u30fc\u305f\u3061\u304c\u591a\u6570\u3044\u308b\u3082\u306e\u3067\u3001<code>Node.js<\/code>\u306b\u4ee5\u4e0b\uff13\u3064\u306e\u6a5f\u80fd\u3092\u4e00\u6c17\u306b\u63d0\u4f9b\u3057\u3066\u304f\u308c\u308b\u30d1\u30c3\u30b1\u30fc\u30b8\u304c\u3042\u308b\u3053\u3068\u3092\u77e5\u308a\u307e\u3057\u305f\u3002<\/p>\n<ul>\n<li>\u30de\u30a4\u30b0\u30ec\u30fc\u30b7\u30e7\u30f3\u3000\u30fb\u30fb\u30fb\u3000DB\u30c6\u30fc\u30d6\u30eb\u306e\u7ba1\u7406<\/li>\n<li>Seed\u3000\u30fb\u30fb\u30fb\u3000\u30c6\u30b9\u30c8\u30c7\u30fc\u30bf\u306e\u7ba1\u7406<\/li>\n<li>\u30e2\u30c7\u30eb\u3000\u30fb\u30fb\u30fb\u3000\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u64cd\u4f5c\uff08<code>Laravel<\/code>\u306e<code>Eloquent<\/code>\u306e\u3088\u3046\u306a\u3082\u306e\uff09<\/li>\n<\/ul>\n<p>\u305d\u306e\u540d\u3082\u300c<a href=\"https:\/\/sequelize.org\/master\/\" target=\"_blank\" rel=\"noopener noreferrer\">Sequelize<\/a>\u300d\u3067\u3059\u3002<\/p>\n<p>\u203b <code>YouTube<\/code>\u3092\u898b\u308b\u9650\u308a\u3001\u300c\u3059\u3043\u30fc\u304f\u3045\u3089\u3044\u305a\u300d\u3068\u8aad\u3080\u3088\u3046\u3067\u3059\u3002\u82f1\u8a9e\u3067<code>SQL<\/code>\u3092\u300c\u3059\u3043\u30fc\u304f\u3045\uff08\u308b\uff09\u300d\u3068\u8aad\u3080\u3053\u3068\u306b\u639b\u3051\u3066\u308b\u306e\u304b\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<p>\u3057\u304b\u3082\u3053\u306e\u30d1\u30c3\u30b1\u30fc\u30b8\u306b\u306f\u3001<code>Laravel<\/code>\u306e<code>artisan<\/code>\u30b3\u30de\u30f3\u30c9\u306e\u3088\u3046\u306b\u5404\u30d5\u30a1\u30a4\u30eb\u3092\u81ea\u52d5\u7684\u306b\u4f5c\u6210\u3057\u3066\u304f\u308c\u308b\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u7528\u30d1\u30c3\u30b1\u30fc\u30b8\u3082\u3042\u308b\u306e\u3067\u3001\u3053\u308c\u3092\u4f7f\u3048\u3070\u4e00\u6c17\u306b\u958b\u767a\u30b9\u30d4\u30fc\u30c9\u304c\u4e0a\u3052\u308b\u3053\u3068\u9593\u9055\u3044\u306a\u3057\u3067\u3059\u3002<\/p>\n<p>\u305d\u3053\u3067\uff01<\/p>\n<p>\u4eca\u56de\u306f\u3001<code>Node.js<\/code>\u306e\u30d1\u30c3\u30b1\u30fc\u30b8\u300c<strong>Sequilize<\/strong>\u300d\u306e\u4f7f\u3044\u65b9\u3092\u3054\u7d39\u4ecb\u3057\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<p>\u305c\u3072\u7686\u3055\u3093\u306e\u304a\u5f79\u306b\u7acb\u3066\u308b\u3068\u5b09\u3057\u3044\u3067\u3059\ud83d\ude0a\u2728<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-3405 aligncenter\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2019\/04\/no_18.png\" alt=\"\" width=\"310\" height=\"320\" srcset=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2019\/04\/no_18.png 310w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2019\/04\/no_18-291x300.png 291w\" sizes=\"auto, (max-width: 310px) 100vw, 310px\" \/><\/p>\n<p><strong>\u958b\u767a\u74b0\u5883\uff1a<\/strong> Node.js 8.10\u3001Express 4.1\u3001Sequelize 5.2<\/p>\n<h1>\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3059\u308b<\/h1>\n<p>\u307e\u305a\u306f\u30d1\u30c3\u30b1\u30fc\u30b8\u306e\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3067\u3059\u304c\u3001\u300c<strong>Sequelize<\/strong>\u300d\u672c\u4f53\u3068\u30b3\u30de\u30f3\u30c9\u5165\u529b\u304c\u4f7f\u3048\u308b\u3088\u3046\u306b\u306a\u308b\u300c<strong><span class=\"pln\">sequelize<\/span><span class=\"pun\">&#8211;<\/span><span class=\"pln\">cli<\/span><\/strong>\u300d\u3001\u305d\u3057\u3066\u4eca\u56de\u306f\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306b<code>MySQL<\/code>\u3092\u4f7f\u3044\u307e\u3059\u306e\u3067\u300c<strong>mysql2<\/strong>\u300d\u306e\uff13\u3064\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u307e\u3059\u3002<\/p>\n<p>\u307e\u305a\u306f\u3001\u30bf\u30fc\u30df\u30ca\u30eb\u3067<code>sequelize<\/code>\u3092\u4f7f\u3044\u305f\u3044\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u306e\u30d5\u30a9\u30eb\u30c0\u306b\u79fb\u52d5\u3057\u3066\u4ee5\u4e0b\u306e\u30b3\u30de\u30f3\u30c9\u3092\u5b9f\u884c\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<p><strong>\u672c\u4f53\u30fb\u30fb\u30fb\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u5185\u306b\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb<\/strong><\/p>\n<pre><strong>npm install --save sequelize<\/strong><\/pre>\n<p><strong>CLI\u30fb\u30fb\u30fb \u30b0\u30ed\u30fc\u30d0\u30eb\u74b0\u5883\u306b\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb<\/strong><\/p>\n<pre><strong>sudo npm install -g sequelize-cli<\/strong><\/pre>\n<p>\u203b \u4eca\u5f8c\u3069\u306e\u30d5\u30a9\u30eb\u30c0\u304b\u3089\u3067\u3082\u4f7f\u3048\u308b\u3088\u3046\u306b\u3057\u305f\u3044\u306e\u3067\u30b0\u30ed\u30fc\u30d0\u30eb\u74b0\u5883\u306b\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<p><strong>MySQL<\/strong><\/p>\n<pre><strong>npm install --save mysql2<\/strong><\/pre>\n<p>\u203b \u3082\u3057\u5225\u306eDB\u3092\u4f7f\u3044\u305f\u3044\u5834\u5408\u306f\u4ee5\u4e0b\u3092\u53c2\u7167\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<p><strong>PostgreSQL<\/strong><\/p>\n<pre>npm install --save pg pg-hstore<\/pre>\n<p><strong>MariaDB<\/strong><\/p>\n<pre>npm install --save mariadb<\/pre>\n<p><strong>SQLite<\/strong><\/p>\n<pre>npm install --save sqlite3<\/pre>\n<p><strong>SQL Server<\/strong><\/p>\n<pre>npm install --save tedious\r\n<\/pre>\n<h1>\u6e96\u5099\u3059\u308b<\/h1>\n<p>\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u304c\u5b8c\u4e86\u3057\u305f\u3089\u3001\u4ee5\u4e0b\u306e\u30b3\u30de\u30f3\u30c9\u3067\u521d\u671f\u5316\u3092\u3057\u3066\u304a\u304d\u307e\u3057\u3087\u3046\u3002<\/p>\n<pre><strong>npx sequelize-cli init<\/strong><\/pre>\n<p>\u3059\u308b\u3068\u3001\u4ee5\u4e0b\uff14\u3064\u306e\u30d5\u30a9\u30eb\u30c0\u304c\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u5185\u306b\u4f5c\u6210\u3055\u308c\u307e\u3059\u3002<\/p>\n<ul>\n<li>seeders<\/li>\n<li>models<\/li>\n<li>migrations<\/li>\n<li>config<\/li>\n<\/ul>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-5548\" style=\"border: 3px solid #000;\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/01\/sequelize_1.png\" alt=\"\" width=\"466\" height=\"219\" srcset=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/01\/sequelize_1.png 466w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/01\/sequelize_1-300x141.png 300w\" sizes=\"auto, (max-width: 466px) 100vw, 466px\" \/><\/p>\n<p>\u3067\u306f\u3001\u300c<strong>config<\/strong>\u300d\u30d5\u30a9\u30eb\u30c0\u5185\u306b\u300c<strong>config.json<\/strong>\u300d\u3068\u3044\u3046\u30d5\u30a1\u30a4\u30eb\u304c\u4f5c\u6210\u3055\u308c\u3066\u3044\u308b\u306e\u3067\u3001\u4e2d\u8eab\u3092\u958b\u3044\u3066\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u63a5\u7d9a\u60c5\u5831\u3092\u66f8\u304d\u8fbc\u3093\u3067\u304a\u304d\u307e\u3057\u3087\u3046\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-5549\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/01\/sequelize_2.png\" alt=\"\" width=\"195\" height=\"85\" \/><\/p>\n<p>\uff08\u4eca\u56de\u306f\u958b\u767a\u74b0\u5883\u306e<code>development<\/code>\u3060\u3051\u5909\u66f4\u3057\u307e\u3059\u304c\u3001\u30c6\u30b9\u30c8\u74b0\u5883\u306e<code>test<\/code>\u3001\u672c\u756a\u74b0\u5883\u306e<code>production<\/code>\u3082\u304a\u597d\u307f\u3067\u5909\u66f4\u3057\u3066\u304f\u3060\u3055\u3044\uff09<\/p>\n<pre>{\r\n    <strong>\"development\": {\r\n        \"username\": \"root\",\r\n        \"password\": \"********\",\r\n        \"database\": \"express41\",\r\n        \"host\": \"127.0.0.1\",\r\n        \"dialect\": \"mysql\",\r\n        \"operatorsAliases\": 0\r\n    }<\/strong>,\r\n    \"test\": {\r\n        \"username\": \"root\",\r\n        \"password\": null,\r\n        \"database\": \"database_test\",\r\n        \"host\": \"127.0.0.1\",\r\n        \"dialect\": \"mysql\",\r\n        \"operatorsAliases\": 0\r\n    },\r\n    \"production\": {\r\n        \"username\": \"root\",\r\n        \"password\": null,\r\n        \"database\": \"database_production\",\r\n        \"host\": \"127.0.0.1\",\r\n        \"dialect\": \"mysql\",\r\n        \"operatorsAliases\": 0\r\n    }\r\n}<\/pre>\n<p>\u203b \u5b9f\u884c\u74b0\u5883\u306e\u5207\u308a\u66ff\u3048\u306f\u3001<code>process.env.NODE_ENV<\/code>\u306e\u5024\u3092\u5909\u66f4\u3059\u308c\u3070OK\u3067\u3059\u3002<code>.env<\/code>\u3092\u4f7f\u3063\u3066\u5909\u66f4\u3059\u308b\u5834\u5408\u306f\u3001<a href=\"#dotenv\">Node.js\u3067.env\u3092\u4f7f\u3046\u65b9\u6cd5<\/a>\u3092\u3054\u89a7\u304f\u3060\u3055\u3044\u3002<br \/>\n\u203b \u307e\u305f\u3001<code>operatorsAliases<\/code>\u3092<code>false<\/code>\u306b\u3057\u3066\u3044\u3066\u8b66\u544a\u304c\u51fa\u308b\u5834\u5408\u306f<code>0<\/code>\u306b\u5909\u66f4\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<h1 id=\"use_migration\">\u30de\u30a4\u30b0\u30ec\u30fc\u30b7\u30e7\u30f3\u3092\u4f7f\u3046<\/h1>\n<p>\u30de\u30a4\u30b0\u30ec\u30fc\u30b7\u30e7\u30f3\u3068\u306f\u3001\u3056\u3063\u304f\u308a\u8a00\u3046\u3068\u300c<strong>\u7c21\u5358\u306bDB\u30c6\u30fc\u30d6\u30eb\u3092\u69cb\u7bc9\u3067\u304d\u308b<\/strong>\u300d\u6a5f\u80fd\u306e\u3053\u3068\u3067\u3059\u3002\u3072\u3068\u3064\u305a\u3064\u898b\u3066\u3044\u304d\u307e\u3057\u3087\u3046\u3002<\/p>\n<h2>\u30c6\u30fc\u30d6\u30eb\u3092\u3064\u304f\u308b<\/h2>\n<p>\u4f8b\u3048\u3070\u3001<code>Users<\/code>\u3068\u3044\u3046\u540d\u524d\u306e\u30c6\u30fc\u30d6\u30eb\u3092DB\u306b\u3064\u304f\u308b\uff08and \u524a\u9664\u3059\u308b\uff09\u30de\u30a4\u30b0\u30ec\u30fc\u30b7\u30e7\u30f3\u306e\u30d5\u30a1\u30a4\u30eb\u3092\u4f5c\u6210\u3059\u308b\u306b\u306f\u4ee5\u4e0b\u306e\u30b3\u30de\u30f3\u30c9\u3092\u5b9f\u884c\u3057\u307e\u3059\u3002<\/p>\n<pre><strong>npx sequelize-cli migration:generate --name Users<\/strong><\/pre>\n<p><strong>\u3010\u6ce8\u610f\uff01\u3011\u305f\u3060\u3057\u3001<code>sequelize<\/code>\u306f\u3001\u30e2\u30c7\u30eb\u3092\u4f5c\u6210\u3059\u308b\u969b\u306b\u3082\u81ea\u52d5\u3067\u30de\u30a4\u30b0\u30ec\u30fc\u30b7\u30e7\u30f3\u3092\u4f5c\u6210\u3059\u308b\u4ed5\u69d8\u306b\u306a\u3063\u3066\u3044\u307e\u3059\u3002\u305d\u306e\u305f\u3081\u3001\u3082\u3057\u30e2\u30c7\u30eb\u3082\u5fc5\u8981\u306a\u5834\u5408\u306f\u30e2\u30c7\u30eb\u4f5c\u6210\u306e\u30b3\u30de\u30f3\u30c9\u3092\u4f7f\u3046\u3053\u3068\u3092\u304a\u3059\u3059\u3081\u3057\u307e\u3059\u3002\uff08\u30de\u30a4\u30b0\u30ec\u30fc\u30b7\u30e7\u30f3\u304c\u91cd\u8907\u3057\u3066\u3057\u307e\u3046\u305f\u3081\uff09\u5b9f\u969b\u306e\u30b3\u30de\u30f3\u30c9\u306f\u3001<a href=\"#use_model\">\u30e2\u30c7\u30eb\u3092\u4f7f\u3046<\/a>\u3092\u3054\u89a7\u304f\u3060\u3055\u3044\u3002<\/strong><\/p>\n<p>\u3059\u308b\u3068\u3001\u300c<strong>\/migrations\/********-Users.js<\/strong>\u300d\u3068\u3044\u3046\u30d5\u30a1\u30a4\u30eb\u304c\u4f5c\u6210\u3055\u308c\u308b\u306e\u3067\u4ee5\u4e0b\uff16\u3064\u306e\u30d5\u30a3\u30fc\u30eb\u30c9\u3092\u8ffd\u52a0\u3057\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n<ul>\n<li>id<\/li>\n<li>name<\/li>\n<li>email<\/li>\n<li>password<\/li>\n<li>createdAt<\/li>\n<li>updatedAt<\/li>\n<\/ul>\n<pre>'use strict';\r\n\r\nmodule.exports = {\r\n  up: (queryInterface, Sequelize) =&gt; {\r\n    return queryInterface.createTable('Users', {\r\n      <strong>id: {\r\n        allowNull: false,\r\n        autoIncrement: true,\r\n        primaryKey: true,\r\n        type: Sequelize.INTEGER\r\n      },\r\n      name: {\r\n        type: Sequelize.STRING\r\n      },\r\n      email: {\r\n        type: Sequelize.STRING\r\n      },\r\n      password: {\r\n        type: Sequelize.STRING\r\n      },\r\n      createdAt: {\r\n        allowNull: false,\r\n        type: Sequelize.DATE\r\n      },\r\n      updatedAt: {\r\n        allowNull: false,\r\n        type: Sequelize.DATE\r\n      }<\/strong>\r\n    });\r\n  },\r\n\r\n  down: (queryInterface, Sequelize) =&gt; {\r\n    return queryInterface.dropTable('Users');\r\n  }\r\n};\r\n<\/pre>\n<p>\u305d\u3057\u3066\u3001\u30d5\u30a1\u30a4\u30eb\u3092\u4fdd\u5b58\u3057\u305f\u3089\u4ee5\u4e0b\u306e\u30b3\u30de\u30f3\u30c9\u3067\u30de\u30a4\u30b0\u30ec\u30fc\u30b7\u30e7\u30f3\u3092\u5b9f\u884c\u3057\u307e\u3059\u3002<\/p>\n<pre><strong>npx sequelize-cli db:migrate<\/strong><\/pre>\n<p>\u30b3\u30de\u30f3\u30c9\u3092\u5b9f\u884c\u3059\u308b\u3068<code>DB<\/code>\u306f\u3053\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<p>\u3010\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u3011<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-5550\" style=\"border: 3px solid #000;\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/01\/sequelize_3.png\" alt=\"\" width=\"661\" height=\"332\" srcset=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/01\/sequelize_3.png 661w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/01\/sequelize_3-300x151.png 300w\" sizes=\"auto, (max-width: 661px) 100vw, 661px\" \/><\/p>\n<p>\u3010Users\u30c6\u30fc\u30d6\u30eb\u3011<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-5569\" style=\"border: 3px solid #000;\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/01\/sequelize_4.png\" alt=\"\" width=\"348\" height=\"347\" srcset=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/01\/sequelize_4.png 348w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/01\/sequelize_4-300x300.png 300w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/01\/sequelize_4-150x150.png 150w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/01\/sequelize_4-100x100.png 100w\" sizes=\"auto, (max-width: 348px) 100vw, 348px\" \/><\/p>\n<h3>\u30d5\u30a3\u30fc\u30eb\u30c9\u306e\u8a2d\u5b9a\u9805\u76ee<\/h3>\n<p>\u306a\u304a\u3001\u30d5\u30a3\u30fc\u30eb\u30c9\u3067\u8a2d\u5b9a\u3067\u304d\u308b\u4e3b\u306a\u9805\u76ee\u306f\u6b21\u306e\u3068\u304a\u308a\u3067\u3059\u3002<\/p>\n<ul>\n<li>type\u3000\u30fb\u30fb\u30fb\u3000\u30c7\u30fc\u30bf\u30bf\u30a4\u30d7\uff08\u6b21\u306e\u9805\u76ee\u3092\u53c2\u7167\u3057\u3066\u304f\u3060\u3055\u3044\uff09<\/li>\n<li>primaryKey\u3000\u30fb\u30fb\u30fb\u3000\u30d7\u30e9\u30a4\u30de\u30ea\u30fc\u30ad\u30fc\uff08true \/ false\uff09<\/li>\n<li>autoIncrement\u3000\u30fb\u30fb\u30fb\u3000\u30aa\u30fc\u30c8\u30a4\u30f3\u30af\u30ea\u30e1\u30f3\u30c8\uff08true \/ false\uff09<\/li>\n<li>unique\u3000\u30fb\u30fb\u30fb\u3000\u30e6\u30cb\u30fc\u30af\u3001\u540c\u3058\u5024\u304c\u306a\u3044\uff08true \/ false\uff09<\/li>\n<li>allowNull\u3000\u30fb\u30fb\u30fb\u3000Null\u306e\u8a31\u53ef\uff08true \/ false\uff09<\/li>\n<li>defaultValue\u3000\u30fb\u30fb\u30fb\u3000\u30c7\u30d5\u30a9\u30eb\u30c8\u5024<\/li>\n<li>comment\u3000\u30fb\u30fb\u30fb\u3000\u30d5\u30a3\u30fc\u30eb\u30c9\u306e\u8aac\u660e<\/li>\n<\/ul>\n<h3>\u30c7\u30fc\u30bf\u30bf\u30a4\u30d7<\/h3>\n<p>\u4e3b\u306a\u30c7\u30fc\u30bf\u30bf\u30a4\u30d7\u306f\u4ee5\u4e0b\u306e\u3068\u304a\u308a\u3067\u3059\u3002<\/p>\n<ul>\n<li>Sequelize.STRING\u3000\u30fb\u30fb\u30fb\u3000\u6587\u5b57\u5217<\/li>\n<li>Sequelize.TEXT\u3000\u30fb\u30fb\u30fb\u3000\u30c6\u30ad\u30b9\u30c8<\/li>\n<li>Sequelize.INTEGER\u3000\u30fb\u30fb\u30fb\u3000\u6570\u5b57<\/li>\n<li>Sequelize.INTEGER.UNSIGNED\u3000\u30fb\u30fb\u30fb\u3000\u6570\u5b57\uff08\u30bc\u30ed\u4ee5\u4e0a\uff09<\/li>\n<li>Sequelize.BIGINT\u3000\u30fb\u30fb\u30fb\u3000\uff08\u5927\u304d\u3044\u6570\u5b57\uff09<\/li>\n<li>Sequelize.FLOAT\u3000\u30fb\u30fb\u30fb\u3000\u6d6e\u52d5\u5c0f\u6570\u70b9\u6570\u578b<\/li>\n<li>Sequelize.BOOLEAN\u3000\u30fb\u30fb\u30fb\u3000true \/ false<\/li>\n<li>Sequelize.DATE\u3000\u30fb\u30fb\u30fb\u3000\u65e5\u6642<\/li>\n<li>Sequelize.DATEONLY\u3000\u30fb\u30fb\u30fb\u3000\u65e5\u4ed8<\/li>\n<\/ul>\n<h3>FOREIGN KEY<\/h3>\n<p>\u4f8b\u3048\u3070\u3001\u5148\u307b\u3069\u4f5c\u3063\u305f<code>Users<\/code>\u30c6\u30fc\u30d6\u30eb\u3068\u9023\u52d5\u3059\u308b\u30c6\u30fc\u30d6\u30eb<code>posts<\/code>\u306e<code>user_id<\/code>\u306b<code>FOREEIGN KEY<\/code>\u3092\u8a2d\u5b9a\u3059\u308b\u5834\u5408\u306f\u6b21\u306e\u3088\u3046\u306b\u3057\u307e\u3059\u3002<\/p>\n<pre>'use strict';\r\n\r\nmodule.exports = {\r\n  up: (queryInterface, Sequelize) =&gt; {\r\n    return queryInterface.createTable('posts', {\r\n      id: {\r\n        type: Sequelize.INTEGER.UNSIGNED,\r\n        primaryKey: true,\r\n        autoIncrement: true,\r\n        allowNull: false\r\n      },\r\n      <strong>user_id: {\r\n        type: Sequelize.INTEGER,\r\n        references: { model: 'Users', key: 'id'}, \/\/ \u5916\u90e8\u30ad\u30fc\r\n        onUpdate: 'cascade',  \/\/ \uff08\u4efb\u610f\uff09\u9023\u52d5\u3057\u3066\u81ea\u52d5\u66f4\u65b0\u3059\u308b\u5834\u5408\r\n        onDelete: 'cascade'   \/\/ \uff08\u4efb\u610f\uff09\u9023\u52d5\u3057\u3066\u81ea\u52d5\u524a\u9664\u3059\u308b\u5834\u5408\r\n      }<\/strong>\r\n    });\r\n  },\r\n\r\n  down: (queryInterface, Sequelize) =&gt; {\r\n    return queryInterface.dropTable('posts');\r\n  }\r\n};<\/pre>\n<h3>\u30a4\u30f3\u30c7\u30c3\u30af\u30b9<\/h3>\n<p>\u30c6\u30fc\u30d6\u30eb\u306b\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8a2d\u5b9a\u3059\u308b\u5834\u5408\u306f\u6b21\u306e\u3088\u3046\u306b\u3057\u307e\u3059\u3002<\/p>\n<pre>'use strict';\r\n\r\nmodule.exports = {\r\n  up: (queryInterface, Sequelize) =&gt; {\r\n    return queryInterface.createTable('posts', {\r\n      \/\/ \u7701\u7565\r\n    })<strong>.then(() =&gt; { \/\/ \u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8ffd\u52a0\r\n\r\n      return queryInterface.addIndex('posts', ['user_id']);\r\n\r\n    })<\/strong>;\r\n  },\r\n\r\n  down: (queryInterface, Sequelize) =&gt; {\r\n    return queryInterface.dropTable('posts');\r\n  }\r\n};<\/pre>\n<h2>\u30de\u30a4\u30b0\u30ec\u30fc\u30b7\u30e7\u30f3\u3092\u3072\u3068\u3064\u524d\u306b\u623b\u3059<\/h2>\n<p>\u4f8b\u3048\u3070\u3001\u300c\u3055\u3063\u304d\u30de\u30a4\u30b0\u30ec\u30fc\u30b7\u30e7\u30f3\u3057\u305f\u3051\u3069\u3001\u629c\u3051\u3066\u308b\u9805\u76ee\u304c\u3042\u3063\u305f\u304b\u3089\u3084\u308a\u76f4\u3057\u305f\u3044\u300d\u3068\u3044\u3046\u5834\u5408\uff08\u3044\u308f\u3086\u308b\u3001\u30ed\u30fc\u30eb\u30d0\u30c3\u30af\uff09\u306f\u4ee5\u4e0b\u306e\u30b3\u30de\u30f3\u30c9\u3092\u5b9f\u884c\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<pre><strong>npx sequelize-cli db:migrate:undo<\/strong><\/pre>\n<h2>\u30de\u30a4\u30b0\u30ec\u30fc\u30b7\u30e7\u30f3\u3092\u5168\u3066\u5143\u306b\u623b\u3059<\/h2>\n<p>\u3072\u3068\u3064\u3060\u3051\u3067\u306a\u304f\u3001\u5168\u3066\u306e\u30de\u30a4\u30b0\u30ec\u30fc\u30b7\u30e7\u30f3\u3092\u306f\u3058\u3081\u304b\u3089\u3084\u308a\u76f4\u3057\u305f\u3044\u5834\u5408\u306f\u3001\u3053\u3061\u3089\u306e\u30b3\u30de\u30f3\u30c9\u3067\u3059\u3002<\/p>\n<pre><strong>npx sequelize-cli db:migrate:undo:all<\/strong><\/pre>\n<h1>Seed\u3092\u4f7f\u3046<\/h1>\n<p><code>Seed<\/code>\u3068\u306fDB\u30c6\u30fc\u30d6\u30eb\u306b\u521d\u671f\u5024\u3068\u3057\u3066\u7528\u610f\u3059\u308b\uff08\u30c6\u30b9\u30c8\uff09\u30c7\u30fc\u30bf\u3067\u3059\u3002<code>Seed<\/code>\u3092\u4f5c\u3063\u3066\u304a\u304f\u3068\u3001\u30c6\u30fc\u30d6\u30eb\u3092\u521d\u671f\u5316\u3057\u3066\u3082\u307e\u305f\u540c\u3058\u30c7\u30fc\u30bf\u3092\u7528\u610f\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u306e\u3067\u3068\u3066\u3082\u4fbf\u5229\u3067\u3059\u3002<\/p>\n<h2>\u30c7\u30fc\u30bf\u3092\u8ffd\u52a0\u3059\u308b<\/h2>\n<p>\u4f8b\u3048\u3070\u3001<code>Users<\/code>\u306b\u767b\u9332\u3059\u308b\u30c6\u30b9\u30c8\u30e6\u30fc\u30b6\u30fc\u306e\u30c7\u30fc\u30bf\u3092\u3064\u304f\u308b\u5834\u5408\u3067\u3059\u3002<br \/>\n\u307e\u305a\u306f\u4ee5\u4e0b\u306e\u30b3\u30de\u30f3\u30c9\u3092\u5b9f\u884c\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<pre><strong>npx sequelize-cli seed:generate --name test-users<\/strong><\/pre>\n<p>\u3059\u308b\u3068\u3001\u300c<strong>\/seeders\/********-test-users.js<\/strong>\u300d\u3068\u3044\u3046\u30d5\u30a1\u30a4\u30eb\u304c\u4f5c\u6210\u3055\u308c\u308b\u306e\u3067\u4e2d\u8eab\u3092\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<pre>'use strict';\r\n\r\nmodule.exports = {\r\n  <strong>up: (queryInterface, Sequelize) =&gt; {\r\n    const now = new Date();\r\n    return queryInterface.bulkInsert('Users', [\r\n      { name: '\u592a\u90ce',  email: 'taro@example.com', password: 'taro-password', createdAt: now, updatedAt: now},\r\n      { name: '\u6b21\u90ce',  email: 'jiro@example.com', password: 'jiro-password', createdAt: now, updatedAt: now},\r\n      { name: '\u4e09\u90ce',  email: 'saburo@example.com', password: 'saburo-password', createdAt: now, updatedAt: now},\r\n      { name: '\u56db\u90ce',  email: 'shiro@example.com', password: 'shiro-password', createdAt: now, updatedAt: now},\r\n      { name: '\u4e94\u90ce',  email: 'goro@example.com', password: 'goro-password', createdAt: now, updatedAt: now},\r\n    ], {});\r\n  },\r\n\r\n  down: (queryInterface, Sequelize) =&gt; {\r\n    return queryInterface.bulkDelete('Users', null, {});\r\n  }<\/strong>\r\n};<\/pre>\n<p>\u3067\u306f\u3001\u4ee5\u4e0b\u306e\u30b3\u30de\u30f3\u30c9\u3092\u5b9f\u884c\u3057\u3066<code>Seed<\/code>\u3092\u5b9f\u884c\u3057\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n<pre><strong>npx sequelize-cli db:seed:all<\/strong><\/pre>\n<p>\u5b9f\u884c\u304c\u7d42\u308f\u308b\u3068\u30c6\u30fc\u30d6\u30eb\u306f\u3053\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-5567\" style=\"border: 3px solid #000;\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/01\/sequelize_5.png\" alt=\"\" width=\"674\" height=\"367\" srcset=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/01\/sequelize_5.png 674w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/01\/sequelize_5-300x163.png 300w\" sizes=\"auto, (max-width: 674px) 100vw, 674px\" \/><\/p>\n<h2>\u30c7\u30fc\u30bf\u3092\u5143\u306b\u623b\u3059<\/h2>\n<p><code>Seed<\/code>\u3092\u5143\u306b\u623b\u3059\u5834\u5408\u306f\u4ee5\u4e0b\u306e\u30b3\u30de\u30f3\u30c9\u3092\u4f7f\u3063\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<pre><strong>npx sequelize-cli db:seed:undo:all<\/strong><\/pre>\n<p>\u305f\u3060\u3057\u3001\u30aa\u30fc\u30c8\u30a4\u30f3\u30af\u30ea\u30e1\u30f3\u30c8\u306f\u3053\u306e\u65b9\u6cd5\u3067\u306f\u521d\u671f\u5316\u3055\u308c\u306a\u3044\u306e\u3067\u3001\u500b\u4eba\u7684\u306b\u306f<code>npx sequelize-cli db:migrate:undo:all<\/code>\u3067\u30c6\u30fc\u30d6\u30eb\u3092\u524a\u9664\u3057\u3066\u307e\u305f\u5b9f\u884c\u3059\u308b\u65b9\u304c\u3066\u3063\u3068\u308a\u65e9\u3044\u3088\u3046\u306a\u6c17\u304c\u3057\u307e\u3059\u3002<\/p>\n<h1 id=\"use_model\">\u30e2\u30c7\u30eb\u3092\u4f7f\u3046<\/h1>\n<p>\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306e\u64cd\u4f5c\u304c\u697d\u306b\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308b\u300c\u30c7\u30fc\u30bf\u306e\u7ba1\u7406\u4eba\u300d\u304c\u30e2\u30c7\u30eb\u306e\u5f79\u76ee\u3067\u3059\u3002<\/p>\n<h2>\u30e2\u30c7\u30eb\u3092\u3064\u304f\u308b<\/h2>\n<p>\u4f8b\u3048\u3070\u3001<code>User<\/code>\u3068\u3044\u3046\u30e2\u30c7\u30eb\u3092\u4f5c\u6210\u3059\u308b\u5834\u5408\u3067\u3059\u3002<\/p>\n<pre><strong>npx sequelize-cli model:generate --name User --attributes name:string,email:string,password:string\r\n<\/strong><\/pre>\n<p><code>sequelize<\/code>\u306f\u3001<code>id<\/code>\u3001<code>CreatedAt<\/code>\u3001<code>UpdatedAt<\/code>\u306e\uff13\u30d5\u30a3\u30fc\u30eb\u30c9\u3092\u81ea\u52d5\u7684\u306b\u4f5c\u6210\u3057\u3066\u304f\u308c\u307e\u3059\u306e\u3067\u3001\u305d\u308c\u4ee5\u5916\u3092\u6307\u5b9a\u3059\u308b\u3060\u3051\u3067OK\u3067\u3059\u3002<\/p>\n<p>\u3059\u308b\u3068\u3001\u4ee5\u4e0b\uff12\u3064\u306e\u30d5\u30a1\u30a4\u30eb\u304c\u4f5c\u6210\u3055\u308c\u307e\u3059\u3002<\/p>\n<ul>\n<li>\u30e2\u30c7\u30eb\u3000\u30fb\u30fb\u30fb\u3000\u300c\/<strong>models\/user.js<\/strong>\u300d<\/li>\n<li>\u30de\u30a4\u30b0\u30ec\u30fc\u30b7\u30e7\u30f3\u3000\u30fb\u30fb\u30fb\u3000\u300c<strong>\/migrations\/*******-create-user.js<\/strong>\u300d<\/li>\n<\/ul>\n<p>\u30de\u30a4\u30b0\u30ec\u30fc\u30b7\u30e7\u30f3\u306e\u8a2d\u5b9a\u30fb\u5b9f\u884c\u306f<a href=\"#use_migration\">\u30de\u30a4\u30b0\u30ec\u30fc\u30b7\u30e7\u30f3\u3092\u4f7f\u3046<\/a>\u3092\u53c2\u7167\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<h2>\u30e2\u30c7\u30eb\u3092\u4f7f\u3046<\/h2>\n<p>\u3067\u306f\u3001<code>Users<\/code>\u30c6\u30fc\u30d6\u30eb\u306e\u30c7\u30fc\u30bf\u3092\u5168\u3066\u53d6\u5f97\u3059\u308b\u5834\u5408\u306e\u4f8b\u3092\u898b\u3066\u307f\u307e\u3057\u3087\u3046\u3002\u30a2\u30af\u30bb\u30b9\u3059\u308bURL\u306f\u300c<strong>http:\/\/******\/all-users<\/strong>\u300d\u3067\u3059\u3002<\/p>\n<p>\u300c<strong>app.js<\/strong>\u300d\u3092\u958b\u3044\u3066\u6b21\u306e\u3088\u3046\u306b\u3057\u307e\u3059\u3002<\/p>\n<pre>const express = require('express');\r\nconst app = express();\r\n<strong>const User = require('.\/models').User;<\/strong>\r\n\r\n\/\/ \u30eb\u30fc\u30c8\r\n<strong>app.get('\/all-users', (req, res) =&gt; {\r\n\r\n  User.findAll().then(users =&gt; {\r\n\r\n    res.send(users);\r\n\r\n  });\r\n\r\n});<\/strong>\r\n\r\nconst PORT = process.env.PORT || 5000;\r\napp.listen(PORT, () =&gt; {\r\n  console.log(`${PORT}\u756a\u306e\u30dd\u30fc\u30c8\u3067\u5f85\u6a5f\u4e2d\u3067\u3059...`);\r\n});<\/pre>\n<p>\u3067\u306f\u3001<code>npm<\/code>\u3092\u518d\u8d77\u52d5\u3057\u3066\u30d6\u30e9\u30a6\u30b6\u304b\u3089\u898b\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-5571\" style=\"border: 3px solid #000;\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/01\/sequelize_6.png\" alt=\"\" width=\"510\" height=\"150\" srcset=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/01\/sequelize_6.png 510w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/01\/sequelize_6-300x88.png 300w\" sizes=\"auto, (max-width: 510px) 100vw, 510px\" \/><\/p>\n<p>\u3061\u306a\u307f\u306b\u3001\u30d6\u30e9\u30a6\u30b6\u304b\u3089\u30a2\u30af\u30bb\u30b9\u3057\u3066\u304d\u305f\u3068\u304d\u306b\u30b3\u30f3\u30bd\u30fc\u30eb\u3067\u306f\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u6bce\u56deSQL\u6587\u304c\u8868\u793a\u3055\u308c\u308b\u3053\u3068\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-5574\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/01\/sequelize_7.png\" alt=\"\" width=\"736\" height=\"44\" srcset=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/01\/sequelize_7.png 736w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/01\/sequelize_7-300x18.png 300w\" sizes=\"auto, (max-width: 736px) 100vw, 736px\" \/><\/p>\n<p>\u3082\u3061\u308d\u3093\u30ed\u30fc\u30ab\u30eb\u74b0\u5883\u3060\u3068\u3001\u958b\u767a\u306b\u3068\u3063\u3066\u30d7\u30e9\u30b9\u3067\u3059\u304c\u672c\u756a\u74b0\u5883\u3067\u306f\u4e0d\u8981\u306a\u3082\u306e\u3067\u3059\u3002\u305d\u306e\u305f\u3081\u3001\u3082\u3057\u3053\u306eSQL\u6587\u3092\u975e\u8868\u793a\u306b\u3057\u305f\u3044\u5834\u5408\u306f\u3001\u300c<strong>\/config\/config.json<\/strong>\u300d\u3092\u958b\u3044\u3066\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u3059\u308b\u3068\u3044\u3044\u3067\u3057\u3087\u3046\u3002<\/p>\n<pre>{\r\n  \"development\": {\r\n    \"username\": \"root\",\r\n    \"password\": \"xxxxxxxx\",\r\n    \"database\": \"express41\",\r\n    \"host\": \"127.0.0.1\",\r\n    \"dialect\": \"mysql\",\r\n    \"operatorsAliases\": 0,\r\n    <strong>\"logging\": false<\/strong>\r\n  },\r\n\r\n\/\/ \u7701\u7565<\/pre>\n<p>\u203b \u518d\u8d77\u52d5\u3059\u308b\u306e\u3092\u5fd8\u308c\u306a\u3044\u3067\u304f\u3060\u3055\u3044\u3002<\/p>\n<p>\u3010\u8ffd\u8a18\uff1a2020.1.11\u3011<\/p>\n<p>\u30e2\u30c7\u30eb\u306e\u3088\u308a\u8a73\u3057\u3044\u4f7f\u3044\u65b9\u306f\u3001<a href=\"https:\/\/blog.capilano-fw.com\/?p=5582\">\u4fdd\u5b58\u7248\uff01\u300csequelize\u300d\u30e2\u30c7\u30eb\u306e\u4f7f\u3044\u65b9\u5b9f\u4f8b\u30fb\u516853\u4ef6<\/a>\u306b\u3081\u3068\u3081\u3066\u3044\u307e\u3059\u306e\u3067\u3001\u305c\u3072\u3054\u89a7\u304f\u3060\u3055\u3044\u3002<\/p>\n<h1 id=\"dotenv\">\u304a\u307e\u3051\uff1aNode.js\u3067.env\u3092\u4f7f\u3046\u65b9\u6cd5<\/h1>\n<p><code>Laravel<\/code>\u3067\u3082\u4f7f\u308f\u308c\u3066\u3044\u308b\u74b0\u5883\u5909\u6570\u306e\u8a2d\u5b9a\u30b7\u30b9\u30c6\u30e0<code>.env<\/code>\u3092\u5c0e\u5165\u3057\u3066\u7c21\u5358\u306b\u5b9f\u884c\u74b0\u5883\u304c\u5909\u66f4\u306b\u306a\u308b\u3088\u3046\u306b\u3057\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n<p>\u307e\u305a\u306f\u3001\u30d1\u30c3\u30b1\u30fc\u30b8\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u307e\u3059\u3002<\/p>\n<pre><strong>npm install --save dotenv<\/strong><\/pre>\n<p>\u6b21\u306b\u3001\u30eb\u30fc\u30c8\u30d5\u30a9\u30eb\u30c0\u306b<code>.env<\/code>\u3068\u3044\u3046\u30d5\u30a1\u30a4\u30eb\u3092\u4f5c\u6210\u3057\u3066\u4e2d\u8eab\u3092\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u3057\u307e\u3059\u3002<\/p>\n<p>\u3010\u958b\u767a\u74b0\u5883\u306e\u5834\u5408\u3011<\/p>\n<pre><strong>NODE_ENV=development<\/strong><\/pre>\n<p>\u3010\u30c6\u30b9\u30c8\uff08\u30b9\u30c6\u30fc\u30b8\u30f3\u30b0\uff09\u74b0\u5883\u306e\u5834\u5408\u3011<\/p>\n<pre><strong>NODE_ENV=test<\/strong><\/pre>\n<p>\u3010\u672c\u756a\u74b0\u5883\u306e\u5834\u5408\u3011<\/p>\n<pre><strong>NODE_ENV=production<\/strong><\/pre>\n<p>\u305d\u3057\u3066\u3001\u300c<strong>app.js<\/strong>\u300d\u306e\u5148\u982d\u3067<code>dotenv<\/code>\u3092\u8aad\u307f\u8fbc\u307f\u307e\u3059\u3002<\/p>\n<pre><strong>require('dotenv').config()<\/strong>\r\nconst express = require('express');\r\nconst app = express();\r\n\r\n\/\/ \u4ee5\u4e0b\u7701\u7565<\/pre>\n<p>\u3053\u308c\u3067<code>.env<\/code>\u3092\u5404\u74b0\u5883\u3054\u3068\u306b\u7528\u610f\u3059\u308c\u3070\u5207\u308a\u66ff\u3048\u306fOK\u3067\u3059\u3002\uff08\u306a\u304a\u3001<code>git<\/code>\u3092\u4f7f\u3063\u3066\u30d0\u30fc\u30b8\u30e7\u30f3\u7ba1\u7406\u3059\u308b\u5834\u5408\u306f\u5fd8\u308c\u305a\u306b<code>.gitignore<\/code>\u5185\u306b<code>.env<\/code>\u3092\u8ffd\u52a0\u3057\u3066\u304a\u304d\u307e\u3057\u3087\u3046\uff09<\/p>\n<p>\u306a\u304a\u3001<code>.env<\/code>\u304c\u8a2d\u7f6e\u3055\u308c\u3066\u3044\u308b\u3088\u308a\u3082\u4e0b\u306e\u968e\u5c64\u3067\u5229\u7528\u3059\u308b\uff08\u4f8b\u3048\u3070\u3001\u300c<strong>\/test\/test.js<\/strong>\u300d\u5185\u304b\u3089<code>.env<\/code>\u306e\u5185\u5bb9\u3092\u53d6\u5f97\u3059\u308b\uff09\u5834\u5408\u306f\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b<code>path<\/code>\u3092\u6307\u5b9a\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<pre>require('dotenv').config(<strong>{ path: '..\/.env' }<\/strong>);<\/pre>\n<h1>\u304a\u308f\u308a\u306b<\/h1>\n<p>\u3068\u3044\u3046\u3053\u3068\u3067\u3001\u4eca\u56de\u306f<code>Node.js<\/code>\uff08<code>Express<\/code>\uff09\u3067\u300c\u30de\u30a4\u30b0\u30ec\u30fc\u30b7\u30e7\u30f3\u300d\u3001\u300cSeed\u300d\u3001\u300c\u30e2\u30c7\u30eb\u300d\u304c\u7c21\u5358\u306b\u4f7f\u3048\u308b\u3088\u3046\u306b\u306a\u308b\u300c<strong>Sequelize<\/strong>\u300d\u306e\u4f7f\u3044\u65b9\u3092\u3054\u7d39\u4ecb\u3057\u307e\u3057\u305f\u3002<\/p>\n<p><code>Laravel<\/code>\u3092\u30e1\u30a4\u30f3\u306b\u958b\u767a\u3092\u3057\u3066\u3044\u308b\u8eab\u3068\u3057\u3066\u306f\u3001\u3053\u306e\u30d1\u30c3\u30b1\u30fc\u30b8\u3082\u6700\u521d\u304b\u3089<code>Express<\/code>\u306b\u540c\u68b1\u3057\u3066\u304f\u308c\u305f\u3089\u3044\u3044\u306e\u306b\u3068\u601d\u3046\u3050\u3089\u3044\u4fbf\u5229\u306a\u3082\u306e\u3067\u3057\u305f\u3002<\/p>\n<p>\u305d\u3057\u3066\u3001\u3053\u308c\u306f\u30e2\u30c7\u30eb\u306e\u4f7f\u3044\u65b9\u3092\u8abf\u3079\u3066\u3044\u308b\u3068\u304d\u306b\u611f\u3058\u305f\u306e\u3067\u3059\u304c\u3001\u3084\u306f\u308a\u3044\u3044\u8a2d\u8a08\u601d\u60f3\u306f\u4ed6\u306e\u30d7\u30ed\u30b0\u30e9\u30e0\u306b\u3082\u5f71\u97ff\u3092\u3059\u308b\u306e\u3067\u3001\uff08\u3069\u3061\u3089\u304c\u5148\u304b\u306f\u77e5\u308a\u307e\u305b\u3093\u304c\uff09\u3084\u306f\u308a<code>Laravel<\/code>\u3068\u4f3c\u3066\u3044\u308b\u90e8\u5206\u304c\u3042\u3063\u305f\u308a\u3057\u307e\u3059\u3002\u4ee5\u524d\u4f7f\u3063\u305f\u3053\u3068\u304c\u3042\u308b<code>Python<\/code>\u306e\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u3067\u3082\u540c\u3058\u3088\u3046\u306b\u611f\u3058\u305f\u3053\u3068\u304c\u3042\u308b\u306e\u3067\u3001\u300c\u826f\u3044\u958b\u767a\u8a2d\u5b9a\u300d\u306f\u8a00\u8a9e\u304c\u9055\u3048\u3069\u540c\u3058\u65b9\u6cd5\u3092\u5411\u3044\u3066\u3044\u308b\u3088\u3046\u306a\u6c17\u304c\u3057\u307e\u3057\u305f\u3002<\/p>\n<p>\u3068\u3044\u3046\u3053\u3068\u306f\u3001\u3072\u3068\u3064\u306e\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u3092\u5b66\u7fd2\u3059\u308c\u3070\u4ed6\u306e\u8a00\u8a9e\u306e\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u3067\u3042\u3063\u3066\u3082\u7fd2\u5f97\u306f\u65e9\u304f\u306a\u3063\u3066\u3044\u308b\u3068\u3044\u3063\u3066\u3044\u3044\u3068\u601d\u3044\u307e\u3059\u3002\u6d41\u308c\u3068\u3057\u3066\u306f\u5b09\u3057\u3044\u3067\u3059\u306d\ud83d\ude0a\u2728<\/p>\n<p>\u305c\u3072\u3001\u307f\u306a\u3055\u3093\u3082\u300c<strong>Sequelize<\/strong>\u300d\u3092\u8a66\u3057\u3066\u307f\u3066\u304f\u3060\u3055\u3044\u306d\u3002<\/p>\n<p>\u3067\u306f\u3067\u306f\u301c\uff01<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-428 aligncenter\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2018\/04\/30.png\" alt=\"\" width=\"205\" height=\"350\" srcset=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2018\/04\/30.png 205w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2018\/04\/30-176x300.png 176w\" sizes=\"auto, (max-width: 205px) 100vw, 205px\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u3055\u3066\u3055\u3066\u3001\u524d\u3005\u56de\u306e\u300c\u5165\u9580\uff01Express\u306e\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3068\u57fa\u672c\u306e\u307e\u3068\u3081\u300d\u3067\u3082\u5c11\u3057\u66f8\u3044\u305f\u306e\u3067\u3059\u304c\u3001Node.js\u306e\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u300cExpress\u300d\u3092\u672c\u683c\u7684\u306b\u4f7f\u3063\u3066\u307f &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/blog.capilano-fw.com\/?p=5546\" class=\"more-link\"><span class=\"screen-reader-text\">&#8220;\u4fbf\u5229\uff01Node.js\u306bDB\u30de\u30a4\u30b0\u30ec\u30fc\u30b7\u30e7\u30f3\u3001Seed\u3001\u30e2\u30c7\u30eb\u3092\u7528\u610f\u3059\u308b\u300cSequelize\u300d&#8221; \u306e<\/span>\u7d9a\u304d\u3092\u8aad\u3080<\/a><\/p>\n","protected":false},"author":1,"featured_media":5578,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[201,203],"tags":[60],"class_list":["post-5546","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-express","category-node-js","tag-60"],"_links":{"self":[{"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/posts\/5546","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5546"}],"version-history":[{"count":30,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/posts\/5546\/revisions"}],"predecessor-version":[{"id":5726,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/posts\/5546\/revisions\/5726"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/media\/5578"}],"wp:attachment":[{"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5546"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5546"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5546"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}