Dyrected
Examples & RecipesRecipe Library

Restrict content operations by user role

Allow public reads, editor writes, and administrator deletion with collection access rules.

This generated recipe is review-ready source material. Use it from the runtime where it appears in the sidebar and search results.

Allow public reads, editor writes, and administrator deletion with collection access rules.

Use this when

  • only editors can update content
  • restrict deletion to admins
  • make content publicly readable
  • add role based access

Dyrected concepts

access, AuthenticatedUser, roles

Additional packages: No additional packages.

Decisions and cautions

Use this recipe only when its runtime matches the project you are documenting or building. Cloud recipes must stay inside the managed content backend boundary. Self-hosted recipes may use the server runtime, database, hooks, and infrastructure you control.

Complete recipe

This is the canonical source compiled and behavior-tested by @dyrected/knowledge.

import { defineCollection, defineTextField } from "@dyrected/core";

export const Articles = defineCollection({
  slug: "articles",
  access: {
    read: () => true,
    create: ({ user }) => user?.roles?.some((role) => role === "editor" || role === "admin") ?? false,
    update: ({ user }) => user?.roles?.some((role) => role === "editor" || role === "admin") ?? false,
    delete: ({ user }) => user?.roles?.includes("admin") ?? false,
  },
  fields: [defineTextField({ name: "title", label: "Title", required: true })],
});

On this page

Dyrected| Cloud

Get your backend ready in minutes

Use a managed database, storage, APIs, and admin dashboard without setting up the infrastructure yourself.

Set Up My Backend