Skip to content
项目

关于

Node.js backend for Nellavio dashboard starter

网站与应用开发

这些数字如何统计

实测星标增长

24小时
0
7天
0
15天
正在收集历史
1个月
正在收集历史

各周期内的星标净变化,周期相互重叠,并非每日新增。缺失的测量保持空白。星标代表关注度,不代表软件质量。

README.md

原文

    Nellavio Backend

Open source Node.js backend for Nellavio dashboard

Overview

It provides data for Nellavio - an open source dashboard starter built with Next.js & TypeScript. The REST API serves 7 endpoints covering homepage metrics, orders, customers, products, analytics, calendar events and notifications, described by an OpenAPI 3.1 document. Better Auth enables a production-ready authentication flow, handling session management, user credentials and RBAC with three roles (admin, editor, viewer) enforced via the admin plugin.

The frontend works independently by default - it loads mock data from backendBackup.json and keeps route protection disabled. Connecting this backend enables real database functionality, live data fetching on each request and a complete authentication system.

Tech stack

Node.js, Fastify, PostgreSQL, Prisma, Zod, OpenAPI, Docker, Better Auth

Endpoints

  • /api/data/* - REST API with 7 GET endpoints (homepage, analytics, orders, customers, products, events, notifications) - requires a session
  • /docs - Swagger UI with the OpenAPI document at /docs/json - available outside production only
  • /api/auth/* - Better Auth endpoints (sign-in, sign-up, session management)
  • /api/auth/admin/* - Admin endpoints (list-users, set-role, ban-user, update-user, etc.) - requires admin role
  • /health - Health check endpoint for monitoring

Project Structure

├── prisma
│   ├── migrations           # Database migrations
│   ├── schema.prisma        # Database schema
│   └── seed.ts              # Database seeding script
├── src
│   ├── assets               # Static assets
│   ├── data                 # Mock data for seeding
│   ├── plugins              # Session check, error handler, API docs
│   ├── routes               # REST API routes
│   ├── schemas              # Zod response schemas
│   ├── scripts              # OpenAPI document generator
│   ├── services             # Homepage & analytics data
│   ├── tests                # Test files
│   │   └── helpers          # Test utilities
│   ├── utils                # Utility functions
│   ├── access.ts            # RBAC role & permission definitions
│   ├── auth.ts              # Better Auth configuration
│   ├── config.ts            # Environment validation
│   ├── db.ts                # Prisma client
│   └── server.ts            # Fastify server setup
├── openapi.json             # Generated OpenAPI document
└── package.json

How to run

You can run this backend locally using commands below and access the data in Swagger UI http://localhost:4000/docs or Prisma Studio http://localhost:5555/. Alternatively, you can deploy it on services like AWS, Back4App, Render or Heroku.

Localhost

  1. Clone the repository:
git clone https://github.com/nellavio/nellavio-backend.git
cd nellavio-backend
  1. Install dependencies:
npm install
  1. Set up database:

You need a PostgreSQL database running. You can use an existing local installation, spin one up with Docker, or use a free cloud provider of your choice.

  1. Set up environment variables:

Create a .env file in the root directory with the following variables:

# Database
DATABASE_URL=postgresql://user:password@localhost:5432/dbname

# Better Auth
BETTER_AUTH_SECRET=your-secret-key-here-generate-with-openssl-rand-base64-32
BETTER_AUTH_URL=http://localhost:4000/api/auth
  1. Apply migrations and seed the database:
npx prisma migrate deploy
npx prisma db seed
  1. Run the server:
npm run dev

Server will be available at:

  • REST API: http://localhost:4000/api/data
  • Better Auth: http://localhost:4000/api/auth
  • Health check: http://localhost:4000/health
  • Swagger UI: http://localhost:4000/docs
  1. Set up the first admin (optional):

New users are assigned the viewer role by default. To grant admin access, register a user through the frontend and then run:

npx prisma studio

Open the User table, find your user, change role from viewer to admin, and save. Sign out and sign back in to refresh your session.

Remote Deployment

Set up a PostgreSQL database on your hosting platform, then configure environment variables

  1. Generate and set BETTER_AUTH_SECRET: openssl rand -base64 32
  2. Set DATABASE_URL to your PostgreSQL connection string
  3. Set BETTER_AUTH_URL to your production URL (e.g., https://your-api.app/api/auth)
  4. Set ALLOWED_ORIGINS to your frontend domain (e.g., https://your-app.vercel.app)
  5. (Optionally) Set NODE_ENV=production

Build & Start Commands:

Most platforms will ask for build and start commands. Use the following:

  • Build Command: npm install && npx prisma generate && npm run build
  • Start Command: npx prisma migrate deploy && npm start

After the first deployment, you need to seed the database with initial data. Set the DATABASE_URL in your local .env file to the remote database connection string and run npx prisma migrate deploy and npx prisma db seed.

Connecting Frontend

After deploying backend, you can update your front-end .env file. Follow front-end README.md for specific instructions.

RBAC implementation

Roles and permissions are defined in src/access.ts using Better Auth's createAccessControl. Three roles - admin, editor and viewer - each declare per-resource permissions (dashboard, analytics, orders, customers, products). The admin role additionally inherits Better Auth's built-in admin statements (adminAc), which grant access to user management endpoints.

The roles are registered in src/auth.ts via the admin plugin with viewer as the default role. Admin endpoints (/api/auth/admin/*) such as list-users, set-role and ban-user are restricted to users with the admin role and use cookie-based session authentication. Data endpoints currently use a binary auth gate (authenticated or not) via the verifySession pre-handler - the per-resource permissions are ready to be checked in route handlers when needed.

Available Commands

Command Action
npm install Installs dependencies
npm run build Compiles TypeScript to JavaScript
npm run dev Starts dev server with hot reload
npm start Starts production server at localhost:4000
npm test Runs test suite
npm run test:watch Runs tests in watch mode
npm run lint Runs ESLint to check code quality
npm run lint:fix Runs ESLint and auto-fixes issues
npm run type-check Runs TypeScript type checking
npm run format Formats code with Prettier
npm run format:check Checks if code is properly formatted
npm run openapi:generate Regenerates openapi.json from route schemas

Prisma

Command Action
npx prisma migrate dev --name init Creates and applies migrations based on schema changes
npx prisma migrate deploy Applies existing migrations
npx prisma generate Generates Prisma Client from schema
npx prisma db seed Seeds database with mock data
npx prisma migrate reset Drops database, re-applies all migrations and seeds
npx prisma studio Opens Prisma Studio at localhost:5555

Docker support

You can run this application in a containerized environment using these Docker commands

Command Action
docker build -t nellavio . Builds a Docker image from the Dockerfile
docker run -p 4000:4000 -e DATABASE_URL="DB_URL" nellavio Runs the container with database connection

API docs

The REST API is described by the OpenAPI document in openapi.json, generated from the Zod route schemas. After changing a route or a schema, run npm run openapi:generate - the test suite fails when the file no longer matches the code. Outside production, Swagger UI is served at http://localhost:4000/docs. A static version of the API reference is available online:

https://api-docs.nellavio.com/

Data viewer

There is a simple data viewer available if you want to take a look at the data in a table form. Please note that although the authentication flow is designed to be production-ready, the rest of this backend serves as an optional data source that delivers sample data for dashboard views. As a result, there is no real business logic for the data layer, and most of the database schema is intentionally simplified.

https://data-viewer.nellavio.com/

探索相关项目

加载中…