{"id":13714807,"url":"https://github.com/s1owjke/prisma-query-formatter","last_synced_at":"2026-01-31T19:11:18.864Z","repository":{"id":240300683,"uuid":"802101712","full_name":"s1owjke/prisma-query-formatter","owner":"s1owjke","description":"Small utility for Prisma query formatting and param substitution","archived":false,"fork":false,"pushed_at":"2024-12-22T17:03:27.000Z","size":100,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-29T22:05:28.482Z","etag":null,"topics":["database","logging","prisma","prisma-client","prisma-extension","query"],"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/s1owjke.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":"2024-05-17T14:24:57.000Z","updated_at":"2025-03-12T02:08:50.000Z","dependencies_parsed_at":"2024-11-26T20:37:19.216Z","dependency_job_id":null,"html_url":"https://github.com/s1owjke/prisma-query-formatter","commit_stats":null,"previous_names":["s1owjke/prisma-query-formatter"],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/s1owjke%2Fprisma-query-formatter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/s1owjke%2Fprisma-query-formatter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/s1owjke%2Fprisma-query-formatter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/s1owjke%2Fprisma-query-formatter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/s1owjke","download_url":"https://codeload.github.com/s1owjke/prisma-query-formatter/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252806400,"owners_count":21807198,"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":["database","logging","prisma","prisma-client","prisma-extension","query"],"created_at":"2024-08-03T00:00:50.298Z","updated_at":"2026-01-31T19:11:18.857Z","avatar_url":"https://github.com/s1owjke.png","language":"TypeScript","funding_links":[],"categories":[":safety_vest: Community Prisma Tools"],"sub_categories":[],"readme":"# Prisma Query Formatter\n\n[![Published on npm](https://img.shields.io/npm/v/prisma-query-formatter?color=brightgreen)](https://www.npmjs.com/package/prisma-query-formatter) [![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT)\n\nThis small zero-dependency utility correctly formats Prisma queries by substituting placeholders with their corresponding values, support all databases including PostgreSQL.\n\nInternally, Prisma uses its [own](https://github.com/prisma/prisma-engines/blob/5.13.0/quaint/src/ast/values.rs#L547) Rust implementation for stringifying params, so you couldn't just use `JSON.parse` to convert them back to an array (sometimes it's not valid json, like double quotes are not escaped in strings).\n\nNote that this utility is designed for logging purposes only, because Prisma irreversibly converts some types of parameters, such as  [blobs](https://github.com/prisma/prisma-engines/blob/5.13.0/quaint/src/ast/values.rs#L571).\n\n## How to use it\n\nJust register a query event handler and use the `formatQuery` util to substitute the query params (don't forget to enable [event-based](https://www.prisma.io/docs/orm/reference/prisma-client-reference#log) logging in your Prisma client configuration).\n\n```typescript\nimport { PrismaClient } from \"@prisma/client\";\nimport { formatQuery } from \"prisma-query-formatter\";\n\nconst prisma = new PrismaClient({\n  log: [\n    { emit: \"event\", level: \"query\" },\n  ],\n});\n\nprisma.$on(\"query\", (e) =\u003e {\n  console.log(formatQuery(e.query, e.params));\n});\n```\n\nFor example, this query:\n\n```typescript\nawait db.user.findUnique({\n  select: { id: true },\n  where: { email: \"john@example.com\" },\n});\n```\n\nWill be logged to the console as:\n\n```text\nSELECT `User`.`id` FROM `User` WHERE `User`.`email` = \"john@example.com\"\n```\n\n## Error handling\n\nThe library now performs a validation to ensure that the number of placeholders in a query matches the number of params, as a mismatch may indicate an error.\n\n- If there is a mismatch and the option `throwOnError` is set to true, it will throw an error\n- If `throwOnError` is set to false (default value), it will return the concatenated query and parameters as is\n\nHere is an example with error handler:\n\n```typescript\nprisma.$on(\"query\", (e) =\u003e {\n  try {\n    console.log(formatQuery(e.query, e.params, { throwOnError: true }));\n  } catch {\n    console.log(`${e.query} ${e.params}`);\n  }\n});\n```\n\n## Query formatting\n\nAll whitespace symbols in multiline queries (usually raw queries written manually) will be replaced with single space, for example:\n\n```typescript\nawait db.$queryRaw`\n  SELECT \n    DISTINCT role\n  FROM User\n  WHERE\n    status = ${Status.Active}\n`;\n```\n\nWill be logged with params as:\n\n```text\nSELECT DISTINCT role FROM User WHERE status = \"Active\"\n```\n\nTo escape whitespace symbols such as `\\f\\n\\r\\t\\v` in params, use the `escapeWhitespace` function as `paramTransformer`, this will allow you to output multiline strings more concisely (all your logs will be on one line).\n\n```typescript\nimport { formatQuery, escapeWhitespace } from \"prisma-query-formatter\";\n\nprisma.$on(\"query\", (e) =\u003e {\n  console.log(formatQuery(e.query, e.params, { paramTransformer: escapeWhitespace }));\n});\n```\n\nAnd then, they will be displayed like this:\n\n```text\nSELECT `Post`.`id` FROM `Post` WHERE `Post`.`title` = \"first\\nsecond\"\n```\n\n## Custom transformers\n\nYou can provide a custom `paramTransformer` function to customize how parameters are displayed.\n\nFor example, to limit param length to the first 255 characters:\n\n```typescript\nimport { formatQuery } from \"prisma-query-formatter\";\n\nconst MAX_PARAM_LENGTH = 255;\n\nconst limitLength = (param: string) =\u003e {\n  if (param.length \u003c= MAX_PARAM_LENGTH) {\n    return param;\n  }\n\n  return `${param.substring(0, MAX_PARAM_LENGTH)}...`;\n};\n\nprisma.$on(\"query\", (e) =\u003e {\n  console.log(formatQuery(e.query, e.params, { paramTransformer: limitLength() }));\n});\n```\n\n## Migrations\n\nHow to upgrade from older versions.\n\n### 1.0.0\n\nThe `escapeParams` option has been removed. The library now supports a custom `paramTransformer` that allows you to control param rendering and exports the `escapeWhitespace` function instead.\n\nBefore (version 0.2.2 and earlier):\n\n```typescript\nimport { formatQuery } from \"prisma-query-formatter\";\n\nformatQuery(query, params, { escapeParams: true });\n````\n\nAfter:\n\n```typescript\nimport { formatQuery, escapeWhitespace } from \"prisma-query-formatter\";\n\nformatQuery(query, params, { paramTransformer: escapeWhitespace });\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fs1owjke%2Fprisma-query-formatter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fs1owjke%2Fprisma-query-formatter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fs1owjke%2Fprisma-query-formatter/lists"}