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 🙏

© 2026 – Pkg Stats / Ryan Hefner

prisma-smart-query

v1.0.3

Published

A small helper for building Prisma query options with filtering, sorting, field limiting, pagination, and relations include.

Readme

prisma-smart-query

A TypeScript-first smart query builder for Prisma that converts Express.js query parameters into Prisma query options.

✅ Works in JavaScript and TypeScript projects
✅ Filtering with operators (gte, lte, in, contains, etc.)
✅ Full-text search across multiple fields
✅ Sorting (?sort=-createdAt,name)
✅ Pagination (?page=2&limit=10)
✅ Field selection (?fields=id,name)
✅ Relation inclusion (?include=posts,profile)
✅ Auto-removes sensitive fields (like passwords)


🚀 Installation

npm install prisma-smart-query

🟩 Quick Example (JavaScript)

const express = require("express");
const { PrismaClient } = require("@prisma/client");
const { buildPrismaQueryOptions } = require("prisma-smart-query");

const prisma = new PrismaClient();
const app = express();

app.get("/users", async (req, res) => {
  const {
    queryOptions,
    meta: { page, limit },
  } = buildPrismaQueryOptions(
    req,
    {}, // baseWhere
    ["name", "email"], // searchable fields
    {
      defaultSort: { createdAt: "desc" },
      sensitiveFields: ["password"],
    }
  );

  const users = await prisma.user.findMany(queryOptions);
  const total = await prisma.user.count({ where: queryOptions.where });

  res.json({
    meta: {
      page,
      limit,
      total,
      totalPages: Math.ceil(total / limit),
    },
    data: users,
  });
});

app.listen(3000, () => console.log("Server running on http://localhost:3000"));

🟦 Quick Example (TypeScript)

import express, { Request, Response } from "express";
import { PrismaClient } from "@prisma/client";
import { buildPrismaQueryOptions } from "prisma-smart-query";

const prisma = new PrismaClient();
const app = express();

app.get("/products", async (req: Request, res: Response) => {
  const {
    queryOptions,
    meta: { page, limit },
  } = buildPrismaQueryOptions(
    req,
    { isActive: true }, // baseWhere filter
    ["name", "description"], // searchable fields
    { defaultSort: { createdAt: "asc" } }
  );

  const products = await prisma.product.findMany(queryOptions);
  const total = await prisma.product.count({ where: queryOptions.where });

  res.json({
    meta: {
      page,
      limit,
      total,
      totalPages: Math.ceil(total / limit),
    },
    data: products,
  });
});

app.listen(4000, () => console.log("Server running on http://localhost:4000"));

🔎 Supported Query Parameters

| Param | Example | Description | | ----------- | ----------------------------- | ------------------------------------------------------------- | | page | ?page=2 | Current page (default: 1) | | limit | ?limit=10 | Results per page (default: 10) | | sort | ?sort=-createdAt,name | Multi-field sorting (- = descending) | | fields | ?fields=id,name,email | Select specific fields | | include | ?include=posts,profile | Include relations | | search | ?search=john | Search across specified fields | | filters | ?age[gte]=18&price[lte]=100 | Operator-based filters (gte, lte, in, contains, etc.) |

✅ Example Queries

GET /users?page=2&limit=5 GET /users?sort=-createdAt,name GET /users?search=alice GET /users?age[gte]=18 GET /users?fields=id,name,email&include=posts

🧪 Playground Example

Here’s what happens under the hood when you hit an endpoint with query params:

GET /users?page=2&limit=5&sort=-createdAt,name&search=john&age[gte]=18&fields=id,name,email&include=posts

Generated Prisma Query Options

{
  "where": {
    "age": { "gte": 18 },
    "OR": [
      { "name": { "contains": "john", "mode": "insensitive" } },
      { "email": { "contains": "john", "mode": "insensitive" } }
    ]
  },
  "orderBy": [{ "createdAt": "desc" }, { "name": "asc" }],
  "select": {
    "id": true,
    "name": true,
    "email": true,
    "posts": true
  },
  "skip": 5,
  "take": 5
}

Response Example

{
  "meta": {
    "page": 2,
    "limit": 5,
    "total": 42,
    "totalPages": 9
  },
  "data": [
    {
      "id": 6,
      "name": "John Doe",
      "email": "[email protected]",
      "posts": [...]
    },
    ...
  ]
}

Prisma Smart Query

License: MIT npm version Build Status