A complete REST API example demonstrating CRUD operations and advanced query capabilities using NestJS and Azure Cosmos DB for NoSQL. Youtube Video found here
This application demonstrates how to:
- Build a RESTful API with NestJS framework
- Connect to Azure Cosmos DB using the official SDK
- Implement CRUD operations with TypeScript
- Perform advanced queries with pagination, filtering, and sorting
- Optimize queries for performance and cost efficiency
- Handle configuration and environment variables
- Implement proper error handling and middleware
Before you begin, ensure you have the following installed:
- Node.js (v20 or later)
- pnpm (v9.7 or later) or npm v10 or later
- An active Azure subscription with a Cosmos DB account
- Azure CLI (optional, for resource management)
- Postman or similar tool for testing API endpoints
-
Clone the repository:
git clone https://github.com/kenya-data-platform-user-group/Queries-in-Azure-Cosmos-DB-for-NoSQL-with-Nest.js cd Nest-CosmosDB-Query
-
Install dependencies:
pnpm install
or
npm install
-
Create a
.env
file in the root directory with the following variables:PORT=3000 NODE_ENV=development AZURE_COSMOS_DB_ENDPOINT=<your-cosmosdb-endpoint> AZURE_COSMOS_DB_KEY=<your-cosmosdb-key> COSMOSDB_DATABASE_NAME=<your-database-name>
The project uses the following main dependencies:
pnpm add @nestjs/config @azure/cosmos uuid class-transformer class-validator @nestjs/mapped-types
The application creates two containers in your Cosmos DB:
users
- Stores user informationblogs
- Stores blog entries with nested comments
Both containers are created automatically when the application starts for the first time.
# development mode
pnpm run start
# watch mode (recommended for development)
pnpm run start:dev
# production mode
pnpm run start:prod
Method | Endpoint | Description |
---|---|---|
GET | /blogs | Get all blogs |
GET | /blogs/:id | Get a specific blog by ID |
POST | /blogs | Create a new blog |
PATCH | /blogs/:id | Update a specific blog by ID |
DELETE | /blogs/:id | Delete a specific blog by ID |
GET | /blogs/with-comments | Get all blogs with comments |
POST | /blogs/many | Create multiple blogs at once |
GET | /blogs/mock | Create mock blogs for testing |
DELETE | /blogs/all-blogs | Remove all blogs |
GET | /blogs/recent-comments | Find blogs with recent comments |
GET | /blogs/most-active-author-comments | Find most active comment authors |
GET | /blogs/most-active | Find most active blogs by comment count |
POST | /blogs/:id/comments | Add a comment to a blog |
PATCH | /blogs/:blogId/comments/:commentId | Update a comment in a blog |
DELETE | /blogs/:blogId/comments/:commentId | Delete a comment from a blog |
GET | /blogs/:blogId/comments/:commentId | Get a specific comment from a blog |
src
├── app.module.ts # Main application module
├── main.ts # Application entry point
├── http-exception.filter.ts # Global exception filter
├── database/ # Database connection module
│ ├── database.service.ts # Azure Cosmos DB service
│ └── indexing-policies/ # Custom indexing policies
├── blogs/ # Blogs module
│ ├── dto/ # Data Transfer Objects
│ ├── entities/ # Entity definitions
│ ├── blogs.controller.ts # REST API controller
│ └── blogs.service.ts # Business logic with query implementations
└──
This project includes several development tools:
- ESLint : Linting with TypeScript support