Receita: CMS de Blog
Visão Geral
Seção intitulada “Visão Geral”Construa um backend de blog com:
- Artigos com conteúdo em markdown e imagens de capa
- Autores com perfis
- Categorias com uma relação muitos-para-muitos
Coleções
Seção intitulada “Coleções”Autores
Seção intitulada “Autores”import { defineCollection } from "@rebasepro/cms-types";
export const authorsCollection = defineCollection({ slug: "authors", name: "Authors", singularName: "Author", table: "authors", properties: { name: { type: "string", name: "Name", validation: { required: true } }, email: { type: "string", name: "Email", email: true, validation: { required: true, unique: true } }, avatar: { type: "string", name: "Avatar", storage: { storagePath: "avatars", acceptedFiles: ["image/*"], maxSize: 2 * 1024 * 1024 } }, bio: { type: "string", name: "Bio", admin: { multiline: true } } }, admin: { icon: "person" }});Categorias
Seção intitulada “Categorias”import { defineCollection } from "@rebasepro/cms-types";export const categoriesCollection = defineCollection({ slug: "categories", name: "Categories", singularName: "Category", table: "categories", properties: { name: { type: "string", name: "Name", validation: { required: true } }, slug: { type: "string", name: "Slug", validation: { required: true, unique: true } }, color: { type: "string", name: "Color", enum: [ { id: "blue", label: "Blue", color: "blue" }, { id: "green", label: "Green", color: "green" }, { id: "red", label: "Red", color: "pink" }, { id: "orange", label: "Orange", color: "orange" } ] } }, admin: { icon: "label" }});Artigos
Seção intitulada “Artigos”import { defineCollection } from "@rebasepro/cms-types";// The row shape, so callbacks below see typed `values` instead of `unknown`.type Article = { title: string; slug: string; status: string; publishedAt?: Date | null;};
export const articlesCollection = defineCollection({ slug: "articles", name: "Articles", singularName: "Article", table: "articles", history: true, properties: { title: { type: "string", name: "Title", validation: { required: true } }, slug: { type: "string", name: "URL Slug", validation: { required: true, unique: true } }, author: { type: "relation", name: "Author", relation: { kind: "belongsTo", target: () => authorsCollection, localKey: "author_id" } }, categories: { type: "relation", name: "Categories", relation: { kind: "manyToMany", target: () => categoriesCollection, through: { table: "article_categories", sourceColumn: "article_id", targetColumn: "category_id" } } }, status: { type: "string", name: "Status", enum: [ { id: "draft", label: "Draft", color: "gray" }, { id: "review", label: "In Review", color: "orange" }, { id: "published", label: "Published", color: "green" } ], defaultValue: "draft" }, cover_image: { type: "string", name: "Cover Image", storage: { storagePath: "articles/covers", acceptedFiles: ["image/*"] } }, content: { type: "string", name: "Content", admin: { markdown: true } }, excerpt: { type: "string", name: "Excerpt", admin: { multiline: true }, validation: { max: 300 } }, publishedAt: { type: "date", name: "Published At" }, createdAt: { type: "date", name: "Created At", autoValue: "on_create", admin: { readOnly: true } } }, callbacks: { beforeSave: async ({ values, status }) => { // Auto-generate slug if (values.title && !values.slug) { values.slug = values.title .toLowerCase() .replace(/[^a-z0-9]+/g, "-"); } // Set publishedAt when publishing if (values.status === "published" && !values.publishedAt) { // `publishedAt` is a `date` property, so its value is a Date. values.publishedAt = new Date(); } return values; } }, securityRules: [ { operation: "select", using: "{status} = 'published'" }, { operation: "select", ownerField: "authorId" }, { operations: ["insert", "update"], ownerField: "authorId" }, { operation: "delete", roles: ["admin"] } ], admin: { icon: "article", defaultViewMode: "table" }});Configuração
Seção intitulada “Configuração”- Salve os três arquivos em
config/collections/. Adicione-os ao arraycollectionsemconfig/collections/index.tspara escolher onde eles aparecem na navegação — cada arquivo do diretório é carregado de qualquer forma. - Com o
rebase devem execução, é só isso: salvar regenera o esquema, reinicia o backend, e o arranque cria as tabelas, a tabela de junção e suas políticas de row-level security. Fora dorebase dev, contra seu próprio PostgreSQL, executerebase schema generatee depoisrebase db push.
Agora você tem um CMS de blog totalmente funcional com:
- Gerenciamento de autores com upload de avatares
- Marcação de categorias através de relações muitos-para-muitos
- Edição de conteúdo em Markdown
- Fluxo de trabalho Rascunho → Revisão → Publicado
- Slugs de URL gerados automaticamente
- Políticas de RLS limitando os autores aos seus próprios artigos
- Trilha de auditoria completa através do histórico de entidades
Consultando a partir do SDK
Seção intitulada “Consultando a partir do SDK”Use o SDK tipado para buscar artigos com suas relações:
// The row shape you expect back — without it every field arrives as `unknown`.type Article = { title: string; status: string; publishedAt?: string | null; authorId: string; author?: { name: string }; categories?: { name: string }[];};
// Fetch published articles with author and categories includedconst { data: articles } = await client.data .collection<Article>("articles") .where("status", "==", "published") .include("author", "categories") .orderBy("publishedAt", "desc") .limit(10) .find();
// Rows come back flat — there is no `values` wrapper on the SDK.for (const article of articles) { console.log(article.title); console.log(article.author?.name); // Hydrated relation console.log(article.authorId); // Scalar FK console.log(article.categories); // Array of related entities}Relacionados
Seção intitulada “Relacionados”- Definindo Coleções — a API de coleções que esta receita utiliza
- Relações — as vinculações completas de autores e tags
- Regras de Segurança (RLS) — publicando sem expor rascunhos