Overview
Type-safe extensions for the TypeORM QueryBuilder. Write selects, joins, conditions and ordering as property callbacks the compiler checks against your entities, page through results with totals or cursors, and build the raw SQL you still need with helpers that quote it for the database. The playground runs them in the browser.
Features
- Typed queries: methods that take property callbacks instead of strings.
selectTyped,orderByTyped,groupByTyped/havingTyped,distinctOnTyped(only PostgreSQL),returningTypedfor INSERT / UPDATE / DELETE.whereTyped, also on keys inside JSON columns; parameters can be checked against the column type (strictParams).whereIsInTyped,whereIsAnyTyped(only PostgreSQL), case-insensitive search with escaped wildcards (whereILikeTyped,whereSearchTyped,whereLowerEqualsTyped), trigram similarity as a filter, a score and an order (whereSimilarTyped,whereSearchTypedwithsimilar,addSimilarityScoreTyped,orderBySimilarityTyped, only PostgreSQL; see Fuzzy search with pg_trgm).- typed joins and map joins (
leftJoinTyped,innerJoinAndMapOneTyped, …),typed<Root>()for the aliases a query joins,whereRelationExistsTypedbuilt from the relation metadata.
- Pagination: a page with its total,
hasMoreand the order it was read in (getManyWithTotals,getRawManyWithTotals), keyset pages with a cursor (getManyWithCursor,getRawManyWithCursor), or a page of any query (applyPagination). - Where:
whereIsIn/whereIsNotInthat handle empty arrays;whereIsAnyfor= ANY(array)with the array bound as one parameter, array and JSON operators (only PostgreSQL); null-safewhereIsDistinctFrom; period filters in a time zone (applyPeriodFilter);whereExists/whereNotExistswith the subquery's parameters renamed apart. - Order: order from a client's filter (
applyOrder), or by the position in an array of ids (orderByArrayPosition, only PostgreSQL). - Joins and unions:
leftJoinLateral/innerJoinLateral,innerJoinUnnest,fromUnionand CTE unions. - tableHelper: typesafe SQL fragments: column references, conditions, joins, counts, sums, JSON objects and paths, time zones. Given the entity class, it writes the database name of a renamed column.
- Columns:
@CalculatedColumn, a value each query computes;@GeneratedColumn, a column the database generates, which TypeORM reads and never writes. - Reserved qualifier guard: an opt-in check of the SQL each query runs for a reserved word used as a qualifier without quotes (
order."userId",user.id), which PostgreSQL rejects; it throws or warns before the query runs (only PostgreSQL and CockroachDB).
Getting Started
Get started by installing the package:
npm install typeorm-extensions
Then import the package root to init all extensions:
import 'typeorm-extensions';
Or import specific extension to init only it, if needed:
import 'typeorm-extensions/dist/extensions/name.extension';
Helpers are exported from the package root, and from typeorm-extensions/helpers, which installs no extension. Use the latter if you import extensions one by one:
import { tableHelper } from 'typeorm-extensions/helpers'; // no side effects
import { tableHelper } from 'typeorm-extensions'; // also installs all extensions
What you'll need
- typeorm
^0.3.12or^1.0.0
Use new query builder methods
Examples can be found in the Docs/Examples section, and every method is described in the API reference.