nestjs-odata
v1.0.4
Published
NestJS OData module
Readme
@nestjs-odata
Elegant and modular OData integration for NestJS applications.
Supports MongoDB (Mongoose) and SQL (Prisma, TypeORM).
Provides decorators to auto-parse and run OData queries with minimal setup.
📦 Installation
Option 1: Local module (recommended during development)
npm i nestjs-odataOption 2: Directly from GitHub
npm install git+https://github.com/azavjo40/nestjs-odata.git🚀 Usage
1. Import ODataModule in your AppModule
import { ODataModule } from 'nestjs/odata';
@Module({
imports: [
ODataModule.forRoot({
defaultType: 'mongo',
debug: true
})
],
})
export class AppModule {}2. Use decorators in your controller
import { OData, ODataResult } from 'nestjs/odata';
@Get()
@OData(UserModel, 'mongo', {
maxLimit: 100,
allowedFields: ['name', 'email', 'age']
})
findAll(@ODataResult() result) {
return result;
}🧠 What the decorators do
@OData(entity, type, options)— Method decorator- Parses OData query parameters from the request
- Injects repository/service
- Applies filters, sorting, limit, skip, etc.
- Saves result to
req.odataResult
@ODataResult()— Param decorator- Injects the parsed result from OData processing into your method
📖 Supported OData query params
| Parameter | Description | Example |
|---------------|--------------------------|-----------------------------------|
| $filter | Filtering records | age gt 30 and isActive eq true |
| $orderby | Sorting | createdAt desc |
| $top | Limit | 10 |
| $skip | Offset | 20 |
| $select | Select specific fields | email,name |
| $expand | Include relations (Mongo only) | posts,profile |
🛡 Options in @OData
@OData(UserModel, 'mongo', {
maxLimit: 100, // limits $top max value
allowedFields: ['email', 'name'] // filters allowed $select fields
})✍️ Manual Mongo filter support
For now, Mongo filtering supports simple format like:
$filter=age gt 25
$orderby=name desc
$select=name,email🧩 Adapters
Internally, the system uses a simple service-per-type:
ODataMongoServicefor MongooseODataSqlServicefor Prisma/TypeORM (basic version now)
You can extend or replace them using forRoot():
ODataModule.forRoot({
adapters: {
mongo: CustomMongoAdapter,
sql: CustomSqlAdapter
}
})💡 License
MIT — use, modify, contribute freely.
