npm.io
1.3.5 • Published 6 months ago

typeorm-extensions

Licence
ISC
Version
1.3.5
Deps
0
Size
69 kB
Vulns
0
Weekly
0
Stars
3

TypeORM Extensions

npm version npm downloads Issues PRs Welcome

Description

TypeORM Extensions is a library that provides additional functionality and extensions for the TypeORM QueryBuilder. It aims to simplify common tasks and enhance the capabilities of TypeORM.

Features

  • Simplicity: applying pagination and ordering to queries is as simple as calling a method.
  • Type Safety: the library is written in TypeScript and provides type-safe QueryBuilder methods based on Entity metadata.
  • Flexibility: the library is designed to be flexible and can be used with any TypeORM entity.

Installation

To install typeorm-extensions:

Install using npm, yarn or pnpm:

npm install typeorm-extensions

Usage

import 'typeorm-extensions'; // Import the library root to extend the QueryBuilder with all extensions

// Or init specific extension: import 'typeorm-extensions/dist/extensions/pagination.extension';

const query = myDataSource
  .createQueryBuilder()
  .from(UserEntity, 'users')
  // Join relation defined in entity model
  .leftJoinTyped(user => user.images, 'images')
  // Select type-safe properties
  .selectTyped(user => ([
    id: user.id,
    name: user.name,
    email: user.email,
  ]))
  .whereTyped(user => user.name, 'ILIKE', 'John')
  .andWhereTyped(user => user.metadata, ` ->> 'jsonField' =`, 'some-value')
  // Order by type-safe own and relation properties
  .orderByTyped(user => user.images.url, 'ASC', 'NULLS LAST')
  // Use pagination as simple as that
  .applyPaginationFilter({ page: 1, pageSize: 10 }, { useTakeAndSkip: true });

Documentation

For more information, please refer to the documentation.

ToDo list

  • Add tests for pagination and order extensions
  • Virtual column decorator and its query-builder methods (addSelectVirtual, orderByVirtual, getManyWithVirtual, getOneWithVirtual)
  • Add withVirtualColumns option to findManyWithTotals method to use getManyWithVirtual instead of getMany
  • Add isVirtual option to OrderParam to indicate usage of orderByVirtual method