1.3.5 • Published 2 years ago

typeorm-query-builder-extended v1.3.5

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

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
1.3.5

2 years ago

1.2.8

2 years ago

1.1.9

2 years ago

1.2.7

2 years ago

1.1.8

2 years ago

1.2.6

2 years ago

1.3.4

2 years ago

1.3.3

2 years ago

1.3.2

2 years ago

1.3.1

2 years ago

1.2.2

2 years ago

1.3.0

2 years ago

1.2.9

2 years ago

1.1.0

3 years ago

1.0.9

3 years ago

1.0.8

3 years ago

1.0.11

3 years ago

1.0.10

3 years ago

1.0.13

3 years ago

1.0.12

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago