Socket
Socket
Sign inDemoInstall

typeorm-express-query-builder

Package Overview
Dependencies
0
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    typeorm-express-query-builder

Easily transform an Express req.query into TypeORM query


Version published
Weekly downloads
713
decreased by-10.54%
Maintainers
1
Install size
59.2 kB
Created
Weekly downloads
 

Readme

Source

TypeORM Express Query Builder logo

TypeORM Express Query Builder

Easily transform an Express req.query into TypeORM query






Contributing · License

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?

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
-: Descendant
order=+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

Keywords

FAQs

Last updated on 14 Nov 2022

Did you know?

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc