Your First Project

Updated for nself v0.4.8 v0.4.8


Let's build your first nself project step by step. We'll create a complete backend for a todo application with user authentication, real-time data, and file storage.

What We'll Build

In this tutorial, we'll create a backend that includes:

  • PostgreSQL database with todo items table
  • GraphQL API with Hasura
  • User authentication with JWT tokens
  • File storage for attachments
  • Real-time subscriptions for live updates

Step 1: Create Project Directory

First, create a new directory for your project and navigate to it:

mkdir todo-backend
cd todo-backend

Step 2: Initialize nself

Initialize a new nself project:

nself init

You'll be asked a few questions (or accept defaults):

  • Project name
  • Domain (default: local.nself.org)
  • Which services to enable

What gets created:

todo-backend/
├── .env           # Your configuration
├── .env.secrets   # Sensitive credentials (auto-generated)
└── nself/         # Project directory

Step 3: Build and Start

Generate the Docker configuration and start services:

nself build    # Generate docker-compose.yml, nginx configs, etc.
nself start    # Launch all services

First start takes 2-5 minutes (downloading Docker images).

Check status:

nself status   # Service health
nself urls     # Access URLs

Step 4: Define Database Schema

nself uses a database-first approach. Create schema.dbml to define your todo application schema:

// Todo Application Schema

Table users {
  id uuid [pk, default: `gen_random_uuid()`]
  email varchar(255) [unique, not null]
  display_name varchar(255)
  created_at timestamp [default: `now()`]
  updated_at timestamp [default: `now()`]
}

Table todos {
  id uuid [pk, default: `gen_random_uuid()`]
  user_id uuid [not null, ref: > users.id]
  title varchar(255) [not null]
  description text
  completed boolean [default: false]
  due_date timestamp
  created_at timestamp [default: `now()`]
  updated_at timestamp [default: `now()`]
  
  indexes {
    user_id
    completed
    (user_id, completed) [name: "idx_user_completed"]
  }
}

Table todo_attachments {
  id uuid [pk, default: `gen_random_uuid()`]
  todo_id uuid [not null, ref: > todos.id]
  file_url varchar(500) [not null]
  file_name varchar(255) [not null]
  file_size integer
  mime_type varchar(100)
  uploaded_at timestamp [default: `now()`]
  
  indexes {
    todo_id
  }
}

Step 5: Apply Schema

Apply your schema with a single command:

nself db schema apply schema.dbml

This command:

  1. Creates SQL migration from your DBML
  2. Runs the migration
  3. Generates mock data for testing
  4. Seeds sample users

Sample users created (local/staging):

  • admin@example.com (admin role)
  • user@example.com (user role)
  • demo@example.com (viewer role)

Step 6: Access Your Services

Trust SSL certificates for HTTPS access:

nself trust

Your backend is now running! Access the services:

ServiceURLDescription
GraphQL APIhttps://api.local.nself.orgHasura console + API
Authhttps://auth.local.nself.orgAuthentication service
Adminhttps://admin.local.nself.orgAdmin dashboard (if enabled)
Mailhttps://mail.local.nself.orgEmail testing UI (if enabled)

Step 7: Test Your API

Open the Hasura Console at https://api.local.nself.org/console and try this GraphQL query:

mutation CreateTodo {
  insert_todos_one(object: {
    title: "Learn nself"
    description: "Complete the tutorial"
    user_id: "00000000-0000-0000-0000-000000000000"
  }) {
    id
    title
    created_at
  }
}

Next Steps

Add Authentication

Configure authentication rules in Hasura Console:

  1. Go to Data -> todos -> Permissions
  2. Add role "user"
  3. Set row-level security: { "user_id": { "_eq": "X-Hasura-User-Id" } }

Install Plugins (v0.4.8)

Extend your backend with third-party integrations:

# List available plugins
nself plugin list

# Install Stripe for payments
nself plugin install stripe

# Install GitHub for CI/CD integration
nself plugin install github

# Sync data from Stripe
nself plugin stripe sync

# View synced customers
nself plugin stripe customers list

Deploy to Production

When ready for production:

# Create production environment
nself env create prod production

# Edit server configuration
# .environments/prod/server.json

# Deploy
nself deploy prod

Complete Project Files

Your project structure should now look like:

todo-backend/
├── .env                  # Main configuration
├── .env.secrets          # Sensitive credentials
├── schema.dbml           # Your database schema
├── docker-compose.yml    # Generated orchestration
├── nginx/                # Reverse proxy configs
├── postgres/             # Database initialization
│   └── migrations/       # SQL migrations
├── ssl/                  # SSL certificates
├── services/             # Custom services (if any)
└── nself/
    ├── migrations/       # Migration files
    ├── seeds/            # Seed data files
    ├── types/            # Generated types
    └── backups/          # Database backups

Summary

Congratulations! You've built a complete backend with:

  • PostgreSQL database with custom schema
  • GraphQL API with Hasura
  • Authentication service ready
  • File storage configured
  • Local SSL domains working
  • Development environment running

v0.4.8: Plugin System Available

Extend your backend with Stripe, GitHub, or Shopify integrations. Run nself plugin list to see available plugins and nself plugin install stripe to get started.

Next, explore: