teunmooij

query

by teunmooij
payload-tools
APIs & SDKs
v1
^1.6.22
MIT

Overview

Summary generated from the README

Provides strongly typed Filter and Query types built on Payload's where syntax, plus a select feature to choose output fields. A selectPlugin enables a select query parameter on REST endpoints for chosen collections or globals, while a select function works on internal API results. Targets Payload 1.x.

Features

  • Typed Filter type that checks paths, operators and operands against your collection type
  • Query type with function operands resolved later from data such as the current user
  • toFilter function converts a Query to a Filter, reusable across requests
  • selectPlugin adds a select query parameter to REST endpoints, configurable per collection or global
  • select function trims documents from the internal API to chosen fields

Use cases

  • Write compile-time checked where queries for Payload collections
  • Build per-user filters such as only showing documents authored by the current user
  • Reduce REST response payloads by selecting only needed fields

From the author

Payload typesafe query utility

Installation

npm i payload-query

143

Stars

15

Forks

13

Open Issues

2 years ago

Last Updated

28

Weekly Downloads

v1.0.2

npm Version

18.7 KB

Unpacked Size

1

Dependencies

Repository stars

143+2 since Sep 6

Show data
Repository stars by day (UTC)
DateStars
Dec 5, 2026143
Dec 4, 2026143
Dec 3, 2026143
Dec 2, 2026143
Dec 1, 2026143
Nov 30, 2026143
Nov 29, 2026143
Nov 28, 2026143
Nov 27, 2026143
Nov 26, 2026143
Nov 25, 2026143
Nov 24, 2026143
Nov 23, 2026143
Nov 22, 2026143
Nov 21, 2026143
Nov 20, 2026143
Nov 19, 2026143
Nov 18, 2026143
Nov 17, 2026143
Nov 16, 2026143
Nov 15, 2026143
Nov 14, 2026143
Nov 13, 2026143
Nov 12, 2026143
Nov 11, 2026143
Nov 10, 2026143
Nov 9, 2026143
Nov 8, 2026143
Nov 7, 2026142
Nov 6, 2026142
Nov 5, 2026142
Nov 4, 2026142
Nov 3, 2026142
Nov 2, 2026142
Nov 1, 2026142
Oct 31, 2026142
Oct 30, 2026142
Oct 29, 2026142
Oct 28, 2026142
Oct 27, 2026142
Oct 26, 2026142
Oct 25, 2026142
Oct 24, 2026142
Oct 23, 2026142
Oct 22, 2026142
Oct 21, 2026142
Oct 20, 2026142
Oct 19, 2026142
Oct 18, 2026142
Oct 17, 2026142
Oct 16, 2026142
Oct 15, 2026142
Oct 14, 2026142
Oct 13, 2026142
Oct 12, 2026142
Oct 11, 2026142
Oct 10, 2026142
Oct 9, 2026142
Oct 8, 2026142
Oct 7, 2026142
Oct 6, 2026142
Oct 5, 2026142
Oct 4, 2026142
Oct 3, 2026142
Oct 2, 2026142
Oct 1, 2026142
Sep 30, 2026142
Sep 29, 2026142
Sep 28, 2026142
Sep 27, 2026142
Sep 26, 2026142
Sep 25, 2026142
Sep 24, 2026142
Sep 23, 2026142
Sep 22, 2026142
Sep 21, 2026142
Sep 20, 2026142
Sep 19, 2026142
Sep 18, 2026142
Sep 17, 2026142
Sep 16, 2026141
Sep 15, 2026141
Sep 14, 2026141
Sep 13, 2026141
Sep 12, 2026141
Sep 11, 2026141
Sep 10, 2026141
Sep 9, 2026141
Sep 8, 2026141
Sep 7, 2026141
Sep 6, 2026141

Shared by 5 plugins in teunmooij/payload-tools.

npm weekly downloads

27−2 since Sep 6

Show data
npm weekly downloads by day (UTC)
DateWeekly downloads
Dec 5, 202627
Dec 4, 202631
Dec 3, 202631
Dec 2, 202689
Dec 1, 2026123
Nov 30, 2026119
Nov 29, 2026121
Nov 28, 2026122
Nov 27, 2026106
Nov 26, 2026106
Nov 25, 202647
Nov 24, 202611
Nov 23, 20269
Nov 22, 20268
Nov 21, 20267
Nov 20, 20266
Nov 19, 20267
Nov 18, 202612
Nov 17, 202611
Nov 16, 202614
Nov 15, 202610
Nov 14, 202610
Nov 13, 202612
Nov 12, 202618
Nov 11, 202617
Nov 10, 202620
Nov 9, 202624
Nov 8, 202629
Nov 7, 202634
Nov 6, 202636
Nov 5, 202631
Nov 4, 202629
Nov 3, 202634
Nov 2, 202642
Nov 1, 202657
Oct 31, 202655
Oct 30, 202660
Oct 29, 202657
Oct 28, 202655
Oct 27, 202649
Oct 26, 202635
Oct 25, 202617
Oct 24, 202618
Oct 23, 20269
Oct 22, 20269
Oct 21, 202611
Oct 20, 202613
Oct 19, 202612
Oct 18, 202625
Oct 17, 202635
Oct 16, 202635
Oct 15, 202636
Oct 14, 202638
Oct 13, 202634
Oct 12, 202637
Oct 11, 202623
Oct 10, 20269
Oct 9, 20269
Oct 8, 202610
Oct 7, 20266
Oct 6, 202616
Oct 5, 202614
Oct 4, 202615
Oct 3, 202616
Oct 2, 202617
Oct 1, 202616
Sep 30, 202618
Sep 29, 202613
Sep 28, 202613
Sep 27, 202612
Sep 26, 202614
Sep 25, 202613
Sep 24, 202614
Sep 23, 202612
Sep 22, 202612
Sep 21, 202614
Sep 20, 202621
Sep 19, 202618
Sep 18, 202619
Sep 17, 202618
Sep 16, 202621
Sep 15, 202616
Sep 14, 202615
Sep 13, 20268
Sep 12, 20268
Sep 11, 20267
Sep 10, 202627
Sep 9, 202628
Sep 8, 202629
Sep 7, 202629
Sep 6, 202629

