queryd provides a lightweight observability layer for monitoring slow database queries in Node.js. This driver-agnostic solution integrates seamlessly with Prisma and allows for quick instrumentation, ensuring database performance remains optimal while keeping user experience smooth.
slow-query-detector is an advanced query observability tool designed specifically for Node.js applications. The project provides a driver-agnostic solution for effective slow database query detection and integrates seamlessly with Prisma through its optional package, @olegkoval/queryd/prisma. This enables developers to catch slow queries before they impact user experience.
EXPLAIN ANALYZE capabilities to investigate slow queries in detail.Getting started with slow-query-detector is straightforward:
import postgres from "postgres";
import {
createSlowQueryDetector,
wrapTaggedTemplate,
createConsoleLogger,
runWithDbContext,
} from "@olegkoval/queryd";
const sql = postgres(process.env.DATABASE_URL!);
const detector = createSlowQueryDetector(
{
warnThresholdMs: 200,
dbName: "primary",
requestBudget: { maxQueries: 80, maxTotalDurationMs: 2_000 },
},
{ logger: createConsoleLogger() },
);
const db = wrapTaggedTemplate(sql, detector);
await runWithDbContext({ requestId: "req-home-1", userId: "u-42" }, async () => {
await db`select ${1}::int`;
});
To validate performance, the project provides benchmarking scripts:
npm run benchmark
Slow-query-detector supports advanced configurations such as request budgets, allowing developers to define limits for the maximum number of queries and total duration per request.
For those using Prisma, integration is seamless:
import { PrismaClient } from "@prisma/client";
import { createSlowQueryDetector, createConsoleLogger, runWithDbContext } from "@olegkoval/queryd";
import { wrapPrismaClient } from "@olegkoval/queryd/prisma";
const base = new PrismaClient();
const detector = createSlowQueryDetector(
{
warnThresholdMs: 200,
dbName: "primary",
requestBudget: { maxQueries: 80, maxTotalDurationMs: 2_000 },
},
{ logger: createConsoleLogger() },
);
export const prisma = wrapPrismaClient(base, detector);
await runWithDbContext({ requestId: "req-1", userId: "u-1" }, async () => {
await prisma.$queryRaw`SELECT 1`;
});
For additional information on usage, advanced configurations, and contributing to the project, refer to the project documentation.
By leveraging the features of slow-query-detector, developers can significantly enhance database query performance, leading to improved user experiences.
No comments yet.
Sign in to be the first to comment.