odata-v4-sql

raw JSON →
0.1.2 verified Fri May 01 auth: no javascript

An OData V4 request to SQL query compiler, converting OData query segments ($filter, $select, $skip, $top, $orderby, $expand) into SQL WHERE clauses or full SELECT statements. Version 0.1.2 is current, with basic support for $filter, $select, $skip, $top, $orderby, and $expand. It differs from alternatives by focusing solely on SQL generation, not full OData server implementations. Ships TypeScript types.

error Cannot find module 'odata-v4-sql'
cause Package not installed or typo in package name.
fix
Run npm install odata-v4-sql
error createFilter is not a function
cause Default import used instead of named import.
fix
Use import { createFilter } from 'odata-v4-sql'
error TypeError: filter.from is not a function
cause Using deprecated `from` method on filter object.
fix
Use createQuery instead of filter.from()
error Unexpected token in OData filter
cause Invalid OData filter syntax passed to createFilter.
fix
Validate filter string against OData spec or use odata-v4-parser for better error messages.
gotcha The createFilter function does not sanitize input; SQL injection risk if used with raw user input.
fix Always validate or parameterize queries when combining with user input.
deprecated The `from` method on the filter result is deprecated.
fix Use `createQuery` instead to generate full SELECT statements.
breaking In version 0.1.0, the `createFilter` signature changed from accepting a single parameter to requiring an options object.
fix Update calls to pass options object: createFilter(filterString, options).
gotcha Only $filter, $select, $skip, $top, $orderby, and $expand are supported; $count, $search, $apply are not implemented.
fix Use alternative packages or manually implement unsupported features.
npm install odata-v4-sql
yarn add odata-v4-sql
pnpm add odata-v4-sql

Shows how to convert an OData $filter string into a SQL WHERE clause using the createFilter function.

import { createFilter } from 'odata-v4-sql';

// Example OData $filter from request: "Id eq 42 and Status eq 'active'"
const filter = createFilter("Id eq 42 and Status eq 'active'");
console.log(filter.where); // WHERE clause: "[Id] = 42 AND [Status] = 'active'"
// Use with your SQL driver:
// db.query(`SELECT * FROM Users WHERE ${filter.where}`);