Skip to content

A highly versatile, fast and secured OData Version 4.01 SQL Connector which provides functionality to convert different types of OData segments into SQL query statements, that can be executed over an SQL database.

License

Notifications You must be signed in to change notification settings

udaysingh236/odata-sql-connect

Repository files navigation

odata-sql-connect

A highly versatile, fast and secured OData Version 4.01 SQL Connector which provides functionality to convert different types of OData segments into SQL query statements, that can be executed over an SQL database.

Potential Usecases

  • Create high speed, Odata 4.01 compliant data sharing APIs.
  • Can be used over most of the famous Databases like My SQL, My SQL server, Oracle and Postgres.

Special Unique features

  • Support for Odata V4.01 new features like 'in' operator.
  • Better and logical support for 'not' expression
  • By default, strict usage of bind variables or sql pramaters to avoid sql and odata injection.
  • Database specific SQL query functions gets generated.
  • Support for almost all the Filter operator.
  • Support for almost all the Query Functions.
  • Support for $orderby, $skip, $top, $count, $select.
  • Better erroring

Example

More examples can be found here

import { odataSql, DbTypes } from '@slackbyte/odata-sql-connect';

// Inpur str for Oracle, Postgres, MySql, MsSql
import { orderByStr, stringFuncs, topSkipObj } from '../tests/allTestInOutput';

const odataSqlPostgres = odataSql({ dbType: DbTypes.PostgreSql });
const { error: filterErr, where, parameters } = odataSqlPostgres.createFilter(stringFuncs);
if (filterErr) {
    console.error(filterErr.message);
} else {
    console.log(`Oracle/Postgres Example => Where clause: ${where}`);
    console.log(`Oracle/Postgres Example => Parameters/Bind Variables: ${Object.fromEntries(parameters)}`);
}

const { error: orderByErr, orderBy } = odataSqlPostgres.createOrderBy(orderByStr);
if (orderByErr) {
    console.error(orderByErr.message);
} else {
    console.log(`Oracle/Postgres Example => orderBy clause: ${orderBy}`);
}

const { error: topSkipErr, top, skip } = odataSqlPostgres.createTopSkip(topSkipObj);
if (topSkipErr) {
    console.error(topSkipErr.message);
} else {
    console.log(`Oracle/Postgres Example => Top: ${top}, Skip: ${skip}`);
}

// FIlter with named parameter prefix

const odataSqlMsSqlPrefix = odataSql({ dbType: DbTypes.MsSql, namedParamPrefix: 'value' });
const { error: filterPrefixErr, where: preWhere, parameters: preParameters } = odataSqlMsSqlPrefix.createFilter(stringFuncs);
if (filterPrefixErr) {
    console.error(filterPrefixErr.message);
} else {
    console.log(`MsSql Example => Where clause: ${preWhere}`);
    console.log(`MsSql Example => Parameters/Bind Variables: ${Object.fromEntries(preParameters)}`);
}

// With Raw parameters

const odataSqlPostgresRaw = odataSql({ dbType: DbTypes.PostgreSql, namedParamPrefix: 'var', useRawParameters: true });
const { error: filterRawErr, where: rawWhere, parameters: rawParameters } = odataSqlPostgresRaw.createFilter(stringFuncs);
if (filterRawErr) {
    console.error(filterRawErr.message);
} else {
    console.log(`Postgres Example => Where clause: ${rawWhere}`);
    const valArr = [...rawParameters.values()];
    console.log(`Postgres Example => Parameters/Bind Variables Values Array: ${valArr}`);
    console.log(`Postgres Example => Parameters/Bind Variables: ${Object.fromEntries(rawParameters)}`);
}

How to build

To run the connector in local, please clone the repository and follow the below steps:

npm install
npm run dev

To test and for test coverage:

npm test
npm run test:report

Supported Features

  • $count
  • $filter
    • Comparison Operators
      • eq
      • ne
      • lt
      • le
      • gt
      • ge
      • has
      • in
    • Logical Operators
      • and
      • or
      • not
    • Arithmetic Operators
      • add
      • sub
      • mul
      • div
      • divby
      • mod
    • String Functions
      • indexof
      • contains
      • endswith
      • startswith
      • length
      • substring
      • tolower
      • toupper
      • trim
      • concat
    • Date Functions
      • year
      • month
      • day
      • hour
      • minute
      • second
      • fractionalseconds
      • date
      • time
      • totaloffsetminutes
      • now
      • mindatetime
      • maxdatetime
    • Math Functions
      • round
      • floor
      • ceiling
    • Type and conditional Functions
      • cast
      • isof
      • case
  • $select
  • $top
  • $skip
  • $orderby

CONTRIBUTING

I love your inputs! and I want to make your contribution to this project easy and transparent, whether it's:

  • Reporting a bug
  • Discussing the current state of the code
  • Submitting a fix
  • Proposing new features

Please raise a pull request. 😊

Made with love in INDIA. ❤️

About

A highly versatile, fast and secured OData Version 4.01 SQL Connector which provides functionality to convert different types of OData segments into SQL query statements, that can be executed over an SQL database.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published