@@ -18,13 +18,16 @@ import {
1818} from './filter'
1919import { ToyShopEntity } from './__tests__/toy-shop.entity'
2020import { ToyShopAddressEntity } from './__tests__/toy-shop-address.entity'
21+ import * as process from 'process'
22+ import { CatHairEntity } from './__tests__/cat-hair.entity'
2123
2224const isoStringToDate = ( isoString ) => new Date ( isoString )
2325
2426describe ( 'paginate' , ( ) => {
2527 let dataSource : DataSource
2628 let catRepo : Repository < CatEntity >
2729 let catToyRepo : Repository < CatToyEntity >
30+ let catHairRepo : Repository < CatHairEntity >
2831 let toyShopRepo : Repository < ToyShopEntity >
2932 let toyShopAddressRepository : Repository < ToyShopAddressEntity >
3033 let catHomeRepo : Repository < CatHomeEntity >
@@ -36,17 +39,18 @@ describe('paginate', () => {
3639 let toysShops : ToyShopEntity [ ]
3740 let catHomes : CatHomeEntity [ ]
3841 let catHomePillows : CatHomePillowEntity [ ]
42+ let catHairs : CatHairEntity [ ] = [ ]
3943
4044 beforeAll ( async ( ) => {
4145 dataSource = new DataSource ( {
4246 ...( process . env . DB === 'postgres'
4347 ? {
4448 type : 'postgres' ,
45- host : 'localhost' ,
46- port : 5432 ,
47- username : 'root' ,
48- password : 'pass' ,
49- database : 'test' ,
49+ host : process . env . DB_HOST || 'localhost' ,
50+ port : + process . env . DB_PORT || 5432 ,
51+ username : process . env . DB_USERNAME || 'root' ,
52+ password : process . env . DB_PASSWORD || 'pass' ,
53+ database : process . env . DB_DATABASE || 'test' ,
5054 }
5155 : {
5256 type : 'sqlite' ,
@@ -61,6 +65,7 @@ describe('paginate', () => {
6165 CatHomeEntity ,
6266 CatHomePillowEntity ,
6367 ToyShopEntity ,
68+ process . env . DB === 'postgres' ? CatHairEntity : undefined ,
6469 ] ,
6570 } )
6671 await dataSource . initialize ( )
@@ -163,6 +168,18 @@ describe('paginate', () => {
163168
164169 // add friends to Milo
165170 await catRepo . save ( { ...cats [ 0 ] , friends : cats . slice ( 1 ) } )
171+
172+ catHairs = [ ]
173+
174+ if ( process . env . DB === 'postgres' ) {
175+ catHairRepo = dataSource . getRepository ( CatHairEntity )
176+ catHairs = await catHairRepo . save ( [
177+ catHairRepo . create ( { name : 'short' , colors : [ 'white' , 'brown' , 'black' ] } ) ,
178+ catHairRepo . create ( { name : 'long' , colors : [ 'white' , 'brown' ] } ) ,
179+ catHairRepo . create ( { name : 'buzzed' , colors : [ 'white' ] } ) ,
180+ catHairRepo . create ( { name : 'none' } ) ,
181+ ] )
182+ }
166183 } )
167184
168185 if ( process . env . DB === 'postgres' ) {
@@ -2813,6 +2830,61 @@ describe('paginate', () => {
28132830 } )
28142831 } )
28152832
2833+ if ( process . env . DB === 'postgres' ) {
2834+ describe ( 'should return results for an array column' , ( ) => {
2835+ it . each `
2836+ operator | data | expectedIndexes
2837+ ${ '$not:$null' } | ${ undefined } | ${ [ 0 , 1 , 2 , 3 ] }
2838+ ${ '$lt' } | ${ 2 } | ${ [ 2 , 3 ] }
2839+ ${ '$lte' } | ${ 2 } | ${ [ 1 , 2 , 3 ] }
2840+ ${ '$btw' } | ${ '1,2' } | ${ [ 1 , 2 ] }
2841+ ${ '$gte' } | ${ 2 } | ${ [ 0 , 1 ] }
2842+ ${ '$gt' } | ${ 2 } | ${ [ 0 ] }
2843+ ` ( 'with $operator operator' , async ( { operator, data, expectedIndexes } ) => {
2844+ const config : PaginateConfig < CatHairEntity > = {
2845+ sortableColumns : [ 'id' ] ,
2846+ filterableColumns : {
2847+ colors : true ,
2848+ } ,
2849+ }
2850+
2851+ const queryFilter = `${ operator } ${ data ? `:${ data } ` : '' } `
2852+ const query : PaginateQuery = {
2853+ path : '' ,
2854+ filter : {
2855+ colors : queryFilter ,
2856+ } ,
2857+ }
2858+
2859+ const result = await paginate < CatHairEntity > ( query , catHairRepo , config )
2860+
2861+ expect ( result . meta . filter ) . toStrictEqual ( {
2862+ colors : queryFilter ,
2863+ } )
2864+ expect ( result . data ) . toStrictEqual ( expectedIndexes . map ( ( index ) => catHairs [ index ] ) )
2865+ expect ( result . links . current ) . toBe ( `?page=1&limit=20&sortBy=id:ASC&filter.colors=${ queryFilter } ` )
2866+ } )
2867+
2868+ it ( 'should work with search' , async ( ) => {
2869+ const config : PaginateConfig < CatHairEntity > = {
2870+ sortableColumns : [ 'id' ] ,
2871+ searchableColumns : [ 'colors' ] ,
2872+ }
2873+
2874+ const query : PaginateQuery = {
2875+ path : '' ,
2876+ search : 'brown' ,
2877+ }
2878+
2879+ const result = await paginate < CatHairEntity > ( query , catHairRepo , config )
2880+
2881+ expect ( result . meta . search ) . toStrictEqual ( 'brown' )
2882+ expect ( result . data ) . toStrictEqual ( [ catHairs [ 0 ] , catHairs [ 1 ] ] )
2883+ expect ( result . links . current ) . toBe ( `?page=1&limit=20&sortBy=id:ASC&search=brown` )
2884+ } )
2885+ } )
2886+ }
2887+
28162888 if ( process . env . DB !== 'postgres' ) {
28172889 describe ( 'should return result based on virtual column' , ( ) => {
28182890 it ( 'should return result sorted and filter by a virtual column in main entity' , async ( ) => {
0 commit comments