Each point is npm's rolling 7-day download count.

Health Score

45/100Fair

Composite score based on GitHub activity, stars, npm downloads, publish recency, dependency count, and package size.

Badge for your README

Payload Directory health badge
Markdown
[![Payload Directory](https://payloaddirectory.dev/badge/611765581-query.svg)](https://payloaddirectory.dev/plugins/611765581-query)
HTML
<a href="https://payloaddirectory.dev/plugins/611765581-query"><img src="https://payloaddirectory.dev/badge/611765581-query.svg" alt="Payload Directory"></a>
Updated Jul 17, 2024Created Mar 9, 2023Published Apr 18, 2023

README

author snyk downloads npm version license

payload-query

Easy to use query utility for your Payload cms.

Main features:

  • create type safe queries
  • predefine queries to be constructed with data passed in later (e.g. request data)
  • select output fields on REST and Internal API.

Installation

With yarn:

yarn add payload-query

With npm:

npm install payload-query

Usage - Filter

payload-query is built on top of Payload's query system.

All examples below assume you have a collection that generates a type as follows:

type User = {
  id: string;
  name: string;
};

type Post = {
  color: 'mint' | 'dark-grey' | 'white';
  author: string | User;
  featured: boolean;
};

Filter type

The Filter type is a strongly typed version of Payload's Where type and can be directly used everywhere where the Where type is used. By providing your collection type as type it will tell you exactly which paths, operators and operands you can use.

import { Filter } from 'payload-query';

const query: Filter<Post> = {
  color: {
    equals: 'mint',
  },
};

As you can see the query looks exactly as the ones you're already using, but there's one big difference: this one is fully type-checked. If you change the name of the color property, try to compare it to a value that's not part of the union or try an operator that's not supported for string fields, you'll get a typescript error.

Query type

Sometimes your query is slightly different every time you use it. For instance if you want to only find some documents where the current user is the author. In that cause easch user has a different query. For such situations you can use the Query type:

import { Query } from 'payload-query';

const query: Query<Post, User> = {
  author: {
    equals: user => user.id,
  },
};

With the Query type instead of providing a value as operand, you can instead provide a function that takes some data, typed by the 2nd type parameter, and returns a value. You can also still use values if you choose, as shown in the example below:

import { Query } from 'payload-query';

const query: Query<Post, User> = {
  and: [
    {
      color: {
        equals: 'mint',
      },
    },
    {
      author: {
        equals: user => user.id,
      },
    },
  ],
};

Query > Filter

When you have a Query, you of course need to turn it into a Filter before you can use it in Payload. For this payload-query provides a toFilter function.

import { toFilter } from 'payload-query';
import query = '...';

const data = { id: '123', name: 'Mike' };

const filter = toFilter(query, data);

or, if you want to reuse your query:

import { toFilter } from 'payload-query';
import query = '...';

const authorOnly = toFilter(query);

const filter = authorOnly({ id: '123', name: 'Mike' });

Circular objects

If your collection contains circular references, it is impossible to build the full paths to all possible fields. Therefore such collections cannot be used as type parameter to create a query type. If you run into this issue, there's 2 ways to solve it:

  • use Pick<MyCollection, 'field1'|'field2'> to only pick the fields you want to use in your query
  • use any as collection type (but you'll loose type safety).

Usage - Select

payload-query lets you define a selection of fields that you want in your query results, like a SELECT clause in a SQL statement, or a projection in a MongoDB query.

Select type

The select type defines which fields to select and has the following structure:

type Select<T> = Partial<Record<keyof T, boolean>>;

// To select only color of a post:
const select: Select<Post> = {
  color: true,
  id: false,
};

// Exclude the id field, include everything else
const select2: Select<Post> = { id: false };

By default the id field is selected and all others are not. To select a field, add it to the selection, with a value of true, to omit the id, include it with a value of false. Nested fields are not yet supported and are either all selected or not at all. If the select only contains fields with a value of false, all fields that are not part of the select will be included.

Select - REST api

When using the rest api, a select query parameter can be used to filter to output. To enable this, the selectPlugin must be first added to the payload config:

import { buildConfig } from 'payload/config';
import { selectPlugin } from 'payload-query';

export default buildConfig({
  plugins: [selectPlugin()],
  // The rest of your config goes here
});

The select plugin takes an optional Options parameter, which lets you define on which collections / globals you want to enable select. If omitted, it is enabled on all.

After the plugin is enabled you can use a select query parameter in the same way you would with the where parameter:

http://localhost:3000/api/posts?select[color]=true

Select - Internal api

When using the internal api, select can be used as follows:

import { select } from 'payload-query';

const doc = payload.findByID({ collection: 'posts', id: 123 });
const filtered = select({ color: true }, doc);

Or when using the same filter multiple times:

import { select } from 'payload-query';

const { docs } = payload.find({ collection: 'posts' });
const selectColor = select({ color: true });

const filtered = docs.map(doc => selectColor(doc));

Version history

See changelog

Similar plugins