{"id":200611,"date":"2025-05-06T12:53:24","date_gmt":"2025-05-06T04:53:24","guid":{"rendered":"https:\/\/server.hk\/cnblog\/200611\/"},"modified":"2025-05-06T12:53:24","modified_gmt":"2025-05-06T04:53:24","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8-sequelize-%e7%9a%84-op-%e5%af%b9%e8%b1%a1%e8%bf%9b%e8%a1%8c%e7%81%b5%e6%b4%bb%e7%9a%84%e7%bb%84%e5%90%88%e6%9f%a5%e8%af%a2%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/200611\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528 Sequelize \u7684 Op \u5bf9\u8c61\u8fdb\u884c\u7075\u6d3b\u7684\u7ec4\u5408\u67e5\u8be2\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u5982\u4f55\u4f7f\u7528 Sequelize \u7684 Op \u5bf9\u8c61\u8fdb\u884c\u7075\u6d3b\u7684\u7ec4\u5408\u67e5\u8be2\uff1f<\/h1>\n<p>\u73cd\u60dc\u65f6\u95f4\uff0c\u52e4\u594b\u5b66\u4e60\uff01\u4eca\u5929\u7ed9\u5927\u5bb6\u5e26\u6765\uff0c\u6b63\u6587\u5185\u5bb9\u4e3b\u8981\u6d89\u53ca\u5230\u7b49\u7b49\uff0c\u5982\u679c\u4f60\u6b63\u5728\u5b66\u4e60\uff0c\u6216\u8005\u662f\u5bf9\u6709\u7591\u95ee\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u6211\uff01\u540e\u9762\u6211\u4f1a\u6301\u7eed\u66f4\u65b0\u76f8\u5173\u5185\u5bb9\u7684\uff0c\u5e0c\u671b\u90fd\u80fd\u5e2e\u5230\u6b63\u5728\u5b66\u4e60\u7684\u5927\u5bb6\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241109\/1731153111672f4cd7101cf.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u4ece\u7ec4\u5408\u67e5\u8be2\u5230 sequelize \u5999\u7528<\/strong><\/p>\n<p>\u5728\u8fdb\u884c\u6570\u636e\u5e93\u67e5\u8be2\u65f6\uff0c\u7ec4\u5408\u67e5\u8be2\u7684\u9700\u6c42\u7ecf\u5e38\u51fa\u73b0\u3002\u5728 sequelize orm \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 op \u7c7b\u6765\u6ee1\u8db3\u4e0d\u540c\u7684\u67e5\u8be2\u6761\u4ef6\u3002<\/p>\n<p><strong>\u793a\u4f8b\u67e5\u8be2<\/strong><\/p>\n<p>\u6839\u636e\u5b57\u6bb5\u7684\u4efb\u610f\u7ec4\u5408\u6765\u67e5\u8be2\uff0c\u53ef\u4ee5\u91c7\u7528\u4ee5\u4e0b\u6b65\u9aa4\uff1a<\/p>\n<ol>\n<li>\u5c06\u67e5\u8be2\u6761\u4ef6\u5206\u89e3\u4e3a\u5b57\u6bb5\u3001\u503c\u548c\u64cd\u4f5c\uff08\u5982 and\u3001or\u3001like\uff09\u3002<\/li>\n<li>\u904d\u5386\u6761\u4ef6\u6570\u7ec4\u5e76\u6784\u5efa sequelize op \u5bf9\u8c61\u3002<\/li>\n<\/ol>\n<p><strong>\u793a\u4f8b\u4ee3\u7801 \u4f18\u5316<\/strong><\/p>\n<p>\u7136\u800c\uff0c\u4e0a\u8ff0\u4ee3\u7801\u5b58\u5728\u4e00\u4e9b\u590d\u6742\u6027\u548c\u9519\u8bef\u3002\u8fd9\u91cc\u6709\u4e00\u4e2a\u4f18\u5316\u8fc7\u7684\u7248\u672c\uff1a<\/p>\n<pre>let where = {};\nfor (let i = 0; i &lt; wherearr.length; i++) {\n  const item = wherearr[i];\n  if (item[0] === and || item[0] === or || item[0] === not) {\n    if (item[0] === and) {\n      if (item[3] === equality) {\n        where[item[1]] = item[2];\n      } else if (item[3] === like) {\n        where[item[1]] = { [op.like]: `%${item[2]}%` };\n      }\n    } else if (item[0] === or) {\n      if (item[3] === equality) {\n        where[op.or] = [\n          { [item[1]]: item[2] },\n          { [item[1]]: item[2] },\n        ];\n      } else if (item[3] === like) {\n        where[op.or] = [\n          { [item[1]]: { [op.like]: `%${item[2]}%`} },\n          { [item[1]]: { [op.like]: `%${item[2]}%`} },\n        ];\n      }\n    }\n  } else {\n    if (item[2] === equality) {\n      where[item[0]] = item[1];\n    } else if (item[2] === like) {\n      where[item[0]] = { [op.like]: `%${item[1]}%` };\n    }\n  }\n}<\/pre>\n<p><strong>\u6f14\u793a<\/strong><\/p>\n<pre>const whereObject = {\n  AND: ['field1', 'value1', 'equality'],\n  OR: ['field2', 'value2', 'equality'],\n  field3: 'value3',\n};\n\nconst where = buildWhereObject(whereObject);\nconst query = {\n  where: where,\n};\n\n\/\/ \u6267\u884c\u67e5\u8be2\nconst result = await Model.findAll(query);<\/pre>\n<p><strong>\u7ed3\u8bba<\/strong><\/p>\n<p>\u901a\u8fc7\u7ed3\u5408 sequelize \u7684 op \u5bf9\u8c61\u548c\u81ea\u5b9a\u4e49\u7684\u904d\u5386\u903b\u8f91\uff0c\u53ef\u4ee5\u8f7b\u677e\u6784\u5efa\u590d\u6742\u7684\u7ec4\u5408\u67e5\u8be2\u3002<\/p>\n<p>\u4eca\u5929\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u5e0c\u671b\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff1b\u5173\u4e8e\u6570\u636e\u5e93\u7684\u6280\u672f\u77e5\u8bc6\u6211\u4eec\u4f1a\u4e00\u70b9\u70b9\u6df1\u5165\u4ecb\u7ecd\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u4e3b\u673a\u5b9d\u8d1d\u516c\u4f17\u53f7\uff0c\u4e00\u8d77\u5b66\u4e60\u7f16\u7a0b~<\/p>\n<dl>\n<dt>\n <\/dt>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u4f7f\u7528 Sequelize \u7684&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-200611","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/200611","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=200611"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/200611\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=200611"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=200611"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=200611"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}