From 1da159d1102bf0ded9ce50d447c821258c7be800 Mon Sep 17 00:00:00 2001 From: eug-vs Date: Sat, 17 Apr 2021 19:57:19 +0300 Subject: feat: add ignoreCaseRegex hook --- src/hooks/ignoreCaseRegex.ts | 11 +++++++++++ src/services/index.ts | 3 ++- src/services/products.service.ts | 2 +- 3 files changed, 14 insertions(+), 2 deletions(-) create mode 100644 src/hooks/ignoreCaseRegex.ts (limited to 'src') diff --git a/src/hooks/ignoreCaseRegex.ts b/src/hooks/ignoreCaseRegex.ts new file mode 100644 index 0000000..e996000 --- /dev/null +++ b/src/hooks/ignoreCaseRegex.ts @@ -0,0 +1,11 @@ +import { HookContext } from '@feathersjs/feathers'; +import _ from 'lodash'; + +const ignoreCaseRegex = async (context: HookContext): Promise => { + context.params.query = _.mapValues(context.params.query, (data, key) => { + return key.startsWith('$') ? data : _.set(data, '$options', 'i'); + }); + return context; +}; + +export default ignoreCaseRegex; diff --git a/src/services/index.ts b/src/services/index.ts index 42b1fbd..b94380d 100644 --- a/src/services/index.ts +++ b/src/services/index.ts @@ -6,6 +6,7 @@ import Tranfers from './transfers.service'; import Spreadsheets from './spreadsheets.service'; import Uploads from './uploads.service'; import sortByDate from '../hooks/sortByDate'; +import ignoreCaseRegex from '../hooks/ignoreCaseRegex'; export default (app: Application): void => { app.configure(Products); @@ -19,7 +20,7 @@ export default (app: Application): void => { app.hooks({ before: { - find: sortByDate + find: [sortByDate, ignoreCaseRegex] }, }) }; diff --git a/src/services/products.service.ts b/src/services/products.service.ts index f3ae2b6..9934a66 100644 --- a/src/services/products.service.ts +++ b/src/services/products.service.ts @@ -2,7 +2,7 @@ import { Application } from '@feathersjs/express'; import service from 'feathers-mongoose'; import Model from '../models/product/product.model'; -const Products = service({ Model }); +const Products = service({ Model, whitelist: ['$options', '$regex'] }); export default (app: Application): void => { app.use('/products', Products); -- cgit v1.2.3