Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@tool-kid/express-query-adapter

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@tool-kid/express-query-adapter

  • 0.1.1
  • latest
  • npm
  • Socket score

Version published
Weekly downloads
4.9K
decreased by-14.09%
Maintainers
1
Weekly downloads
 
Created
Source

Express Query Adapter logo

Express Query Adapter

Easily transform an Express req.query into your favourite query tool






Contributing · License

Installation

npm install @tool-kid/express-query-adapter

How it works?

Usage

Use getQueryBuilder exported from package and pass your req.query as an argument:

import { getQueryBuilder } from '@tool-kid/express-query-adapter';

const builder = await getQueryBuilder({ adapter: 'typeorm' });
const builtQuery = builder.build(req.query);
// Now your query is built, pass it to your favourite tool
const results = await fooRepository.find(builtQuery);

Adapters

  • TypeORM

TypeORM

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 qb = await getQueryBuilder({ adapter: 'typeorm' });
  const built = qb.build(req.query); // => Parsed into req.query
});

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 qb = await getQueryBuilder({ adapter: 'typeorm' });
  const built = qb.build(req.query); // => Parsed into req.body
});

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 = getQueryBuilder({ adapter: 'typeorm', profile: 'enabled' | 'disabled' | ConfigProgile });
const builtQuery = builder.build(req.query);

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

FAQs

Package last updated on 23 Nov 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

SocketSocket SOC 2 Logo

Product

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

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc