{"id":34674671,"url":"https://github.com/martijndeh/lego","last_synced_at":"2025-12-24T20:02:29.565Z","repository":{"id":65380419,"uuid":"48989531","full_name":"martijndeh/lego","owner":"martijndeh","description":"A lightweight SQL (string) builder using ES6 template strings. Lego embraces SQL instead of adding yet another abstraction layer.","archived":false,"fork":false,"pushed_at":"2018-01-19T15:00:19.000Z","size":152,"stargazers_count":55,"open_issues_count":0,"forks_count":3,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-11-03T14:22:27.095Z","etag":null,"topics":["javascript","postgres","sql"],"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/martijndeh.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":"2016-01-04T10:13:46.000Z","updated_at":"2023-01-20T09:56:28.000Z","dependencies_parsed_at":"2023-01-20T12:06:06.882Z","dependency_job_id":null,"html_url":"https://github.com/martijndeh/lego","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/martijndeh/lego","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/martijndeh%2Flego","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/martijndeh%2Flego/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/martijndeh%2Flego/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/martijndeh%2Flego/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/martijndeh","download_url":"https://codeload.github.com/martijndeh/lego/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/martijndeh%2Flego/sbom","scorecard":{"id":621410,"data":{"date":"2025-08-11","repo":{"name":"github.com/martijndeh/lego","commit":"aa40da7fc3034e8d24304a21710533e3153e33b2"},"scorecard":{"version":"v5.2.1-40-gf6ed084d","commit":"f6ed084d17c9236477efd66e5b258b9d4cc7b389"},"score":3,"checks":[{"name":"Maintained","score":0,"reason":"0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0","details":null,"documentation":{"short":"Determines if the project is \"actively maintained\".","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#maintained"}},{"name":"Code-Review","score":0,"reason":"Found 1/29 approved changesets -- score normalized to 0","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":"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":"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":"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":"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":"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":"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":"License","score":10,"reason":"license file detected","details":["Info: project has a license file: LICENSE:0","Info: FSF or OSI recognized license: MIT License: LICENSE:0"],"documentation":{"short":"Determines if the project has defined a license.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#license"}},{"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":"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 2 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-21T05:19:11.554Z","repository_id":65380419,"created_at":"2025-08-21T05:19:11.555Z","updated_at":"2025-08-21T05:19:11.555Z"},"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28007457,"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","status":"online","status_checked_at":"2025-12-24T02:00:07.193Z","response_time":83,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"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","postgres","sql"],"created_at":"2025-12-24T20:01:23.017Z","updated_at":"2025-12-24T20:02:29.548Z","avatar_url":"https://github.com/martijndeh.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Lego.js\n[![Build Status](https://travis-ci.org/martijndeh/lego.svg?branch=master)](https://travis-ci.org/martijndeh/lego)\n[![Coverage Status](https://coveralls.io/repos/martijndeh/lego/badge.svg?branch=master\u0026service=github)](https://coveralls.io/github/martijndeh/lego?branch=master)\n\nA lightweight SQL (string) builder using ES6 template strings. Lego embraces SQL instead of adding yet another abstraction layer.\n\n```js\nLego.sql `SELECT * FROM users WHERE name = ${name}`;\n```\n\nLego does not do simple string concatenation. Instead, Lego creates parameterized queries. For example, the following query is created from the previous call:\n\n```sql\nSELECT * FROM users WHERE name = $1\n```\n\n## Quick start\n\nLego uses ES6 template strings. From the template string, a parameterized query is created and passed to the driver. The driver creates a pool of connections with the url from `process.env.DATABASE_URL`.\n\n```js\nLego.sql `INSERT INTO projects (name) VALUES (${name}) RETURNING *`\n\t.then((projects) =\u003e {\n\t\treturn Lego.sql `INSERT INTO project_settings (project_id) VALUES (${projects[0].id})`;\n\t})\n\t.then(() =\u003e {\n\t\t// Ready! :-)\n\t})\n```\n\nYou can also nest arrays of Lego instances:\n```js\nLego.sql `INSERT INTO projects (name) VALUES ${projects.map((project) =\u003e {\n\treturn Lego.sql `(${project.name})`;\n})}`;\n```\n\nWhich creates and executes the query `INSERT INTO projects (name) VALUES ($1), ($2)`.\n\n### Lego#append\n\nIn some cases, you want to append a statement:\n\n```js\nconst lego = Lego.sql `SELECT * FROM tests`;\n\nif (shouldOrderBy) {\n\tlego.append `ORDER BY value`;\n}\n```\n\n### Lego#raw\n\nYou cannot pass raw values to your queries, unless you use `Lego#raw`. Be very careful not to use this with user input.\n\n```js\nconst column = 'name';\nLego.sql `UPDATE users SET ${Lego.raw(column)} = ${value}`;\n```\n\n### Return value\n\n`Lego.sql` returns a Promise-like object and the query is executed when `.then(..)` is invoked. The Promise is resolved with the query's result.\n\nIn `DELETE`, `UPDATE` and `INSERT` queries, when not using a `RETURNING` clause, the number of affected rows is resolved. Otherwise, the row data is resolved.\n\n## Rows to objects\n\nLego makes it easy to parse rows and transform them to objects. Consider the following rows:\n\n```js\nconst rows = [{\n\tid: 1,\n\ttest_id: 1,\n\ttest_name: 'Test 1'\n}, {\n\tid: 1,\n\ttest_id: 2,\n\ttest_name: 'Test 2'\n}];\n```\n\nThese rows are flat but do contain 1 root object and 2 child objects. Something like the below:\n\n```js\nconst objects = [{\n\tid: 1,\n\ttests: [{\n\t\tid: 1,\n\t\tname: 'Test 1'\n\t}, {\n\t\tid: 2,\n\t\tname: 'Test 2'\n\t}]\n}]\n```\n\nYou can transform the rows by simply passing the rows to Lego's parse system and providing a definition object:\n```js\nLego.parse(rows, [{\n\tid: 'id',\n\ttests: [{\n\t\tid: 'test_id',\n\t\tname: 'test_name'\n\t}]\n}]);\n```\n\nThe definition object describes how to map columns and rows to objects. Every property refers to a column name. You can also call `.parse(..)` on a Lego object directly.\n\n```js\nconst project = await Lego.sql `SELECT\n\tprojects.id,\n\tprojects.created_at,\n\tproject_members.id member_id,\n\tproject_members.name member_name,\n\tproject_members.email member_email\n\tproject_members.joined_at member_joined_at\nFROM projects\nINNER JOIN project_members ON projects.id = project_members.project_id\nWHERE\n\tprojects.id = ${projectID}`\n\t.parse(rows, {\n\t\tid: 'id',\n\t\tcreatedAt: 'created_at',\n\t\tmembers: [{\n\t\t\tid: 'member_id',\n\t\t\tname: 'member_name',\n\t\t\temail: 'member_email',\n\t\t\tjoinedAt: 'member_joined_at'\n\t\t}]\n\t});\n```\n\nPlease have a look at the [parse test cases](https://github.com/martijndeh/lego/blob/master/test/parse.js) to learn more about the different ways to transform rows to objects.\n\n### Lego#first\n\nOr if you just want the first result from a query (or null if there were no results):\n\n```js\nLego.sql `SELECT * FROM accounts LIMIT 1`\n\t.first()\n\t.then((account) =\u003e {\n\t\t// account is the first row from the query, or null if no rows were found.\n\t});\n```\n\n## Transactions\n\nTransactions are also supported. You can either chain the calls manually by returning a promise in the transaction's `callback`:\n\n```js\nLego.transaction((transaction) =\u003e {\n\treturn transaction.sql `UPDATE money SET value = value + 100`\n\t\t.then(() =\u003e {\n\t\t\treturn transaction.sql `UPDATE money SET value = value - 100`;\n\t\t});\n});\n```\n\nOr use the transaction's queue which invokes the queries in series:\n\n```js\nLego.transaction((transaction) =\u003e {\n\ttransaction.sql `UPDATE money SET value = value + 100`;\n\ttransaction.sql `UPDATE money SET value = value - 100`;\n});\n```\n\nAlternatively, you can construct regular Lego instances and assign them to the transaction:\n\n```js\nLego.transaction((transaction) =\u003e {\n\treturn Lego\n\t\t.sql `UPDATE money SET value = value + 100`\n\t\t.transacting(transaction)\n\t\t.then(() =\u003e {\n\t\t\treturn Lego\n\t\t\t\t.sql `UPDATE money SET value = value - 100`\n\t\t\t\t.transacting(transaction);\n\t\t});\n});\n```\n\n`Lego#transaction` returns a promise with the result of the transaction's `callback`.\n\n## Migrations\n\nTo create a migration you can simply invoke `lego migrate:make`. This creates an empty migration with an `up` and a `down` function in which you can write your queries to alter your database.\n\nTo execute migrations, simply invoke `lego migrate:latest`. Migrations are executed in a transaction. The transaction is passed as argument in the migrate functions.\n\n```js\nexport function up(transaction) {\n\ttransaction.sql `CREATE TABLE tests (name TEXT UNIQUE, value INTEGER)`;\n\ttransaction.sql `INSERT INTO tests VALUES ('Martijn', 123)`;\n}\n\nexport function down(transaction) {\n\ttransaction.sql `DROP TABLE tests`;\n}\n```\n\nLego creates a `migrations` table to keep track of all the migrations. This migrations table is created in it's own schema called `lego`, so don't worry about any collisions.\n\nTo execute your migrations when you call `npm run release` you should add a run script to your `package.json`:\n\n```json\n\"release\": \"node -r babel-register ./node_modules/.bin/lego migrate:latest\",\n```\n\n## CLI\n\nThe command line interface supports the following commands:\n\n```\nlego version                         Prints the version of Lego.\nlego migrate:make                    Creates a new migration file.\nlego migrate:latest                  Migrates to the latest migration.\nlego migrate:rollback                Rolls back the previous migration.\nlego migrate:\u003cversion\u003e               Migrates or rolls back to the target migration \u003cversion\u003e.\n```\n\n## Environment variables\n\nVariable | Description\n---------|-------------\n`DATABASE_URL` | The connection string to the database.\n`LEGO_DISABLE_SSL` | By default, Lego requires an SSL connection to the database. To disable this, you can set the `LEGO_DISABLE_SSL` environment variable to `false`.\n`LEGO_MAX_POOL_SIZE` | Sets the maximum pool size. If you don't set the max pool size, the [driver](https://www.npmjs.com/package/pg) sets a default value.\n`LEGO_MIN_POOL_SIZE` | Sets the minimum pool size. If you don't set the min pool size, the [driver](https://www.npmjs.com/package/pg) sets a default value.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmartijndeh%2Flego","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmartijndeh%2Flego","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmartijndeh%2Flego/lists"}