{"id":5582,"date":"2020-01-11T00:03:25","date_gmt":"2020-01-10T15:03:25","guid":{"rendered":"https:\/\/blog.capilano-fw.com\/?p=5582"},"modified":"2020-02-27T01:18:24","modified_gmt":"2020-02-26T16:18:24","slug":"%e4%bf%9d%e5%ad%98%e7%89%88%ef%bc%81%e3%80%8csequelize%e3%80%8d%e3%83%a2%e3%83%87%e3%83%ab%e3%81%ae%e4%bd%bf%e3%81%84%e6%96%b9%e5%ae%9f%e4%be%8b%e3%83%bb%e5%85%a853%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/blog.capilano-fw.com\/?p=5582","title":{"rendered":"\u4fdd\u5b58\u7248\uff01\u300csequelize\u300d\u30e2\u30c7\u30eb\u306e\u4f7f\u3044\u65b9\u5b9f\u4f8b\u30fb\u516859\u4ef6"},"content":{"rendered":"<p>\u3055\u3066\u3055\u3066\u3001\u524d\u56de\u306e\u8a18\u4e8b\u300c<a href=\"https:\/\/blog.capilano-fw.com\/?p=5546\">Node.js\u306bDB\u30de\u30a4\u30b0\u30ec\u30fc\u30b7\u30e7\u30f3\u3001Seed\u3001\u30e2\u30c7\u30eb\u3092\u7528\u610f\u3059\u308b<\/a>\u300d\u3067\u306f\u3001\u300c<strong>Sequelize<\/strong>\u300d\u3068\u3044\u3046\u30d1\u30c3\u30b1\u30fc\u30b8\u3092\u4f7f\u3063\u3066\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u5468\u308a\u306e\u958b\u767a\u3092\u697d\u306b\u3057\u3066\u307f\u307e\u3057\u305f\u3002<\/p>\n<p>\u524d\u56de\u306f\u3001\u305d\u306e\u300c<strong>Sequelize<\/strong>\u300d\u306e\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3084\u30b3\u30de\u30f3\u30c9\u306e\u4f7f\u3044\u65b9\u3092\u30e1\u30a4\u30f3\u306b\u3057\u3066\u3054\u7d39\u4ecb\u3057\u305f\u306e\u3067\u3059\u304c\u3001\u5b9f\u306f\u3072\u3068\u3064\u60c5\u5831\u304c\u591a\u3059\u304e\u3066\u307e\u3068\u3081\u3066\u3044\u306a\u3044\u90e8\u5206\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<p>\u305d\u308c\u306f\u30fb\u30fb\u30fb<\/p>\n<p><strong style=\"font-size: 35px;\">\u30e2\u30c7\u30eb\u3092\u4f7f\u3063\u3066\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u64cd\u4f5c\uff08\u53d6\u5f97\uff0f\u8ffd\u52a0\uff0f\u66f4\u65b0\uff0f\u524a\u9664\uff09\u3059\u308b\u65b9\u6cd5<\/strong><\/p>\n<p>\u3067\u3059\u3002<\/p>\n<p>\u305d\u3053\u3067\uff01<\/p>\n<p>\u4eca\u56de\u306f\u300c<strong>Sequelize<\/strong>\u300d\u30e2\u30c7\u30eb\u306e\u4f7f\u3044\u65b9\u3092\u5b9f\u4f8b\u3067\u304a\u5c4a\u3051\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-3344 aligncenter\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2019\/04\/no_22.png\" alt=\"\" width=\"320\" height=\"320\" srcset=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2019\/04\/no_22.png 320w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2019\/04\/no_22-150x150.png 150w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2019\/04\/no_22-300x300.png 300w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2019\/04\/no_22-100x100.png 100w\" sizes=\"auto, (max-width: 320px) 100vw, 320px\" \/><\/p>\n<p><strong>\u958b\u767a\u74b0\u5883\uff1a<\/strong> Node.js 8.10\u3001Express 4.1\u3001Sequelize 5.2<\/p>\n<h1>\u524d\u63d0\u3068\u3057\u3066<\/h1>\n<p>\u4eca\u56de\u306e\u8a18\u4e8b\u306f\u300c<strong>sequelize<\/strong>\u300d\u304c\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3055\u308c\u3066\u3044\u3066\u3001\u3055\u3089\u306b\u3001\u3059\u3067\u306b\u30e2\u30c7\u30eb\u304c\u4f5c\u6210\u3055\u308c\u3066\u3044\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<p>\u3082\u3057\u307e\u3060\u6e96\u5099\u304c\u6574\u3063\u3066\u3044\u306a\u3044\u5834\u5408\u306f\u3001<a href=\"https:\/\/blog.capilano-fw.com\/?p=5546#i-12\">\u524d\u56de\u8a18\u4e8b\u306e\u300c\u30e2\u30c7\u30eb\u3092\u4f7f\u3046\u300d<\/a>\u3092\u53c2\u8003\u306b\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<h1>\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3059\u308b<\/h1>\n<h2>findByPk()\uff1a ID\u3092\u4f7f\u3063\u3066\u30c7\u30fc\u30bf\u53d6\u5f97<\/h2>\n<p>\u4f8b\u3048\u3070\u3001ID\u756a\u53f7\u304c<code>1<\/code>\u306e\u30e6\u30fc\u30b6\u30fc\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3057\u305f\u3044\u5834\u5408\u306f\u3001<code>findByPk()<\/code>\u3092\u4f7f\u3044\u307e\u3059\u3002<\/p>\n<pre>User.<strong>findByPk<\/strong>(1).then(user =&gt; {\r\n\r\n  \/\/\r\n\r\n});<\/pre>\n<h2>findOne()\uff1a \u6761\u4ef6\u3092\u307f\u305f\u3059\u6700\u521d\u306e\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3059\u308b<\/h2>\n<p>\u4f8b\u3048\u3070\u3001ID\u304c<code>3<\/code>, <code>4<\/code>, <code>5<\/code>\u306e\u3046\u3061\u3067\u4e00\u756a\u6700\u521d\u306e\u30c7\u30fc\u30bf\uff08\u3064\u307e\u308a\u3001<code>ID: 3<\/code>\uff09\u306e\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3059\u308b\u5834\u5408\u3067\u3059\u3002<\/p>\n<pre>User.<strong>findOne<\/strong>({ where: { id: [3, 4, 5] } }).then(user =&gt; {\r\n\r\n  \/\/\r\n\r\n});<\/pre>\n<h2>findAll()\uff1a \u6761\u4ef6\u3092\u307f\u305f\u3059\u5168\u3066\u306e\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3059\u308b<\/h2>\n<p>\u4f8b\u3048\u3070\u3001ID\u304c<code>3<\/code>, <code>4<\/code>, <code>5<\/code>\u306b\u8a72\u5f53\u3059\u308b\u30c7\u30fc\u30bf\u3092\u5168\u3066\u53d6\u5f97\u3059\u308b\u5834\u5408\u3067\u3059\u3002<\/p>\n<pre>User.<strong>findAll<\/strong>({ where: { id: [3, 4, 5] } }).then(users =&gt; {\r\n\r\n  \/\/\r\n\r\n});\r\n<\/pre>\n<h2>findOrCreate()\uff1a \u6761\u4ef6\u3092\u307f\u305f\u3059\u30c7\u30fc\u30bf\u304c\u306a\u3051\u308c\u3070\u65b0\u898f\u4f5c\u6210<\/h2>\n<p>\u4f8b\u3048\u3070\u3001ID\u304c<code>6<\/code>\u306e\u3082\u306e\u3092\u63a2\u3057\u3001\u3082\u3057\u306a\u3051\u308c\u3070\u30c7\u30fc\u30bf\u3092\u65b0\u898f\u767b\u9332\u3059\u308b\u5834\u5408\u3067\u3059\u3002<\/p>\n<pre>User.<strong>findOrCreate<\/strong>({\r\n  where: { id: 6 },\r\n  <strong>defaults: { \/\/ \u65b0\u898f\u767b\u9332\u3059\u308b\u30c7\u30fc\u30bf\r\n    name: '\u516d\u90ce',\r\n    email: 'rokuro@example.com',\r\n    password: 'rokuro-password',\r\n    createdAt: new Date(),\r\n    updatedAt: new Date()\r\n  }<\/strong>\r\n}).then((<strong>[user, created]<\/strong>) =&gt; {\r\n\r\n  if(created) { \/\/ \u30c7\u30fc\u30bf\u304c\u65b0\u898f\u4f5c\u6210\u3055\u308c\u305f\u5834\u5408\r\n\r\n    \/\/\r\n\r\n  }\r\n\r\n});<\/pre>\n<p>\u203b \u3061\u306a\u307f\u306b\u3001<code>then()<\/code>\u306e\u4e2d\u8eab\u304c<code>[user, created]<\/code>\u3068\u914d\u5217\u306b\u306a\u3063\u3066\u3044\u308b\u3053\u3068\u306b\u6ce8\u610f\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<h2 id=\"find_and_count_all\">findAndCountAll()\uff1a \u30da\u30fc\u30b8\u51e6\u7406\u306e\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3059\u308b<\/h2>\n<p>\u4f8b\u3048\u3070\u3001\uff11\u30da\u30fc\u30b8\u3054\u3068\u306b\uff11\uff10\u4ef6\u305a\u3064\u8868\u793a\u3057\u3066\u3044\u3066\u3001\uff12\u30da\u30fc\u30b8\u76ee\u306e\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3057\u305f\u3044\u5834\u5408\u3067\u3059\u3002<\/p>\n<pre>const page = 2;\r\nconst perPage = 10;\r\n\r\n<strong>User.findAndCountAll({\r\n  offset: (page - 1) * perPage,\r\n  limit: perPage\r\n})<\/strong>.then(result =&gt; {\r\n\r\n  const count = result.count; \/\/ \u5168\u30c7\u30fc\u30bf\u306e\u4ef6\u6570\r\n  const rows = result.rows;   \/\/ \u6761\u4ef6\u3092\u307f\u305f\u3059\u30c7\u30fc\u30bf\r\n\r\n});<\/pre>\n<p>\u203b\u3061\u306a\u307f\u306b<code>hasOne<\/code>\u3084<code>hasMany<\/code>\u306a\u3069\u306e\u7d50\u5408\u3092\u4f7f\u3046\u5834\u5408\u306f\u3001<code>distinct<\/code>\u3092<code>true<\/code>\u306b\u3057\u3066\u304f\u3060\u3055\u3044\u3002\u305d\u3046\u3057\u306a\u3044\u3068\u9593\u9055\u3063\u305f\u4ef6\u6570\u304c\u8fd4\u3063\u3066\u304f\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<pre>const page = 2;\r\nconst perPage = 10;\r\n\r\nUser.findAndCountAll({\r\n  offset: (page - 1) * perPage,\r\n  limit: perPage,\r\n  include: [{ model: Post }],\r\n  <strong>distinct:true<\/strong>\r\n}).then(result =&gt; {\r\n\r\n  \/\/ \u7701\u7565\r\n\r\n});<\/pre>\n<p>\u203b<code>MySQL<\/code>\u306a\u3069\u306e\u74b0\u5883\u306b\u3088\u3063\u3066\u3001<code>distinct<\/code>\u3092<code>true<\/code>\u306b\u3059\u308b\u3068\u30a8\u30e9\u30fc\u306b\u306a\u3063\u3066\u3057\u307e\u3046\u5834\u5408\u304c\u3042\u308a\u307e\u3059\u3002\u3053\u308c\u306f\u3001<code>DISTINCT(*)<\/code>\u3068\u3044\u3046\u8868\u73fe\u304c\u4f7f\u3048\u306a\u3044\u305f\u3081\u3067\u3001\u305d\u306e\u5834\u5408\u306f\u3001<code>include<\/code>\u306b\u7a7a\u767d\u306e\u914d\u5217\u3092\u30bb\u30c3\u30c8\u3057\u3066\u307f\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<pre>findAndCountAll({\r\n  distinct: true,\r\n  <strong>include: []<\/strong>\r\n})<\/pre>\n<p>\u307e\u305f\u3001<code>findAndCountAll()<\/code>\u306f\u5c11\u3057\u4e0d\u4fbf\u306b\u3082\u30da\u30fc\u30b8\u51e6\u7406\u3067\u91cd\u8981\u306a\u300c<strong>\u6700\u5f8c\u306e\u30da\u30fc\u30b8\u756a\u53f7<\/strong>\u300d\u3084\u300c<strong>\u73fe\u5728\u304c\u6700\u5f8c\u306e\u30da\u30fc\u30b8\u304b\u3069\u3046\u304b<\/strong>\u300d\u306a\u3069\u306e\u60c5\u5831\u3092\u63d0\u4f9b\u3057\u3066\u304f\u308c\u307e\u305b\u3093\u3002\u305d\u306e\u305f\u3081\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u3057\u3066\u8a08\u7b97\u3059\u308b\u3068\u3044\u3044\u3067\u3057\u3087\u3046\u3002<\/p>\n<pre>\/\/ \u7701\u7565\r\n\r\n.then(data =&gt; {\r\n\r\n  <strong>data.lastPage = (data.count &gt; 0) ? Math.ceil(data.count \/ perPage) : 1;\r\n  data.isLast = (page === data.lastPage);<\/strong>\r\n  res.send(data);\r\n\r\n});<\/pre>\n<h2>count()\uff1a \u8a72\u5f53\u3059\u308b\u30c7\u30fc\u30bf\u306e\u4ef6\u6570\u3092\u53d6\u5f97\u3059\u308b<\/h2>\n<p>\u6761\u4ef6\u306b\u5408\u3046\u30c7\u30fc\u30bf\u306e\u300c\u4ef6\u6570\u300d\u3092\u53d6\u5f97\u3059\u308b\u5834\u5408\u306f\u3001<code>count()<\/code>\u3092\u4f7f\u3044\u307e\u3059\u3002<\/p>\n<pre>User.<strong>count({ where: { id: [2, 3, 4] } })<\/strong>.then(dataCount =&gt; {\r\n\r\n  \/\/\r\n\r\n});\r\n<\/pre>\n<h2>max()\uff1a \u8a72\u5f53\u3059\u308b\u6700\u5927\u5024\u3092\u53d6\u5f97\u3059\u308b<\/h2>\n<p>\u6761\u4ef6\u306b\u5408\u3046\u30c7\u30fc\u30bf\u306e\u6700\u5927\u5024\u3092\u53d6\u5f97\u3059\u308b\u5834\u5408\u306f<code>max()<\/code>\u3092\u4f7f\u3044\u307e\u3059\u3002<\/p>\n<pre>User.<strong>max('id', { where: { id: [2, 3, 4] } })<\/strong>.then(maxId =&gt; {\r\n\r\n  \/\/\r\n\r\n});\r\n<\/pre>\n<h2>min()\uff1a \u8a72\u5f53\u3059\u308b\u6700\u5c0f\u5024\u3092\u53d6\u5f97\u3059\u308b<\/h2>\n<p>\u6761\u4ef6\u306b\u5408\u3046\u30c7\u30fc\u30bf\u306e\u6700\u5c0f\u5024\u3092\u53d6\u5f97\u3059\u308b\u5834\u5408\u306f<code>min()<\/code>\u3092\u4f7f\u3044\u307e\u3059\u3002<\/p>\n<pre>User.<strong>min('id', { where: { id: [2, 3, 4] } })<\/strong>.then(minId =&gt; {\r\n\r\n  \/\/\r\n\r\n});\r\n<\/pre>\n<h2>sum()\uff1a \u8a72\u5f53\u3059\u308b\u30c7\u30fc\u30bf\u306e\u5408\u8a08\u3092\u53d6\u5f97\u3059\u308b<\/h2>\n<p>\u4f8b\u3048\u3070\u3001\u30c6\u30b9\u30c8\u3067\u53d6\u5f97\u3057\u305f\u70b9\u6570\u306e\u5408\u8a08\u3092\u53d6\u5f97\u3059\u308b\u5834\u5408\u3067\u3059\u3002<\/p>\n<pre>User.<strong>sum('score', { where: { id: [2, 3, 4] } })<\/strong>.then(scoreTotal =&gt; {\r\n\r\n  \/\/\r\n\r\n});\r\n<\/pre>\n<h1>\u30c7\u30fc\u30bf\u53d6\u5f97\u306e\u8a2d\u5b9a\u3092\u3059\u308b<\/h1>\n<h2>where\uff1a\u00a0\u6761\u4ef6\u3092\u6307\u5b9a\u3059\u308b<\/h2>\n<p><code>sequlize<\/code>\u306e\u6761\u4ef6\u6307\u5b9a\u306f\u3001\u5c02\u7528\u306e\u6bd4\u8f03\u6f14\u7b97\u5b50\u3092\u4f7f\u3048\u308b\u3088\u3046\u306b\u3057\u3066\u304a\u304f\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u307e\u305a\u306f\u4ee5\u4e0b\u306e\u30b3\u30fc\u30c9\u3092\u8ffd\u52a0\u3057\u3066\u304a\u3044\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<pre><strong>const Sequelize = require('sequelize');\r\nconst Op = Sequelize.Op;<\/strong><\/pre>\n<p>\u3067\u306f\u3001\u3072\u3068\u3064\u305a\u3064\u898b\u3066\u3044\u304d\u307e\u3057\u3087\u3046\uff01<\/p>\n<p>\u203b \u306a\u304a\u3001SQL\u6587\u3082\u4f8b\u3068\u3057\u3066\u8f09\u305b\u3066\u3044\u307e\u3059\u304c\u3001\u305d\u308c\u3089\u306f\u5206\u304b\u308a\u3084\u3059\u304f\u8aac\u660e\u3059\u308b\u305f\u3081\u306b\u30c6\u30fc\u30d6\u30eb\u540d\u306a\u3069\u3092\u7701\u3044\u305f\u7c21\u7565\u30d0\u30fc\u30b8\u30e7\u30f3\u3068\u306a\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n<h3>\u57fa\u672c\u7684\u306a\u4f7f\u3044\u65b9\uff08\u7279\u5b9a\u306e\u5024\u3092\u3082\u3064\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\uff09<\/h3>\n<p>\u4f8b\u3048\u3070<code>ID:3<\/code>\u306e\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3059\u308b\u5834\u5408\u306f\u3053\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre>User.findOne({\r\n  <strong>where: {\r\n    id: 1\r\n  }<\/strong>\r\n}).then(user =&gt; {\r\n\r\n  \/\/\r\n\r\n});<\/pre>\n<p>\u3082\u3061\u308d\u3093\u6570\u5024\u3060\u3051\u3067\u306a\u304f\u3001\u6587\u5b57\u5217\u3082\u4f7f\u3048\u307e\u3059\u3002<\/p>\n<p><code>WHERE<\/code>\u53e5\u306f\u3053\u3046\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre>WHERE <strong>id = 3<\/strong><\/pre>\n<p>\u307e\u305f\u3001\u540c\u3058\u3088\u3046\u306b\u3057\u3066<code>NULL<\/code>\u3082\u554f\u984c\u306a\u304f\u4f7f\u3048\u307e\u3059\u304c\u3001\u305d\u306e\u5834\u5408<code>WHERE<\/code>\u53e5\u306f<code>IS<\/code>\u3092\u4f7f\u3046\u5f62\u306b\u306a\u308b\u5834\u5408\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<pre>User.findAll({\r\n  where: {\r\n    <strong>deletedAt: null<\/strong>\r\n  }\r\n}).then(users =&gt; {})\r\n<\/pre>\n<pre>WHERE<strong> deletedAt IS NULL<\/strong><\/pre>\n<h3>AND\u6761\u4ef6<\/h3>\n<p>\u4f8b\u3048\u3070\u3001\u30e1\u30fc\u30eb\u30a2\u30c9\u30ec\u30b9\u304c\u300c<strong>taro@example.com<\/strong>\u300d\u3067\u3001\u3057\u304b\u3082\u540d\u524d\u304c\u300c<strong>\u592a\u90ce<\/strong>\u300d\u306e\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3059\u308b\u5834\u5408\u3067\u3059\u3002<\/p>\n<pre>User.findOne({\r\n  where: {\r\n    <strong>[Op.and]: {\r\n      email: 'taro@example.com',\r\n      name: '\u592a\u90ce'\r\n    }<\/strong>\r\n  }\r\n}).then(user =&gt; {\r\n\r\n  \/\/\r\n\r\n});<\/pre>\n<p><code>WHERE<\/code>\u53e5\u306f\u3053\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre>WHERE email = 'taro@example.com' <strong>AND<\/strong> name = '\u592a\u90ce'<\/pre>\n<h3>OR\u6761\u4ef6<\/h3>\n<p>\u4f8b\u3048\u3070\u3001\u30e1\u30fc\u30eb\u30a2\u30c9\u30ec\u30b9\u304c\u300c<strong>taro@example.com<\/strong>\u300d\u304b\u3001<strong>\u3082\u3057\u304f\u306f<\/strong>\u540d\u524d\u304c\u300c<strong>\u6b21\u90ce<\/strong>\u300d\u306e\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3059\u308b\u5834\u5408\u3067\u3059\u3002<\/p>\n<pre>User.findAll({\r\n  where: {\r\n    <strong>[Op.or]: {\r\n      email: 'taro@example.com',\r\n      name: '\u6b21\u90ce'\r\n    }<\/strong>\r\n  }\r\n}).then(users =&gt; {\r\n\r\n  \/\/\r\n\r\n});<\/pre>\n<p><code>WHERE<\/code>\u53e5\u306f\u3053\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre>WHERE email = 'taro@example.com' <strong>OR<\/strong> name = '\u592a\u90ce'<\/pre>\n<h3>\u7279\u5b9a\u306e\u6570\u5024\u300c\u8d85\u300d\u306e\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3059\u308b<\/h3>\n<p>\u4f8b\u3048\u3070\u3001\u300c<strong>id &gt; 3<\/strong>\u300d\u306e\u6761\u4ef6\u306b\u5408\u3046\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3059\u308b\u5834\u5408\u3067\u3059\u3002\uff08\u3064\u307e\u308a\u3001<code>id:3<\/code>\u306f<strong>\u542b\u307e\u306a\u3044<\/strong>\uff09<\/p>\n<pre>User.findAll({\r\n  where: {\r\n    <strong>id: {\r\n      [Op.gt]: 3\r\n    }<\/strong>\r\n  }\r\n}).then(users =&gt; {\r\n\r\n  \/\/\r\n\r\n});<\/pre>\n<p>\u203b\u300c<strong>gt<\/strong>\u300d\u306f\u3001<code>greater than<\/code> \u3068\u8a00\u3046\u610f\u5473\u3067\u3059\u3002<\/p>\n<h3>\u7279\u5b9a\u306e\u6570\u5024\u300c\u4ee5\u4e0a\u300d\u306e\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3059\u308b<\/h3>\n<p>\u4f8b\u3048\u3070\u3001\u300c<strong>id &gt;= 3<\/strong>\u300d\u306e\u6761\u4ef6\u306b\u5408\u3046\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3059\u308b\u5834\u5408\u3067\u3059\u3002\uff08\u3064\u307e\u308a\u3001<code>id:3<\/code>\u3092<strong>\u542b\u3080<\/strong>\uff09<\/p>\n<pre>User.findAll({\r\n  where: {\r\n    <strong>id: {\r\n      [Op.gte]: 3\r\n    }<\/strong>\r\n  }\r\n}).then(users =&gt; {\r\n\r\n  \/\/\r\n\r\n});\r\n<\/pre>\n<p>\u203b\u300c<strong>gte<\/strong>\u300d\u306f<code>greater than \/ equal<\/code> \u3068\u3044\u3046\u610f\u5473\u3067\u3059\u3002<\/p>\n<h3>\u7279\u5b9a\u306e\u6570\u5024\u300c\u672a\u6e80\u300d\u306e\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3059\u308b<\/h3>\n<p>\u4f8b\u3048\u3070\u3001\u300c<strong>id &lt; 3<\/strong>\u300d\u306e\u6761\u4ef6\u306b\u5408\u3046\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3059\u308b\u5834\u5408\u3067\u3059\u3002\uff08\u3064\u307e\u308a\u3001<code>id:3<\/code>\u306f<strong>\u542b\u307e\u306a\u3044<\/strong>\uff09<\/p>\n<pre>User.findAll({\r\n  where: {\r\n    <strong>id: {\r\n      [Op.lt]: 3\r\n    }<\/strong>\r\n  }\r\n}).then(users =&gt; {\r\n\r\n  \/\/\r\n\r\n});<\/pre>\n<p>\u203b\u300c<strong>lt<\/strong>\u300d\u306f\u3001<code>less than<\/code> \u3068\u8a00\u3046\u610f\u5473\u3067\u3059\u3002<\/p>\n<h3>\u7279\u5b9a\u306e\u6570\u5024\u300c\u4ee5\u4e0b\u300d\u306e\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3059\u308b<\/h3>\n<p>\u4f8b\u3048\u3070\u3001\u300c<strong>id &lt;= 3<\/strong>\u300d\u306e\u6761\u4ef6\u306b\u5408\u3046\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3059\u308b\u5834\u5408\u3067\u3059\u3002\uff08\u3064\u307e\u308a\u3001<code>id:3<\/code>\u3092<strong>\u542b\u3080<\/strong>\uff09<\/p>\n<pre>User.findAll({\r\n  where: {\r\n    <strong>id: {\r\n      [Op.lte]: 3\r\n    }<\/strong>\r\n  }\r\n}).then(users =&gt; {\r\n\r\n  \/\/\r\n\r\n});\r\n<\/pre>\n<p>\u203b\u300c<strong>lte<\/strong>\u300d\u306f\u3001<code>less than \/ equal<\/code> \u3068\u3044\u3046\u610f\u5473\u3067\u3059\u3002<\/p>\n<h3>\u7279\u5b9a\u306e\u5024\u300c\u4ee5\u5916\u300d\u306e\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3059\u308b<\/h3>\n<p>\u4f8b\u3048\u3070\u3001ID\u304c <strong>3 \u4ee5\u5916<\/strong>\u306e\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3059\u308b\u5834\u5408\u3067\u3059\u3002<\/p>\n<pre>User.findAll({\r\n  where: {\r\n    <strong>id: {\r\n      [Op.ne]: 3\r\n    }<\/strong>\r\n  }\r\n}).then(users =&gt; {\r\n\r\n  \/\/\r\n\r\n});<\/pre>\n<p>\u203b\u300c<strong>ne<\/strong>\u300d\u306f<code>not equal<\/code>\u3068\u3044\u3046\u610f\u5473\u3067\u3059\u3002<\/p>\n<p><code>WHERE<\/code>\u53e5\u306f\u3053\u3046\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre>WHERE <strong>id != 3<\/strong><\/pre>\n<h3>\u7279\u5b9a\u306e\u7bc4\u56f2\u5185\u306e\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3059\u308b<\/h3>\n<p>\u4f8b\u3048\u3070\u3001ID\u304c<code>3<\/code>\u301c<code>5<\/code>\u306e\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3059\u308b\u5834\u5408\u3067\u3059\u3002\uff08<code>ID:3<\/code> \u3068 <code>ID:5<\/code>\u3092\u542b\u3080\u30c7\u30fc\u30bf\u304c\u53d6\u5f97\u3055\u308c\u307e\u3059\uff09<\/p>\n<pre>User.findAll({\r\n  where: {\r\n    <strong>id: {\r\n      [Op.between]: [3, 5]\r\n    }<\/strong>\r\n  }\r\n}).then(users =&gt; {\r\n\r\n  \/\/\r\n\r\n});<\/pre>\n<h3>\u7279\u5b9a\u306e\u7bc4\u56f2\u300c\u4ee5\u5916\u300d\u306e\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3059\u308b<\/h3>\n<p>\u4f8b\u3048\u3070\u3001ID\u304c<code>3<\/code>\u301c<code>5<\/code>\u300c\u4ee5\u5916\u300d\u306e\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3059\u308b\u5834\u5408\u3067\u3059\u3002\uff08<code>ID:3 \u301c 5<\/code>\u306f\u3001<strong>\u542b\u307e\u306a\u3044<\/strong>\uff09<\/p>\n<pre>User.findAll({\r\n  where: {\r\n    <strong>id: {\r\n      [Op.notBetween]: [3, 5]\r\n    }<\/strong>\r\n  }\r\n}).then(users =&gt; {\r\n\r\n  \/\/\r\n\r\n});<\/pre>\n<h3>\u6307\u5b9a\u3057\u305f\u8907\u6570\u306e\u5024\u3092\u3082\u3064\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3059\u308b<\/h3>\n<p>\u4f8b\u3048\u3070\u3001ID\u304c<code>3<\/code>\u3068<code>5<\/code>\u306e\u3082\u306e<strong>\u3060\u3051<\/strong>\u3092\u53d6\u5f97\u3059\u308b\u5834\u5408\u3067\u3059\u3002<\/p>\n<pre>User.findAll({\r\n  where: {\r\n    <strong>id: {\r\n      [Op.in]: [3, 5]\r\n    }<\/strong>\r\n  }\r\n}).then(users =&gt; {\r\n\r\n  \/\/\r\n\r\n});\r\n<\/pre>\n<p><code>WHERE<\/code>\u53e5\u306f\u3053\u3046\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre>WHERE id <strong>IN (3, 5)<\/strong><\/pre>\n<h3>\u6307\u5b9a\u3057\u305f\u8907\u6570\u306e\u5024\u3092\u300c\u3082\u305f\u306a\u3044\u300d\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3059\u308b<\/h3>\n<p>\u4f8b\u3048\u3070\u3001ID\u304c<code>3<\/code>\u3068<code>5<\/code> <strong>\u4ee5\u5916\u306e\u30c7\u30fc\u30bf\u3060\u3051<\/strong>\u3092\u53d6\u5f97\u3059\u308b\u5834\u5408\u3067\u3059\u3002<\/p>\n<pre>User.findAll({\r\n  where: {\r\n    <strong>id: {\r\n      [Op.notIn]: [3, 5]\r\n    }<\/strong>\r\n  }\r\n}).then(users =&gt; {\r\n\r\n  \/\/\r\n\r\n});\r\n<\/pre>\n<p><code>WHERE<\/code>\u53e5\u306f\u3053\u3046\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre>WHERE id <strong>NOT IN (3, 5)<\/strong><\/pre>\n<h3>\u3042\u3044\u307e\u3044\u691c\u7d22\u3059\u308b<\/h3>\n<p>\u4f8b\u3048\u3070\u3001\u30e1\u30fc\u30eb\u30a2\u30c9\u30ec\u30b9\u306b\u300c<strong>@example<\/strong>\u300d\u304c\u542b\u307e\u308c\u3066\u3044\u308b\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3059\u308b\u5834\u5408\u3067\u3059\u3002<\/p>\n<pre>User.findAll({\r\n  where: {\r\n    <strong>email: {\r\n      [Op.like]: '%@example%'\r\n    }<\/strong>\r\n  }\r\n}).then(users =&gt; {\r\n\r\n  \/\/\r\n\r\n});<\/pre>\n<p>\u307e\u305f\u3001\u30e1\u30fc\u30eb\u30a2\u30c9\u30ec\u30b9\u304c\u300c<strong>taro@<\/strong>\u300d\u304b\u3089\u59cb\u307e\u308b\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3059\u308b\u5834\u5408\u306f\u3053\u3061\u3089\u3002<\/p>\n<pre>User.findAll({\r\n  where: {\r\n    <strong>email: {\r\n      [Op.like]: 'taro@%'\r\n    }<\/strong>\r\n  }\r\n}).then(users =&gt; {\r\n\r\n  \/\/\r\n\r\n});<\/pre>\n<p>\u305d\u3057\u3066\u3001\u30e1\u30fc\u30eb\u30a2\u30c9\u30ec\u30b9\u304c\u300c<strong>@example.com<\/strong>\u300d\u3067\u7d42\u308f\u308b\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3059\u308b\u5834\u5408\u3067\u3059\u3002<\/p>\n<pre>User.findAll({\r\n  where: {\r\n    <strong>email: {\r\n      [Op.like]: '%@example.com'\r\n    }<\/strong>\r\n  }\r\n}).then(users =&gt; {\r\n\r\n  \/\/\r\n\r\n});\r\n<\/pre>\n<p><code>WHERE<\/code>\u53e5\u306f\u3001\u305d\u308c\u305e\u308c\u3053\u3046\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre>WHERE email <strong>LIKE '%@example%'\r\n<\/strong>\r\nWHERE email <strong>LIKE 'taro@%'\r\n<\/strong>\r\nWHERE email <strong>LIKE '%@example.com'\r\n<\/strong><\/pre>\n<h3>\u3042\u3044\u307e\u3044\u691c\u7d22\u300c\u4ee5\u5916\u306e\u30c7\u30fc\u30bf\u300d\u3092\u53d6\u5f97\u3059\u308b<\/h3>\n<p>\u4f8b\u3048\u3070\u3001\u30e1\u30fc\u30eb\u30a2\u30c9\u30ec\u30b9\u306b\u300c<strong>@example<\/strong>\u300d\u304c<strong>\u542b\u307e\u308c\u3066\u3044\u306a\u3044<\/strong>\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3059\u308b\u5834\u5408\u3067\u3059\u3002<\/p>\n<pre>User.findAll({\r\n  where: {\r\n    <strong>email: {\r\n      [Op.notLike]: '%@example%'\r\n    }<\/strong>\r\n  }\r\n}).then(users =&gt; {\r\n\r\n  \/\/\r\n\r\n});<\/pre>\n<p><code>WHERE<\/code>\u53e5\u306f\u3053\u3046\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre>WHERE email <strong>NOT LIKE '%@example%'<\/strong><\/pre>\n<h3>\u6307\u5b9a\u306e\u5024\u4ee5\u5916\u306e\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3059\u308b<\/h3>\n<p>\u4f8b\u3048\u3070\u3001\u5e74\u9f62\u304c<code>null<\/code>\u4ee5\u5916\u306e\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3059\u308b\u5834\u5408\u3067\u3059\u3002<\/p>\n<pre>User.findAll({\r\n  where: {\r\n    <strong>age: {\r\n      [Op.not]: null\r\n    }<\/strong>\r\n  }\r\n}).then(users =&gt; {\r\n\r\n  \/\/\r\n\r\n});\r\n<\/pre>\n<p><code>WHERE<\/code>\u53e5\u306f\u3053\u3046\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre>WHERE age <strong>IS NOT NULL<\/strong><\/pre>\n<h2>order\uff1a \u4e26\u3073\u9806\u3092\u6307\u5b9a\u3059\u308b<\/h2>\n<h3>\u5c0f \u2192 \u5927\u306e\u9806\u306b\u4e26\u3079\u308b<\/h3>\n<p>\u4f8b\u3048\u3070\u3001\u5e74\u9f62\u306e\u5c0f\u3055\u3044\u65b9\u304b\u3089\u4e26\u3079\u66ff\u3048\u308b\u5834\u5408\u3067\u3059\u3002<\/p>\n<pre>User.findAll({\r\n  <strong>order: [\r\n    ['age', 'ASC']\r\n  ]<\/strong>\r\n}).then(users =&gt; {\r\n\r\n  \/\/\r\n\r\n});\r\n<\/pre>\n<h3>\u5927 \u2192 \u5c0f\u306e\u9806\u306b\u4e26\u3079\u308b<\/h3>\n<p>\u4f8b\u3048\u3070\u3001\u5e74\u9f62\u306e\u5927\u304d\u3044\u65b9\u304b\u3089\u4e26\u3079\u66ff\u3048\u308b\u5834\u5408\u3067\u3059\u3002<\/p>\n<pre>User.findAll({\r\n  <strong>order: [\r\n    ['age', 'DESC']\r\n  ]<\/strong>\r\n}).then(users =&gt; {\r\n\r\n  \/\/\r\n\r\n});<\/pre>\n<h3>\u30e9\u30f3\u30c0\u30e0\u306a\u9806\u306b\u4e26\u3079\u308b<\/h3>\n<p>\u30e9\u30f3\u30c0\u30e0\u306a\u9806\u756a\u306b\u30c7\u30fc\u30bf\u3092\u4e26\u3079\u66ff\u3048\u308b\u5834\u5408\u306f\u3001\u6b21\u306e\u3088\u3046\u306b\u3057\u307e\u3059\u3002<\/p>\n<pre>User.findAll(<strong>{\r\n  order: sequelize.random()\r\n}<\/strong>).then(users =&gt; {\r\n\r\n  \/\/\r\n\r\n});<\/pre>\n<p>\u203b <code>sequelize<\/code>\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u306b\u3064\u3044\u3066\u306f\u3001<a href=\"#use_instance\">Sequelize\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u53d6\u5f97\u3059\u308b<\/a>\u3092\u3054\u89a7\u304f\u3060\u3055\u3044\u3002<\/p>\n<h3>\u7d50\u5408\u3057\u305f\u5148\u3067\u4e26\u3079\u66ff\u3048<\/h3>\n<p>\u4f8b\u3048\u3070\u3001\u300c\u672c\u300d\u3068\u300c\u672c\u306e\u30ab\u30c6\u30b4\u30ea\u300d\u304c\u300c<strong>hasOne\uff08\uff11\uff1a\uff11\uff09<\/strong>\u300d\u3067\u7d50\u5408\u3057\u3066\u3044\u3066\u3001\u300c\u30ab\u30c6\u30b4\u30ea\u540d\u300d\u3067\u4e26\u3079\u66ff\u3048\u3092\u884c\u3046\u5834\u5408\u3067\u3059\u3002<\/p>\n<pre>const Book = require('.\/models').Book;\r\nconst BookCategory = require('.\/models').BookCategory;\r\n\r\nBook.findAll({\r\n  include: [\r\n    { model: BookCategory }\r\n  ],\r\n  <strong>order: [\r\n    [BookCategory, 'name', 'ASC']\r\n  ]<\/strong>\r\n}).then(books =&gt; {\r\n\r\n  \/\/\r\n\r\n});<\/pre>\n<h3>\u6307\u5b9a\u3057\u305f\u5024\u306e\u9806\u3067\u4e26\u3079\u66ff\u3048<\/h3>\n<p>\u4f8b\u3048\u3070\u3001\u81ea\u5206\u3067\u6307\u5b9a\u3057\u305fID\u756a\u53f7\u306e\u9806\u306b\u4e26\u3079\u66ff\u3048\u3092\u3057\u305f\u3044\u5834\u5408\u3067\u3059\u3002<span style=\"font-size: 1rem;\">\u4eca\u56de\u306f\u3001<\/span><code>id<\/code><span style=\"font-size: 1rem;\">\u304c <code>5<\/code>, <code>1<\/code>, <code>3<\/code> \u306e\u9806\u3067\u4e26\u3079\u66ff\u3048\u308b\u3088\u3046\u306b\u3057\u3066\u307f\u307e\u3059\u3002<\/span><\/p>\n<pre>User.findAll({\r\n  <strong>order: [[sequelize.fn('FIELD', sequelize.col('id'), 5, 1, 3), 'ASC']]<\/strong>\r\n}).then(users =&gt; {\r\n\r\n  \/\/\r\n\r\n});<\/pre>\n<p>\u306a\u304a\u3001<code>ASC<\/code>\u3092<code>DESC<\/code>\u306b\u5909\u66f4\u3059\u308b\u3068\u9006\u9806\u306b\u5909\u66f4\u3067\u304d\u307e\u3059\u3002<\/p>\n<p>\u3055\u3089\u306b\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u300c\u30b9\u30d7\u30ec\u30c3\u30c9\u69cb\u6587\u300d\u3092\u4f7f\u3046\u3068\u3001\u5909\u6570\u306b<code>id<\/code>\u60c5\u5831\u304c\u5165\u3063\u3066\u3044\u308b\u5834\u5408\u3067\u3082\u697d\u306b\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre>const ids = [5, 1, 3];\r\n\r\nUser.findAll({\r\n  order: [[sequelize.fn('FIELD', sequelize.col('id'), <strong>...ids<\/strong>), 'ASC']]\r\n}).then(users =&gt; {\r\n\r\n  \/\/\r\n\r\n});<\/pre>\n<p>\u203b <code>sequelize<\/code>\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u306b\u3064\u3044\u3066\u306f\u3001<a href=\"#use_instance\">Sequelize\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u53d6\u5f97\u3059\u308b<\/a>\u3092\u3054\u89a7\u304f\u3060\u3055\u3044\u3002<\/p>\n<p>\u203b\u307e\u305f\u3001\u5229\u7528\u3059\u308bSQL\u306b\u3088\u3063\u3066\u306f\u4f7f\u3048\u306a\u3044\u5834\u5408\u304c\u3042\u308a\u307e\u3059\u3002\uff08MySQL\u306fOK\u3067\u3057\u305f\uff09<\/p>\n<h3>\u81ea\u5206\u3067\u6307\u5b9a\u3059\u308b<\/h3>\n<p>\u300c<strong>Sequelize<\/strong>\u300d\u306b\u983c\u3089\u305a\u3001\u81ea\u5206\u81ea\u4fe1\u3067<code>ORDER<\/code>\u53e5\u3092\u6307\u5b9a\u3059\u308b\u5834\u5408\u306f\u3001\u3053\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre>User.findAll({\r\n  order: <strong>sequelize.literal('age ASC')<\/strong>\r\n}).then(users =&gt; {\r\n\r\n  \/\/\r\n\r\n});\r\n<\/pre>\n<p>\u203b <code>sequelize<\/code>\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u306b\u3064\u3044\u3066\u306f\u3001<a href=\"#use_instance\">Sequelize\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u53d6\u5f97\u3059\u308b<\/a>\u3092\u3054\u89a7\u304f\u3060\u3055\u3044\u3002<\/p>\n<h2 id=\"use_group\">group\uff1a \u30b0\u30eb\u30fc\u30d7\u5316\u3059\u308b<\/h2>\n<p>\u4f8b\u3048\u3070\u3001\u300c\u672c\u300d\u3092\u300c\u30ab\u30c6\u30b4\u30eaID\u300d\u3067\u30b0\u30eb\u30fc\u30d7\u5316\u3057\u3001\u30ab\u30c6\u30b4\u30ea\u3054\u3068\u306e\u4ef6\u6570\u3092\u53d6\u5f97\u3059\u308b\u5834\u5408\u3067\u3059\u3002<\/p>\n<pre>Book.findAll({\r\n  attributes: [\r\n    'category_id',\r\n    [sequelize.fn('COUNT', sequelize.col('category_id')), 'category_id_count']\r\n  ],\r\n  <strong>group: 'category_id'<\/strong>\r\n}).then(books =&gt; {\r\n\r\n  \/\/\r\n\r\n});<\/pre>\n<p>\u203b\u8907\u6570\u30d5\u30a3\u30fc\u30eb\u30c9\u3067\u30b0\u30eb\u30fc\u30d7\u5316\u3059\u308b\u5834\u5408\u306f\u3001\u914d\u5217\u3067\u6307\u5b9a\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<p><span style=\"font-size: 1rem;\">\u203b <\/span><code>sequelize<\/code><span style=\"font-size: 1rem;\">\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u306b\u3064\u3044\u3066\u306f\u3001<\/span><a style=\"font-size: 1rem;\" href=\"#use_instance\">Sequelize\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u53d6\u5f97\u3059\u308b<\/a><span style=\"font-size: 1rem;\">\u3092\u3054\u89a7\u304f\u3060\u3055\u3044\u3002<\/span><\/p>\n<h2>attributes\uff1a \u53d6\u5f97\u3059\u308b\u30d5\u30a3\u30fc\u30eb\u30c9\u3092\u6307\u5b9a\u3059\u308b<\/h2>\n<h3>\u57fa\u672c\u7684\u306a\u6307\u5b9a\u65b9\u6cd5<\/h3>\n<p>\u4f8b\u3048\u3070\u3001<code>Users<\/code>\u30c6\u30fc\u30d6\u30eb\u304b\u3089\u300c<strong>name<\/strong>\u300d\u3068\u300c<strong>email<\/strong>\u300d\u3060\u3051\u3092\u53d6\u5f97\u3059\u308b\u5834\u5408\u3067\u3059\u3002<\/p>\n<pre>User.findAll({\r\n  <strong>attributes: ['name', 'email']<\/strong>\r\n}).then(users =&gt; {\r\n\r\n  \/\/\r\n\r\n});<\/pre>\n<h3>\u7279\u5b9a\u306e\u30d5\u30a3\u30fc\u30eb\u30c9\u3092\u9664\u5916\u3059\u308b<\/h3>\n<p>\u4f8b\u3048\u3070\u3001\u300c<strong>createdAt<\/strong>\u300d\u3068\u300c<strong>updatedAt<\/strong>\u300d\u3060\u3051\u9664\u5916\u3057\u3066\u30c7\u30fc\u30bf\u53d6\u5f97\u3059\u308b\u5834\u5408\u3067\u3059\u3002<\/p>\n<pre>User.findAll({\r\n  <strong>attributes: {\r\n    exclude: ['createdAt', 'updatedAt']\r\n  }<\/strong>\r\n}).then(users =&gt; {\r\n\r\n  \/\/\r\n\r\n});<\/pre>\n<h3>COUNT()\u3067\u30ab\u30a6\u30f3\u30c8\u3059\u308b<\/h3>\n<p>\u3044\u308f\u3086\u308b<code>COUNT()<\/code>\u3092\u4f7f\u3044\u305f\u3044\u5834\u5408\u3067\u3059\u3002<br \/>\n\u8a73\u3057\u304f\u306f\u3001<a href=\"#use_group\">\u30b0\u30eb\u30fc\u30d7\u5316\u3059\u308b<\/a>\u3092\u3054\u89a7\u304f\u3060\u3055\u3044\u3002<\/p>\n<h2>limit\uff1a \u53d6\u5f97\u3059\u308b\u4ef6\u6570\u3092\u6307\u5b9a\u3059\u308b<\/h2>\n<p>\u53d6\u5f97\u3059\u308b\u30c7\u30fc\u30bf\u4ef6\u6570\u3092\u6307\u5b9a\u3059\u308b\u5834\u5408\u3067\u3059\u3002<br \/>\n\u8a73\u3057\u304f\u306f\u3001<a href=\"#find_and_count_all\">\u30da\u30fc\u30b8\u51e6\u7406\u306e\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3059\u308b<\/a>\u3092\u3054\u89a7\u304f\u3060\u3055\u3044\u3002<\/p>\n<h2>offset\uff1a \u53d6\u5f97\u3059\u308b\u958b\u59cb\u4f4d\u7f6e\u3092\u6307\u5b9a\u3059\u308b<\/h2>\n<p>\u53d6\u5f97\u3059\u308b\u30c7\u30fc\u30bf\u306e\u958b\u59cb\u4f4d\u7f6e\u3092\u6307\u5b9a\u3059\u308b\u5834\u5408\u3067\u3059\u3002<br \/>\n\u8a73\u3057\u304f\u306f\u3001<a href=\"#find_and_count_all\">\u30da\u30fc\u30b8\u51e6\u7406\u306e\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3059\u308b<\/a>\u3092\u3054\u89a7\u304f\u3060\u3055\u3044\u3002<\/p>\n<h1>Eager Loading\u3092\u8a2d\u5b9a\u3059\u308b<\/h1>\n<p><code>Eager Loading<\/code>\u3068\u306f\u3001\u7c21\u5358\u306b\u8a00\u3046\u3068\u300c<strong>\uff12\u3064\u4ee5\u4e0a\u306e\u30c6\u30fc\u30d6\u30eb\u3092\u7d50\u5408\u3057\u3066\u4e00\u6c17\u306b\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3059\u308b<\/strong>\u300d\u3053\u3068\u3067\u3059\u3002<\/p>\n<p>\u3072\u3068\u3064\u305a\u3064\u898b\u3066\u3044\u304d\u307e\u3057\u3087\u3046\uff01<\/p>\n<h2>hasOne<\/h2>\n<p>\u4f8b\u3048\u3070\u3001\u300c\u672c\u300d\u306e\u30c7\u30fc\u30bf\u306b\u300c\u672c\u306e\u30ab\u30c6\u30b4\u30ea\u300d\u304c\uff11\u3064\u3060\u3051\u8a2d\u5b9a\u3055\u308c\u3066\u3044\u308b\u3001\u3064\u307e\u308a\u300c\u672c\u300d\u3068\u300c\u672c\u306e\u30ab\u30c6\u30b4\u30ea\u300d\u304c\u300c<strong>\uff11\uff1a\uff11<\/strong>\u300d\u306e\u95a2\u4fc2\u3067\u7d50\u5408\u3059\u308b\u5834\u5408\u3067\u3059\u3002<\/p>\n<p>\u307e\u305a\u3001\u300c<strong>models\/book.js<\/strong>\u300d\u3092\u958b\u3044\u3066<code>Book<\/code>\u30e2\u30c7\u30eb\u306b<code>hasOne<\/code>\u306e\u7d50\u5408\u60c5\u5831\u3092\u8ffd\u52a0\u3057\u307e\u3059\u3002<\/p>\n<pre>'use strict';\r\nmodule.exports = (sequelize, DataTypes) =&gt; {\r\n  const Book = sequelize.define('Book', {\r\n    name: DataTypes.STRING,\r\n    category_id: DataTypes.INTEGER\r\n  }, {});\r\n  Book.associate = function(models) {\r\n    <strong>Book.hasOne(models.BookCategory, {\r\n      sourceKey: 'category_id',\r\n      foreignKey: 'id'\r\n    });<\/strong>\r\n  };\r\n  return Book;\r\n};\r\n<\/pre>\n<p>\u305d\u3057\u3066\u3001<code>include<\/code>\u3092\u4f7f\u3063\u3066\u30c7\u30fc\u30bf\u53d6\u5f97\u3057\u307e\u3059\u3002<\/p>\n<pre>Book.findAll({\r\n  <strong>include: [\r\n    { model: BookCategory }\r\n  ]<\/strong>\r\n}).then(books =&gt; {\r\n\r\n  for(let book of books) {\r\n\r\n    console.log(<strong>book.BookCategory<\/strong>);\r\n\r\n  }\r\n\r\n});\r\n<\/pre>\n<h2>hasMany<\/h2>\n<p>\u4f8b\u3048\u3070\u3001\u300c\u30e6\u30fc\u30b6\u30fc\u300d\u3068\u300c\u30e6\u30fc\u30b6\u30fc\u304c\u6295\u7a3f\u3057\u305f\u30b3\u30f3\u30c6\u30f3\u30c4\uff08\u8907\u6570\u306e\u53ef\u80fd\u6027\u3042\u308a\uff09\u300d\u3092\u7d50\u5408\u3057\u305f\u300c<strong>\uff11\uff1a\u591a<\/strong>\u300d\u306e\u95a2\u4fc2\u306b\u306a\u308b\u3088\u3046\u7d50\u5408\u3059\u308b\u5834\u5408\u3067\u3059\u3002<\/p>\n<p>\u307e\u305a\u3001\u300c<strong>models\/user.js<\/strong>\u300d\u3092\u958b\u3044\u3066<code>User<\/code>\u30e2\u30c7\u30eb\u306b<code>hasMany<\/code>\u306e\u7d50\u5408\u60c5\u5831\u3092\u8ffd\u52a0\u3057\u307e\u3059\u3002<\/p>\n<pre>'use strict';\r\nmodule.exports = (sequelize, DataTypes) =&gt; {\r\n  const User = sequelize.define('User', {\r\n    name: DataTypes.STRING,\r\n    email: DataTypes.STRING,\r\n    password: DataTypes.STRING\r\n  }, {});\r\n  User.associate = function(models) {\r\n    <strong>User.hasMany(models.Post, {\r\n      foreignKey: 'user_id',\r\n      sourceKey: 'id'\r\n    });<\/strong>\r\n  };\r\n  return User;\r\n};<\/pre>\n<p>\u305d\u3057\u3066\u3001<code>include<\/code>\u3092\u4f7f\u3063\u3066\u30c7\u30fc\u30bf\u53d6\u5f97\u3057\u307e\u3059\u3002<\/p>\n<pre>User.findAll({\r\n<strong>  include: [\r\n    { model: Post }\r\n  ]<\/strong>\r\n}).then(users =&gt; {\r\n\r\n  for(let user of users) {\r\n\r\n    console.log(<strong>user.Posts<\/strong>);\r\n\r\n  }\r\n\r\n});<\/pre>\n<h2>BelongsTo<\/h2>\n<p>\u4f8b\u3048\u3070\u3001\u300c\u30e6\u30fc\u30b6\u30fc\u8a73\u7d30\u300d\u3068\u300c\u30e6\u30fc\u30b6\u30fc\u300d\u306e\u30c6\u30fc\u30d6\u30eb\u304c\u300c<strong>\uff11\uff1a\uff11<\/strong>\u300d\u306e\u95a2\u4fc2\u3067\u7d50\u5408\u3059\u308b\u5834\u5408\u3067\u3059\u3002<\/p>\n<p>\u307e\u305a\u3001\u300c<strong>models\/userdetail.js<\/strong>\u300d\u3092\u958b\u3044\u3066<code>UserDetail<\/code>\u30e2\u30c7\u30eb\u306b<code>belongsTo<\/code>\u306e\u7d50\u5408\u60c5\u5831\u3092\u8ffd\u52a0\u3057\u307e\u3059\u3002<\/p>\n<pre>'use strict';\r\nmodule.exports = (sequelize, DataTypes) =&gt; {\r\n  const UserDetail = sequelize.define('UserDetail', {\r\n    zip: DataTypes.STRING,\r\n    address: DataTypes.STRING\r\n  }, {});\r\n  UserDetail.associate = function(models) {\r\n    <strong>UserDetail.belongsTo(models.User, {\r\n      foreignKey: 'id',\r\n      sourceKey: 'user_id'\r\n    });<\/strong>\r\n  };\r\n  return UserDetail;\r\n};\r\n<\/pre>\n<p>\u305d\u3057\u3066\u3001<code>include<\/code>\u3092\u4f7f\u3063\u3066\u30c7\u30fc\u30bf\u53d6\u5f97\u3057\u307e\u3059\u3002<\/p>\n<pre>UserDetail.findAll({\r\n  <strong>include: [\r\n    { model: User }\r\n  ]<\/strong>\r\n}).then(userDetails =&gt; {\r\n\r\n    for(let detail of userDetails) {\r\n\r\n      <strong>console.log(detail.User);<\/strong>\r\n\r\n    }\r\n\r\n});\r\n<\/pre>\n<h2>BelongsToMany<\/h2>\n<p>\u4f8b\u3048\u3070\u3001\u307e\u305a\u4ee5\u4e0b\uff12\u3064\u306e\u30c6\u30fc\u30d6\u30eb\u304c\u3042\u308b\u3068\u3057\u307e\u3059\u3002<\/p>\n<ul>\n<li>Users\u3000\u30fb\u30fb\u30fb\u3000\u30e6\u30fc\u30b6\u30fc<\/li>\n<li>Posts\u3000\u30fb\u30fb\u30fb\u3000\u30e6\u30fc\u30b6\u30fc\u304c\u6295\u7a3f\u3057\u305f\u8a18\u4e8b<\/li>\n<\/ul>\n<p>\u305d\u3057\u3066\u3001\u3053\u3053\u306b\u3082\u3046\uff11\u3064\u300c\u30e6\u30fc\u30b6\u30fc\u304c\u3010\u3044\u3044\u306d\uff01\u3011\u3092\u3057\u305f\u30c7\u30fc\u30bf\u300d\u3092\u7ba1\u7406\u3059\u308b\u300c<strong>PostLikes<\/strong>\u300d\u30c6\u30fc\u30d6\u30eb\u3092\u3064\u304f\u308a\u307e\u3059\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-5597\" style=\"border: 3px solid #000;\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/01\/sequelize_model_1.png\" alt=\"\" width=\"497\" height=\"389\" srcset=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/01\/sequelize_model_1.png 497w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/01\/sequelize_model_1-300x235.png 300w\" sizes=\"auto, (max-width: 497px) 100vw, 497px\" \/><\/p>\n<p>\u3053\u306e\u5834\u5408\u3001\u30e6\u30fc\u30b6\u30fc\u304b\u3089\u898b\u308b\u3068\u8907\u6570\u306e\u8a18\u4e8b\u306b\u300c\u3044\u3044\u306d\uff01\u300d\u3057\u305f\u30c7\u30fc\u30bf\u304c\u5b58\u5728\u3057\u3066\u3044\u3066\u3001\u3055\u3089\u306b\u3001\u8a18\u4e8b\u304b\u3089\u898b\u3066\u3082\u8907\u6570\u306e\u30e6\u30fc\u30b6\u30fc\u304c\u300c\u3044\u3044\u306d\uff01\u300d\u3057\u3066\u308b\u306e\u3067\u3001\u300c<strong>\u591a\uff1a\u591a<\/strong>\u300d\u306e\u95a2\u4fc2\u306b\u306a\u3063\u3066\u3044\u307e\u3059\u3002\u3064\u307e\u308a\u3001<code>PostLikes<\/code>\u306f\u4e2d\u9593\u30c6\u30fc\u30d6\u30eb\u306b\u306a\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>\u3067\u306f\u3001\u8a18\u4e8b\u30c7\u30fc\u30bf\u3092\u57fa\u6e96\u3068\u3057\u3066\u3001\u300c\u3044\u3044\u306d\uff01\u300d\u3057\u305f\u30e6\u30fc\u30b6\u30fc\u306e\u30c7\u30fc\u30bf\u3082\u4e00\u7dd2\u306b\u53d6\u5f97\u3057\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n<p>\u307e\u305a\u3001\u300c<strong>models\/post.js<\/strong>\u300d\u3092\u958b\u3044\u3066<code>Post<\/code>\u30e2\u30c7\u30eb\u306b<code>belongsToMany<\/code>\u306e\u7d50\u5408\u60c5\u5831\u3092\u8ffd\u52a0\u3057\u307e\u3059\u3002<\/p>\n<pre>'use strict';\r\nmodule.exports = (sequelize, DataTypes) =&gt; {\r\n  const Post = sequelize.define('Post', {\r\n    user_id: DataTypes.INTEGER,\r\n    content: DataTypes.TEXT\r\n  }, {});\r\n  Post.associate = function(models) {\r\n    <strong>Post.belongsToMany(models.User, {\r\n      through: models.PostLike,\r\n      foreignKey: 'post_id',\r\n      otherKey: 'user_id',\r\n      as: 'likes'\r\n    });<\/strong>\r\n  };\r\n  return Post;\r\n};<\/pre>\n<p>\u203b \u3061\u306a\u307f\u306b\u3001\u5206\u304b\u308a\u3084\u3059\u304f\u3059\u308b\u305f\u3081\u306b\u300c<strong>likes<\/strong>\u300d\u3068\u3044\u3046\u540d\u524d\u3092\u3064\u3051\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>\u305d\u3057\u3066\u3001<code>include<\/code>\u3092\u4f7f\u3063\u3066\u30c7\u30fc\u30bf\u53d6\u5f97\u3057\u307e\u3059\u3002<\/p>\n<pre>Post.findAll({\r\n  <strong>include: [\r\n    { model: User, as: 'likes' }\r\n  ]<\/strong>\r\n}).then(posts =&gt; {\r\n\r\n  \/\/\r\n\r\n});<\/pre>\n<h2>Setter\u3068Getter\u3067\u30c7\u30fc\u30bf\u3092\u52a0\u5de5\u3059\u308b<\/h2>\n<h3>Getter\u306e\u4f7f\u3044\u65b9<\/h3>\n<p>\u4f8b\u3048\u3070\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u30e6\u30fc\u30b6\u30fc\u30c6\u30fc\u30d6\u30eb\u306b\u300c<strong>firstName\uff08\u540d\uff09<\/strong>\u300d\u300c<strong>lastName\uff08\u59d3\uff09<\/strong>\u300d\u304c\u5b58\u5728\u3057\u3066\u3044\u308b\u3068\u3057\u307e\u3059\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-5606\" style=\"border: 3px solid #000;\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/01\/sequelize_model_4.png\" alt=\"\" width=\"346\" height=\"316\" srcset=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/01\/sequelize_model_4.png 346w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/01\/sequelize_model_4-300x274.png 300w\" sizes=\"auto, (max-width: 346px) 100vw, 346px\" \/><\/p>\n<p>\u305d\u3057\u3066\u3001\u30c7\u30fc\u30bf\u53d6\u5f97\u3057\u305f\u3068\u304d\u306b\u3053\u306e\uff12\u3064\u306e\u5024\u3092\u7d50\u5408\u3057\u305f\u5024\u300c<strong>fullName<\/strong>\u300d\u3092\u53d6\u5f97\u3059\u308b\u5834\u5408\u3067\u3059\u3002<\/p>\n<p>\u300c<strong>\/models\/user.js<\/strong>\u300d\u3092\u958b\u3044\u3066\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u8a2d\u5b9a\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<pre>'use strict';\r\nmodule.exports = (sequelize, DataTypes) =&gt; {\r\n  const User = sequelize.define('User', {\r\n    firstName: DataTypes.STRING,\r\n    lastName: DataTypes.STRING,\r\n    email: DataTypes.STRING,\r\n    password: DataTypes.STRING\r\n  }, {\r\n    <strong>getterMethods: {\r\n      fullName() {\r\n        return this.lastName +' '+ this.firstName;\r\n      }\r\n    }<\/strong>\r\n  });\r\n  User.associate = function(models) {\r\n    \/\/\r\n  };\r\n  return User;\r\n};<\/pre>\n<p>\u3053\u308c\u3067\u3001<code>fullName<\/code>\u3092\u901a\u3057\u3066\u7d50\u5408\u3055\u308c\u305f\u540d\u524d\u3092\u53d6\u5f97\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre>User.findOne({\r\n  where: {\r\n    id: 1\r\n  }\r\n}).then((user) =&gt; {\r\n\r\n  console.log(user.fullName); \/\/ \u4f8b\uff1a \u5c71\u7530 \u592a\u90ce\r\n\r\n});<\/pre>\n<p>\u306a\u304a\u3001\u3082\u3057\u521d\u671f\u5024\u306b<code>FullName<\/code>\u3092\u8ffd\u52a0\u3057\u3066\u304a\u304d\u305f\u3044\u5834\u5408\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b<code>DataTypes.VIRTUAL<\/code>\u3092\u4f7f\u3063\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<pre>'use strict';\r\nmodule.exports = (sequelize, DataTypes) =&gt; {\r\n    const User = sequelize.define('User', {\r\n        firstName: DataTypes.STRING,\r\n        lastName: DataTypes.STRING,\r\n        email: DataTypes.STRING,\r\n        password: DataTypes.STRING,\r\n        <strong>fullName: DataTypes.VIRTUAL, \/\/ \u2190\u3053\u3053<\/strong>\r\n    }, {\r\n        getterMethods: {\r\n            fullName() {\r\n                return this.lastName +' '+ this.firstName;\r\n            }\r\n        }\r\n    });\r\n    User.associate = function(models) {\r\n        \/\/\r\n    };\r\n    return User;\r\n};<\/pre>\n<h3>Setter\u306e\u4f7f\u3044\u65b9<\/h3>\n<p>\u4f8b\u3048\u3070\u3001<code>fullName<\/code>\u306b\u300c<strong>\u65b0\u5c71\u7530 \u65b0\u592a\u90ce<\/strong>\u300d\u3068\u30bb\u30c3\u30c8\u3055\u308c\u305f\u3089\u3001\u305d\u308c\u305e\u308c\u300c<strong>firstName<\/strong>\u300d\u3068\u300c<strong>lastName<\/strong>\u300d\u306b\u30c7\u30fc\u30bf\u30bb\u30c3\u30c8\u3059\u308b\u5834\u5408\u3067\u3059\u3002<\/p>\n<p>\u300c<strong>\/models\/user.js<\/strong>\u300d\u3092\u958b\u3044\u3066\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u8a2d\u5b9a\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<pre>'use strict';\r\nmodule.exports = (sequelize, DataTypes) =&gt; {\r\n  const User = sequelize.define('User', {\r\n    firstName: DataTypes.STRING,\r\n    lastName: DataTypes.STRING,\r\n    email: DataTypes.STRING,\r\n    password: DataTypes.STRING\r\n  }, {\r\n    <strong>setterMethods: {\r\n      fullName(value) {\r\n        const [lastName, firstName] = value.split(' ');\r\n        console.log(lastName, firstName)\r\n        this.setDataValue('firstName', firstName);\r\n        this.setDataValue('lastName', lastName);\r\n      }\r\n    }<\/strong>\r\n  });\r\n  User.associate = function(models) {\r\n    \/\/\r\n  };\r\n  return User;\r\n};<\/pre>\n<p>\u3053\u308c\u3067\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u3059\u308c\u3070\u300c<strong>firstName<\/strong>\u300d\u300c<strong>lastName<\/strong>\u300d\u304c\u4e00\u6c17\u306b\u30bb\u30c3\u30c8\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre>User.findOne({\r\n  where: {\r\n    id: 1\r\n  }\r\n}).then((user) =&gt; {\r\n\r\n  <strong>user.fullName = '\u65b0\u5c71\u7530 \u65b0\u592a\u90ce';\r\n  user.save();<\/strong>\r\n\r\n});<\/pre>\n<p>\u203b \u305f\u3060\u3057\u3001\u3053\u308c\u306f\u30b5\u30f3\u30d7\u30eb\u306a\u306e\u3067\u5b9f\u969b\u306e\u958b\u767a\u3067\u306f<code>trim()<\/code>\u3084\u30d0\u30ea\u30c7\u30fc\u30b7\u30e7\u30f3\u306a\u3069\u3067\u6b63\u3057\u3044\u30c7\u30fc\u30bf\u3092\u7528\u610f\u3059\u308b\u3053\u3068\u3092\u5fd8\u308c\u306a\u3044\u3067\u304f\u3060\u3055\u3044\u3002<\/p>\n<h1>Scope\u3092\u4f7f\u3046<\/h1>\n<p><code>Scope<\/code>\u3068\u306f\u3001\u300c\u691c\u7d22\u3084\u4e26\u3079\u66ff\u3048\u306e\u6761\u4ef6\u300d\u3092\u4f7f\u3044\u307e\u308f\u3057\u304c\u3067\u304d\u308b\u3088\u3046\u306b\u3059\u308b\u6a5f\u80fd\u3067\u3059\u3002<\/p>\n<p>\u4f8b\u3048\u3070\u3001\u3042\u306a\u305f\u306e\u30b5\u30a4\u30c8\u3067\u3088\u304f\u300c<strong>\u627f\u8a8d\u6e08\u306e\u30e6\u30fc\u30b6\u30fc<\/strong>\u300d\u3092\u62bd\u51fa\u3059\u308b\u3053\u3068\u304c\u3042\u308b\u3068\u3057\u307e\u3057\u3087\u3046\u3002<\/p>\n<p>\u901a\u5e38\u3067\u3042\u308c\u3070\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u305d\u306e\u90fd\u5ea6<code>where<\/code>\u3067\u6307\u5b9a\u3059\u308b\u3053\u3068\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre>{\r\n  <strong>where: {<\/strong>\r\n<strong>    accepted: true<\/strong>\r\n<strong>  }<\/strong>\r\n}<\/pre>\n<p>\u305f\u3060\u3001\u6bce\u56de\u6bce\u56de\u3053\u306e\u8a18\u8ff0\u3092\u3059\u308b\u306e\u306f\u3081\u3093\u3069\u3046\u3067\u3059\u3057\u3001\u3069\u3053\u304b\u3067\u8a18\u8ff0\u30df\u30b9\u304c\u4f5c\u3089\u308c\u3066\u3057\u307e\u3046\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u3002<\/p>\n<p>\u305d\u3053\u3067\u3001<code>Scope<\/code>\u306e\u767b\u5834\u3067\u3059\u3002<code>Scope<\/code>\u306f\u3053\u306e\u9762\u5012\u306a\u90e8\u5206\u3092\u300c\u5171\u901a\u5316\u300d\u3057\u3066\u3001\u3044\u3064\u3067\u3082\u547c\u3073\u51fa\u305b\u308b\u3088\u3046\u306b\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\uff08\u3082\u3061\u308d\u3093\u3001<code>Scope<\/code>\u306b\u306f<code>where<\/code>\u3060\u3051\u3067\u306a\u304f\u3001<code>order<\/code>\u3084<code>attributes<\/code>\u306a\u3069\u3082\u4f7f\u3048\u307e\u3059\uff09<\/p>\n<p>\u3067\u306f\u5b9f\u969b\u306e\u4f8b\u3092\u898b\u3066\u307f\u307e\u3057\u3087\u3046\uff01<\/p>\n<h2>Global scope<\/h2>\n<p>\u30b0\u30ed\u30fc\u30d0\u30eb\u30fb\u30b9\u30b3\u30fc\u30d7\u306f\u3001\u7279\u306b\u8a2d\u5b9a\u3057\u306a\u304f\u3066\u3082\u5fc5\u305a\u8ffd\u52a0\u3057\u3066\u304f\u308c\u308b\u6761\u4ef6\u306e\u3053\u3068\u3067\u3059\u3002<\/p>\n<p>\u4f8b\u3048\u3070\u3001\u300c<strong>\u79d8\u5bc6\u306e\u8cea\u554f<\/strong>\u300d\u3084\u300c<strong>\u79d8\u5bc6\u306e\u8cea\u554f\u306e\u7b54\u3048<\/strong>\u300d\u306f\u3001\u57fa\u672c\u7684\u306b\u53d6\u5f97\u3059\u3079\u304d\u3067\u306f\u306a\u3044\u7279\u5225\u306a\u30c7\u30fc\u30bf\u3067\u3059\u3002\u3068\u3044\u3046\u3053\u3068\u3067\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u30b0\u30ed\u30fc\u30d0\u30eb\u30b9\u30b3\u30fc\u30d7\u3092\u8ffd\u52a0\u3057\u3066\u3053\u308c\u3089\u306e\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3057\u306a\u3044\u3088\u3046\u306b\u3057\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n<pre>module.exports = (sequelize, DataTypes) =&gt; {\r\n  const User = sequelize.define('User', {\r\n    \/\/ \u7701\u7565\r\n  }, {\r\n    <strong>defaultScope: {<\/strong>\r\n<strong>      attributes: { exclude: ['secret_question', 'secret_answer'] },<\/strong>\r\n<strong>    },<\/strong>\r\n \r\n\/\/ \u7701\u7565<\/pre>\n<h2>Scope<\/h2>\n<p>\u6b21\u306b\u4f7f\u3046\u6642\u306b\u306f\u81ea\u5206\u3067\u547c\u3073\u51fa\u3057\u3066\u4f7f\u3046\u901a\u5e38\u306e\u30b9\u30b3\u30fc\u30d7\u3067\u3059\u3002\u4eca\u56de\u306f<code>hasMany<\/code>\u306e\u7d50\u5408\u3092\u3059\u308b\u305f\u3081\u306e\u30b9\u30b3\u30fc\u30d7\u3092\u4f5c\u3063\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n<pre>User.associate = (models) =&gt; {\r\n\r\n  <strong>User.addScope('posts', {<\/strong>\r\n<strong>    include: [{ model: models.Post }]<\/strong>\r\n<strong>  });\r\n<\/strong>\r\n}<\/pre>\n<p>\u203b\u3061\u306a\u307f\u306b\u672c\u5bb6\u306e\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u3067\u306f<code>{ scope: { ***** }}<\/code>\u3067\u3082\u8a2d\u5b9a\u3067\u304d\u308b\u3068\u66f8\u304b\u308c\u3066\u3044\u307e\u3059\u304c\u3001\u7d50\u5408\u3092\u542b\u3080\u5834\u5408\u306f\u3046\u307e\u304f\u3044\u304d\u307e\u305b\u3093\u3002\u30e2\u30c7\u30eb\u304c\u5168\u3066\u8aad\u307f\u8fbc\u307e\u308c\u3066\u3044\u306a\u3044\u305f\u3081\u3067\u3059\u3002<\/p>\n<p>\u305d\u3057\u3066\u3001\u3053\u306e\u300c<strong>posts<\/strong>\u300d\u30b9\u30b3\u30fc\u30d7\u3092\u4f7f\u3046\u306b\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<pre>User.<strong>scope('posts')<\/strong>.findAll();\r\n\r\n\/\/ \u3082\u3057\u304f\u306f\u8907\u6570\u306e\u5834\u5408\r\n\r\nUser.<strong>scope(['posts', 'friends'])<\/strong>.findAll();<\/pre>\n<p>\u307e\u305f\u3001\u30b9\u30b3\u30fc\u30d7\u306b\u306f\u30d1\u30e9\u30e1\u30fc\u30bf\u30fc\u3092\u6e21\u3059\u3053\u3068\u3082\u3067\u304d\u307e\u3059\u3002<br \/>\n<span style=\"font-size: 1rem;\">\u305d\u306e\u5834\u5408\u306f\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u95a2\u6570\u5316\u3057\u3066\u8a2d\u5b9a\u3057\u307e\u3059\u3002<\/span><\/p>\n<p><span style=\"font-size: 1rem;\">\u4f8b\u3068\u3057\u3066<code>userId<\/code>\u3092\u30d1\u30e9\u30e1\u30fc\u30bf\u30fc\u3068\u3057\u3066\u6e21\u3057\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/span><\/p>\n<pre>User.associate = (models) =&gt; {\r\n\r\n  <strong>User.addScope('posts', (userId) =&gt; {<\/strong>\r\n<strong>    return {\r\n      where: { id: userId }\r\n    }<\/strong>\r\n<strong>  });\r\n<\/strong>\r\n}<\/pre>\n<p>\u4f7f\u3044\u65b9\u306f\u3053\u3046\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre>const userId = 1;\r\nUser.<strong>scope({ method: ['posts', userId] })<\/strong>.findAll();<\/pre>\n<h1>\u30c7\u30fc\u30bf\u3092\u8ffd\u52a0\u3059\u308b<\/h1>\n<h2>\uff11\u3064\u3060\u3051\u8ffd\u52a0<\/h2>\n<p>\u30c7\u30fc\u30bf\u3092\uff11\u3064\uff08\uff11\u884c\uff09\u3060\u3051\u8ffd\u52a0\u3059\u308b\u5834\u5408\u306f<code>create()<\/code>\u3092\u4f7f\u3044\u307e\u3059\u3002<\/p>\n<pre>User.<strong>create({\r\n  name: '\u516d\u90ce',\r\n  email: 'rokuro@example.com',\r\n  password: 'rokuro-password',\r\n  age: 22,\r\n  accepted: false,\r\n  createdAt: new Date(),\r\n  updatedAt: new Date()\r\n})<\/strong>.then(user =&gt; {\r\n\r\n  \/\/\r\n\r\n});\r\n<\/pre>\n<h2>\u8907\u6570\u30c7\u30fc\u30bf\u3092\u8ffd\u52a0<\/h2>\n<p>\u4f8b\u3048\u3070\u3001\u8907\u6570\u306e\u30e6\u30fc\u30b6\u30fc\u3092\u4e00\u6c17\u306b\u8ffd\u52a0\u3059\u308b\u5834\u5408\u306f<code>bulkCreate()<\/code>\u3092\u4f7f\u3044\u307e\u3059\u3002<br \/>\n\u305d\u306e\u305f\u3081\u3001<code>Seeder<\/code>\u306a\u3069\u3067\u91cd\u5b9d\u3059\u308b\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u3002<\/p>\n<pre>const now = new Date();\r\nUser.<strong>bulkCreate([\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])<\/strong>.then(users =&gt; {\r\n\r\n  \/\/\r\n\r\n});<\/pre>\n<h1>\u30c7\u30fc\u30bf\u3092\u66f4\u65b0\u3059\u308b<\/h1>\n<h2>update()\u3092\u4f7f\u3046\u5834\u5408<\/h2>\n<p>\u6761\u4ef6\u306b\u5408\u3046\u30c7\u30fc\u30bf\u5168\u3066\u3092\u66f4\u65b0\u3059\u308b\u5834\u5408\u306f\u3001<code>update()<\/code>\u3092\u4f7f\u3044\u307e\u3059\u3002<\/p>\n<p>\u4f8b\u3048\u3070\u3001\u4ee5\u4e0b\u306f<code>id:1<\/code>\u306e\u540d\u524d\u3092\u300c<strong>\u771f\u592a\u90ce<\/strong>\u300d\u3001\u30e1\u30fc\u30eb\u30a2\u30c9\u30ec\u30b9\u3092\u300c<strong>real-taro@example.com<\/strong>\u300d\u306b\u5909\u66f4\u3059\u308b\u5834\u5408\u3067\u3059\u3002<\/p>\n<pre><strong>User.update(\r\n  { name: '\u771f\u592a\u90ce', email: 'real-taro@example.com' },\r\n  { where: { id: 1 } }\r\n).then(() =&gt; {\r\n\r\n  \/\/\r\n\r\n});<\/strong><\/pre>\n<p>\u203b \u3053\u306e\u5834\u5408<code>UpdatedAt<\/code>\u3082\u81ea\u52d5\u7684\u306b\u66f4\u65b0\u3055\u308c\u307e\u3059\u3002<\/p>\n<h2>save()\u3092\u4f7f\u3046\u5834\u5408<\/h2>\n<p>\u3059\u3067\u306b<code>findOne()<\/code>\u306a\u3069\u3067\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3057\u3066\u3044\u308b\u5834\u5408\u306b\u4f7f\u3048\u308b\u65b9\u6cd5\u3067\u3059\u3002<\/p>\n<pre>User.findOne().then(user =&gt; {\r\n\r\n  <strong>user.name = '\u65b0\u592a\u90ce';<\/strong>\r\n<strong>  user.email = 'shintaro@example.com';\r\n  user.save();<\/strong>\r\n\r\n});<\/pre>\n<p>\u203b \u3053\u306e\u5834\u5408<code>UpdatedAt<\/code>\u3082\u81ea\u52d5\u7684\u306b\u66f4\u65b0\u3055\u308c\u307e\u3059\u3002<\/p>\n<h2>updatedAt\u306e\u65e5\u6642\u3060\u3051\u3092\u66f4\u65b0\u3059\u308b<\/h2>\n<p><code>updatedAt<\/code>\u306f\u3001\u4ed6\u306e\u30c7\u30fc\u30bf\u304c\u66f4\u65b0\u3055\u308c\u305f\u5834\u5408\u306b\u81ea\u52d5\u7684\u306b\u65b0\u3057\u3044\u65e5\u6642\u304c\u4fdd\u5b58\u3055\u308c\u308b\u3088\u3046\u306b\u306a\u3063\u3066\u3044\u308b\u306e\u3067\u3059\u304c\u3001\u9006\u306b\u8a00\u3046\u3068\u300c<strong>\u4ed6\u306e\u30c7\u30fc\u30bf\u304c\u66f4\u65b0\u3055\u308c\u306a\u3044\u306a\u3089\u3001\u305a\u3063\u3068\u53e4\u3044\u307e\u307e<\/strong>\u300d\u3068\u3044\u3046\u610f\u5473\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<p>\u305d\u306e\u305f\u3081\u3001\u3082\u3057\u624b\u52d5\u3067<code>updatedAt<\/code>\u3060\u3051\u3092\u66f4\u65b0\u3057\u305f\u3044\u5834\u5408\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u3057\u307e\u3059\u3002<\/p>\n<pre>User.findOne().then(user =&gt; {\r\n\r\n  user.updatedAt = new Date();\r\n  <strong>user.changed('updatedAt', true);<\/strong>\r\n  user.save();\r\n\r\n});<\/pre>\n<h1>\u30c7\u30fc\u30bf\u3092\u524a\u9664\u3059\u308b<\/h1>\n<h2>fineOne()\u306a\u3069\u3067\u30c7\u30fc\u30bf\u53d6\u5f97\u3055\u308c\u3066\u3044\u308b\u5834\u5408<\/h2>\n<p>\u3059\u3067\u306b\u30c7\u30fc\u30bf\u53d6\u5f97\u3055\u308c\u3066\u3044\u308b\u5834\u5408\u306f\u3001\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u672c\u4f53\u304b\u3089<code>destroy()<\/code>\u3092\u5b9f\u884c\u3057\u307e\u3059\u3002<\/p>\n<pre>User.findOne({\r\n  where: { id: 10 }\r\n}).then(user =&gt; {\r\n\r\n  <strong>user.destroy();<\/strong>\r\n\r\n});<\/pre>\n<h2>\u6761\u4ef6\u306b\u5408\u3046\u5168\u30c7\u30fc\u30bf\u3092\u524a\u9664\u3059\u308b\u5834\u5408<\/h2>\n<p>\u6761\u4ef6\u306b\u5408\u3046\u30c7\u30fc\u30bf\u5168\u3066\u3092\u524a\u9664\u3059\u308b\u5834\u5408\u306f\u3001\u30e2\u30c7\u30eb\u306e<code>destroy()<\/code>\u3092\u5b9f\u884c\u3057\u307e\u3059\u3002<\/p>\n<pre>User.<strong>destroy({\r\n  where: {\r\n    id: [3, 5]\r\n  }\r\n})<\/strong>.then(() =&gt; {\r\n\r\n  \/\/\r\n\r\n});<\/pre>\n<h2>\u30bd\u30d5\u30c8\u30c7\u30ea\u30fc\u30c8\u3092\u4f7f\u3046\u5834\u5408<\/h2>\n<p>\u30bd\u30d5\u30c8\u30c7\u30ea\u30fc\u30c8\u3068\u306f\u3001\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u304b\u3089\u5b9f\u969b\u306b\u30c7\u30fc\u30bf\u3092\u524a\u9664\u3059\u308b\u306e\u3067\u306f\u306a\u304f\u3001\u524a\u9664\u3055\u308c\u305f\u3053\u3068\u304c\u308f\u304b\u308b\u30c7\u30fc\u30bf\u3092\u7528\u610f\u3057\u3066\u304a\u304d\u300c<strong>\u524a\u9664\u3055\u308c\u305f\u3082\u306e<\/strong>\u300d\u3068\u3057\u3066\u6271\u3046\u6a5f\u80fd\u306e\u3053\u3068\u3067\u3059\u3002\uff08\u306a\u305c\u306a\u3089\u3001\u7269\u7406\u7684\u306b\u30c7\u30fc\u30bf\u524a\u9664\u3057\u3066\u3057\u307e\u3046\u3068\u6c38\u9060\u306b\u5fa9\u6d3b\u3067\u304d\u306a\u3044\u304b\u3089\u3067\u3059\uff09<\/p>\n<h3>\u30bd\u30d5\u30c8\u30c7\u30ea\u30fc\u30c8\u3092\u6709\u52b9\u306b\u3059\u308b<\/h3>\n<p>\u300c<strong>Sequelize<\/strong>\u300d\u306e\u5834\u5408\u3001\u300c<strong>deletedAt<\/strong>\u300d\u3068\u3044\u3046\u30d5\u30a3\u30fc\u30eb\u30c9\uff08\u521d\u671f\u5024\uff1a<code>null<\/code>\uff09\u3092\u7528\u610f\u3057\u3001\u3053\u3053\u306b\u65e5\u4ed8\u30c7\u30fc\u30bf\u304c\u5165\u3063\u3066\u3044\u308c\u3070\u300c<strong>\u524a\u9664\u3055\u308c\u305f\u3082\u306e<\/strong>\u300d\u3068\u3057\u3066\u6271\u308f\u308c\u307e\u3059\u3002<\/p>\n<p>\u3067\u306f\u3001\u4f8b\u3068\u3057\u3066<code>User<\/code>\u30e2\u30c7\u30eb\u306b\u30bd\u30d5\u30c8\u30c7\u30ea\u30fc\u30c8\u6a5f\u80fd\u3092\u3064\u3051\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n<p>\u30de\u30a4\u30b0\u30ec\u30fc\u30b7\u30e7\u30f3\u306f\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b<code>deletedAt<\/code>\u3092\u8ffd\u52a0\u3057\u3066\u5b9f\u884c\u3057\u3066\u304f\u3060\u3055\u3044\u3002\uff08<code>Null<\/code>\u306f\u6709\u52b9\u306b\u3059\u308b\uff09<\/p>\n<pre>'use strict';\r\nmodule.exports = {\r\n  up: (queryInterface, Sequelize) =&gt; {\r\n    return queryInterface.createTable('Users', {\r\n      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      <strong>deletedAt: {\r\n        type: Sequelize.DATE\r\n      },<\/strong>\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      }\r\n    });\r\n  },\r\n  down: (queryInterface, Sequelize) =&gt; {\r\n    return queryInterface.dropTable('Users');\r\n  }\r\n};<\/pre>\n<p>\u30c6\u30fc\u30d6\u30eb\u306f\u3053\u3046\u306a\u308a\u307e\u3059\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-5601\" style=\"border: 3px solid #000;\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/01\/sequelize_model_2.png\" alt=\"\" width=\"356\" height=\"295\" srcset=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/01\/sequelize_model_2.png 356w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/01\/sequelize_model_2-300x249.png 300w\" sizes=\"auto, (max-width: 356px) 100vw, 356px\" \/><\/p>\n<p>\u3067\u306f\u3001\u6b21\u306b\u300c<strong>\/models\/user.js<\/strong>\u300d\u3092\u958b\u3044\u3066<code>paranoid<\/code>\u30aa\u30d7\u30b7\u30e7\u30f3\u3092\u8ffd\u52a0\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<pre>'use strict';\r\nmodule.exports = (sequelize, DataTypes) =&gt; {\r\n  const User = sequelize.define('User', {\r\n    name: DataTypes.STRING,\r\n    email: DataTypes.STRING,\r\n    password: DataTypes.STRING\r\n  }, {\r\n    <strong>paranoid: true<\/strong>\r\n  });\r\n  User.associate = function(models) {\r\n    \/\/\r\n  };\r\n  return User;\r\n};<\/pre>\n<p>\u3053\u308c\u3067\u30bd\u30d5\u30c8\u30c7\u30ea\u30fc\u30c8\u304c\u6709\u52b9\u306b\u306a\u308a\u307e\u3057\u305f\u3002<br \/>\n\u5b9f\u969b\u306b\u524a\u9664\u3057\u3066\u30c6\u30fc\u30d6\u30eb\u3092\u78ba\u8a8d\u3057\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-5603\" style=\"border: 3px solid #000;\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/01\/sequelize_model_3.png\" alt=\"\" width=\"547\" height=\"364\" srcset=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/01\/sequelize_model_3.png 547w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/01\/sequelize_model_3-300x200.png 300w\" sizes=\"auto, (max-width: 547px) 100vw, 547px\" \/><\/p>\n<p>\u3053\u306e\u3088\u3046\u306b\u65e5\u4ed8\u304c\u8ffd\u52a0\u3055\u308c\u307e\u3059\u3002<\/p>\n<p>\u305d\u3057\u3066\u3001\u3053\u306e\u72b6\u614b\u3067<code>findAll()<\/code>\u3057\u3066\u3082<code>ID:3<\/code>\u3068<code>ID:5<\/code>\u306e\u30c7\u30fc\u30bf\u306f\u53d6\u5f97\u3055\u308c\u308b\u3053\u3068\u306f\u3042\u308a\u307e\u305b\u3093\u3002<\/p>\n<h3>\u30bd\u30d5\u30c8\u30c7\u30ea\u30fc\u30c8\u3055\u308c\u305f\u30c7\u30fc\u30bf\u3082\u542b\u3081\u3066\u30c7\u30fc\u30bf\u53d6\u5f97\u3059\u308b<\/h3>\n<p>\u3082\u3057\u3001\u30bd\u30d5\u30c8\u30c7\u30ea\u30fc\u30c8\u3055\u308c\u305f\u30c7\u30fc\u30bf\u3082\u542b\u3081\u3066\u53d6\u5f97\u3057\u305f\u3044\u5834\u5408\u306f<code>paranoid<\/code>\u30aa\u30d7\u30b7\u30e7\u30f3\u3092\u4f7f\u3044\u307e\u3059\u3002<\/p>\n<pre>User.findAll(<strong>{ paranoid: false }<\/strong>).then(users =&gt; {\r\n\r\n  \/\/\r\n\r\n});<\/pre>\n<h3>\u30bd\u30d5\u30c8\u30c7\u30ea\u30fc\u30c8\u305b\u305a\u3001\u7269\u7406\uff08\u5b8c\u5168\uff09\u524a\u9664\u3059\u308b<\/h3>\n<p>\u30bd\u30d5\u30c8\u30c7\u30ea\u30fc\u30c8\u3092\u4f7f\u3063\u3066\u3044\u308b\u3051\u308c\u3069\u3082\u3001\u7269\u7406\u7684\u306b\u524a\u9664\u3057\u305f\u3044\u3068\u3044\u3046\u5834\u5408\u306f<code>force<\/code>\u30aa\u30d7\u30b7\u30e7\u30f3\u3092\u4f7f\u3044\u307e\u3059\u3002<\/p>\n<pre>User.destroy({\r\n  where: {\r\n    id: [3, 5]\r\n  },\r\n  <strong>force: true<\/strong>\r\n}).then(() =&gt; {\r\n\r\n  \/\/\r\n\r\n});<\/pre>\n<h3>\u30bd\u30d5\u30c8\u30c7\u30ea\u30fc\u30c8\u3055\u308c\u305f\u30c7\u30fc\u30bf\u3092\u5fa9\u6d3b\u3055\u305b\u308b<\/h3>\n<p>\u3059\u3067\u306b\u30bd\u30d5\u30c8\u30c7\u30ea\u30fc\u30c8\u3055\u308c\u305f\u30c7\u30fc\u30bf\u3092\u5fa9\u6d3b\u3055\u305b\u308b\u306b\u306f<code>restore()<\/code>\u3092\u4f7f\u3044\u307e\u3059\u3002<\/p>\n<pre>User.findOne({\r\n  where: {\r\n    id: 1\r\n  },\r\n  paranoid: false\r\n}).then(user =&gt; {\r\n\r\n  <strong>user.restore();<\/strong>\r\n\r\n});<\/pre>\n<h1>\u76f4\u63a5SQL\u6587\u3092\u5b9f\u884c\u3059\u308b<\/h1>\n<p>\u76f4\u63a5SQL\u6587\u3092\u5b9f\u884c\u3059\u308b\u5834\u5408\u306f\u3001<code>query()<\/code>\u3092\u4f7f\u3044\u307e\u3059\u3002<\/p>\n<pre>const Sequelize = require('sequelize');\r\nconst config = require('.\/config\/config');\r\nconst sequelize = new Sequelize(config[process.env.NODE_ENV]);\r\n\r\nsequelize.<strong>query('SELECT COUNT(id) FROM Users')<\/strong>.then(users =&gt; {\r\n\r\n  \/\/\r\n\r\n});\r\n<\/pre>\n<h1>\u305d\u306e\u4ed6<\/h1>\n<h2 id=\"use_instance\">Sequelize\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u53d6\u5f97\u3059\u308b<\/h2>\n<p>\u74b0\u5883\u306b\u3088\u308b\u81ea\u52d5\u5207\u308a\u66ff\u3048\u3084\u3001\u5c11\u3057\u7279\u6b8a\u306a\u5185\u5bb9\u3092\u4f5c\u6210\u3057\u3088\u3046\u3068\u3059\u308b\u5834\u5408\u306b\u300c<strong>Sequelize<\/strong>\u300d\u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u304c\u5fc5\u8981\u306b\u306a\u308b\u3053\u3068\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<p>\u305d\u3093\u306a\u5834\u5408\u306f\u6b21\u306e\u3088\u3046\u306b\u3057\u3066\u53d6\u5f97\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<pre><strong>const Sequelize = require('sequelize');<\/strong>\r\n<strong>const config = require('.\/config\/config');<\/strong>\r\n\r\n\/\/ Sequelize \u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\r\n<strong>const sequelize = new Sequelize({<\/strong>\r\n<strong>  dialect: config[process.env.NODE_ENV].dialect<\/strong>\r\n<strong>});\r\n<\/strong><\/pre>\n<p>\u203b\u00a0<code>dialect<\/code>\u306b\u300c<strong>mysql<\/strong>\u300d\u3068\u76f4\u66f8\u304d\u3057\u3066\u3057\u307e\u3046\u3068\u74b0\u5883\u304c\u5909\u308f\u3063\u305f\u3068\u304d\u306b\u30a8\u30e9\u30fc\u304c\u767a\u751f\u3059\u308b\u53ef\u80fd\u6027\u304c\u3042\u308b\u306e\u3067\u5c11\u3057\u8907\u96d1\u306a\u5f62\u306b\u3057\u3066\u3044\u307e\u3059\u304c\u3001\u305d\u306e\u5fc3\u914d\u304c\u7121\u3044\u5834\u5408\u306f\u76f4\u63a5\u66f8\u304d\u8fbc\u3093\u3067\u3082\u3044\u3044\u3067\u3057\u3087\u3046\u3002<\/p>\n<h2>\u30bf\u30a4\u30e0\u30be\u30fc\u30f3\u3092\u6307\u5b9a\u3059\u308b<\/h2>\n<p><code>Sequelize<\/code>\u306b\u306f\u3001\u81ea\u52d5\u7684\u306b\u300c<strong>createdAt<\/strong>\u300d\u3084\u300c<strong>updatedAt<\/strong>\u300d\u306e\u65e5\u6642\u3092\u4fdd\u5b58\u3057\u3066\u304f\u308c\u308b\u6a5f\u80fd\u304c\u3042\u308a\u307e\u3059\u304c\u3001\u5b9f\u884c\u74b0\u5883\u306b\u3088\u3063\u3066\u306f\u30bf\u30a4\u30e0\u30be\u30fc\u30f3\u304c\u6b63\u3057\u304f\u306a\u3044\u5834\u5408\u304c\u3042\u308a\u307e\u3059\u3002\u305d\u306e\u305f\u3081\u3001\u3082\u3057\u65e5\u672c\u306e\u30bf\u30a4\u30e0\u30be\u30fc\u30f3\u306b\u8a2d\u5b9a\u3057\u305f\u3044\u5834\u5408\u306f\u3001<code>config\/config.json<\/code>\u306e\u4e2d\u3067\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u8a2d\u5b9a\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<pre>{\r\n  \"development\": {\r\n    \/\/ \u7701\u7565\r\n    <strong>\"timezone\": \"+09:00\"<\/strong>\r\n},\r\n\r\n\/\/ \u7701\u7565<\/pre>\n<p>\u306a\u304a\u3001<code>Laravel<\/code>\u306a\u3069\u3067\u3082\u3088\u304f\u8a2d\u5b9a\u3059\u308b\u300c<strong>Asia\/Tokyo<\/strong>\u300d\u3068\u3044\u3046\u8868\u73fe\u3092\u4f7f\u3046\u3068\u3001\u6b21\u306e\u3088\u3046\u306a\u8b66\u544a\u304c\u8868\u793a\u3055\u308c\u308b\u3053\u3068\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<blockquote><p>Ignoring invalid timezone passed to Connection: Asia\/Tokyo. This is currently a warning, but in future versions of MySQL2, an error will be thrown if you pass an invalid configuration option to a Connection<\/p><\/blockquote>\n<p>\uff08\u8d85\u610f\u8a33\uff09\u30bf\u30a4\u30e0\u30be\u30fc\u30f3\u304c\u6b63\u3057\u304f\u306a\u3044\u304b\u3089\u7121\u8996\u3059\u308b\u306d\u3002\u3053\u308c\u3001\u4eca\u306f\u8b66\u544a\u3060\u3051\u3069\u5c06\u6765\u306f\u30a8\u30e9\u30fc\u306b\u306a\u308b\u304b\u3089\uff08\u6c17\u3092\u3064\u3051\u3066\u301c\ud83d\ude0f\uff09<\/p>\n<h2>\u5404SQL\u306e\u95a2\u6570\u3092\u4f7f\u3046<\/h2>\n<p>\u4f8b\u3048\u3070\u3001MySQL\u306b\u306f<code>ISNULL()<\/code>\u3068\u3044\u3046\u95a2\u6570\u304c\u3042\u308a\u307e\u3059\u304c\u3001\u3053\u306e\u95a2\u6570\u3092\u4f7f\u3063\u3066\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u9806\u3067\u4e26\u3079\u66ff\u3048\u308b\u5834\u5408\u3067\u3059\u3002<\/p>\n<ol>\n<li>\u30c7\u30fc\u30bf\u304cNULL\u306e\u3082\u306e\uff08\u304c\u5148\uff09<\/li>\n<li>NULL\u4ee5\u5916\u306e\u3082\u306e\uff08\u304c\u5f8c\uff09<\/li>\n<\/ol>\n<pre>order: [\r\n    <strong>[sequelize.fn('isnull', sequelize.col('checkedAt')), 'DESC'],<\/strong>\r\n    ['checkedAt', 'ASC']\r\n],<\/pre>\n<p>\u203b <code>sequelize<\/code>\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u306b\u3064\u3044\u3066\u306f\u3001<a href=\"#use_instance\">Sequelize\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u53d6\u5f97\u3059\u308b<\/a>\u3092\u3054\u89a7\u304f\u3060\u3055\u3044\u3002<\/p>\n<h2>\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u30e1\u30bd\u30c3\u30c9\u3092\u8ffd\u52a0\u3059\u308b<\/h2>\n<p>\u4f8b\u3048\u3070\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306bDB\u304b\u3089\u53d6\u5f97\u3057\u305f\u30c7\u30fc\u30bf\u304b\u3089\u72ec\u81ea\u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u3044\u305f\u3044\u5834\u5408\u3067\u3059\u3002<\/p>\n<pre>User.findByPk(1).then(user =&gt; {\r\n\r\n  const password = 'xxxxx';\r\n\r\n  if(<strong>user.isValidPassword(password)<\/strong>) {\r\n\r\n    console.log('\u6b63\u3057\u3044\u30d1\u30b9\u30ef\u30fc\u30c9\u3067\u3059\uff01');\r\n\r\n  }\r\n\r\n});<\/pre>\n<p>\u3053\u306e\u5834\u5408\u306f\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u30e2\u30c7\u30eb\u306e\u4e2d\u306b\u72ec\u81ea\u306e\u30e1\u30bd\u30c3\u30c9\u3092\u8ffd\u52a0\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<pre>'use strict';\r\n\r\nconst bcrypt = require('bcrypt');\r\nmodule.exports = (sequelize, DataTypes) =&gt; {\r\n  const User = sequelize.define('User', {\r\n    name: DataTypes.STRING,\r\n    email: DataTypes.STRING,\r\n    emailVerifiedAt: DataTypes.DATE,\r\n    password: DataTypes.STRING,\r\n    rememberToken: DataTypes.STRING\r\n  }, {});\r\n  User.associate = function(models) {\r\n    \/\/ associations can be defined here\r\n  };\r\n  <strong>User.prototype.isValidPassword = function(password){\r\n\r\n    if(!password) return false;\r\n    return bcrypt.compareSync(password, this.password)\r\n\r\n  };<\/strong>\r\n  return User;\r\n};\r\n<\/pre>\n<h1>\u304a\u308f\u308a\u306b<\/h1>\n<p>\u3068\u3044\u3046\u3053\u3068\u3067\u3001\u4eca\u56de\u306f\u300c<strong>Sequelize<\/strong>\u300d\u306e\u30e2\u30c7\u30eb\u3092\u3069\u306e\u3088\u3046\u306b\u4f7f\u3046\u304b\u3092\u5b9f\u4f8b\u3067\u3054\u7d39\u4ecb\u3057\u307e\u3057\u305f\u3002<\/p>\n<p>\u5229\u7528\u3067\u304d\u308b\u3053\u3068\u304c\u591a\u3044\u306e\u3067\u5c11\u3057\u9577\u3044\u8a18\u4e8b\u306b\u306a\u3063\u3066\u3057\u307e\u3044\u307e\u3057\u305f\u304c\u3001\u30d6\u30c3\u30af\u30de\u30fc\u30af\u3057\u3066\u30c1\u30fc\u30c8\u30b7\u30fc\u30c8\uff08\u30ab\u30f3\u30da\uff09\u3068\u3057\u3066\u4f7f\u3063\u3066\u3044\u305f\u3060\u3051\u308b\u3068\u5b09\u3057\u3044\u3067\u3059\u3002<\/p>\n<p>\u306a\u304a\u3001<code>Laravel<\/code>\u597d\u304d\u306a\u79c1\u306e\u96d1\u611f\u3068\u3057\u3066\u306f\u3001<\/p>\n<p><strong>\u300c\uff08\u3069\u3061\u3089\u304c\u5f8c\u304b\u5148\u304b\u3067\u306f\u306a\u304f\uff09\u4f3c\u3066\u3044\u308b\u306e\u3067\u958b\u767a\u3057\u3084\u3059\u3044\uff01\u300d<\/strong><\/p>\n<p>\u3067\u3057\u305f\u3002<\/p>\n<p>\u4f8b\u3048\u3070\u3001<code>Setter<\/code>\u3084<code>Getter<\/code>\u306f\u305d\u306e\u307e\u307e<code>Accessor<\/code>\u3068<code>Mutator<\/code>\u3067\u3059\u3057\u3001<code>createdAt<\/code>\u3084<code>updatedAt<\/code>\u3001<code>deletedAt<\/code>\u3068\u3044\u3046\u30d5\u30a3\u30fc\u30eb\u30c9\u540d\u3082\u307b\u307c\u540c\u3058\u3002<code>hasOne<\/code>, <code>hasMany<\/code>\u306a\u3069\u306e\u8a2d\u5b9a\u3082\u4f3c\u3066\u3044\u307e\u3059\u3088\u306d\u3002<\/p>\n<p>\u305d\u3093\u306a\u4e2d\u3001\uff11\u70b9<code>Laravel<\/code>\u3068\u9055\u3046\u306a\u3068\u611f\u3058\u305f\u306e\u306f<code>WHERE<\/code>\u53e5\u3084<code>ORDER<\/code>\u53e5\u306e\u6307\u5b9a\u65b9\u6cd5\u3067\u3059\u3002<code>Laravel<\/code>\u306f\u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u3063\u3066\u8a2d\u5b9a\u3057\u3066\u3044\u304d\u307e\u3059\u304c\u3001<code>Sequelize<\/code>\u3067\u306f\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3068\u3057\u3066\u4e00\u6c17\u306b\u30bb\u30c3\u30c8\u3059\u308b\u5f62\u306b\u306a\u3063\u3066\u3044\u307e\u3059\u3002\uff08\u3053\u3053\u306f\u3001\u500b\u4eba\u7684\u306b<code>Laravel<\/code>\u306e\u65b9\u304c\u4f7f\u3044\u3084\u3059\u3044\u304b\u306a\u3001\u3068\u3044\u3046\u611f\u3058\u3067\u3059\u3002\u5143\u3005\u3053\u306e\u5f62\u5f0f\u304c\u5acc\u3067<code>CakePHP<\/code>\u3092\u4f7f\u3046\u306e\u3092\u3084\u3081\u307e\u3057\u305f\u306e\u3067\u30fb\u30fb\u30fb\ud83d\ude05\uff09<\/p>\n<p>\u4f8b\u3048\u3070\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u5f62\u304c\u3044\u3044\u306a\u30fb\u30fb\u30fb\u306a\u3093\u3066\u3002\uff08<code>Laravel<\/code>\u4f9d\u5b58\u75c7\u3057\u3087\u3046\u304b\ud83d\ude02\uff09<\/p>\n<pre>\/\/ \u3053\u306e\u4f8b\u306f\u6b63\u3057\u304f\u3042\u308a\u307e\u305b\u3093\uff01\u79c1\u306e\u5e0c\u671b\uff08\u5984\u60f3\uff1f\uff1f\uff09\u3067\u3059\r\n\r\nUser.findOne(<strong>query =&gt; {\r\n\r\n  query.where('id', 1);\r\n  query.orderBy('createdAt', 'ASC');\r\n  query.hasWhere(UserDetail, q =&gt; {\r\n    q.where('age', '&gt;', 20);\r\n  });\r\n\r\n}<\/strong>).then(user =&gt; {});<\/pre>\n<p>\u305f\u3060\u3001\u3084\u306f\u308a\u4eba\u6c17\u306e\u30d1\u30c3\u30b1\u30fc\u30b8\u3060\u3051\u3042\u3063\u3066\u3001\u3044\u307e\u306e\u3068\u3053\u308d\u300c<strong>Node.js<\/strong>\u300d\u3067DB\u5468\u308a\u306e\u958b\u767a\u3092\u3059\u308b\u306a\u3089\u300c<strong>Sequelize<\/strong>\u300d\u306f\u5916\u305b\u306a\u3044\u3060\u308d\u3046\u3001\u3068\u3044\u3046\u8003\u3048\u3067\u3059\u3002<\/p>\n<p>\u305c\u3072\u7686\u3055\u3093\u3082\u3053\u306e\u4fbf\u5229\u3055\u3092\u4f53\u9a13\u3057\u3066\u307f\u3066\u304f\u3060\u3055\u3044\u306d\u3002<\/p>\n<p>\u3067\u306f\u3067\u306f\u301c\u266a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-3467 aligncenter\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2019\/04\/no_9.png\" alt=\"\" width=\"216\" height=\"320\" srcset=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2019\/04\/no_9.png 216w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2019\/04\/no_9-203x300.png 203w\" sizes=\"auto, (max-width: 216px) 100vw, 216px\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u3055\u3066\u3055\u3066\u3001\u524d\u56de\u306e\u8a18\u4e8b\u300cNode.js\u306bDB\u30de\u30a4\u30b0\u30ec\u30fc\u30b7\u30e7\u30f3\u3001Seed\u3001\u30e2\u30c7\u30eb\u3092\u7528\u610f\u3059\u308b\u300d\u3067\u306f\u3001\u300cSequelize\u300d\u3068\u3044\u3046\u30d1\u30c3\u30b1\u30fc\u30b8\u3092\u4f7f\u3063\u3066\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u5468\u308a\u306e\u958b &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/blog.capilano-fw.com\/?p=5582\" class=\"more-link\"><span class=\"screen-reader-text\">&#8220;\u4fdd\u5b58\u7248\uff01\u300csequelize\u300d\u30e2\u30c7\u30eb\u306e\u4f7f\u3044\u65b9\u5b9f\u4f8b\u30fb\u516859\u4ef6&#8221; \u306e<\/span>\u7d9a\u304d\u3092\u8aad\u3080<\/a><\/p>\n","protected":false},"author":1,"featured_media":5616,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[201,203],"tags":[60],"class_list":["post-5582","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\/5582","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=5582"}],"version-history":[{"count":46,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/posts\/5582\/revisions"}],"predecessor-version":[{"id":5974,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/posts\/5582\/revisions\/5974"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/media\/5616"}],"wp:attachment":[{"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5582"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5582"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5582"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}