Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
73a786a
Add Mastra Next.js Postgres Redis sample
raphaeltm Apr 1, 2026
de74713
Refine mastra postgres redis sample UX
raphaeltm Apr 4, 2026
6712d03
Simplify mastra tasks events sample
raphaeltm Apr 7, 2026
8f22b7e
feat(mastra-sample): SSE streaming, pgvector search, provider-agnosti…
raphaeltm Apr 7, 2026
992327c
chore: add root .gitignore and remove accidental node_modules
raphaeltm Apr 7, 2026
d6fde06
fix(mastra-sample): review cleanup — dead code, missing env vars, typ…
raphaeltm Apr 7, 2026
d799145
docs(mastra-sample): doc comments, dedup filters, fix tsx dep, remove…
raphaeltm Apr 8, 2026
dee95ac
ci: add TEST_EMBEDDING_MODEL to deploy workflow
raphaeltm Apr 8, 2026
784db2b
docs(mastra-sample): align README with standard samples format
raphaeltm Apr 8, 2026
8c92e0b
feat(mastra-sample): route all LLM calls through Mastra for provider …
raphaeltm Apr 10, 2026
b5176b1
Clarify background worker description in README
raphaeltm Apr 10, 2026
83e5023
fix(mastra-sample): remove temporary DROP TABLE on schema init
raphaeltm Apr 10, 2026
bd1598b
Use provider-backed model services for local dev
raphaeltm Apr 15, 2026
a183f82
update to model with better tool calling abilities locally
raphaeltm Apr 15, 2026
7246bc8
Merge branch 'feat/mastra-nextjs-postgres-redis-sample' of github.com…
raphaeltm Apr 15, 2026
d38c65e
update name
raphaeltm Apr 15, 2026
79ab9ad
update name
raphaeltm Apr 15, 2026
a070699
Merge branch 'main' into feat/mastra-nextjs-postgres-redis-sample
raphaeltm Apr 17, 2026
47c69fc
rename mastra++ to mastra-extended
raphaeltm Apr 17, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/deploy-changed-samples.yml
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ jobs:
TEST_MONGO_INITDB_ROOT_USERNAME: ${{ secrets.TEST_MONGO_INITDB_ROOT_USERNAME }}
TEST_MONGO_INITDB_ROOT_PASSWORD: ${{ secrets.TEST_MONGO_INITDB_ROOT_PASSWORD }}
TEST_LLM_MODEL: ${{ secrets.TEST_LLM_MODEL }}
TEST_EMBEDDING_MODEL: ${{ secrets.TEST_EMBEDDING_MODEL }}
TEST_N8N_ENCRYPTION_KEY: ${{ secrets.TEST_N8N_ENCRYPTION_KEY }}
TEST_NC_DB: ${{ secrets.TEST_NC_DB }}
TEST_NC_S3_ENDPOINT: ${{ secrets.TEST_NC_S3_ENDPOINT }}
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ When you add a new sample, make sure to add any config vals to the `deploy-chang
| [Managed LLM](./samples/managed-llm) | An app using Managed LLMs with Defang's OpenAI Access Gateway. | LLM, OpenAI, Python, Bedrock, Vertex | Python |
| [Managed LLM with Docker Model Provider](./samples/managed-llm-provider) | An app using Managed LLMs with a Docker Model Provider, deployed with Defang. | LLM, Python, Bedrock, Vertex, Docker Model Provider | Python |
| [Mastra & Next.js](./samples/mastra-nextjs) | An AI-powered tool for chatting with GitHub repositories using Mastra and Google Gemini. | AI, GitHub, Mastra, Next.js, PostgreSQL, TypeScript | TypeScript, JavaScript, Docker |
| [Mastra & Next.js & PostgreSQL & Redis](./samples/mastra-nextjs-postgres-redis) | A multi-service Mastra support and ops copilot with background jobs, PostgreSQL, Redis, and managed LLM access. | Mastra, Next.js, PostgreSQL, Redis, BullMQ, AI, Agents, Support, Operations | TypeScript, JavaScript, Docker |
| [Model Context Protocol (MCP) Chatbot](./samples/mcp) | An MCP (Model Context Protocol) chatbot assistant built with Next.js, Python, and Anthropic Claude. | MCP, Next.js, Python, Quart, Claude, AI, Anthropic, TypeScript, React, JavaScript | nodejs, python |
| [Metabase & PostgreSQL](./samples/metabase) | A simple Metabase configuration with a PostgreSQL database. | Metabase, PostgreSQL, Analytics, Database | SQL |
| [n8n](./samples/n8n) | A n8n app running on Defang. | n8n, PostgreSQL, Docker | Docker, Shell |
Expand Down
1 change: 1 addition & 0 deletions samples/mastra-extended/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
98 changes: 98 additions & 0 deletions samples/mastra-extended/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
# Mastra & Next.js & PostgreSQL & Redis

[![1-click-deploy](https://raw.githubusercontent.com/DefangLabs/defang-assets/main/Logos/Buttons/SVG/deploy-with-defang.svg)](https://portal.defang.dev/redirect?url=https%3A%2F%2Fgithub.com%2Fnew%3Ftemplate_name%3Dsample-mastra-nextjs-postgres-redis-template%26template_owner%3DDefangSamples)

This sample shows a multi-service AI app built with [Mastra](https://mastra.ai/), Next.js, PostgreSQL (with pgvector), Redis, and BullMQ, deployed with Defang from a single Docker Compose file.

A background worker generates sample tasks and events with an LLM, pushes them to a queue, then they get classified and stored with vector embeddings in PostgreSQL for semantic search. A Mastra-powered chat agent uses tools to inspect the current state before answering questions.

## Prerequisites

1. Download [Defang CLI](https://github.com/DefangLabs/defang)
2. (Optional) If you are using [Defang BYOC](https://docs.defang.io/docs/concepts/defang-byoc) authenticate with your cloud provider account
3. (Optional for local development) [Docker CLI](https://docs.docker.com/engine/install/)

## Development

To run the application locally for development, use the development compose file:

```bash
docker compose -f compose.dev.yaml up --build
```

This will:

- Start the Next.js app on `http://localhost:3000`
- Start PostgreSQL on port 5432
- Start Redis on port 6379
- Start a background worker for item classification
- Start Docker model-provider services for chat + embeddings

