@@ -41,6 +41,7 @@ describe('paginate', () => {
4141 let catHomes : CatHomeEntity [ ]
4242 let catHomePillows : CatHomePillowEntity [ ]
4343 let catHairs : CatHairEntity [ ] = [ ]
44+ let underCoats : CatHairEntity [ ] = [ ]
4445
4546 beforeAll ( async ( ) => {
4647 const dbOptions : Omit < Partial < BaseDataSourceOptions > , 'poolSize' > = {
@@ -193,13 +194,26 @@ describe('paginate', () => {
193194 await catRepo . save ( { ...cats [ 0 ] , friends : cats . slice ( 1 ) } )
194195
195196 catHairs = [ ]
197+ underCoats = [ ]
196198
197199 if ( process . env . DB === 'postgres' ) {
198200 catHairRepo = dataSource . getRepository ( CatHairEntity )
199201 catHairs = await catHairRepo . save ( [
200- catHairRepo . create ( { name : 'short' , colors : [ 'white' , 'brown' , 'black' ] } ) ,
201- catHairRepo . create ( { name : 'long' , colors : [ 'white' , 'brown' ] } ) ,
202- catHairRepo . create ( { name : 'buzzed' , colors : [ 'white' ] } ) ,
202+ catHairRepo . create ( {
203+ name : 'short' ,
204+ colors : [ 'white' , 'brown' , 'black' ] ,
205+ metadata : { length : 5 , thickness : 1 } ,
206+ } ) ,
207+ catHairRepo . create ( {
208+ name : 'long' ,
209+ colors : [ 'white' , 'brown' ] ,
210+ metadata : { length : 20 , thickness : 5 } ,
211+ } ) ,
212+ catHairRepo . create ( {
213+ name : 'buzzed' ,
214+ colors : [ 'white' ] ,
215+ metadata : { length : 0.5 , thickness : 10 } ,
216+ } ) ,
203217 catHairRepo . create ( { name : 'none' } ) ,
204218 ] )
205219 }
@@ -3023,7 +3037,6 @@ describe('paginate', () => {
30233037 }
30243038
30253039 const result = await paginate < CatHairEntity > ( query , catHairRepo , config )
3026-
30273040 expect ( result . meta . filter ) . toStrictEqual ( {
30283041 colors : queryFilter ,
30293042 } )
@@ -3051,6 +3064,98 @@ describe('paginate', () => {
30513064 } )
30523065 }
30533066
3067+ if ( process . env . DB === 'postgres' ) {
3068+ describe ( 'should be able to filter on jsonb columns' , ( ) => {
3069+ beforeAll ( async ( ) => {
3070+ underCoats = await catHairRepo . save ( [
3071+ catHairRepo . create ( {
3072+ name : 'full' ,
3073+ colors : [ 'orange' ] ,
3074+ metadata : { length : 50 , thickness : 2 } ,
3075+ underCoat : catHairs [ 0 ] ,
3076+ } ) ,
3077+ ] )
3078+ } )
3079+
3080+ it ( 'should filter with single value' , async ( ) => {
3081+ const config : PaginateConfig < CatHairEntity > = {
3082+ sortableColumns : [ 'id' ] ,
3083+ filterableColumns : {
3084+ 'metadata.length' : true ,
3085+ } ,
3086+ }
3087+ const query : PaginateQuery = {
3088+ path : '' ,
3089+ filter : {
3090+ 'metadata.length' : '$eq:5' ,
3091+ } ,
3092+ }
3093+
3094+ const result = await paginate < CatHairEntity > ( query , catHairRepo , config )
3095+
3096+ expect ( result . meta . filter ) . toStrictEqual ( {
3097+ 'metadata.length' : '$eq:5' ,
3098+ } )
3099+ expect ( result . data ) . toStrictEqual ( [ catHairs [ 0 ] ] )
3100+ expect ( result . links . current ) . toBe ( '?page=1&limit=20&sortBy=id:ASC&filter.metadata.length=$eq:5' )
3101+ } )
3102+
3103+ it ( 'should filter with multiple values' , async ( ) => {
3104+ const config : PaginateConfig < CatHairEntity > = {
3105+ sortableColumns : [ 'id' ] ,
3106+ filterableColumns : {
3107+ 'metadata.length' : true ,
3108+ 'metadata.thickness' : true ,
3109+ } ,
3110+ }
3111+ const query : PaginateQuery = {
3112+ path : '' ,
3113+ filter : {
3114+ 'metadata.length' : '$eq:0.5' ,
3115+ 'metadata.thickness' : '$eq:10' ,
3116+ } ,
3117+ }
3118+
3119+ const result = await paginate < CatHairEntity > ( query , catHairRepo , config )
3120+
3121+ expect ( result . meta . filter ) . toStrictEqual ( {
3122+ 'metadata.length' : '$eq:0.5' ,
3123+ 'metadata.thickness' : '$eq:10' ,
3124+ } )
3125+ expect ( result . data ) . toStrictEqual ( [ catHairs [ 2 ] ] )
3126+ expect ( result . links . current ) . toBe (
3127+ '?page=1&limit=20&sortBy=id:ASC&filter.metadata.length=$eq:0.5&filter.metadata.thickness=$eq:10'
3128+ )
3129+ } )
3130+
3131+ it ( 'should filter on a nested property through a relation' , async ( ) => {
3132+ const config : PaginateConfig < CatHairEntity > = {
3133+ sortableColumns : [ 'id' ] ,
3134+ filterableColumns : {
3135+ 'underCoat.metadata.length' : true ,
3136+ } ,
3137+ relations : [ 'underCoat' ] ,
3138+ }
3139+ const query : PaginateQuery = {
3140+ path : '' ,
3141+ filter : {
3142+ 'underCoat.metadata.length' : '$eq:50' ,
3143+ } ,
3144+ }
3145+
3146+ const result = await paginate < CatHairEntity > ( query , catHairRepo , config )
3147+
3148+ expect ( result . meta . filter ) . toStrictEqual ( {
3149+ 'underCoat.metadata.length' : '$eq:50' ,
3150+ } )
3151+ expect ( result . data ) . toStrictEqual ( [ underCoats [ 0 ] ] )
3152+ expect ( result . links . current ) . toBe (
3153+ '?page=1&limit=20&sortBy=id:ASC&filter.underCoat.metadata.length=$eq:50'
3154+ )
3155+ } )
3156+ } )
3157+ }
3158+
30543159 if ( process . env . DB !== 'postgres' ) {
30553160 describe ( 'should return result based on virtual column' , ( ) => {
30563161 it ( 'should return result sorted and filter by a virtual column in main entity' , async ( ) => {
0 commit comments