{"id":18083443,"url":"https://github.com/leizongmin/leizm-sql","last_synced_at":"2025-04-12T17:13:51.562Z","repository":{"id":55627589,"uuid":"155519793","full_name":"leizongmin/leizm-sql","owner":"leizongmin","description":"SQL查询构造器","archived":false,"fork":false,"pushed_at":"2020-12-17T06:07:31.000Z","size":97,"stargazers_count":5,"open_issues_count":2,"forks_count":3,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-12T17:13:46.028Z","etag":null,"topics":["mysql","node","npm","query-builder","sql"],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/leizongmin.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-10-31T07:59:25.000Z","updated_at":"2024-09-06T08:23:43.000Z","dependencies_parsed_at":"2022-08-15T04:50:36.859Z","dependency_job_id":null,"html_url":"https://github.com/leizongmin/leizm-sql","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leizongmin%2Fleizm-sql","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leizongmin%2Fleizm-sql/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leizongmin%2Fleizm-sql/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leizongmin%2Fleizm-sql/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/leizongmin","download_url":"https://codeload.github.com/leizongmin/leizm-sql/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248602312,"owners_count":21131616,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["mysql","node","npm","query-builder","sql"],"created_at":"2024-10-31T14:07:59.026Z","updated_at":"2025-04-12T17:13:51.541Z","avatar_url":"https://github.com/leizongmin.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![NPM version][npm-image]][npm-url]\n[![build status][travis-image]][travis-url]\n[![Test coverage][coveralls-image]][coveralls-url]\n[![David deps][david-image]][david-url]\n[![node version][node-image]][node-url]\n[![npm download][download-image]][download-url]\n[![npm license][license-image]][download-url]\n\n[npm-image]: https://img.shields.io/npm/v/@leizm/sql.svg?style=flat-square\n[npm-url]: https://npmjs.org/package/@leizm/sql\n[travis-image]: https://img.shields.io/travis/leizongmin/leizm-sql.svg?style=flat-square\n[travis-url]: https://travis-ci.org/leizongmin/leizm-sql\n[coveralls-image]: https://img.shields.io/coveralls/leizongmin/leizm-sql.svg?style=flat-square\n[coveralls-url]: https://coveralls.io/r/leizongmin/leizm-sql?branch=master\n[david-image]: https://img.shields.io/david/leizongmin/leizm-sql.svg?style=flat-square\n[david-url]: https://david-dm.org/leizongmin/leizm-sql\n[node-image]: https://img.shields.io/badge/node.js-%3E=_6.0-green.svg?style=flat-square\n[node-url]: http://nodejs.org/download/\n[download-image]: https://img.shields.io/npm/dm/@leizm/sql.svg?style=flat-square\n[download-url]: https://npmjs.org/package/@leizm/sql\n[license-image]: https://img.shields.io/npm/l/@leizm/sql.svg\n\n# @leizm/sql\n\nSQL 查询构造器\n\n## 安装\n\n```bash\nnpm install @leizm/sql --save\n```\n\n## 使用方法\n\n```typescript\nimport { table, expr, query } from \"@leizm/sql\";\nimport * as mysql from \"mysql\";\n\n// 普通查询\ntable(\"test\")\n  .select(\"a\", \"b\")\n  .where({ a: 1 })\n  .and(\"b=?\", [2])\n  .orderBy(\"b DESC\")\n  .offset(10)\n  .limit(5)\n  .build();\n// SELECT `a`, `b` FROM `test` WHERE `a`=1 AND `b`=2 ORDER BY b DESC LIMIT 10,5\n\n// 连表查询\ntable(\"hello\")\n  .select(\"*\")\n  .as(\"A\")\n  .leftJoin(\"world\")\n  .as(\"B\")\n  .on(\"A.id=B.id\")\n  .where(\"1\")\n  .and(\"2\")\n  .offset(2)\n  .limit(3)\n  .build();\n// SELECT `A`.*, `B`.* FROM `hello` AS `A` LEFT JOIN `world` AS `B` ON A.id=B.id WHERE 1 AND 2 LIMIT 2,3\n\n// 插入数据\ntable(\"test1\")\n  .insert({\n    a: 123,\n    b: 456,\n  })\n  .build();\n// INSERT INTO `test1` (`a`, `b`) VALUES (123, 456);\n\n// 批量插入数据\ntable(\"test1\")\n  .insert([\n    {\n      a: 123,\n      b: 456,\n    },\n    {\n      a: 789,\n      b: 110,\n    },\n  ])\n  .build();\n// INSERT INTO `test1` (`a`, `b`) VALUES (123, 456),\n// (789, 110)\");\n\n// 插入数据，忽略已经存在记录\ntable(\"test1\")\n  .insert({ a: 123, b: 456 })\n  .ignore()\n  .build();\n// INSERT IGNORE INTO `test1` (`a`, `b`) VALUES (123, 456)\n\n// 插入数据，如果记录已经存在则改为更新\ntable(\"test1\")\n  .insert({ a: 123, b: 456 })\n  .onDuplicateKeyUpdate()\n  .set({ a: \"xxx\" })\n  .build();\n// INSERT INTO `test1` (`a`, `b`) VALUES (123, 456) ON DUPLICATE KEY UPDATE `a`='xxx'\n\n// 替换数据\ntable(\"test1\")\n  .replace({\n    a: 123,\n    b: 456,\n  })\n  .build();\n// REPLACE INTO `test1` (`a`, `b`) VALUES (123, 456);\n\n// 更新数据\ntable(\"test1\")\n  .update({\n    a: 123,\n    b: 456,\n  })\n  .where({\n    b: 777,\n  })\n  .limit(12)\n  .build();\n// UPDATE `test1` SET `a`=123, `b`=456 WHERE `b`=777 LIMIT 12\n\n// 删除数据\ntable(\"test1\")\n  .delete()\n  .where({\n    b: 777,\n  })\n  .limit(12)\n  .build();\n// DELETE FROM `test1` WHERE `b`=777 LIMIT 12\n\n// 子查询\ntable(\"test1\")\n  .select(\"*\")\n  .where(\"a=? AND b IN ???\", [\n    123,\n    table(\"test2\")\n      .select(\"id\")\n      .where({ id: { $lt: 10 } })\n      .limit(100),\n  ])\n  .build();\n// SELECT * FROM `test1` WHERE a=123 AND b IN (SELECT `id` FROM `test2` WHERE `id`\u003c10 LIMIT 100)\n\n// 查询克隆\nconst q = table(\"test1\")\n  .select(\"*\")\n  .where({ a: 123 });\nq.clone()\n  .where({ b: 456 })\n  .offset(10)\n  .limit(20)\n  .build();\n// SELECT * FROM `test1` WHERE `a`=123 AND `b`=456 LIMIT 10,20\nq.clone()\n  .where({ b: 789, c: 666 })\n  .orderBy(\"a DESC\")\n  .build();\n// SELECT * FROM `test1` WHERE `a`=123 AND `b`=789 AND `c`=666 ORDER BY a DESC\n\n// 条件表达式\ntable(\"test\")\n  .select(\"*\")\n  .where(\n    expr()\n      .and(\"a=?\", [123])\n      .or({ b: 456 })\n      .and({ c: { $in: [789] } })\n      .or(\"d=:d\", { d: 666 }),\n  )\n  .build();\n// SELECT * FROM `test` WHERE (a=123 OR `b`=456 AND `c` IN (789) OR d=666)\n\n// 快捷执行MySQL查询\nconst conn = mysql.createConnection({});\nquery(\n  conn,\n  table(\"test\")\n    .select(\"*\")\n    .limit(1),\n)\n  .then(ret =\u003e console.log(ret))\n  .catch(err =\u003e console.error(err));\n```\n\n## License\n\n```text\nMIT License\n\nCopyright (c) 2018 Zongmin Lei \u003cleizongmin@gmail.com\u003e\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fleizongmin%2Fleizm-sql","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fleizongmin%2Fleizm-sql","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fleizongmin%2Fleizm-sql/lists"}