odata-v4-mysql

raw JSON →
0.1.1 verified Fri May 01 auth: no javascript maintenance

OData V4 MySQL Connector (v0.1.1) converts OData V4 query segments like $filter, $select, $skip, $top, $orderby, and $expand into MySQL SQL statements. Provides functions createFilter, createQuery, and createDelete. Supports TypeScript types, can be used with any MySQL driver. No major updates since 2016; considered stable but low activity. Differentiator: direct mapping of OData to MySQL SQL, lightweight, no ORM needed.

error Cannot find module 'odata-v4-mysql'
cause Package not installed or typo in import path.
fix
Run 'npm install odata-v4-mysql' and verify import path is correct.
error Property 'where' does not exist on type '{}'
cause Using createFilter without type assertions in TypeScript.
fix
Add type import: import { createFilter, Filter } from 'odata-v4-mysql'; then const filter: Filter = createFilter(...);
error mysql connection.query is not a function
cause Using wrong MySQL driver or not creating connection properly.
fix
Install 'mysql' package and create connection with mysql.createConnection().
gotcha createFilter returns an object with 'where' and 'parameters', not a complete SQL string.
fix Use filter.where and filter.parameters separately in your query.
gotcha The package does not support authentication or any middleware; it only compiles OData segments to SQL.
fix You must provide your own HTTP server and OData parser (e.g., odata-v4-server).
gotcha The 'createQuery' function is not shown in the README; it exists and can be used for more complex queries.
fix Use createQuery to generate full SELECT statements including $select, $skip, $top, $orderby.
npm install odata-v4-mysql
yarn add odata-v4-mysql
pnpm add odata-v4-mysql

Create a MySQL connection, build an OData $filter, and execute the query with parameters.

import { createFilter } from 'odata-v4-mysql';
import * as mysql from 'mysql';

const connection = mysql.createConnection({
  host: 'localhost',
  user: 'root',
  password: process.env.DB_PASSWORD ?? '',
  database: 'test'
});

// Example OData filter: $filter=Id eq 42
const filter = createFilter('Id eq 42');
connection.query(`SELECT * FROM Users WHERE ${filter.where}`, filter.parameters, (err, rows) => {
  if (err) throw err;
  console.log(rows);
});
connection.end();