Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@odata2ts/odata-query-builder

odata2ts18.4kMIT0.18.2TypeScript support: included

Allows for building type-safe OData queries with a fluent API

query builder, odata, ts, odata client, query, odata2ts, type-safe

readme

npm (scoped)

OData Query Builder

The OData Query Builder allows for building type-safe OData queries for V2 and V4 services.

The query builder depends on generated models and q-objects to offer a powerful and easy-to-use API. These models and q-objects are generated via odata2ts out of an existing OData service.

The query builder offers operations which you probably know from SQL: select, filter, count, skip, top. You also get the ability to expand related entities which in turn can be filtered, selected or further expanded.

Selecting and expanding really means that the client has the power to shape the response structure. GraphQL should come to mind...

Additionally, OData V4 specifies even more functionalities like searching or aggregation.

What Does it Look Like?

A complex query could look like this:

import { createUriBuilderV4 } from "@odata2ts/odata-uri-builder";
import { qPerson } from "../generated-src/QTrippin.ts"

// In this minimal example the path ("People") as well as the q-object (qPerson) are provided manually.
// When using the generated service you will be provided with the builder and the proper q-object
createQueryBuilderV4("People", qPerson)
  .select("lastName", "age")                            // typesafe: only model attributes are allowed
  .filter(qPerson.userName.equals("russellwhyte"))      // typesafe: only string values are allowed for comparison with string props
  .expand("homeAddress")                                // typesafe: only expandable properties are allowed
  .expanding("trips", (builder, qTrip) => {             // typesafe: only expandable properties are allowed
    builder
      .select("tripId", "budget", "description")
      .top(1)
      .filter(qTrip.budget.gt(1000));
  })
  .build();

Result without encoding:
/People?$select=LastName,Age
&$filter=UserName eq 'russellwhyte'
&$expand=HomeAddress,Trips($select=TripId,Budget,Description;top=5;$filter=Budget gt 1000)

Server Capabilities

Be aware that the OData service you use does not have to implement all querying capabilities.

