New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

typeorm-query-builder-extended

Package Overview
Dependencies
Maintainers
1
Versions
28
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

typeorm-query-builder-extended

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

latest
Source
npmnpm
Version
1.3.5
Version published
Weekly downloads
12
50%
Maintainers
1
Weekly downloads
 
Created
Source

TypeORM Query Builder Extended

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

LookupBehaviourExampleTypes Working on
(none)Return entries that match with valuefoo=samString, Number, Enums
containsReturn entries that contains valuefoo__contains=saString, Enums
startswithReturn entries that starts with valuefoo__startswith=aString
endswithReturn entries that ends with valuefoo__endswith=amString
ltReturn entries with value less than or equal to providedfoo__lt=18Number, Date
lteReturn entries with value less than providedfoo__lte=18Number, Date
gtReturns entries with value greater than providedfoo__gt=18Number, Date
gteReturn entries with value greater than or equal to providedfoo__gte=18Number, Date
inReturn entries that match with values in listfoo__in=developer,testerString, Enums, Number
betweenReturn entries in rangefoo__between=1,27String, Number
jsonarrcontainReturn entries that match a jsonb array column that contains value in the listfoo__jsonarrcontain=RED,BLUE,ORANGEString, Number, Enums
notReturn entries that do not match with valuefoo__not=ACTIVEString, Number, Enums

Keywords

typeorm

FAQs

Package last updated on 10 Apr 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts