npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@lembdev/typeorm-filters-converter

v1.0.0

Published

Advanced filters handling and conversion for TypeORM based projects

Downloads

5

Readme

@lembdev/typeorm-filters-converter

This lib is intended to be used for converting filters in the context of SQL-based debate repositories (e.g. TypeORM ones). Particularly, it helps in the creation of filter conditions for using with repository queries and query builders in TypeORM. It also provides ways to handle global filters which are applied to all other filters.

Table of Contents

  1. Constructor
  2. setFilters
  3. getFilters
  4. addFilters
  5. setGlobalFilter
  6. toRepositoryConditions
  7. toQueryBuilderConditions
  8. getAffectedColumns
  9. getAffectedRepositoryRelations

Constructor

constructor(
  filters?: IFilter<T> | IFilter<T>[],
  fieldMapping?: Map<string, string>
)

Creates an instance of filters converter. Takes filters and field mapping as inputs. Filters could either be a single filter or an array of them.

setFilters

setFilters(filters: IFilter<T> | IFilter<T>[]): void

Resets the existing filters and sets the new filters. The new filters can be either a single filter or an array of filters. getFilters

Examples:

const converter = new FiltersConverter();
converter.setFilters({ id: { eq: 1 } });
const converter = new FiltersConverter();
converter.setFilters([{ id: { eq: 1 } }, { id: { eq: 2 } }]);

getFilters

getFilters(): IFilter<T>[]

Returns the filters that are currently being applied, as an array of individual filters.

Examples:

const converter = new FiltersConverter();
converter.setFilters({ id: { eq: 1 } });
converter.setGlobalFilter({ age: { gte: 19 } });
converter.getFilters(); // [{ id: { eq: 1 }, age: { gte: 19 } }]

addFilters

addFilters(filters?: IFilter<T> | IFilter<T>[]): this

Adds more filters without resetting the previously set filters. These new filters can be either a single filter or an array of filters.

Examples:

const converter = new FiltersConverter();
converter.setFilters({ name: { eq: 'Sam' } });
converter.addFilters({ age: { gte: 19 } });
converter.getFilters(); // [{ name: { eq: 'Sam' } }, { age: { gte: 19 } }]

setGlobalFilter

setGlobalFilter(filters?: IFilter<T>): this

Sets a global filter that gets applied to every filter in the filter set.

Examples:

const converter = new FiltersConverter();
converter.setFilters([{ name: { eq: 'Sam' } }, { name: { eq: 'Alex' } }]);
converter.setGlobalFilter({ age: { gte: 19 } });
// same as
converter.setFilters([
  { name: { eq: 'Sam' }, age: { gte: 19 } },
  { name: { eq: 'Alex' }, age: { gte: 19 } },
]);
const converter = new FiltersConverter();
converter.setFilters([{ name: { eq: 'Sam' } }, { name: { eq: 'Alex' } }]);
converter.setGlobalFilter({ age: { gte: 19 } });
converter.setGlobalFilter({ sex: { eq: 'male' } });
// same as
converter.setFilters([
  { name: { eq: 'Sam' }, age: { gte: 19 }, sex: { eq: 'male' } },
  { name: { eq: 'Alex' }, age: { gte: 19 }, sex: { eq: 'male' } },
]);

toRepositoryConditions

toRepositoryConditions(): FindConditions<T>[]

Transforms the filters to a format that can be handled by TypeORM repositories for filtering purposes.

Examples:

const converter = new FiltersConverter();
converter.setFilters({ name: { eq: 'Sam' } });

const users = await this.userRepository.find({
  where: converter.toRepositoryConditions(),
});

toQueryBuilderConditions

toQueryBuilderConditions(): Brackets

Transforms the filters into a format that can be handled by TypeORM's QueryBuilder.

Examples:

const converter = new FiltersConverter();
converter.setFilters({ name: { eq: 'Sam' } });

const users = await this.dataSource
  .createQueryBuilder()
  .select()
  .from('users', 'user')
  .where(converter.toQueryBuilderConditions())
  .getRawMany();

getAffectedColumns

getAffectedColumns(): string[]

Returns an array containing the names of the columns that have been affected by the filter conditions.

getAffectedRepositoryRelations

getAffectedRepositoryRelations(repository: Repository<T>): string[]

Returns an array containing the names of the relations that have been affected by the filter conditions, based on the given repository.