1.7.0 • Published 1 year ago

typeorm-express-query-builder v1.7.0

Weekly downloads
262
License
MIT
Repository
-
Last release
1 year ago

TypeORM Express Query Builder

This library allows you to transfrom automatically Express.js req.query into TypeORM findOptions queries.

Installation

npm install typeorm-express-query-builder

How it works?

npm.io

Usage

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

import QueryBuilder from 'typeorm-express-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
icontainsReturn entries that contains value and ignoring casefoo__icontains=Lopez
istartswithReturn entries that starts with value and ignoring casefoo__istartswith=R
iendswithReturn entries that ends with value and ignoring casefoo__iendswith=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 range (numeric, dates)foo__between=1,27

Notice: you can use negative logic prefixing lookup with __not.

Example: foo__not__contains=value

Options

Pagination

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

Ordering

OptionDefaultBehaviourExample
order-Order for fields:+: Ascendant -: Descendantorder=+foo,-name,+surname

Selection

OptionDefaultBehaviourExample
select-Fields to select as response. If no provided, it select all fields.select=name,surname,foo.nested
with-Entity relations to attach to querywith=posts,comments

Profile

If you need to disable some capabilities, you can do using shortcuts to enable|disable by default or provide a custom Profile.

A Profile describe capabilities that can be used by clients & its behaviour.

const qb = new QueryBuilder(req.query, 'enabled' | 'disabled' | ConfigProgile)

ConfigProfile

ConfigProfile object looks like:

const customProfile: ConfigProfile = {
  options: {
    pagination: {
      status: 'enabled',
      paginate: true,
      itemsPerPage: 25,
    },
    ordering: {
      status: 'enabled',
    },
    relations: {
      status: 'enabled',
    },
    select: {
      status: 'enabled',
    },
  },
  policy: 'skip',
}
FieldDefaultBehaviourType
options'enabled'Profile optionsProfileOptions
policy'skip'Policy to apply in cases client try use disabled optionsFindPolicyType
1.7.0

1 year ago

1.6.0

2 years ago

1.5.1

2 years ago

1.5.0

3 years ago

1.4.0

3 years ago

1.3.2

3 years ago

1.3.1

3 years ago

1.3.0

3 years ago

1.2.0

4 years ago

1.1.4

4 years ago

1.1.3

5 years ago

1.1.2

5 years ago

1.1.1

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago