{"id":15984224,"url":"https://github.com/jliuhtonen/pg-literally","last_synced_at":"2026-03-07T18:04:16.449Z","repository":{"id":255557065,"uuid":"849538566","full_name":"jliuhtonen/pg-literally","owner":"jliuhtonen","description":"Tagged sql template literals and fragments for pg","archived":false,"fork":false,"pushed_at":"2026-02-08T04:54:43.000Z","size":137,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2026-02-08T12:29:06.989Z","etag":null,"topics":["node-js","pg","pg-promise","sql","typescript"],"latest_commit_sha":null,"homepage":"","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/jliuhtonen.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,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2024-08-29T19:13:37.000Z","updated_at":"2026-02-08T04:54:40.000Z","dependencies_parsed_at":"2024-10-27T17:10:19.095Z","dependency_job_id":"4a6eec4f-44c4-4e34-ab3b-cdaac6632b07","html_url":"https://github.com/jliuhtonen/pg-literally","commit_stats":{"total_commits":36,"total_committers":2,"mean_commits":18.0,"dds":0.2777777777777778,"last_synced_commit":"2ee9e3130885837ab73baf8006b863b063601847"},"previous_names":["jliuhtonen/pg-literally"],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/jliuhtonen/pg-literally","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jliuhtonen%2Fpg-literally","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jliuhtonen%2Fpg-literally/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jliuhtonen%2Fpg-literally/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jliuhtonen%2Fpg-literally/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jliuhtonen","download_url":"https://codeload.github.com/jliuhtonen/pg-literally/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jliuhtonen%2Fpg-literally/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":30225456,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-07T17:00:40.062Z","status":"ssl_error","status_checked_at":"2026-03-07T17:00:39.026Z","response_time":53,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: 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":["node-js","pg","pg-promise","sql","typescript"],"created_at":"2024-10-08T02:05:11.274Z","updated_at":"2026-03-07T18:04:16.432Z","avatar_url":"https://github.com/jliuhtonen.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# pg-literally 📚\n\nSQL template tag literals and SQL fragments for Node and Postgres. Compatible with `node-pg` and `pg-promise`.\n\n## Why?\n\nIt's best to use parameterized queries to prevent SQL injection attacks instead of escaping the interpolated values in your application or library code. However, you need to provide values for your queries and doing that separately clumsy. This library provides a way to build parametrized queries from tagged template literals.\n\nThere are also some nice tools for formatting SQL queries in template tag literals like [`prettier-plugin-sql`](https://github.com/un-ts/prettier/tree/master/packages/sql) and [syntax highlighting in code editors like VSCode](https://marketplace.visualstudio.com/items?itemName=frigus02.vscode-sql-tagged-template-literals).\n\n## How?\n\n```ts\nimport { sql } from \"pg-literally\"\n\nconst name = \"Gandalf\"\nconst age = 24000\n\nconst query = sql`\n  SELECT *\n  FROM characters\n  WHERE name = ${name}\n  AND age = ${age}\n`\n```\n\nResult:\n\n```json\n{\n  \"text\": \"SELECT * FROM characters WHERE name = $1 AND age = $2\",\n  \"values\": [\"Gandalf\", 24000]\n}\n```\n\nHere are some examples of how you can use the `query` with `node-pg` or `pg-promise`:\n\n### Example 1: Using `node-pg`\n\n```ts\nimport { Client } from \"pg\"\nimport { sql } from \"pg-literally\"\n\nconst name = \"Gandalf\"\nconst age = 24000\n\nconst query = sql`\n  SELECT *\n  FROM characters\n  WHERE name = ${name}\n  AND age = ${age}\n`\n\nconst client = new Client()\nclient.connect()\n\nawait client.query(query)\n```\n\n### Example 2: Using `pg-promise`\n\n```ts\nimport { Database } from \"pg-promise\"\nimport { sql } from \"pg-literally\"\n\nconst name = \"Gandalf\"\nconst age = 24000\n\nconst query = sql`\n  SELECT *\n  FROM characters\n  WHERE name = ${name}\n  AND age = ${age}\n`\n\nconst db = new Database(\"connection-string\")\n\nawait db.one(query)\n```\n\n## Reference\n\n### `sql`\n\nThe `sql` function is a template tag that returns an object with a `text` property and a `values` property. The `text` property is the SQL query with placeholders for the values. The `values` property is an array of the values that should be substituted into the query.\n\n```ts\nconst query = sql`\n  SELECT *\n  FROM characters\n  WHERE name = ${name}\n  AND age = ${age}\n`\n```\n\nThe `query` object will look like this:\n\n```ts\n{\n  text: 'SELECT * FROM characters WHERE name = $1 AND age = $2',\n  values: ['Gandalf', 24000]\n}\n```\n\n### `sqlFragment`\n\nThe `sqlFragment` function can be used to create a SQL fragment that can be used in a larger query. It works the same way as the `sql` function, but it does not return an object with a `text` and `values` property. Instead, it returns a `SqlFragment` type that can be joined and combined together before actually rendering it to query placeholder and values array.\n\nAnd yes, you can put sql fragments in sql fragments.\n\n```ts\nconst whereFragment = sqlFragment`\n  name = ${name}\n  AND age = ${age}\n`\n```\n\nYou can then use the `whereFragment` in a larger query like this:\n\n```ts\nconst query = sql`\n  SELECT *\n  FROM characters\n  WHERE ${whereFragment}\n`\n```\n\n### `joinSqlFragments`\n\nThe `joinSqlFragments` function can be used to join two `SqlFragment` objects together. It returns a new `SqlFragment` object that represents the combined fragments.\n\n```ts\nconst whereFragment1 = sqlFragment`name = ${name}`\nconst whereFragment2 = sqlFragment`age = ${age}`\nconst combinedFragment = joinSqlFragments(\n  whereFragment1,\n  whereFragment2,\n  \"\\nAND \",\n)\n\nconst query = sql`\n  SELECT *\n  FROM characters\n  WHERE ${combinedFragment}\n`\n```\n\nResult:\n\n```json\n{\n  \"text\": \"SELECT * FROM characters WHERE name = $1\\nAND age = $2\",\n  \"values\": [\"Gandalf\", 24000]\n}\n```\n\n### `combineFragments`\n\nThe `combineFragments` function can be used to combine multiple `SqlFragment` objects together. It returns a new `SqlFragment` object that represents the combined fragments. Basically, this is syntactic sugar for reducing an array of `SqlFragment` objects.\n\n```ts\nimport { combineFragments, sql, sqlFragment as sqlF } from \"pg-literally\"\n\nconst companiesToInsert = [\n  { name: \"Apple\", address: \"1 Infinite Loop\" },\n  { name: \"Google\", address: \"1600 Amphitheatre Parkway\" },\n  { name: \"Microsoft\", address: \"One Microsoft Way\" },\n  { name: \"Amazon\", address: \"410 Terry Ave. North\" },\n]\n\nconst result = sql`\n  INSERT INTO company (name, address)\n  VALUES ${combineFragments(\n    \",\\n\",\n    ...companiesToInsert.map(\n      (company) =\u003e sqlF`(${[company.name, company.address]})`,\n    ),\n  )}\n`\n```\n\nResult:\n\n```json\n{\n  \"text\": \"INSERT INTO company (name, address) VALUES ($1, $2),\\n($3, $4),\\n($5, $6),\\n($7, $8)\",\n  \"values\": [\n    \"Apple\",\n    \"1 Infinite Loop\",\n    \"Google\",\n    \"1600 Amphitheatre Parkway\",\n    \"Microsoft\",\n    \"One Microsoft Way\",\n    \"Amazon\",\n    \"410 Terry Ave. North\"\n  ]\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjliuhtonen%2Fpg-literally","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjliuhtonen%2Fpg-literally","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjliuhtonen%2Fpg-literally/lists"}