kentloog/nestjs-sequelize-typescript
关于
Nest + sequelize-typescript + JWT + Jest + Swagger
其他项目实测星标增长
- 24小时
- 0
- 7天
- 0
- 15天
- 0
- 1个月
- 正在收集历史
各周期内的星标净变化,周期相互重叠,并非每日新增。缺失的测量保持空白。星标代表关注度,不代表软件质量。
README.md
原文
NestJS + Sequelize + TypeScript starter
A starter kit demonstrating a modern NestJS REST API: JWT authentication, users and posts CRUD, schema migrations, Swagger docs, and a test setup with unit tests and self-contained e2e tests (Testcontainers — no local test database needed).
Tech stack
- NestJS 11.x on Node.js 24 (LTS)
- TypeScript 6.0 (strict)
- Sequelize 6.x + sequelize-typescript via @nestjs/sequelize
- PostgreSQL 18 with the node-postgres driver
- JWT authentication with @nestjs/jwt + bcrypt password hashing
- Swagger API docs via @nestjs/swagger
- Jest 30.x + Testcontainers for testing
- ESLint 10.x + Prettier for code quality
Prerequisites
- Node.js >= 24 and npm >= 11 (
.nvmrcis included —nvm use) - PostgreSQL >= 15 (recommended: 18) for local development
- A Docker-compatible runtime (Docker Desktop, Colima, Podman, ...) — only needed for the e2e tests
Quick start
nvm use
npm install
# create the database and schema (defaults: postgres/postgres@localhost:5432, database "nest")
npm run db:create
npm run db:migrate
# optional: seed sample users
npm run db:seed-dev
# run in watch mode
npm run start:dev
The API is now available at http://localhost:3000 and the Swagger UI at http://localhost:3000/documentation.
Note: the application does not create or alter tables at startup — migrations are the single source of truth for the schema. Run
npm run db:migrateafter pulling changes that add migrations.
Configuration
All settings are environment variables with development defaults:
| Variable | Default (development) | Description |
|---|---|---|
NODE_ENV |
development |
production enables the production config |
DATABASE_HOST |
localhost |
PostgreSQL host |
DATABASE_PORT |
5432 |
PostgreSQL port |
DATABASE_USER |
postgres |
PostgreSQL user |
DATABASE_PASSWORD |
postgres |
PostgreSQL password |
DATABASE_DATABASE |
nest |
Database name |
JWT_PRIVATE_KEY |
jwtPrivateKey |
Secret used to sign and verify JWTs — set a real secret in production |
JWT_EXPIRES_IN |
1d |
Token lifetime (e.g. 15m, 12h, 7d) |
In production (NODE_ENV=production) there are no defaults — the DATABASE_* and JWT_*
variables must be provided explicitly.
API overview
| Method | Endpoint | Auth | Description |
|---|---|---|---|
POST |
/users/register |
— | Register a new user, returns a JWT |
POST |
/users/login |
— | Log in, returns a JWT |
GET |
/users |
Bearer | List all users |
GET |
/users/me |
Bearer | Get the authenticated user |
PUT |
/users/me |
Bearer | Update the authenticated user |
DELETE |
/users/me |
Bearer | Delete the authenticated user |
GET |
/posts |
— | List all posts |
GET |
/posts/:id |
— | Get a single post |
POST |
/posts |
Bearer | Create a post |
PUT |
/posts/:id |
Bearer | Update your own post |
DELETE |
/posts/:id |
Bearer | Delete your own post |
Register or log in to receive a token, then send it as Authorization: Bearer <token>
(in Swagger UI: the Authorize button).
Testing
# unit tests
npm run test
# e2e tests (requires a running Docker daemon)
npm run test:e2e
# coverage
npm run test:cov
The e2e suite is fully self-contained: Testcontainers starts a throwaway PostgreSQL 18 container, runs the migrations against it, and removes it afterwards. No test database setup is required, and it works out of the box on GitHub Actions ubuntu runners.
All scripts
| Script | Description |
|---|---|
npm run start / start:dev / start:debug |
Run the app (once / watch mode / debugger attached) |
npm run build + npm run start:prod |
Compile to dist/ and run the compiled app |
npm run test / test:watch / test:cov / test:debug |
Unit tests |
npm run test:e2e |
End-to-end tests (Testcontainers) |
npm run lint / lint:fix |
Lint (and auto-fix) |
npm run format |
Format with Prettier |
npm run db:create / db:drop |
Create / drop the database |
npm run db:migrate |
Apply pending migrations |
npm run db:seed-dev |
Seed development sample data |
npm run db:reset |
Drop, recreate, migrate, and seed |
Running in production
npm run build
NODE_ENV=production \
DATABASE_HOST=db.host.com \
DATABASE_PORT=5432 \
DATABASE_USER=user \
DATABASE_PASSWORD=pass \
DATABASE_DATABASE=database \
JWT_PRIVATE_KEY=<strong-secret> \
JWT_EXPIRES_IN=1d \
npm run start:prod
Remember to run the migrations against the production database as part of your deploy
(npm run db:migrate with the same DATABASE_* environment variables).
License
探索相关项目
加载中…