Module API
ODataModule
The core module — ORM-agnostic. Import once in your root module.
ODataModule.forRoot()
ODataModule.forRoot(options: ODataModuleOptions): DynamicModuleRegisters the OData service synchronously.
ODataModuleOptions
interface ODataModuleOptions {
/** Base path for the OData service, e.g. '/odata'. Required. */
serviceRoot: string
/** EDM namespace for all entity types. Default: 'Default' */
namespace?: string
/** Maximum value for $top. Requests exceeding this return HTTP 400. Default: 1000 */
maxTop?: number
/** Maximum depth of $expand nesting. Default: 2 */
maxExpandDepth?: number
/** Maximum nesting depth of $filter expressions. Default: 10 */
maxFilterDepth?: number
/** Strategy when a TypeScript type cannot be mapped to an EDM primitive. Default: 'skip' */
unmappedTypeStrategy?: 'skip' | 'error'
/**
* Maximum nesting depth for deep insert POST bodies. Default: 5.
* Prevents denial-of-service via unbounded recursion.
*/
maxDeepInsertDepth?: number
/**
* Fail closed when an entity set has no row-level scope. Default: false.
* See the "Row-level scopes" guide.
*/
requireScope?: boolean
/**
* Maps any exception thrown by an OData route or a $batch operation to an OData error.
* Synchronous; return undefined for the default mapping. See the "Error mapping" guide.
*/
errorMapper?: ODataErrorMapper
/**
* RFC 5646 language of error messages, sent as Content-Language on every error response and
* $batch error part. Default: 'en'. A mapping's own `language` wins.
*/
errorLanguage?: string
/**
* Drop write-body properties that are not in the entity type instead of answering
* 400 Bad Request (OData §11.4.2). Default: false.
*/
ignoreUnknownProperties?: boolean
/**
* @ODataController classes to register and path-patch with serviceRoot.
* Controllers listed here have their route prefix set to `{serviceRoot}/{entitySetName}`.
*/
controllers?: (new (...args: unknown[]) => unknown)[]
}Example:
ODataModule.forRoot({
serviceRoot: '/odata',
namespace: 'MyApp',
maxTop: 500,
maxExpandDepth: 3,
maxFilterDepth: 15,
controllers: [ProductsController, CategoriesController],
})ODataModule.forRootAsync()
ODataModule.forRootAsync(
options: ConfigurableModuleAsyncOptions<ODataModuleOptions>
): DynamicModuleRegisters the OData service with async configuration. Supports useFactory, useClass, and useExisting.
Example with ConfigService:
ODataModule.forRootAsync({
imports: [ConfigModule],
useFactory: (config: ConfigService) => ({
serviceRoot: config.get('ODATA_ROOT', '/odata'),
maxTop: config.get<number>('ODATA_MAX_TOP', 1000),
}),
inject: [ConfigService],
})ODataModule.registeredServiceRoot
static get registeredServiceRoot(): stringReturns the serviceRoot registered via forRoot(). Used by adapter modules (e.g. ODataTypeOrmModule) to inherit the service root without requiring it to be passed again.
ODataModule.globalPrefixExclude()
static globalPrefixExclude(
serviceRootOverride?: string
): ReadonlyArray<{ path: string; method: number }>Build the exclude pattern array for app.setGlobalPrefix() so OData routes (which already include serviceRoot in their PATH_METADATA) don't get double-prefixed by NestJS.
Returns Express 5 splat-style patterns covering both the bare service root and its children — compatible with NestJS 11 and 12.
Parameters:
| Parameter | Type | Description |
|---|---|---|
serviceRootOverride | string? | Explicit service root to use. Defaults to the value registered via forRoot() (or odata). |
Example:
import { ODataModule } from '@nestjs-odata/core'
const app = await NestFactory.create(AppModule)
app.setGlobalPrefix('api', { exclude: ODataModule.globalPrefixExclude() })
// → `/api/users` routes go through; `/odata/...` routes stay unprefixedODataModule.forFeature()
ODataModule.forFeature(entityConfigs: EdmEntityConfig[]): DynamicModuleRegisters EDM entity configurations from a feature module. Typically used directly; the TypeORM adapter (ODataTypeOrmModule.forFeature()) calls this internally.
ODataTypeOrmModule
The TypeORM adapter module. Derives OData EDM from TypeORM entity metadata.
ODataTypeOrmModule.forRoot()
ODataTypeOrmModule.forRoot(options?: {
changesetRunner?: ODataChangesetRunner
batch?: ODataBatchOptions // { enabled?, entitySets?, operationTimeout? }
}): DynamicModuleOptional, imported once in the root module (global). Application-wide adapter settings:
changesetRunnerruns each$batchchangeset atomically in your own unit of work (default: a TypeORMQueryRunnertransaction). See $batch changesets and atomicity. For a runner with injected dependencies, provide theODATA_CHANGESET_RUNNERtoken from a@Global()module instead.batchcontrols the$batchendpoint:enabled: falseanswers 404,entitySetsallow-lists the entity sets operations may reach (resource path, navigation and$expandtargets; others answer 403),operationTimeoutlimits each operation (ms, default 30 000; then 504). Provided under theODATA_BATCH_OPTIONStoken. See $batch.
ODataTypeOrmModule.forFeature()
ODataTypeOrmModule.forFeature(
items: ODataTypeOrmFeatureItem[],
options?: {
serviceRoot?: string
controllers?: Type[]
defaultScope?: ODataScopeDefinition
imports?: ModuleMetadata['imports']
}
): DynamicModuleRegisters TypeORM entities for OData auto-derivation.
items array accepts a mixed list of (EntityClassOrSchema is exported, and is whatever TypeOrmModule.forFeature() accepts: an entity class or an EntitySchema):
- Bare entity class —
Product— the OData entity-set name is derived from the class name (respecting any@ODataEntitySetdecorator, otherwise pluralized). - Object config —
{ entity: UserEntity, name: 'Users' }— explicitly override the OData entity-set name. Takes precedence over@ODataEntitySetand over default pluralization.
type ODataTypeOrmFeatureConfig = {
readonly entity: EntityClassOrSchema
readonly name?: string
/** Row-level scope: a function `(qb, ctx) => void | Promise<void>` or an injectable `ODataScope` class */
readonly scope?: ODataScopeDefinition
/** Route writes through application code: an `ODataWriteHandlers` object or injectable class */
readonly writeHandlers?: ODataWriteHandlersDefinition
/** ETag source: a property name or `(entity) => value`. Overrides @ODataETag / @UpdateDateColumn */
readonly etag?: ODataETagSource
/** PATCH/PUT/DELETE without If-Match answer 428; $metadata gets Core.OptimisticConcurrency */
readonly requireIfMatch?: boolean
}
type ODataTypeOrmFeatureItem = EntityClassOrSchema | ODataTypeOrmFeatureConfigParameters:
| Parameter | Type | Description |
|---|---|---|
items | ODataTypeOrmFeatureItem[] | Entity classes and/or { entity, name } configs |
options.serviceRoot | string? | Optional. Inherits from ODataModule.registeredServiceRoot by default. |
options.controllers | Type[]? | OData controllers (decorated with @ODataController). When provided, forFeature patches their PATH_METADATA with serviceRoot AND registers them — no separate @Module entry needed. |
options.defaultScope | ODataScopeDefinition? | Row-level scope for every item without its own scope. |
options.imports | ModuleMetadata['imports']? | Modules whose providers class-based scopes inject. |
What it does:
- Wraps
TypeOrmModule.forFeature(entities)for repository injection - Registers
TypeOrmEdmInitializerwhich runsonModuleInitto derive EDM from TypeORM metadata (applying any{ entity, name }overrides) - Registers
TypeOrmQueryTranslatorfor$filter,$select,$orderby,$expand,$search,$applytranslation - Registers
TypeOrmAutoHandlerfor zero-boilerplate CRUD operation handling. Multi-entity safe: each request resolves the right Repository from its entity-set name (no need to split into per-feature modules) - Registers
TypeOrmETagProviderandTypeOrmSearchProviderfor ETag concurrency and$searchsupport - Imports
ODataBatchModule, which registersBatchControlleratPOST {serviceRoot}/$batchonce per application: one endpoint serves the entity sets of everyforFeature()module - (When
options.controllersis set) Patches each controller'sPATH_METADATAwithserviceRootand registers it in the dynamic module'scontrollersarray
Examples:
// Simple — bare entity classes
ODataTypeOrmModule.forFeature([Product, Category, Order, OrderItem])
// With set-name overrides (avoids `/odata/UserEntities` URLs)
ODataTypeOrmModule.forFeature([
{ entity: UserEntity, name: 'Users' },
{ entity: ClaudeSessionEntity, name: 'Sessions' },
])
// Single-declaration: controllers live in this dynamic module
ODataTypeOrmModule.forFeature(
[
{ entity: UserEntity, name: 'Users' },
{ entity: ClaudeSessionEntity, name: 'Sessions' },
],
{ controllers: [UsersODataController, SessionsODataController] },
)Request context
ODataModule.forRoot() and forRootAsync() register ODataRequestContextInterceptor as a global interceptor. It keeps the current ExecutionContext and request for the whole handler, so adapters can hand them to application hooks such as row-level scopes.
import { getODataRequestContext, runWithODataRequestContext } from '@nestjs-odata/core'
getODataRequestContext() // { executionContext?, request? } | undefined outside a request
runWithODataRequestContext({ request }, () => handler.handleGet(query, url)) // outside HTTP$batch operations
Each $batch operation runs through its own route, so the request context is the operation's. While it runs, getODataBatchPart() describes it:
import { getODataBatchPart, type ODataBatchPartContext } from '@nestjs-odata/core'
interface ODataBatchPartContext {
method: string // as sent in the batch
url: string // as sent, after $<Content-ID> references are resolved
contentId?: string
inChangeset: boolean
batchRequest: unknown // the platform request of the enclosing $batch request
changeset?: unknown // the changeset's unit of work (TypeORM: its EntityManager)
}
getODataBatchPart(request?) // ODataBatchPartContext | undefined (not in a $batch)The operation is attached to its request (request[ODATA_BATCH_PART], Express adapter) and kept in an AsyncLocalStorage (the only carrier on Fastify). getODataBatchPart(request?) reads request (else the request of the OData request context) first, then the async context: pass the request from guards and middleware.
Lower-level helpers used by the adapter's BatchController, exported for other adapters: dispatchBatchOperation(httpAdapter, { method, url, headers, body?, remoteAddress?, timeoutMs? }, part?) (runs one operation through the Express application or Fastify's inject(), in process, as the batch operation part; rejects with BatchOperationTimeoutError after timeoutMs; the response's headers keep multi-valued headers as arrays, its body is a Buffer), inheritedBatchHeaders(batchHeaders) and batchOperationHeaders(inherited, partHeaders, hasBody) (the headers an operation gets), parseBatchUrl(batchUrl), resolveBatchOperationUrl(operationUrl, batchUrl) (a URL), batchErrorContext(part), headerValue(value) and runWithODataBatchPart(part, fn).
Injection tokens
ODATA_MODULE_OPTIONS
Inject the resolved options (with defaults applied) in custom providers:
import { ODATA_MODULE_OPTIONS, ODataModuleResolvedOptions } from '@nestjs-odata/core'
@Injectable()
export class MyService {
constructor(
@Inject(ODATA_MODULE_OPTIONS)
private readonly options: ODataModuleResolvedOptions,
) {}
}ODataModuleResolvedOptions
interface ODataModuleResolvedOptions {
serviceRoot: string
namespace: string // Always set (default: 'Default')
maxTop: number // Always set (default: 1000)
maxExpandDepth: number // Always set (default: 2)
maxFilterDepth: number // Always set (default: 10)
unmappedTypeStrategy: UnmappedTypeStrategy
maxDeepInsertDepth: number // Always set (default: 5)
}EdmRegistry
The EdmRegistry is a global singleton that stores all registered entity types and sets. It is exported from ODataModule and available for injection throughout the application.
import { EdmRegistry } from '@nestjs-odata/core'
@Injectable()
export class SecurityConfigService implements OnModuleInit {
constructor(private readonly edmRegistry: EdmRegistry) {}
onModuleInit(): void {
this.edmRegistry.setEntitySecurityOptions('Products', {
maxTop: 50,
maxExpandDepth: 1,
maxFilterDepth: 5,
})
}
}EdmRegistry methods
| Method | Signature | Description |
|---|---|---|
register | (entityType, entitySet) => void | Registers an entity type and set |
getEntityType | (name: string) => EdmEntityType | undefined | Gets entity type by name |
getEntitySet | (name: string) => EdmEntitySet | undefined | Gets entity set by name |
getAllEntitySets | () => EdmEntitySet[] | Returns all registered entity sets |
setEntitySecurityOptions | (entitySetName, opts) => void | Sets per-entity security overrides |
getEntitySecurityOptions | (entitySetName) => ODataEntitySecurityOptions | undefined | Gets per-entity security overrides |