Local development uses:

- `ai/qwen2.5:3B-Q4_K_M` for chat/tool-calling
- `mxbai-embed-large` for embeddings

This matches the CrewAI sample's local model setup and relies on Docker Model Runner / model-provider support being available in your local Docker installation. The first run will download both models, so startup can take a few minutes.
If `docker compose -f compose.dev.yaml up` fails with `exec: "model": executable file not found in $PATH`, your local Docker installation does not have Docker Model Runner enabled yet.
To keep iteration practical on CPU-only setups, `compose.dev.yaml` enables `LOCAL_FAST_DATA=true`, which uses deterministic sample generation and classification locally while still exercising the real chat and embedding services.

In deployed environments, the app uses dedicated `chat` and `embedding` model services defined in `compose.yaml`. Defang injects OpenAI-compatible `CHAT_URL` / `CHAT_MODEL` and `EMBEDDING_URL` / `EMBEDDING_MODEL` environment variables automatically, so the application code stays platform-independent.

## Configuration

For this sample, you will need to provide the following [configuration](https://docs.defang.io/docs/concepts/configuration). Note that if you are using the 1-click deploy option, you can set these values as secrets in your GitHub repository and the action will automatically deploy them for you.

### `POSTGRES_PASSWORD`

The password for your PostgreSQL database. You need to set this before deploying for the first time.

*You can easily set this to a random string using `defang config set POSTGRES_PASSWORD --random`*

## Usage

1. Open the app.
2. Click **Generate sample items**.
3. Watch the worker create 10 tasks and 10 events, then fan out per-item classify/embed jobs (progress updates in real time via SSE).
4. Ask questions like:
- `What should I look at first?`
- `Summarize the tasks and events.`
- `Which items seem related?`
- `Find events similar to the deploy rollback.`

## Deployment

> [!NOTE]
> Download [Defang CLI](https://github.com/DefangLabs/defang)

### Defang Playground

Deploy your application to the Defang Playground by opening up your terminal and typing:

```bash
defang compose up
```

### BYOC (Deploy to your own AWS or GCP cloud account)

If you want to deploy to your own cloud account, you can [use Defang BYOC](https://docs.defang.io/docs/tutorials/deploy-to-your-cloud).

The default sample uses Defang's managed model provider services:

- `chat` uses `chat-default`
- `embedding` uses `embedding-default`

If you want to pin different models, edit the `provider.options.model` values in [compose.yaml](/workspaces/defang-global/projects/samples/samples/mastra-nextjs-postgres-redis/compose.yaml).

> [!WARNING]
> **Extended deployment time:** This sample creates a managed PostgreSQL database which may take upwards of 20 minutes to provision on first deployment. Subsequent deployments are much faster (2-5 minutes).

---

Title: Mastra & Next.js & PostgreSQL & Redis

Short Description: A small Defang sample where background jobs classify and embed tasks and events, and a Mastra copilot answers questions with tools.

Tags: Mastra, Next.js, PostgreSQL, Redis, BullMQ, AI, Agents

Languages: TypeScript, JavaScript, Docker
3 changes: 3 additions & 0 deletions samples/mastra-extended/app/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
.next
npm-debug.log
2 changes: 2 additions & 0 deletions samples/mastra-extended/app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
.next
20 changes: 20 additions & 0 deletions samples/mastra-extended/app/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
FROM node:22-bookworm-slim

RUN apt-get update -qq \
&& apt-get install -y curl \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*

WORKDIR /app

COPY package*.json ./

RUN npm ci

COPY . .

RUN npm run build

EXPOSE 3000

CMD ["npm", "start"]
6 changes: 6 additions & 0 deletions samples/mastra-extended/app/next-env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/// <reference types="next" />
/// <reference types="next/image-types/global" />
import "./.next/types/routes.d.ts";

// NOTE: This file should not be edited
// see https://nextjs.org/docs/app/api-reference/config/typescript for more information.
5 changes: 5 additions & 0 deletions samples/mastra-extended/app/next.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import type { NextConfig } from "next";

const nextConfig: NextConfig = {};

export default nextConfig;
Loading
Loading