npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

crud-query-parser

v0.0.3

Published

Parses HTTP requests and converts them into database queries

Downloads

12

Readme

crud-query-parser

This library parses HTTP requests and converts them to TypeORM query builders, allowing advanced filtering, column selection, pagination and relations.

Install

npm install crud-query-parser

Usage

You have to pick a request parser and a query adapter.

const userRepository = AppDataSource.getRepository(UserEntity); // TypeORM repository

// ...

// The request query object
// This object will likely come from the HTTP request
const requestQuery = { ... };  

// Parses the query into a CrudRequest object
const crudRequest = parser.parse(requestQuery);

// Using the query adapter, you can query in your ORM from the CrudRequest
const result = adapter.getMany(userRepository.createQueryBuilder(), crudRequest); // GetManyResult<UserEntity>

// The result object has properties like data, page, total
console.log(result);

Request parsers

CRUD

The CRUD parser is an implementation of the @nestjsx/crud query params format.

import { CrudRequestParser } from 'crud-query-parser/parsers/crud';

const parser = new CrudRequestParser();

// Then, you have to pass a query string object to it
// const crudRequest = parser.parse(request.query);

Database adapters

TypeORM

This adapter works with TypeORM 0.3.x and 0.2.x

import { TypeormQueryAdapter } from 'crud-query-parser/adapters/typeorm';

const adapter = new TypeormQueryAdapter();

// Then, you can pass a query builder to it:
// const result = adapter.getMany(repository.createQueryBuilder(), crudRequest);

Helpers

NestJS

The NestJS integration has OpenAPI support and decorators that automatically parses the request.

Sample code:

@Controller('users')
export class UserController {

  constructor(
    private service: UserService,
  ) {}

  @Get()
  @Crud(CrudRequestParser) // <- You specify which parser to use
  public async getMany(@ParseCrudRequest() crudRequest: CrudRequest) { // <- The request query will be automatically parsed
    return this.service.getMany(crudRequest);
  }

}
@Injectable()
export class UserService {

  protected crudAdapter = new TypeormQueryAdapter();

  constructor(
    @InjectRepository(UserEntity)
    private repository: Repository<UserEntity>,
  ) {}
  
  public async getMany(crudRequest: CrudRequest) {
    return await this.crudAdapter.getMany(this.repository.createQueryBuilder(), crudRequest);
  }

}

Filters

You may need to filter what the user can or cannot query. You can modify the CrudRequest object as needed.

There are a few filters provided by the library, which are listed below.

Enforce a "where" condition

This filter will add the condition on top of all other where conditions

import { ensureCondition } from 'crud-query-parser/filters';

// ...

crudRequest = ensureCondition(crudRequest, {
  field: ['isActive'],
  operator: CrudRequestWhereOperator.EQ,
  value: true,
});

Ensure page limit

This filter will ensure that the requested limit does not go above the maximum. It also sets the default limit whenever the limit is omitted.

import { ensureLimit } from 'crud-query-parser/filters';

// ...

const defaultLimit = 25;
const maxLimit = 100;

crudRequest = ensureLimit(crudRequest, defaultLimit, maxLimit);

Filter property access

This filter removes any property from the request that is not in the allowlist. It removes unallowed properties from the select fields, where conditions, relations and sorting.

import { filterProperties } from 'crud-query-parser/filters';

// ...

crudRequest = filterProperties(crudRequest, [
  'id',
  'name',
  'posts',
  'posts.id',
  'posts.name',
]);

Filter relations

This filter removes any relation from the request that is not in the allowlist. It's the same as the filterProperties but only filters relations.

import { filterRelations } from 'crud-query-parser/filters';

// ...

crudRequest = filterRelations(crudRequest, ['posts']);