A modern monorepo application built with Next.js frontend, NestJS backend, and Supabase database.
- Frontend: Next.js with App Router (TypeScript)
- Backend: NestJS with Swagger documentation
- Database: Supabase (PostgreSQL with real-time capabilities)
- Monorepo: pnpm workspaces
- Testing: Jest
- Authentication: Supabase Auth (ready to use)
mbspro/
├── apps/
│ ├── web/ # Next.js frontend (port 3000)
│ └── api/ # NestJS backend (port 4000)
├── packages/
│ └── shared/ # Shared utilities and types
├── supabase/
│ ├── schema.sql # Database schema for Supabase
│ └── migrations/
│ └── 2025-01-01-extensions-and-indexes.sql # Enable pg_trgm/unaccent and add indexes
├── package.json # Root package.json with workspace scripts
├── pnpm-workspace.yaml
└── README.md
- Frontend (Web): http://localhost:3000
- Backend (API): http://localhost:4000
- API Documentation: http://localhost:4000/docs (Swagger)
- Node.js 18+
- pnpm 8+
- Supabase account (free tier available)
pnpm iFollow the detailed setup guide in SUPABASE_SETUP.md:
- Create a Supabase project at supabase.com
- Get your project URL, anon key, and service role key
- Apply schema: open
supabase/schema.sqlin Supabase SQL Editor and run
# Backend environment
yarn dlx shx cp apps/api/env.example apps/api/.env || cp apps/api/env.example apps/api/.env
# Frontend environment
yarn dlx shx cp apps/web/env.local.example apps/web/.env.local || cp apps/web/env.local.example apps/web/.env.localEdit apps/api/.env with your Supabase credentials:
SUPABASE_URL=https://your-project-id.supabase.co
SUPABASE_ANON_KEY=your_anon_key_here
SUPABASE_SERVICE_ROLE_KEY=your_service_role_key_here
# Direct Postgres (server only)
DATABASE_URL=postgresql://postgres:<password>@db.<project-ref>.supabase.co:5432/postgres?sslmode=require
PGSSLMODE=requirepnpm --filter @mbspro/api seedChoose one of the following:
-
SQL Editor:
- Open Supabase → SQL Editor, paste
supabase/migrations/2025-01-01-extensions-and-indexes.sql - Run the script (enables
pg_trgm,unaccent, creates trigram + FTS indexes)
- Open Supabase → SQL Editor, paste
-
Supabase CLI (optional):
- Install CLI:
npm i -g supabase - Login and link:
supabase login→supabase link --project-ref <ref> - Run:
supabase db query supabase/migrations/2025-01-01-extensions-and-indexes.sql
- Install CLI:
Verification: Use EXPLAIN on similarity or FTS queries to confirm index usage (no need to paste plans).
pnpm --filter @mbspro/api test:connection # env + Supabase client check
pnpm --filter @mbspro/api test:examples # example queries against mbs_itemspnpm devYour application is now running:
- Frontend: http://localhost:3000
- Backend: http://localhost:4000
- API Docs: http://localhost:4000/docs
- The backend uses the Supabase JavaScript client for data access.
DATABASE_URLis optional for tooling/tests and must includesslmode=require.- Never expose server-side keys or
DATABASE_URLto the frontend.
- Open Supabase Dashboard → Project → Settings → Database → "Connection string" → URI (Node).
- Copy the URI that includes
sslmode=require. - Paste into
apps/api/.envasDATABASE_URL=...(server-side only).
-
SQL Editor (recommended):
- Open Supabase → SQL Editor → paste
supabase/migrations/2025-01-01-extensions-and-indexes.sql→ Run. - This enables
pg_trgmandunaccentand creates trigram and FTS indexes.
- Open Supabase → SQL Editor → paste
-
Supabase CLI (optional):
npm i -g supabasesupabase login→supabase link --project-ref <project-ref>supabase db query supabase/migrations/2025-01-01-extensions-and-indexes.sql
Verification: Use EXPLAIN on similarity or FTS queries in SQL Editor to confirm index usage (no need to paste plans).
- Ensure schema is applied (run
supabase/schema.sqlin SQL Editor). - Set
SUPABASE_URLand a server key (SUPABASE_SERVICE_ROLE_KEYpreferred) inapps/api/.env. - Run:
pnpm seed.- The seed uses UPSERT on primary key
code, so it is safe and idempotent.
- The seed uses UPSERT on primary key
The linear fusion ranker computes score = α * bm25 + β * featureBoost.
- Location:
apps/api/src/suggest/ranker.service.ts(DEFAULT_WEIGHTS). - Weights:
alpha(0–1): weight for lexical score (bm25-like similarity)beta(0–1): weight for feature rules sumw1: telehealth match boost (signals.mode telehealth/video/phone + item.telehealth)w2: telehealth mismatch penalty (mode telehealth but item not telehealth)w3: after-hours match boostw4: duration ≥ threshold boostw5: duration < threshold penaltyw6: chronic/care-plan context boost
Tips:
- Increase
alphato favor pure lexical similarity; increasebeta/w*to emphasize domain rules. - For low recall in telehealth cases, increase
w1or reducew2. - For time-based gating, increase
w4andw5magnitude.
After changes, restart the API and re-run pnpm eval to check metrics.
-
Extension permissions:
- If
CREATE EXTENSIONfails, ensure you run in SQL Editor for your project (public schema). UseCREATE EXTENSION IF NOT EXISTS pg_trgm;andunaccent;. - Some org policies can restrict extensions; check Settings → Database → Extensions.
- If
-
SSL connection issues:
- Use
sslmode=requireinDATABASE_URL. - Our TypeORM config sets
ssl: { rejectUnauthorized: false }whenPGSSLMODE=require(seeapps/api/src/config/database.config.ts).
- Use
-
Zero hits / low recall:
- Increase retrieval Top-K in
LexicalRetrieverService(e.g., 50–100) or widen synonyms. - Expand synonyms in
packages/shared/src/index.ts(SignalExtractor) and/or add keywords to items. - Verify indexes/migration applied; without
pg_trgm/FTS indexes, similarity may be slow or less effective.
- Increase retrieval Top-K in
pnpm dev- Start all applications in development modepnpm build- Build all applications for productionpnpm lint- Run linting across all packagespnpm test- Run tests across all packagespnpm seed- Seed the Supabase database fromdata/mbs_seed.jsonpnpm clean- Clean all build artifacts and node_modulespnpm fresh- Clean and reinstall all dependencies
# Apply schema and migrations via SQL Editor or Supabase CLI
# Then seed the database (idempotent)
pnpm seed
# View Supabase dashboard
# Go to https://supabase.com/dashboard/project/[your-project-id]
# Access database directly
# Use Supabase SQL Editor or connect via connection stringWe previously used a local Postgres container for Day-1. The project now uses Supabase.
- Keep historical commands for reference only:
# (Deprecated) Start database
# pnpm db:up
# (Deprecated) Stop database
# pnpm db:down
# (Deprecated) Logs / psql access
# docker compose logs postgres
# docker exec -it mbspro-postgres psql -U mbspro -d mbsproAll Day-1 requirements have been successfully implemented:
- GET /health returns
200status with{ ok: true, ts: "timestamp", database: true } - POST /suggest returns
201status with{ candidates: [] } - Swagger documentation accessible at http://localhost:4000/docs
- mbs_items table exists in Supabase with ≥ 5 rows of synthetic MBS data
- Database seeded with comprehensive MBS items covering:
- Standard consultations (Items 23, 24, 25)
- After-hours care (Items 24, 25)
- Home visits (Item 36)
- Emergency services (Item 44)
- Row Level Security (RLS) enabled for data protection
- Frontend calls /suggest endpoint successfully
- Shows JSON response in formatted display panel
- Loading and error states implemented
- Form validation and user feedback
pnpm --filter @mbspro/api testpasses all tests- E2E tests verify API endpoints work correctly
- Unit tests for controllers and services
- Single command setup:
pnpm i → configure Supabase → pnpm seed → pnpm dev - Both frontend (port 3000) and backend (port 4000) start together
- Hot reload enabled for development
- Frontend: Next.js 14, TypeScript, React
- Backend: NestJS, TypeScript, Supabase Client
- Database: Supabase (PostgreSQL with real-time, auth, and edge functions)
- Testing: Jest
- Package Manager: pnpm
- Real-time subscriptions for live data updates
- Built-in authentication and authorization
- Row Level Security for data protection
- Automatic API generation from database schema
- Database backups and monitoring
- Edge functions for serverless operations
- Storage for file uploads
- Analytics and usage metrics
- Set up authentication using Supabase Auth
- Add real-time subscriptions for live updates
- Implement file uploads with Supabase Storage
- Deploy to production using Supabase hosting
- Set up monitoring and alerts
For detailed Supabase setup instructions, see SUPABASE_SETUP.md.