Skip to content

random42/prisma-extension-log

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

prisma-extension-log

Log your model queries in a simple and customizable way.

Install

npm i prisma-extension-log

Usage

// custom options type
type Options = {
  includeResult?: boolean;
};

const prisma = new PrismaClient().$extends(
  extension({
    log: (data: LogData, options?: Options) => {
      const includeResult = options?.includeResult ?? false;
      const logData = { ...data };
      if (!includeResult) {
        delete logData.result;
      }
      if (logData.error) {
        console.error(logData);
      } else {
        console.log(logData);
      }
    },
  })
);
await prisma.user.findFirst();
await prisma.user.count({
  log: false, // disable logging
});
await prisma.user.findMany({
  log: {
    includeResult: true, // use your options with type safety
  },
});

Log data

type LogData = {
  /**
   * @example User
   */
  model: string;
  /**
   * @example findMany
   */
  operation: string;
  args: unknown;
  /**
   * Milliseconds with some precision, using
   * performance.now()
   */
  time: Milliseconds;
  result?: unknown;
  error?: unknown;
}

Learn more

About

Prisma client extension for logging queries

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published