Jobs are stored in the Payload database in the payload-jobs collection, and you can decide to keep a running list of all jobs, or configure Payload to delete the job when it has been successfully executed. Queuing a new job In order to queue a job, you can use the payload.jobs.queue function. Here’s how you’d… Continue reading Jobs – Queuing a new job in payload
Tag: payload
Using useRouter for Conditional Rendering in Next.js
The useRouter hook from Next.js allows you to access route info inside your components. You can use it for conditional rendering based on the current route. Why Use useRouter? Sometimes, you want to hide/show content depending on the route—like hiding a header on the login page import { useRouter } from ‘next/router’; export default function… Continue reading Using useRouter for Conditional Rendering in Next.js
Restricting Page Access with Middleware in Payload CMS
Payload CMS allows you to use middleware to control access to certain routes or pages. Why Use Middleware? To restrict frontend or admin page access based on authentication or roles, without manually adding logic inside every component or route. Example import { NextFunction, Request, Response } from ‘express’; export const restrictPageAccess = (req: Request, res:… Continue reading Restricting Page Access with Middleware in Payload CMS
Creating Public APIs Without Login in Payload CMS
You can expose APIs in Payload CMS without requiring authentication by adjusting access control in collections or using middleware. Why Do This? Useful for public-facing endpoints like blogs, events, or product listings where login is not needed. Collection Config access: { read: () => true, }, Custom Middleware export const allowPublicAPI = (req, res,… Continue reading Creating Public APIs Without Login in Payload CMS
Populating Relations in Hooks in next js payload CMS
Sometimes, you may need to populate related data in hooks before saving or fetching a document. Example: Before Change Hook import { Payload } from ‘payload’; const populateProfile = async ({ req, data }) => { const profile = await req.payload.findByID({ collection: ‘profiles’, id: data.profile, }); return { …data, profileData: profile }; }; Using Access… Continue reading Populating Relations in Hooks in next js payload CMS
Querying Related Data in the payload
When fetching data via Payload’s REST or GraphQL API, related fields can be populated using depth in the query. REST API Example: GET /api/users?depth=2 This ensures related fields are expanded up to two levels deep. GraphQL Example: query { Users { docs { name profile { bio avatar } } } }
Handling Multiple Relations in payload CMS
You can specify multiple relations within a single field by setting relationTo as an array: { name: ‘relatedItems’, type: ‘relationship’, relationTo: [‘products’, ‘categories’], hasMany: true, } This allows referencing multiple collections (products and categories) in a single field.
What is Payload GraphQL?
Payload GraphQL refers to the GraphQL API provided by Payload, a headless CMS and application framework built with TypeScript, Node.js, React, and MongoDB. Unlike traditional CMS platforms, Payload is code-based and developer-centric, offering a powerful backend that can be extended to power various applications. Its GraphQL API is one of three ways (alongside REST and… Continue reading What is Payload GraphQL?
Implementing Multi-Tenancy in Payload CMS
Multi-tenancy in Payload CMS allows managing multiple tenants (clients, organizations) within a single CMS instance. Setting Up Multi-Tenancy Define a tenant field to associate content with different tenants: const Posts = { slug: ‘posts’, fields: [ { name: ‘tenant’, type: ‘text’, required: true }, { name: ‘title’, type: ‘text’, required: true }, ], access: {… Continue reading Implementing Multi-Tenancy in Payload CMS
GraphQL API with Payload CMS
Payload CMS offers a GraphQL API that allows efficient data fetching with precise control over requested data. GraphQL is enabled by default in Payload CMS. You can access the GraphQL playground at /api/graphql. Fetching Data with GraphQL A simple GraphQL query to fetch posts: query { Posts { docs { … Continue reading GraphQL API with Payload CMS