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

typeorm-query-builder-extended

v1.3.5

Published

Easily transform a query string into a TypeORM SelectQueryBuilder with foreign relation/table

Downloads

153

Readme

This library allows you to automatically transform a Express.js req.query or a valid key/value object into a TypeORM SelectQueryBuilder that allows the option of making queries to a foreign relation/table through the query or key/value object parsed

Installation

yarn add typeorm-query-builder-extended

OR

npm install typeorm-query-builder-extended

How does it work?

A req.query string or valid key/value object is sent to the builder along with the initial SelectQueryBuilder that should have been previously created. The builder converts all props from the query object into a valid SelectQueryBuilder props and appends it to the afore added SelectQueryBuilder

Usage

Use ExtendedQueryBuilder export from package and pass your req.query or key/value object as arguments:

import { ExtendedQueryBuilder } from 'typeorm-query-builder-extended';

const queryBuilder = <YOUR_REPOSITORY>.createQueryBuilder(<YOUR_ALIAS>); // <YOUR_REPOSITORY> i.e: personRepository | <YOUR_ALIAS> i.e: person

const builder = new ExtendedQueryBuilder(queryBuilder, req.query); // The third parameter is an optional array of properties in the req.query that you do not want in the database query object.(i.e: page, limit, etc)
const builtQuery = builder.build();
// Now your query is built, use get the rows queried for
const results = builtQuery.getRawMany();

Structure of string query

One query is mainly of 4 parts:

  • Table Alias The Alias denoting table in the query string
  • Table column name The name of column of the table being queried on
  • Query Lookup The operation done on the table
  • Table Column Value The value of the query operation

Full Structure

foo/?<TABLE_ALIAS>_._<COLUMN_NAME>__<LOOKUP>=<COLUMN_VALUE>

The table alias

Denoted by _._ in the query string

i.e: person_._

/* Omitted... */.createQueryBuilder('person');
  • Or when using joins:
.
.
.
queryBuilder.leftJoin('person.company', 'company');

The table's column name

Denoted by __ i.e:

  • person_._firstName__ (IF YOU HAVE A LOOKUP YOU WANT TO ADD)
  • person_._firstName (IF YOU DO NOT HAVE A LOOKUP YOU WANT TO ADD)

The query's lookup

Check the available lookups here

The table's column value

This is denoted by adding it after your query lookup

Example

  • You can find a demo project of the package here
import { getRepository } from 'typeorm';
import { ExtendedQueryBuilder } from 'typeorm-query-builder-extended';
import { User } from './path-to-your-entity/User'; // Your typeORM entity class

.
.
.

app.get('/foo', (req, res) => {
  const userRepository = getRepository(User);
  const queryBuilder = userRepository.createQueryBuilder('user');
  const builder = new ExtendedQueryBuilder(queryBuilder, req.query);
  const builtQuery = builder.build();
  const results = builtQuery.getMany();
  return res.status(200).send(results); //returned results from the built query
});

Available Lookups

| Lookup | Behaviour | Example | Types Working on | | ------------------ | ------------------------------------------------------------------------------ | ------------------------------------- | ----------------------- | | (none) | Return entries that match with value | foo=sam | String, Number, Enums | | contains | Return entries that contains value | foo__contains=sa | String, Enums | | startswith | Return entries that starts with value | foo__startswith=a | String | | endswith | Return entries that ends with value | foo__endswith=am | String | | lt | Return entries with value less than or equal to provided | foo__lt=18 | Number, Date | | lte | Return entries with value less than provided | foo__lte=18 | Number, Date | | gt | Returns entries with value greater than provided | foo__gt=18 | Number, Date | | gte | Return entries with value greater than or equal to provided | foo__gte=18 | Number, Date | | in | Return entries that match with values in list | foo__in=developer,tester | String, Enums, Number | | between | Return entries in range | foo__between=1,27 | String, Number | | jsonarrcontain | Return entries that match a jsonb array column that contains value in the list | foo__jsonarrcontain=RED,BLUE,ORANGE | String, Number, Enums | | not | Return entries that do not match with value | foo__not=ACTIVE | String, Number, Enums |