Newsletter 3 | 14.11.2024
V3 Stable about to be released!
*Yes we know, that’s been said for the last months… But it seems like we’re on the finish line after Next 15 becoming stable, the US elections etc. There's a community call planned on the 19th November, so make sure to check Discord for more details.
To make the wait sweeter, the Payload team has kicked into higher gear though and released a bunch of very cool new features in recent beta versions:
Join Field
Payload can now natively handle 2 way connections. Example:
You have a collection of “persons” and a collection of “children”, each person has one or many children. The best way to link them is to save the ID of the parent person in a child
Now, in previous versions, when querying a child, you would see the parent and have it populated in an api request, but not the other way around
Now, you can create a join field like this:
{
name: 'children',
type: 'join',
collection: 'children',
on: 'person', // name of field in child that links to parent person
}
and payload will automatically show this sexy field where you can manage all linked children, create new ones and even specify the columns you want to see AND: If you query for a person that has children linked, they will be populated!
Populate property
Speaking about population. You can now selectively choose which collections to populate in a query. Before, you only had the “depth” property to choose how many levels deep you want to populate relationship fields.
Now you have way more control over it. Example:
We have a collection with documents, every document has a relationship field “author” to collection “authors”
http://localhost:3000/api/documents?depth=1
This query fetches 10 documents with depth 1, meaning all direct relationship fields will be populated. If for whatever reason we don’t need the author details in every document, we can now perform this query
http://localhost:3000/api/documents?populate[authors]=false&depth=1
This will only give you back an ID for the author, hence save time and bandwith. There’s a lot more to this, so we’ll likely create a video about the topic very soon.
Free Candy!
Not today, sorry. But would you mind giving us anonymous feedback about how you like the AAP Newsletter?
It just takes 1-2 minutes and helps us massively:
On Demand RSC
Server components were one of the main V3 selling points for us. Unfortunately, they didn’t have access to any document data or ID. Means if you wanted to e.g. create a custom chart in a document, you still had to fetch the data through the client side.
This has changed in Beta 128. Server components in the payload admin panel now get props such as the data & ID of the document you’re editing, or even the specific field data if your Server component is defined as a custom field component.
This enables you to fetch data directly on the server without waiting for the client to load → fetch data → display data cycle.
Example:
import React from 'react'
export default async function Server({ data, user, payload }) {
// data: full document data
// user: full user data
// payload: payload object to perform operations with, e.g.
const childProjects = await payload.find({
collection: 'projects',
where: {
parent: {
equals: data.id // We can use the document data to perform queries
}
}
})
return (
<div>Server Component</div>
)
}
Sort by multiple fields
This is not brand new, but we just recently noticed you can now sort documents by multiple fields.
Just pass in an array of field names instead of a string:
This will sort after price first, if price is the same between 2 docs, it will use the createdAt attribute to determine the order of those 2.
For us this is a game changer. In previous versions, we often had to build our own MongoDB aggregation pipeline to handle more specific requirements like these.
Youtube Channel update
It’s been quiet on our youtube channel for around 2 months now. We didn’t plan to do this, but we were running at capacity with other projects, so unfortunately AAP had to wait a little bit.
We’re not coming back empty handed though, we will have a lot of content coming up this winter, and some interesting news soon!
And that’s it for now! We’re grateful for any constructive feedback, so please reach out to info@10xmedia.de (or even better, fill out the feedback form) if you’re missing something.