When using an unsupported operation, then the server should respond with 501: Not Implemented (since the world's not perfect, you might face 500: Server Error instead).

Some OData services advertise their capabilities via annotations, but this is not covered by the OData spec. In any case odata2ts doesn't support annotations currently.

Inspiration

The approach of generating certain objects to provide type-specific and type-safe query capabilities is by no means an invention of odata2ts. On the contrary, odata2ts has been built in order to realize this approach for the domain of OData queries.

For us, this concept originates in two outstanding Java libraries which are designed for the domain of database queries: QueryDsl and jOOQ.

odata2ts diverges in some aspects from those libraries, but the similarities should be quite obvious, especially when it comes to filtering.

Documentation

Query Builder Documentation

Main documentation for the odata2ts eco system: https://odata2ts.github.io

Tests

See folder test for unit tests.

The example packages also contain some integration tests for querying.

Support, Feedback, Contributing

This project is open to feature requests, suggestions, bug reports, usage questions etc. via GitHub issues.

Contributions and feedback are encouraged and always welcome.

See the contribution guidelines for further information.

Spirit

This project and this module have been created and are maintained in the following spirit:

  • adhere to the OData specification as much as possible
    • support any OData service implementation which conforms to the spec
    • allow to work around faulty implementations if possible
  • stability matters
    • exercise Test Driven Development
    • bomb the place with unit tests (code coverage > 95%)
    • ensure that assumptions & understanding are correct by creating integration tests

License

MIT - see License.

changelog

Change Log

All notable changes to this project will be documented in this file. See Conventional Commits for commit guidelines.

0.18.2 (2024-12-18)

Note: Version bump only for package @odata2ts/odata-query-builder

0.18.1 (2024-10-28)

Note: Version bump only for package @odata2ts/odata-query-builder

0.18.0 (2024-09-11)

Features

  • compile src & test folders as sanity check (12607f0)

  • generator: allow for numeric enums (#308) (a5c36e6)

0.17.0 (2024-08-25)

Code Refactoring

  • vitest instead of jest & switch to ESM (#300) (7bc8888)

BREAKING CHANGES

  • ESM tends to break stuff

0.16.10 (2024-08-01)

Note: Version bump only for package @odata2ts/odata-query-builder

0.16.9 (2024-07-31)

Note: Version bump only for package @odata2ts/odata-query-builder

0.16.8 (2024-07-31)

Note: Version bump only for package @odata2ts/odata-query-builder

0.16.7 (2024-05-03)

Note: Version bump only for package @odata2ts/odata-query-builder

0.16.6 (2024-04-22)

Note: Version bump only for package @odata2ts/odata-query-builder

0.16.5 (2024-03-23)

Note: Version bump only for package @odata2ts/odata-query-builder

0.16.4 (2023-08-08)

Note: Version bump only for package @odata2ts/odata-query-builder

0.16.3 (2023-07-31)

Note: Version bump only for package @odata2ts/odata-query-builder

0.16.2 (2023-06-14)

Note: Version bump only for package @odata2ts/odata-query-builder

0.16.1 (2023-06-10)

Note: Version bump only for package @odata2ts/odata-query-builder

0.16.0 (2023-06-03)

Features

  • force new minor for new http-client-api (c29d5bc)

0.15.10 (2023-04-18)

Note: Version bump only for package @odata2ts/odata-query-builder

0.15.9 (2023-04-13)

Note: Version bump only for package @odata2ts/odata-query-builder

0.15.8 (2023-04-08)

Note: Version bump only for package @odata2ts/odata-query-builder

0.15.7 (2023-04-04)

Note: Version bump only for package @odata2ts/odata-query-builder

0.15.6 (2023-02-14)

Note: Version bump only for package @odata2ts/odata-query-builder

0.15.5 (2023-02-13)

Note: Version bump only for package @odata2ts/odata-query-builder

0.15.4 (2023-02-13)

Note: Version bump only for package @odata2ts/odata-query-builder

0.15.3 (2023-02-13)

Note: Version bump only for package @odata2ts/odata-query-builder

0.15.2 (2023-02-03)

Note: Version bump only for package @odata2ts/odata-query-builder

0.15.1 (2023-01-10)

Note: Version bump only for package @odata2ts/odata-query-builder

0.15.0 (2023-01-07)

Code Refactoring

  • rename odata-uri-builder to odata-query-builder (#98) (e0de825)

BREAKING CHANGES

  • rename module odata-uri-builder to odata-query-builder; API completely refactored by renaming all models, classes, functions, props from "uri" to "query"

0.14.1 (2023-01-05)

Note: Version bump only for package @odata2ts/odata-uri-builder

0.14.0 (2022-12-21)

Features

  • odata-uri-builder: fully implemented search query option (fec301b)

0.13.0 (2022-12-18)

Note: Version bump only for package @odata2ts/odata-uri-builder

0.12.0 (2022-12-18)

Note: Version bump only for package @odata2ts/odata-uri-builder

0.11.2 (2022-09-09)

Note: Version bump only for package @odata2ts/odata-uri-builder

0.11.1 (2022-09-08)

Note: Version bump only for package @odata2ts/odata-uri-builder

0.11.0 (2022-08-25)

Bug Fixes

  • always create errors with new operator (#54) (562dede)

Features

  • uri-builder: allow for null | undefined in nearly all builder methods (#53) (1dd6629)

0.10.0 (2022-08-11)

Code Refactoring

  • uri-builder: composition over inheritance, proper interfaces (#48) (36c8a0a)

  • uri-builder: expose interfaces instead of implementations through factory function (#50) (ae1d960)

Features

  • uri-builder: V2 implementation of "expanding" method (#49) (e237c61)

BREAKING CHANGES

  • uri-builder: ODataUriBuilders must be created in a new way. Previously ODataUriBuilderV4.create(...), now createUriBuilderV4(...). The params stayed the same.

  • refactor(uri-builder): rename interfaces by removing "Model" suffix

  • fix(service): import and use factory function

  • uri-builder: It was possible to select nested props by using q-props (V2 only); this syntax has been removed and will be replaced by making use of the current V4 syntax: you first expand the property (method "expanding") and then select (or expand) on the expanded entity.

  • uri-builder: ODataUriBuilder was removed from export, it might have served as base class but was of no other use.

0.9.0 (2022-08-01)

Features

  • allow v4 system query $search option (#40) (1904884)

0.8.1 (2022-07-20)

Note: Version bump only for package @odata2ts/odata-uri-builder

0.8.0 (2022-07-11)

Features

0.7.2 (2022-06-30)

Bug Fixes

  • add prebulish script to guarantee building before publishing (b6986db)

0.7.1 (2022-06-30)

Note: Version bump only for package @odata2ts/odata-uri-builder

0.7.0 (2022-05-21)

Features

  • uriBuilder: simply use EntityPaths as select args (fd21405)

  • uriBuilder: support for nested selects and nested expands (V2 only) (a7ed0f5)

  • uriBuilder: v2 support for UriBuilder (7a5a504)

  • Feat/refactor query objects (#20) (67b662a), closes #20

Bug Fixes

  • test: collect code coverage from src folders, thus exhibiting untested code (3acef8b)

  • test: make coverage test run again (f2d360b)

BREAKING CHANGES

  • uriBuilder: ODataUriBuilder has become an interface, while the real implementation is ODataUriBuilderV4

  • no EntityFactory anymore, no nominalized types in interfaces anymore, etc.

  • refactor(qObjects): getEntity with prefix option; by default without prefix

  • fix(qObjects): better QPath modelling

  • refactor(uri-builder): only use QueryObjects for typing & remove QEntityModel stuff

  • refactor(odata2model): generate new QObject classes

  • refactor(service): services now require Model as well as QClass

  • refactor(odata2model): generate services with new QObject classes

  • fix(odata2model): fix integration tests

  • fix: make int-tests for coverage task work again

  • fix: skip run-cli tests (don't work on github)

0.6.3 (2021-10-12)

Note: Version bump only for package @odata2ts/odata-uri-builder

0.6.2 (2021-09-17)

Note: Version bump only for package @odata2ts/odata-uri-builder

0.6.1 (2021-09-16)

Note: Version bump only for package @odata2ts/odata-uri-builder

0.6.0 (2021-08-31)

Features

  • uri-builder: order by implementation for uri-builder (3502b75)

0.5.0 (2021-08-16)

Bug Fixes

  • uri-builder: adapt to QEntityCollectionPath (e1b02c1)

  • more service tests, fix prop names vs odata names (489b690)

  • uri-builder: fix tests (e1919d7)

  • url-builder: don't add anything before the given path (1c20145)

Code Refactoring

  • uri-builder: remove byId function (e22cdae)

Features

  • uri-builder: adapt to new QEntityCollectionPath (3eb3983)

BREAKING CHANGES

  • uri-builder: public API has changed by removing byId()

0.4.0 (2021-08-10)

Bug Fixes

  • uri-builder: adapt to breaking changes (c5b5223)

Code Refactoring

  • QEntityModel without key spec (913cd11)

BREAKING CHANGES

  • [query-objects]: QEntityModel without key spec & __collectionPath; [UrlBuilder]: path must be provided explicitly now for any entity set

0.3.0 (2021-07-08)

Bug Fixes

  • adapt to breaking change of QExpression => QFilterExpression (e54714c)

Features

  • uri-builder: reexport QExpression (13006c7)

BREAKING CHANGES

  • QExpression has become QFilterExpression (the change was actually introduced in a previous commit)

0.2.3 (2021-07-01)

Bug Fixes

  • uri-builder: add index.ts (ef27c99)

0.2.2 (2021-07-01)

Note: Version bump only for package @odata2ts/odata-uri-builder

0.2.1 (2021-07-01)

Note: Version bump only for package @odata2ts/odata-uri-builder