Getting Started
OData (Open Data Protocol) is a standardized REST protocol for queryable APIs, backed by OASIS and used by Microsoft, SAP, and enterprise clients. It defines conventions for filtering, sorting, pagination, and metadata — so clients and servers speak the same language.
nestjs-odata gives you spec-compliant OData v4 endpoints in NestJS with zero double-declaration. Define your TypeORM entities once — the library auto-derives the EDM, $metadata, query translation, CRUD, and $batch.
Installation
# pnpm (recommended)
pnpm add @nestjs-odata/core @nestjs-odata/typeorm reflect-metadata
# npm
npm install @nestjs-odata/core @nestjs-odata/typeorm reflect-metadata
# yarn
yarn add @nestjs-odata/core @nestjs-odata/typeorm reflect-metadataRequired peer dependencies (if not already installed):
pnpm add @nestjs/common @nestjs/core typeorm @nestjs/typeormSupported versions
| Peer | Supported ranges |
|---|---|
@nestjs/common | ^10.0.0 || ^11.0.0 || ^12.0.0 |
@nestjs/core | ^10.0.0 || ^11.0.0 || ^12.0.0 |
@nestjs/typeorm | ^10.0.0 || ^11.0.0 || ^12.0.0 |
typeorm | ^0.3.0 || ^1.0.0 |
reflect-metadata | ^0.1.13 || ^0.2.0 |
@nestjs-odata/typeorm needs @nestjs-odata/core 2.2.0 or later (the first core release with NestJS 12 peers).
The repository's own test suites run on NestJS 12 and TypeORM 1 (the workspace's development versions), through the packages' ESM builds. Older majors stay in the peer ranges but are not run in CI.
Both packages ship ESM (import) and CommonJS (require) builds. NestJS 12 is ESM-only, so an ESM application ("type": "module" with "module": "NodeNext") imports the ESM build directly. A CommonJS application on NestJS 12 depends on Node.js loading NestJS's ESM through require(esm) (Node.js 20.19+, 22.12+ or 24+); that combination is not covered by the repository's tests.
TypeScript configuration
Enable decorator metadata in your tsconfig.json:
{
"compilerOptions": {
"experimentalDecorators": true,
"emitDecoratorMetadata": true
}
}Step 1 — Define a TypeORM entity
// product.entity.ts
import { Entity, PrimaryGeneratedColumn, Column } from 'typeorm'
@Entity()
export class Product {
@PrimaryGeneratedColumn()
id: number
@Column()
name: string
@Column('decimal', { precision: 10, scale: 2 })
price: number
@Column({ default: true })
inStock: boolean
}No OData-specific decorators required. The library reads TypeORM metadata automatically.
Step 2 — Create a controller
// products.controller.ts
import { Body, Param, Req, Headers } from '@nestjs/common'
import {
ODataController,
ODataGet,
ODataGetByKey,
ODataPost,
ODataPatch,
ODataDelete,
ODataQueryParam,
type ODataQuery,
} from '@nestjs-odata/core'
import { TypeOrmAutoHandler } from '@nestjs-odata/typeorm'
@ODataController('Products')
export class ProductsController {
constructor(private readonly handler: TypeOrmAutoHandler) {}
@ODataGet('Products', { path: '' })
async getProducts(
@ODataQueryParam('Products') query: ODataQuery,
@Req() req: { originalUrl: string },
) {
return this.handler.handleGet(query, req.originalUrl)
}
@ODataGetByKey('Products')
async getProduct(@Param('key') key: string, @Headers('if-none-match') ifNoneMatch?: string) {
return this.handler.handleGetByKey(key, 'Products', ifNoneMatch)
}
@ODataPost('Products')
async createProduct(@Body() body: Record<string, unknown>) {
return this.handler.handleCreate(body, 'Products')
}
@ODataPatch('Products')
async updateProduct(
@Param('key') key: string,
@Body() body: Record<string, unknown>,
@Headers('if-match') ifMatch?: string,
) {
return this.handler.handleUpdate(key, body, 'Products', ifMatch)
}
@ODataDelete('Products')
async deleteProduct(@Param('key') key: string, @Headers('if-match') ifMatch?: string) {
return this.handler.handleDelete(key, 'Products', ifMatch)
}
}Why req.originalUrl? The handleGet method needs the full request URL (including query string) to build the @odata.context URL in the response envelope. NestJS's @Req() provides the Express/Fastify request object, and originalUrl contains the unmodified path + query string (e.g. /odata/Products?$top=10).
Auto-provided handler:
TypeOrmAutoHandleris automatically registered as an injectable provider byODataTypeOrmModule.forFeature(). You do not need to add it to anyprovidersarray — just inject it in your controller constructor.
Step 3 — Register the modules
// app.module.ts
import 'reflect-metadata'
import { Module } from '@nestjs/common'
import { TypeOrmModule } from '@nestjs/typeorm'
import { ODataModule } from '@nestjs-odata/core'
import { ODataTypeOrmModule } from '@nestjs-odata/typeorm'
import { Product } from './product.entity'
import { ProductsController } from './products.controller'
@Module({
imports: [
// SQLite in-memory for zero-friction setup — no external DB required
TypeOrmModule.forRoot({
type: 'better-sqlite3',
database: ':memory:',
entities: [Product],
synchronize: true,
}),
ODataModule.forRoot({
serviceRoot: '/odata',
controllers: [ProductsController],
}),
ODataTypeOrmModule.forFeature([Product]),
],
})
export class AppModule {}Switching databases: The SQLite in-memory setup requires no external services — perfect for getting started and running tests. For production, swap in PostgreSQL, MySQL, or any TypeORM-supported driver by changing the
TypeOrmModule.forRoot()options (e.g.type: 'postgres',host,port,username,password,database).
Registering multiple entities and custom set names
ODataTypeOrmModule.forFeature() accepts a mixed array of plain entity classes and { entity, name } config objects. Use the object form to override the OData entity-set name without touching the entity class — handy when the TypeORM convention adds an Entity suffix that you don't want in your URLs.
You can also pass controllers in the second argument so you only declare them once (instead of in both ODataModule.forRoot({controllers}) and@Module({controllers})).
@Module({
imports: [
ODataModule.forRoot({
serviceRoot: '/odata',
controllers: [UsersODataController, SessionsODataController],
}),
ODataTypeOrmModule.forFeature(
[
// Bare class → set name auto-derived (pluralized class name)
TagEntity,
// Object form → explicit set name override
{ entity: UserEntity, name: 'Users' },
{ entity: ClaudeSessionEntity, name: 'Sessions' },
],
// Single declaration: controllers go here (no separate @Module entry needed)
{ controllers: [UsersODataController, SessionsODataController] },
),
],
})
export class AppModule {}Requests now reach /odata/Users, /odata/Sessions, /odata/Tags — multi-entity registration in a single forFeature call is safe; each entity set is routed to its own table per request.
Using setGlobalPrefix()
If your app uses app.setGlobalPrefix('api'), exclude the OData service-root routes so they aren't double-prefixed. ODataModule.globalPrefixExclude() returns the correct Express 5 splat patterns:
import { ODataModule } from '@nestjs-odata/core'
import { RequestMethod } from '@nestjs/common'
const app = await NestFactory.create(AppModule)
app.setGlobalPrefix('api', { exclude: ODataModule.globalPrefixExclude() })
await app.listen(3000)The helper reads the serviceRoot registered via forRoot() and returns both the bare-root and child-splat patterns. Pass an explicit value (globalPrefixExclude('/v2/odata')) when overriding for a sub-version mount.
Step 3b — Bootstrap the application
// main.ts
import { NestFactory } from '@nestjs/core'
import type { NestExpressApplication } from '@nestjs/platform-express'
import { AppModule } from './app.module'
async function bootstrap() {
const app = await NestFactory.create<NestExpressApplication>(AppModule)
// Express adds a hash ETag to every response (errors, collections, $metadata). It is not an
// OData ETag, and a client echoing it in If-Match gets 412: turn it off for OData services.
app.set('etag', false)
await app.listen(3000)
}
bootstrap()The library never changes this application-wide setting itself; it logs a warning at startup while Express's etag setting is on. Entities with an ETag (@ODataETag, @UpdateDateColumn or an etag option) keep their own OData ETag header and @odata.etag.
Entities are addressed by their canonical URL, /odata/Products(1) (composite keys: /odata/OrderItems(orderId=1,productId=2)); /odata/Products/1 works too. See Entity URLs.
Step 4 — Query your endpoint
Your OData service is ready. Try these requests:
# List all products
curl http://localhost:3000/odata/Products
# Filter by price
curl 'http://localhost:3000/odata/Products?$filter=price lt 100'
# Select specific fields with count
curl 'http://localhost:3000/odata/Products?$select=name,price&$count=true'
# Get entity metadata
curl http://localhost:3000/odata/$metadataResponse envelope (OData JSON format):
{
"@odata.context": "http://localhost:3000/odata/$metadata#Products",
"value": [
{
"@odata.id": "Products(1)",
"@odata.type": "#Default.Product",
"id": 1,
"name": "Widget",
"price": 9.99,
"inStock": true
},
{
"@odata.id": "Products(2)",
"@odata.type": "#Default.Product",
"id": 2,
"name": "Gadget",
"price": 49.99,
"inStock": false
}
]
}What's auto-generated
GET /odata/$metadata— CSDL XML describing all entity types and setsGET /odata/— OData service document listing all entity setsPOST /odata/$batch— Multi-operation batch endpoint (requiresODataTypeOrmModule); each operation runs through your routes and guardsGET /odata/Products/$count— Plain-text count of entities (automatic withTypeOrmAutoHandler)- Query options —
$filter,$select,$orderby,$top,$skip,$count,$expand
Next steps
- Configuration — Global limits, namespace, async setup
- Query Options —
$filter,$select,$orderby,$search,$apply, pagination - Filter Functions — Lambda
any/all, arithmetic, date/time, string functions - CRUD Operations — Create, update, replace, delete, deep insert, ETag concurrency
- Security —
maxTop,maxExpandDepth,maxFilterDepth, ETag concurrency control