# Contember

> GraphQL-first headless CMS with TypeScript schema definition.

- Category: CMS & Blogging
- Difficulty: intermediate
- Components: contember, postgres
- Tags: contember, graphql, typescript, schema-first
- Source: https://docker.recipes/cms/contember

## Overview

Contember is a modern GraphQL-first headless CMS that takes a unique schema-first approach, allowing developers to define their content models using TypeScript decorators and classes. Unlike traditional CMSs that require manual database schema management, Contember automatically generates GraphQL APIs, database migrations, and admin interfaces from your TypeScript schema definitions. This approach eliminates the typical disconnect between backend models and frontend requirements while maintaining type safety throughout the entire stack.

This stack combines Contember Engine with PostgreSQL to create a powerful content management foundation where schema changes propagate automatically through the entire system. When you modify your TypeScript schema definitions, Contember generates the necessary database migrations, updates the GraphQL API endpoints, and refreshes the admin interface without manual intervention. PostgreSQL provides the robust relational foundation needed for Contember's advanced features like content versioning, complex relationships, and built-in access control lists.

This combination is ideal for development teams who want the flexibility of headless CMS architecture without sacrificing developer experience or type safety. Frontend developers benefit from strongly-typed GraphQL queries that match their TypeScript models, while backend developers can focus on business logic rather than boilerplate API code. The automatic migration system makes this stack particularly valuable for teams practicing continuous deployment, as schema changes can be version-controlled and deployed with confidence.

## Features

- TypeScript-first schema definition with automatic GraphQL API generation from decorated classes
- Built-in Access Control Lists (ACL) system with role-based permissions defined in schema
- Automatic database migration generation when TypeScript schema models change
- Content versioning and audit trail tracking all changes with PostgreSQL JSONB storage
- Multi-tenant architecture support with isolated data and configurable permissions
- Real-time GraphQL subscriptions for live content updates using PostgreSQL LISTEN/NOTIFY
- Integrated admin panel SDK that auto-generates forms based on schema definitions
- Advanced PostgreSQL features like full-text search and JSON querying for content fields

## Use cases

- E-commerce platforms requiring complex product catalogs with custom fields and relationships
- Multi-brand corporate websites needing shared content management with tenant isolation
- SaaS applications requiring user-generated content with granular permission controls
- News and publishing platforms with editorial workflows and content scheduling
- API-first development where mobile apps and web frontends share content sources
- Rapid prototyping of content-heavy applications with evolving data requirements
- Enterprise applications needing audit trails and version control for compliance

## Prerequisites

- Minimum 1GB RAM for PostgreSQL operations and Contember Engine compilation
- Docker and Docker Compose with support for depends_on service ordering
- Basic TypeScript knowledge for schema definition and model relationships
- Understanding of GraphQL queries and mutations for API integration
- Port 4000 available for Contember Engine GraphQL endpoint access
- Environment variables configured for database credentials and admin access

## docker-compose.yml

```yaml
services:
  contember-engine:
    image: contember/engine:latest
    container_name: contember
    restart: unless-stopped
    environment:
      DEFAULT_DB_HOST: postgres
      DEFAULT_DB_NAME: contember
      DEFAULT_DB_USER: contember
      DEFAULT_DB_PASSWORD: ${DB_PASSWORD}
      TENANT_DB_NAME: contember
      CONTEMBER_ROOT_EMAIL: ${ADMIN_EMAIL}
      CONTEMBER_ROOT_PASSWORD: ${ADMIN_PASSWORD}
    ports:
      - "4000:4000"
    depends_on:
      - postgres
    networks:
      - contember-network

  postgres:
    image: postgres:16-alpine
    container_name: contember-postgres
    environment:
      POSTGRES_USER: contember
      POSTGRES_PASSWORD: ${DB_PASSWORD}
      POSTGRES_DB: contember
    volumes:
      - postgres_data:/var/lib/postgresql/data
    networks:
      - contember-network

volumes:
  postgres_data:

networks:
  contember-network:
    driver: bridge
```

## .env

```bash
DB_PASSWORD=changeme
ADMIN_EMAIL=admin@example.com
ADMIN_PASSWORD=changeme
```

## Usage notes

- Docs: https://docs.contember.com/
- GraphQL API at http://localhost:4000/content
- Define schema in TypeScript: @contember/schema
- Automatic database migrations from schema changes
- Built-in ACL (Access Control Lists) for permissions
- Admin panel with @contember/admin SDK

## Troubleshooting

- Schema validation failed during migration: Check TypeScript decorators syntax and ensure all relationships reference existing entities
- GraphQL endpoint returns 500 errors: Verify PostgreSQL connection and ensure database user has CREATE and ALTER privileges
- Admin interface shows empty forms: Confirm schema models include proper field decorators and admin panel configuration
- Migration files not generating: Restart Contember Engine after schema changes and check TypeScript compilation errors
- Access denied errors in GraphQL queries: Review ACL definitions in schema and verify user permissions match query requirements
- PostgreSQL connection refused: Ensure postgres service starts before contember-engine using depends_on configuration

