r1tsuu

fields select

by r1tsuu
payload-enchants
APIs & SDKs
v3
^3.0.0-beta.61
MIT

Overview

Summary generated from the README

Adds field selection to Payload's Local API and REST API so responses contain only the requested fields. Selection is passed through the request context in the Local API or a select query parameter in REST. Relationship fields can also define a default select list through a custom option to limit populated data. Built for Payload 3.0 beta.

Features

  • Select fields in Local API find and findByID calls through the context option
  • Select fields in REST API responses with a select query parameter
  • Supports nested paths for group fields and arrays
  • Limits populated relationship documents to chosen fields
  • Per-field defaultSelect option on relationship fields to limit populated data

Use cases

  • Reduce page data size when documents reference each other through relationships
  • Fetch only id, slug and title from linked documents for navigation links
  • Return lean REST responses to front-end clients without extra endpoints

Installation

npm i @payload-enchants/fields-select

334

Stars

28

Forks

28

Open Issues

1 years ago

Last Updated

26

Weekly Downloads

v1.2.2

npm Version

39.1 KB

Unpacked Size

0

Dependencies

Repository stars

Trends appear after 7 days of data. Daily snapshots have not started yet.

Shared by 7 plugins in r1tsuu/payload-enchants.

npm weekly downloads

Trends appear after 7 days of data. Daily snapshots have not started yet.

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

Health Score

47/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/684372049-fields-select.svg)](https://payloaddirectory.dev/plugins/684372049-fields-select)
HTML
<a href="https://payloaddirectory.dev/plugins/684372049-fields-select"><img src="https://payloaddirectory.dev/badge/684372049-fields-select.svg" alt="Payload Directory"></a>
Updated Jan 4, 2025Created Aug 29, 2023Published Jul 16, 2024

README

Fields select plugin for Payload 3.0

Install

pnpm add @payload-enchants/fields-select

In your payload.config.ts:

/// ....
import { fieldsSelect } from '@payload-enchants/fields-select';

export default buildConfig({
  // ...
  plugins: [fieldsSelect()],
});

Description

Adds an option to select fields from Local API / REST API response like with GraphQL As well solves a problem, when you need only specific fields to populate from relationship field with defaultSelect option Could significally reduce your pages data sizes, especially when they are related to each other. PR with more powerful version of this to Payload - https://github.com/payloadcms/payload/pull/5942

Usage

Local API:

/** As well findByID **/
payload.find({
  collection: 'posts',
  context: {
    /**
     * Selects:
     * top level id, title fields
     * text field from "nestedGroup" group field
     * all fields from "nestedArray" field
     * "title" field from populated relationship document
     **/
    select: ['id', 'title', 'nestedGroup.text', 'nestedArray', 'relationship.title'],
  },
});

REST API:

Use select query parameter, example: ?select[0]=id&select[1]=title

Default Select option for relationship fields

const linkField: RelationshipField = {
  custom: {
    /** Field will be populated only with id, slug and title fields **/
    defaultSelect: ['id', 'slug', 'title'],
  },
  name: 'link',
  relationTo: 'pages',
};

Similar plugins