npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

prisma-query-formatter

v0.2.1

Published

Small utility for Prisma query formatting and param substitution in logs

Downloads

2,837

Readme

Prisma Query Formatter

Published on npm License: MIT

This small zero-dependency utility correctly formats Prisma queries by substituting placeholders with their corresponding values, support all databases including PostgreSQL.

Internally, Prisma uses its own 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).

Note that this utility is designed for logging purposes only, because Prisma irreversibly converts some types of parameters, such as blobs.

How to use it

Just register a query event handler and use the formatQuery util to substitute the query params (don't forget to enable event-based logging in your Prisma client configuration).

import { PrismaClient } from "@prisma/client";
import { formatQuery } from "prisma-query-formatter";

const prisma = new PrismaClient({
  log: [
    { emit: "event", level: "query" },
  ],
});

prisma.$on("query", (e) => {
  console.log(formatQuery(e.query, e.params));
});

For example, this query:

await db.user.findUnique({
  select: { id: true },
  where: { email: "[email protected]" },
});

Will be logged to the console as:

SELECT `User`.`id` FROM `User` WHERE `User`.`email` = "[email protected]"

Error handling

The 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.

  • If there is a mismatch and the option throwOnError is set to true, it will throw an error
  • If throwOnError is set to false (default value), it will return the concatenated query and parameters as is

Here is an example with error handler:

prisma.$on("query", (e) => {
  try {
    console.log(formatQuery(e.query, e.params, { throwOnError: true }));
  } catch {
    console.log(`${e.query} ${e.params}`);
  }
});

Query formatting

All whitespace symbols in multiline queries (usually raw queries written manually) will be replaced with single space, for example:

await db.$queryRaw`
  SELECT 
    DISTINCT role
  FROM User
  WHERE
    status = ${Status.Active}
`;

Will be logged with params as:

SELECT DISTINCT role FROM User WHERE status = "Active"

To escape whitespace symbols such as \f\n\r\t\v in params, use the escapeParams option (false by default), this will allow you to output multiline strings more concisely (all your logs will be on one line).

prisma.$on("query", (e) => {
  console.log(formatQuery(e.query, e.params, { escapeParams: true }));
});

And then, they will be displayed like this:

SELECT `Post`.`id` FROM `Post` WHERE `Post`.`title` = "first\nsecond"