{"id":15658563,"url":"https://github.com/tehshrike/sql-concat","last_synced_at":"2026-03-10T10:06:30.785Z","repository":{"id":30357379,"uuid":"33909852","full_name":"TehShrike/sql-concat","owner":"TehShrike","description":"A MySQL query builder","archived":false,"fork":false,"pushed_at":"2025-05-28T19:05:27.000Z","size":160,"stargazers_count":22,"open_issues_count":2,"forks_count":4,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-09-05T07:54:45.062Z","etag":null,"topics":["javascript","mysql","query","query-builder","sql"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/TehShrike.png","metadata":{"files":{"readme":"readme.md","changelog":"changelog.md","contributing":null,"funding":null,"license":null,"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,"zenodo":null}},"created_at":"2015-04-14T04:00:39.000Z","updated_at":"2025-05-28T19:05:29.000Z","dependencies_parsed_at":"2024-06-19T02:42:33.909Z","dependency_job_id":"354e8bf2-24dc-476b-bd7a-0e9e24fc7465","html_url":"https://github.com/TehShrike/sql-concat","commit_stats":null,"previous_names":[],"tags_count":24,"template":false,"template_full_name":null,"purl":"pkg:github/TehShrike/sql-concat","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TehShrike%2Fsql-concat","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TehShrike%2Fsql-concat/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TehShrike%2Fsql-concat/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TehShrike%2Fsql-concat/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/TehShrike","download_url":"https://codeload.github.com/TehShrike/sql-concat/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TehShrike%2Fsql-concat/sbom","scorecard":{"id":138869,"data":{"date":"2025-08-11","repo":{"name":"github.com/TehShrike/sql-concat","commit":"c64af0006f643283033efc2fbf060672bb67176c"},"scorecard":{"version":"v5.2.1-40-gf6ed084d","commit":"f6ed084d17c9236477efd66e5b258b9d4cc7b389"},"score":2.9,"checks":[{"name":"Binary-Artifacts","score":10,"reason":"no binaries found in the repo","details":null,"documentation":{"short":"Determines if the project has generated executable (binary) artifacts in the source repository.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#binary-artifacts"}},{"name":"Packaging","score":-1,"reason":"packaging workflow not detected","details":["Warn: no GitHub/GitLab publishing workflow detected."],"documentation":{"short":"Determines if the project is published as a package that others can easily download, install, easily update, and uninstall.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#packaging"}},{"name":"Token-Permissions","score":-1,"reason":"No tokens found","details":null,"documentation":{"short":"Determines if the project's workflows follow the principle of least privilege.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#token-permissions"}},{"name":"Code-Review","score":1,"reason":"Found 2/18 approved changesets -- score normalized to 1","details":null,"documentation":{"short":"Determines if the project requires human code review before pull requests (aka merge requests) are merged.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#code-review"}},{"name":"Dangerous-Workflow","score":-1,"reason":"no workflows found","details":null,"documentation":{"short":"Determines if the project's GitHub Action workflows avoid dangerous patterns.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#dangerous-workflow"}},{"name":"Pinned-Dependencies","score":-1,"reason":"no dependencies found","details":null,"documentation":{"short":"Determines if the project has declared and pinned the dependencies of its build process.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#pinned-dependencies"}},{"name":"Maintained","score":1,"reason":"2 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 1","details":null,"documentation":{"short":"Determines if the project is \"actively maintained\".","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#maintained"}},{"name":"CII-Best-Practices","score":0,"reason":"no effort to earn an OpenSSF best practices badge detected","details":null,"documentation":{"short":"Determines if the project has an OpenSSF (formerly CII) Best Practices Badge.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#cii-best-practices"}},{"name":"Security-Policy","score":0,"reason":"security policy file not detected","details":["Warn: no security policy file detected","Warn: no security file to analyze","Warn: no security file to analyze","Warn: no security file to analyze"],"documentation":{"short":"Determines if the project has published a security policy.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#security-policy"}},{"name":"License","score":0,"reason":"license file not detected","details":["Warn: project does not have a license file"],"documentation":{"short":"Determines if the project has defined a license.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#license"}},{"name":"Fuzzing","score":0,"reason":"project is not fuzzed","details":["Warn: no fuzzer integrations found"],"documentation":{"short":"Determines if the project uses fuzzing.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#fuzzing"}},{"name":"Signed-Releases","score":-1,"reason":"no releases found","details":null,"documentation":{"short":"Determines if the project cryptographically signs release artifacts.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#signed-releases"}},{"name":"Vulnerabilities","score":10,"reason":"0 existing vulnerabilities detected","details":null,"documentation":{"short":"Determines if the project has open, known unfixed vulnerabilities.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#vulnerabilities"}},{"name":"Branch-Protection","score":0,"reason":"branch protection not enabled on development/release branches","details":["Warn: branch protection not enabled for branch 'master'"],"documentation":{"short":"Determines if the default and release branches are protected with GitHub's branch protection settings.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#branch-protection"}},{"name":"SAST","score":0,"reason":"SAST tool is not run on all commits -- score normalized to 0","details":["Warn: 0 commits out of 17 are checked with a SAST tool"],"documentation":{"short":"Determines if the project uses static code analysis.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#sast"}}]},"last_synced_at":"2025-08-16T07:25:20.953Z","repository_id":30357379,"created_at":"2025-08-16T07:25:20.953Z","updated_at":"2025-08-16T07:25:20.953Z"},"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":30329698,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-10T05:25:20.737Z","status":"ssl_error","status_checked_at":"2026-03-10T05:25:17.430Z","response_time":106,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["javascript","mysql","query","query-builder","sql"],"created_at":"2024-10-03T13:12:55.711Z","updated_at":"2026-03-10T10:06:30.764Z","avatar_url":"https://github.com/TehShrike.png","language":"JavaScript","readme":"# sql-concat\n\nA MySQL query builder.\n\n```node\nconst q = require('sql-concat')\n```\n\nThe only \"breaking\" change from 1.x to 2.x is that support for versions of node older than 6 was dropped.\n\n## Designed to...\n\n- Build queries programmatically\n- Allow simple combining of query parts and their associated parameters (as opposed to writing a long query string followed by a long array of parameter values)\n- Build queries for the [mysqljs/mysql](https://github.com/mysqljs/mysql) library (specifically, by expecting its [rules for query values](https://github.com/mysqljs/mysql#escaping-query-values) instead of MySQL's stored procedure parameters)\n\n## Features\n\n- Easily compose query parts - the query-builder object is immutable, so you can build up a base query and re-use it over and over again with small modifications (for example, with conditional where clauses or joins)\n- Not as overblown as [knex](http://knexjs.org/), and allows more freedom in using string literals within query chunks\n- Queries should look good when printed out (newlines between clauses, subqueries indented with tabs)\n\n## Looks like\n\n```node\nconst q = require('sql-concat')\n```\n\n\u003c!--js\nvar q = require('./')\n--\u003e\n\n```js\nconst minNumber = 0\nconst result = q.select('table1.some_boring_id, table2.something_interesting, mystery_table.surprise', q`LEAST(table1.whatever, ${minNumber}) AS whatever`)\n\t.from('table1')\n\t.join('table2', 'table1.some_boring_id = table2.id')\n\t.leftJoin('mystery_table', 'mystery_table.twister_reality = table2.probably_null_column')\n\t.where('table1.pants', 'fancy')\n\t.where('table1.britches', '\u003e', 99)\n\t.build()\n\nconst expectedQuery = 'SELECT table1.some_boring_id, table2.something_interesting, mystery_table.surprise, LEAST(table1.whatever, ?) AS whatever\\n'\n\t\t+ 'FROM table1\\n'\n\t\t+ 'JOIN table2 ON table1.some_boring_id = table2.id\\n'\n\t\t+ 'LEFT JOIN mystery_table ON mystery_table.twister_reality = table2.probably_null_column\\n'\n\t\t+ 'WHERE table1.pants = ? AND table1.britches \u003e ?'\n\nresult.sql // =\u003e expectedQuery\n\nresult.values // =\u003e [ 0, 'fancy', 99 ]\n\n```\n\n## A cooler example\n\nShowing off the composability/reusability of the query objects, plus some dynamic query building:\n\n```js\n\n// A partial query that we can just leave here to reuse later:\nconst MOST_RECENT_SALE = q.select('item_sale.item_id, MAX(item_sale.date) AS `date`')\n\t.from('item_sale')\n\t.groupBy('item_sale.item_id')\n\nfunction mostRecentSalePricesQuery(taxable, itemType) {\n\tconst subquery = MOST_RECENT_SALE.where('taxable', taxable)\n\n\tlet query = q.select('item.item_id, item.description, item.type, latest_sale.date AS latest_sale_date, latest_sale.price')\n\t\t.from('item')\n\t\t.join(subquery, 'latest_sale', 'latest_sale.item_id = item.item_id')\n\n\t// Dynamically add new clauses to the query as needed\n\tif (itemType) {\n\t\tquery = query.where('item.item_type', itemType)\n\t}\n\n\treturn query.build()\n}\n\n// Build those dynamic queries:\n\nconst taxableSpecialQuery = mostRecentSalePricesQuery(true, 'special')\n\nconst expectedTaxableSpecialQuery = ['SELECT item.item_id, item.description, item.type, latest_sale.date AS latest_sale_date, latest_sale.price',\n\t'FROM item',\n\t'JOIN (',\n\t'\\tSELECT item_sale.item_id, MAX(item_sale.date) AS `date`',\n\t'\\tFROM item_sale',\n\t'\\tWHERE taxable = ?',\n\t'\\tGROUP BY item_sale.item_id',\n\t') AS latest_sale ON latest_sale.item_id = item.item_id',\n\t'WHERE item.item_type = ?'].join('\\n')\n\ntaxableSpecialQuery.sql // =\u003e expectedTaxableSpecialQuery\ntaxableSpecialQuery.values // =\u003e [ true, 'special' ]\n\nconst nonTaxableQuery = mostRecentSalePricesQuery(false)\n\nconst expectedNonTaxableQuery = ['SELECT item.item_id, item.description, item.type, latest_sale.date AS latest_sale_date, latest_sale.price',\n\t'FROM item',\n\t'JOIN (',\n\t'\\tSELECT item_sale.item_id, MAX(item_sale.date) AS `date`',\n\t'\\tFROM item_sale',\n\t'\\tWHERE taxable = ?',\n\t'\\tGROUP BY item_sale.item_id',\n\t') AS latest_sale ON latest_sale.item_id = item.item_id'].join('\\n')\n\nnonTaxableQuery.sql // =\u003e expectedNonTaxableQuery\nnonTaxableQuery.values // =\u003e [ false ]\n\n```\n\n## API\n\nBecause the [mysql](https://github.com/mysqljs/mysql) package already makes inserting so easy, this module is focused on `SELECT` queries.  I've implemented new clauses as I've needed them, and it's pretty well fleshed out at the moment.\n\nIf you need a clause added that is not implemented yet, feel free to open a pull request.  If you're not sure what the API should look like, open an issue and we can talk it through.\n\n### Clauses\n\nEvery clause method returns a new immutable `q` query object.\n\n- `q.select(expression1, expression2, etc)`\n- `q.from(tablename | subquery, alias)`\n- `q.join(tablename | subquery, [alias], on_expression)`\n- `q.leftJoin(tablename | subquery, [alias], on_expression)`\n- `q.where(expression, [comparator, [value]])`\n- `q.orWhere(expression, [comparator, [value]])`\n- `q.whereLike(expression, value)`\n- `q.orWhereLike(expression, value)`\n- `q.having(expression, [comparator, [value]])`\n- `q.orHaving(expression, [comparator, [value]])`\n- `q.groupBy(expression1, expression2, etc)`\n- `q.orderBy(expression1, expression2, etc)`\n- `q.limit(offset)`\n- `q.forUpdate()`\n- `q.lockInShareMode()`\n\n`expression` strings are inserted without being parameterized, but you can also pass in [tagged template strings](#tagged-template-strings) to do anything special.\n\nAll `value`s are automatically parameterized.  If a `value` is `NULL` it will be automatically compared with `IS`, and if it's an array it will be automatically compared with `IN()`:\n\n```js\nconst whereInResult = q.select('fancy')\n    .from('table')\n    .where('table.pants', [ 'fancy', 'boring' ])\n    .build()\n\nconst whereInQuery = 'SELECT fancy\\n'\n        + 'FROM table\\n'\n        + 'WHERE table.pants IN(?)'\n\nwhereInResult.sql // =\u003e whereInQuery\n\nwhereInResult.values // =\u003e [ [ 'fancy', 'boring' ] ]\n```\n\nPut another way, calling `q.select('column1, column2')` is just as acceptable as calling `q.select('column1', 'column2')` and you should use whichever you prefer.\n\n#### Clause order\n\nClauses are returned in the [correct order](https://github.com/TehShrike/sql-concat/blob/master/constants.js#L19-L35) no matter what order you call the methods in.\n\n```js\nq.from('table').select('column').toString() // `SELECT column\\nFROM table``\n```\n\nHowever, if you call a method multiple times, the values are concatenated in the same order you called them.\n\n```js\nq.from('nifty')\n\t.select('snazzy')\n\t.select('spiffy')\n\t.select('sizzle')\n\t.toString() // `SELECT snazzy, spiffy, sizzle\\nFROM nifty``\n```\n\n### `q.union(query)` and `q.unionAll(query)`\n\nThe `union` and `unionAll` methods return a query object that only contains `union` and `unionAll` methods – once you start unioning queries together, you can keep unioning more queries, but you can't add any other clauses to them.\n\n### `q.build()`\n\nReturns an object with these properties:\n\n- `sql`: a string containing the query, with question marks `?` where escaped values should be inserted.\n- `values`: an array of values to be used with the query.\n\nYou can pass this object directly to the `query` method of the [`mysql`](https://github.com/mysqljs/mysql#performing-queries) library:\n\n```node\nmysql.query(\n\tq.select('Cool!').build(),\n\t(err, result) =\u003e {\n\t\tconsole.log(result)\n\t}\n)\n```\n\n```js\nq.select('column')\n\t.where('id', 3)\n\t.build() // { sql: `SELECT column\\nWHERE id = ?`, values: [ 3 ]}\n```\n\n### `q.toString()`\n\nReturns a string with values escaped by [`sqlstring`](https://github.com/mysqljs/sqlstring#formatting-queries).\n\n```js\nq.select('fancy')\n    .from('table')\n    .where('table.pants', [ 'what\\'s up', 'boring' ])\n    .toString() // =\u003e `SELECT fancy\\nFROM table\\nWHERE table.pants IN('what\\\\'s up', 'boring')`\n```\n\n### Tagged template strings\n\nsql-concat is also a template tag:\n\n```js\nconst rainfall = 3\nconst templateTagResult = q`SELECT galoshes FROM puddle WHERE rain \u003e ${ rainfall }`\n\ntemplateTagResult.sql // =\u003e `SELECT galoshes FROM puddle WHERE rain \u003e ?`\ntemplateTagResult.values // =\u003e [ 3 ]\n```\n\nYou can pass these results into any method as a value.  This allows you to properly parameterize function calls:\n\n```js\nconst shoeSize = 9\nconst functionCallResult = q.select('rubbers')\n\t.from('puddle')\n\t.where('rain', '\u003e', 4)\n\t.where('size', q`LPAD(${ shoeSize }, 2, '0')`)\n\t.build()\n\nconst functionCallQuery = `SELECT rubbers\\n`\n\t+ `FROM puddle\\n`\n\t+ `WHERE rain \u003e ? AND size = LPAD(?, 2, '0')`\n\nfunctionCallResult.sql // =\u003e functionCallQuery\n\nfunctionCallResult.values // =\u003e [ 4, 9 ]\n```\n\n## Long-shot feature\n\nSome syntax for generating nested clauses conditionally would be nice, so you could easily generate something like this dynamically:\n\n```sql\nWHERE important = ? AND (your_column = ? OR your_column = ? OR something_else LIKE ?)\n```\n\nMaybe something like:\n\n```node\nconst whereCondition = q.parenthetical('OR')\n\t.equal('your_column', true)\n\t.equal('your_column', randomVariable)\n\t.like('something_else', anotherVariable)\n\nconst query = q.select('everything')\n\t.from('table')\n\t.where('important', true)\n\t.where(whereCondition)\n```\n\nYou can discuss this feature in [Issue 3](https://github.com/TehShrike/sql-concat/issues/3) if you're interested.\n\n## Running the tests\n\n1. clone the repo\n2. navigate to the cloned directory\n3. `npm install`\n4. `npm test`\n\n## License\n\n[WTFPL](http://wtfpl2.com)\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftehshrike%2Fsql-concat","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftehshrike%2Fsql-concat","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftehshrike%2Fsql-concat/lists"}