lucasrochagit / nest-mongo-query-parser

A MongoDB query string parser to be used in applications developed with NestJS.
Apache License 2.0
12 stars 8 forks source link

feat: add global search #15

Closed cicciodifranco closed 7 months ago

cicciodifranco commented 7 months ago

Overview:

This PR introduces a new feature aimed at enhancing the search capabilities. The primary focus is on enabling users to perform searches using regular expressions (RegExp) across multiple fields

Changes Made:

Key Additions:

Result

Users can now pass options to MongoQuery decorator (useful for further options like default limit or sort)

findAll( MongoQuery({ search: { key: 'q', paths: ['name', 'description'] } }) query: MongoQueryModel)

Request

http://localhost:3000/resources?q=John

Query

{
  "limit": 30,
  "skip": 0,
  "select": {},
  "sort": {},
  "populate": [],
  "filter": {
    "$or": [
      {
        "name": {
          "$regex": "John",
          "$options": "i"
        }
      },
      {
        "description": {
          "$regex": "John",
          "$options": "i"
        }
      }
    ]
  }
}

Impact and Benefits:

lucasrochagit commented 7 months ago

@cicciodifranco nice feature, man. I will release it now!

lucasrochagit commented 7 months ago

@cicciodifranco I had an issue when generating the library package due to an error generated by TypeScript when creating a ParameterDecorator, as MongoQueryOptions cannot be nullable:

TS2322: Type '(...dataOrPipes: (MongoQueryOptions | PipeTransform<any, any> | Type<PipeTransform<any, any>>)[]) => ParameterDecorator' is not assignable to type '(opts?: MongoQueryOptions | undefined) => ParameterDecorator'.   Types of parameters 'dataOrPipes' and 'opts' are incompatible.     Type 'MongoQueryOptions | undefined' is not assignable to type 'MongoQueryOptions | PipeTransform<any, any> | Type<PipeTransform<any, any>>'.       Type 'undefined' is not assignable to type 'MongoQueryOptions | PipeTransform<any, any> | Type<PipeTransform<any, any>>'.

Because of this, I will create and expose a method called MongoQueryOpts, which performs the same operations as MongoQuery, but with the possibility of having search options, as defined in your feature.

If there are questions or new suggestions for implementing this feature, please open a new Pull Request or an Issue, so that I can adjust it.

Thanks for the contribution!

cicciodifranco commented 7 months ago

a possible solution could be

export const MongoQuery: (data: MongoQueryOptions | undefined) => ParameterDecorator =
    createParamDecorator(
        (
          data: MongoQueryOptions | undefined,
          ctx: ExecutionContext,
        ): MongoQueryModel => {
          const query = ctx.getArgByIndex(0).query;
          return parse(query, data);
        },
    );

btw I will open a new pr for that

lucasrochagit commented 7 months ago

@cicciodifranco bruh, I didn't considerate this solution 🤦🏻 . No new pr needed, I will fix this now. Thanks!

Edit: feature available on v.1.0.13