1.3.1 • Published 5 years ago

typeorm-server-query-builder v1.3.1

Weekly downloads
1
License
MIT
Repository
-
Last release
5 years ago

TypeORM Server Query Builder

This library allows you to transfrom automatically url query into TypeORM findOptions queries.

Installation

npm install typeorm-server-query-builder

How it works?

You can use the frontend query builder to go faster without having to worry too much about the syntax.

npm.io

Usage

Use QueryBuilder export from package and pass your req.query as an argument:

import { QueryBuilder } from 'typeorm-server-query-builder';

const builder = new QueryBuilder(req.query);
const builtQuery = builder.build();
// Now your query is built, pass it to your TypeORM repository
const results = await fooRepository.find(builtQuery);

Given the following url query string:

foo/?name__contains=foo&role__in=admin,common&age__gte=18&page=3&limit=10

It will be transformed into:

{
  where: {
    foo: Like('%foo%'),
    role: In(['admin', 'common']),
    age: MoreThanOrEqual(18)
  },
  skip: 20,
  take: 10
}

Different ways of retrieve data

GET, POST method by url query string

GET foo/?name__contains=foo&role__in=admin,common&age__gte=18&page=3&limit=10

POST foo/?name__contains=foo&role__in=admin,common&age__gte=18&page=3&limit=10

app.get('/foo', (req, res) => {
  const queryBuilder = new QueryBuilder(req.query); // => Parsed into req.query
  const built = queryBuilder.build();
})

POST method by body

POST foo/, body: {
  "name__contains": "foo",
  "role__in": "admin,common",
  "age__gte": 18,
  "page": 3,
  "limit": 10
}
app.post('/foo', (req, res) => {
  const queryBuilder = new QueryBuilder(req.body); // => Parsed into req.body
  const built = queryBuilder.build();
})

Available Lookups

LookupBehaviourExample
(none)Return entries that match with valuefoo=raul
containsReturn entries that contains valuefoo__contains=lopez
startswithReturn entries that starts with valuefoo__startswith=r
endswithReturn entries that ends with valuefoo__endswith=dev
isnullReturn entries with null valuefoo__isnull
ltReturn entries with value less than or equal to providedfoo__lt=18
lteReturn entries with value less than providedfoo__lte=18
gtReturns entries with value greater than providedfoo__gt=18
gteReturn entries with value greater than or equal to providedfoo__gte=18
inReturn entries that match with values in listfoo__in=admin,common
betweenReturn entries in rangefoo__between=1,27

Notice

You can use negative logic prefixing lookup with __not. Example: foo__not__contains=value

Querying a column from an embedded entity. Example: user.name=value

OR condition union

// ?$or=name:juste|age__gte:15&$or=user.role:admin
{
  where: [
    { name: 'juste', age: MoreThanOrEqual('15') },
    { user: { role: 'admin' } }
  ]
}

// ?city=Dahomey&$or=name:juste|age__gte:15&$or=user.role:admin
{
  where: [
    { name: 'juste', city: 'Dahomey', age: MoreThanOrEqual('15') },
    { user: { role: 'admin' }, city: 'Dahomey' }
  ]
}

Options

OptionDefaultBehaviourExample
paginationtrueIf true, paginate results. If false, disable paginationpagination=false
page1Return entries for page pagepage=2
limit25Return entries for page page paginated by size limitlimit=15
order-Order for fields:^: Ascendant -: Descendantorder=^foo,-name,^surname
join-Set relationsjoin=posts,comments
select-Set fields selectionselect=name,phoneNumber

Others methods

Remove precautionary fields from the query before building

removeField(field: string): QueryBuilder