Socket
Socket
Sign inDemoInstall

typeorm-simple-query-parser

Package Overview
Dependencies
257
Maintainers
1
Versions
18
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    typeorm-simple-query-parser

TypeORM Query Parser


Version published
Weekly downloads
141
increased by18.49%
Maintainers
1
Install size
22.9 MB
Created
Weekly downloads
 

Readme

Source

Introduction

Typeorm Query Parser is url string parser for typeorm.

Installation

npm i typeorm-simple-query-parser

Usage Example

import { Get, QueryParams, Param } from 'routing-controllers';
import { RequestQueryParser } from 'typeorm-simple-query-parser';
import { MainRepository } from 'typeorm-simple-query-parser';

export class UserController {
    @Get('/users')
    public async getAll(@QueryParams() parseResourceOptions: RequestQueryParser) {
        const resourceOptions = parseResourceOptions.getAll();

        return await this.userRepository.getManyAndCount(resourceOptions);
    }

    @Get('/users/:id')
    public async getOne(@Param('id') id: number, @QueryParams() parseResourceOptions: RequestQueryParser) {
        const resourceOptions = parseResourceOptions.getAll();

        return await this.userRepository.getOneById(id, resourceOptions);
    }
}

export class UserRepository extends MainRepository<User> {
    //
}

Query params

By default, we support these param names:

filter - filter GET result by AND type of condition

filterByOr - filter GET result by OR type of condition

relations - receive joined relational resources in GET result (with all or selected fields)

sortByDesc - sort GET result by some field in DESC order

sortByAsc - sort GET result by some field in ASC order

limit - limit the amount of received resources

page - receive a portion of limited amount of resources

Filters

The way a filter should be formed is:

/users?filter[columnName][operator][not]=value
  • columnName - (Required) - Name of column you want to filter.
  • operator - (Optional | Default: eq) Type of operator you want to use.
  • not - (Optional | Default: false) Negate the filter (Accepted values: yes|true|1).
Example filters

Filter all users whose id start with 1000.

/users?filter[name][sw]=1000

Filter all books whose author is Gentrit.

/books?filter[author.name]=Gentrit

Filter all users whose name start with Gentrit or ends with Abazi.

/users?filter[name][sw]=Gentrit&filterByOr[name][ew]=Abazi
Operators
TypeDescription
ctString contains
swStarts with
ewEnds with
eqEquals
gtGreater than
gteGreater than or equalTo
ltLesser than
lteLesser than or equalTo
inIn array
btBetween
nchis / is not null

Pagination

Two parameters are available: limit and page. limit will determine the number of records per page and page will determine the current page.

/books?limit=10&page=3

Will return books number 30-40.

Sorting

The sortByAsc and sortByDesc query parameters are used to determine by which property the results collection will be ordered.

Usage

The following query parameter sortByAsc will sort results by from the lowest value to the highest value:

/books?sortByAsc=id

The following query parameter sortByDesc will sort results by from the highest value to the lowest value:

/books?sortByDesc=id
Sort multiple columns

You can sort multiple columns separating them with a comma:

/books?sortByDesc=id,name

Including relationships

The relations query parameter will load any relation on the resulting models.

Basic usage

The following query parameter will include the logs relation:

/users?relations=logs

Users will have all their their logs related models loaded.

Load multiple

You can load multiple relationships by separating them with a comma:

/users?relations=logs,tasks
Load nested

You can load nested relationships using the dot . notation:

/users?relations=logs.causer

Tip

You also can control relations, limit and more in controller like this:

@Get('/users')
public async getAll(@QueryParams() parseResourceOptions: RequestQueryParser) {
    const resourceOptions = parseResourceOptions.getAll();

    resourceOptions.skip = 1;

    return await this.userRepository.getManyAndCount(resourceOptions);
}

You also can control relations with scopes and conditions:

@Get('/users')
public async getAll(@QueryParams() parseResourceOptions: RequestQueryParser) {
    const resourceOptions = parseResourceOptions.getAll();

    resourceOptions.scopes = [
	{
	    name: "posts",
	    condition: "{alias}.active = :active",
	    parameters: { active: true },
	}
    ]

    return await this.userRepository.getManyAndCount(resourceOptions);
}

Keywords

FAQs

Last updated on 28 Jul 2023

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