File tree Expand file tree Collapse file tree 3 files changed +23
-9
lines changed
Expand file tree Collapse file tree 3 files changed +23
-9
lines changed Original file line number Diff line number Diff line change @@ -31,7 +31,7 @@ Request url is `GET` `/users?search=mark&page=2&order=-email`
3131``` js
3232const Route = use (' Route' )
3333const Query = use (' Query' )
34- const User = use (' User' )
34+ const User = use (' App/Models/ User' )
3535
3636Route .get (' /users' , async ({ request, response }) => {
3737 const query = new Query (request, { order: ' id' })
@@ -72,7 +72,9 @@ cosnt query = new Query(request, {
7272
7373### Search
7474
75- > Search not fire where clause if search variable is empty.
75+ > Note 1. Search not fire where clause if search variable is empty.
76+
77+ > Note 2. All search columns and values will be translated to lowercase.
7678
7779For basic usage you need add columns where you make search
7880
@@ -105,7 +107,7 @@ You can get parsed values from uri as `page` and `limit`. Variables are optional
105107
106108``` js
107109Route .get (' /users' , async ({ request, response }) => {
108- const query = new Query (request, { order : ' id ' } )
110+ const query = new Query (request)
109111 const users = await User .query ()
110112 .paginate (query .page (), query .limit ())
111113
Original file line number Diff line number Diff line change 11{
22 "name" : " adonis-search" ,
3- "version" : " 1.0.0 " ,
3+ "version" : " 1.0.1 " ,
44 "description" : " Mail provider for adonis framework and has support for all common mailing services to send emails" ,
55 "main" : " index.js" ,
66 "directories" : {
2424 "license" : " MIT" ,
2525 "devDependencies" : {
2626 "@adonisjs/fold" : " ^4.0.2" ,
27- "@adonisjs/lucid" : " ^4.0.16" ,
2827 "@adonisjs/sink" : " ^1.0.13" ,
2928 "coveralls" : " ^2.13.1" ,
3029 "cz-conventional-changelog" : " ^2.0.0" ,
5150 },
5251 "dependencies" : {
5352 "@adonisjs/generic-exceptions" : " ^1.0.0" ,
53+ "@adonisjs/lucid" : " ^4.0.16" ,
5454 "debug" : " ^3.0.0" ,
5555 "lodash" : " ^4.17.4"
5656 },
Original file line number Diff line number Diff line change 22
33const _ = require ( 'lodash' )
44const GE = require ( '@adonisjs/generic-exceptions' )
5+ const Database = use ( 'Database' )
56
67class Query {
78 static get INT ( ) { return 1 }
@@ -78,14 +79,25 @@ class Query {
7879 return
7980 }
8081
81- columns . forEach ( ( column , i ) => {
82+ const whereLike = ( column ) => {
83+ builder . orWhere ( Database . raw ( `LOWER(${ column } )` ) , 'LIKE' , Database . raw ( `LOWER('%${ this . _query . search } %')` ) )
84+ }
85+
86+ const whereEqual = ( column ) => {
87+ builder . orWhere ( column , '=' , this . _query . search )
88+ }
89+
90+ _ . forEach ( columns , ( column , i ) => {
8291 if ( Number . isInteger ( i ) ) {
83- builder . orWhere ( column , 'LIKE' , `% ${ this . _query . search } %` )
92+ whereLike ( column )
8493 } else {
8594 if ( column === this . constructor . INT ) {
86- builder . orWhere ( column , '=' , this . _query . search )
95+ const valueInt = Number . parseInt ( this . _query . search )
96+ if ( Number . isInteger ( valueInt ) ) {
97+ whereEqual ( i )
98+ }
8799 } else {
88- builder . orWhere ( column , 'LIKE' , `% ${ this . _query . search } %` )
100+ whereLike ( i )
89101 }
90102 }
91103 } )
You can’t perform that action at this time.
0 commit comments