{"id":15700883,"url":"https://github.com/evershopcommerce/mysql-query-builder","last_synced_at":"2025-05-12T15:20:15.878Z","repository":{"id":43934346,"uuid":"365724393","full_name":"evershopcommerce/mysql-query-builder","owner":"evershopcommerce","description":"A MySQL query builder for Nodejs","archived":false,"fork":false,"pushed_at":"2023-03-02T15:25:39.000Z","size":211,"stargazers_count":8,"open_issues_count":2,"forks_count":5,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-31T23:38:35.612Z","etag":null,"topics":["mysql","nodejs-mysql","query-builder"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","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/evershopcommerce.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-05-09T10:22:34.000Z","updated_at":"2025-02-03T07:34:22.000Z","dependencies_parsed_at":"2024-06-21T17:51:03.300Z","dependency_job_id":"aa278e3c-1b23-46b0-aebf-600de870f655","html_url":"https://github.com/evershopcommerce/mysql-query-builder","commit_stats":{"total_commits":38,"total_committers":8,"mean_commits":4.75,"dds":0.6578947368421053,"last_synced_commit":"dda0c81de1b42206e67db2062f758ca4aae8d96d"},"previous_names":["kt65/mysql-query-builder"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/evershopcommerce%2Fmysql-query-builder","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/evershopcommerce%2Fmysql-query-builder/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/evershopcommerce%2Fmysql-query-builder/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/evershopcommerce%2Fmysql-query-builder/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/evershopcommerce","download_url":"https://codeload.github.com/evershopcommerce/mysql-query-builder/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248543837,"owners_count":21121838,"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","nodejs-mysql","query-builder"],"created_at":"2024-10-03T19:55:38.628Z","updated_at":"2025-04-20T13:31:07.054Z","avatar_url":"https://github.com/evershopcommerce.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# MySQL query builder for Node\n\n[![Build](https://github.com/kt65/mysql-query-builder/actions/workflows/build.yml/badge.svg)](https://github.com/kt65/mysql-query-builder/actions/workflows/build.yml)\n[![npm version](https://badge.fury.io/js/%40evershop%2Fmysql-query-builder.svg)](https://badge.fury.io/js/%40evershop%2Fmysql-query-builder)\n[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)\n\nA MySQL query builder for NodeJS. \n\n## Installation\n\n```javascript\nnpm install @evershop/mysql-query-builder\n```\n\n## Usage guide\nIt implements async/await.\n### Simple select\n```javascript\n// var mysql = require('mysql');\n// var connection = mysql.createConnection({\n//   host     : 'localhost',\n//   user     : 'me',\n//   password : 'secret',\n//   database : 'my_db'\n// });\n\n// connection.connect();\n// connection.query('SELECT * FROM product WHERE product_id \u003e ?', [1], function (error, results, fields) {\n//   if (error) throw error;\n//    console.log('The solution is: ', results[0].solution);\n// });\n```\n```javascript\nconst {select} = require('@evershop/mysql-query-builder')\n\nconst products = await select(\"*\")\n.from(\"product\")\n.where(\"product_id\", \"\u003e\", 1)\n.execute(pool);\n```\n### More complex where\n```javascript\n// var mysql = require('mysql');\n// var mysql = require('mysql');\n// var connection = mysql.createConnection({\n//   host     : 'localhost',\n//   user     : 'me',\n//   password : 'secret',\n//   database : 'my_db'\n// });\n\n// connection.query('SELECT * FROM product WHERE product_id \u003e ? AND sku LIKE ?', [1, \"sku\"], function (error, results, fields) {\n//   if (error) throw error;\n//    console.log('The solution is: ', results[0].solution);\n// });\n```\n```javascript\nconst {select} = require('@evershop/mysql-query-builder')\n\nconst products = await select(\"*\")\n.from(\"product\")\n.where(\"product_id\", \"\u003e\", 1)\n.and(\"sku\", \"LIKE\", \"sku\")\n.execute(pool);\n```\n### Event more complex where\n```javascript\n// var mysql = require('mysql');\n// var mysql = require('mysql');\n// var connection = mysql.createConnection({\n//   host     : 'localhost',\n//   user     : 'me',\n//   password : 'secret',\n//   database : 'my_db'\n// });\n\n// connection.query('SELECT * FROM product WHERE (product_id \u003e ? AND sku LIKE ?) OR price \u003e ?', [1, \"sku\", 100], function (error, results, fields) {\n//   if (error) throw error;\n//    console.log('The solution is: ', results[0].solution);\n// });\n```\n```javascript\nconst {select} = require('@evershop/mysql-query-builder')\n\nconst query = select(\"*\").from(\"product\");\nquery.where(\"product_id\", \"\u003e\", 1).and(\"sku\", \"LIKE\", \"sku\");\nquery.orWhere(\"price\", \"\u003e\", 100);\n\nconst products = await query.execute(pool);\n```\n\n### Join table\n```javascript\n// var mysql = require('mysql');\n// var mysql = require('mysql');\n// var connection = mysql.createConnection({\n//   host     : 'localhost',\n//   user     : 'me',\n//   password : 'secret',\n//   database : 'my_db'\n// });\n\n// connection.query('SELECT * FROM product LEFT JOIN price ON product.id = price.id WHERE (product_id \u003e ? AND sku LIKE ?) OR price \u003e ?', [1, \"sku\", 100], function (error, results, fields) {\n//   if (error) throw error;\n//    console.log('The solution is: ', results[0].solution);\n// });\n```\n```javascript\nconst {select} = require('@evershop/mysql-query-builder')\n\nconst query = select(\"*\").from(\"product\");\nquery.leftJoin('price').on('product.`product_id`', '=', 'price.`product_id`');\nquery.where(\"product_id\", \"\u003e\", 1).and(\"sku\", \"LIKE\", \"sku\");\nquery.andWhere(\"price\", \"\u003e\", 100);\n\nconst products = await query.execute(pool);\n```\n\n### Insert\u0026update\n\u003ctable\u003e\n\u003ctr\u003e\n\u003cth\u003e user_id \u003c/th\u003e\n\u003cth\u003e name \u003c/th\u003e\n\u003cth\u003e email \u003c/th\u003e\n\u003cth\u003e phone \u003c/th\u003e\n\u003cth\u003e status \u003c/th\u003e\n\u003c/tr\u003e\n\u003ctr\u003e\n\u003ctd\u003e\n  1\n\u003c/td\u003e\n\u003ctd\u003e\n  David\n\u003c/td\u003e\n\u003ctd\u003e\n  emai@email.com\n\u003c/td\u003e\n\u003ctd\u003e\n  123456\n\u003c/td\u003e\n\u003ctd\u003e\n  1\n\u003c/td\u003e\n\u003c/tr\u003e\n\u003c/table\u003e\n\n```javascript\n// var mysql = require('mysql');\n// var mysql = require('mysql');\n// var connection = mysql.createConnection({\n//   host     : 'localhost',\n//   user     : 'me',\n//   password : 'secret',\n//   database : 'my_db'\n// });\n\n//  connection.query('INSERT INTO user SET name=?, email=?, phone=?, status=?', [\"David\", \"email@email.com\", \"123456\", 1], function (error, results, fields) {\n//    if (error) {\n//      return connection.rollback(function() {\n//        throw error;\n//      });\n//    }\n//  });\n```\n```javascript\nconst {insert} = require('@evershop/mysql-query-builder')\n\nconst query = insert(\"user\")\n.given({name: \"David\", email: \"email@email.com\", \"phone\": \"123456\", status: 1, notExistedColumn: \"This will not be a part of the query\"});\nawait query.execute(pool);\n```\n```javascript\nconst {update} = require('@evershop/mysql-query-builder')\n\nconst query = update(\"user\")\n.given({name: \"David\", email: \"email@email.com\", \"phone\": \"123456\", status: 1, notExistedColumn: \"This will not be a part of query\"})\n.where(\"user_id\", \"=\", 1);\nawait query.execute(pool);\n```\n### Working with transaction\n\n```javascript\nconst {insert, getConnection, startTransaction, commit, rollback} = require('@evershop/mysql-query-builder');\n\nconst pool = mysql.createPool({\n    host: \"localhost\",\n    user: \"root\",\n    password: \"123456\",\n    database: \"test\",\n    dateStrings: true\n});\n\n// Create a connection from the pool\nconst connection = await getConnection(pool);\n\n// Start a transaction\nawait startTransaction(connection);\ntry {\n  await insert(\"user\")\n        .given({name: \"David\", email: \"email@email.com\", \"phone\": \"123456\", status: 1, notExistedColumn: \"This will not be a part of the query\"})\n        .execute(connection);\n  await commit(connection);\n} catch(e) {\n  await rollback(connection);\n}\n```\n## Security\n\nAll user provided data will be escaped. Please check [this](https://github.com/mysqljs/mysql#escaping-query-values) for more detail. ","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fevershopcommerce%2Fmysql-query-builder","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fevershopcommerce%2Fmysql-query-builder","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fevershopcommerce%2Fmysql-query-builder/lists"}