@@ -42,7 +42,34 @@ async function testPostgresJsonSelects(db: Kysely<Database>) {
4242 first : eb . ref ( 'first_name' ) ,
4343 last : eb . ref ( 'last_name' ) ,
4444 full : sql < string > `first_name || ' ' || last_name` ,
45+ modified_at : eb . ref ( 'modified_at' ) ,
4546 } ) . as ( 'name' ) ,
47+
48+ // Nest other people with same first name.
49+ ( eb ) =>
50+ jsonArrayFrom (
51+ eb
52+ . selectFrom ( 'person as other' )
53+ . where ( 'other.id' , '!=' , eb . ref ( 'person.id' ) )
54+ . where ( 'other.first_name' , '=' , eb . ref ( 'person.first_name' ) )
55+ . selectAll ( ) ,
56+ ) . as ( 'people_same_first_name' ) ,
57+
58+ // Nest an object that holds the first person that might be a match from the opposite gender and is not married.
59+ ( eb ) =>
60+ jsonObjectFrom (
61+ eb
62+ . selectFrom ( 'person as match' )
63+ . where ( 'match.gender' , '!=' , eb . ref ( 'person.gender' ) )
64+ . where ( 'match.marital_status' , '!=' , 'married' )
65+ . where (
66+ ( eb ) => eb . fn ( 'abs' , [ eb ( 'match.age' , '-' , eb . ref ( 'person.age' ) ) ] ) ,
67+ '<=' ,
68+ 5 ,
69+ )
70+ . limit ( 1 )
71+ . selectAll ( ) ,
72+ ) . as ( 'potential_match' ) ,
4673 ] )
4774
4875 const r1 = await query . execute ( )
@@ -52,7 +79,32 @@ async function testPostgresJsonSelects(db: Kysely<Database>) {
5279 first_name : string
5380 pets : { name : string ; species : 'dog' | 'cat' } [ ]
5481 doggo : { doggo_name : string } | null
55- name : { first : string ; last : string | null ; full : string }
82+ name : {
83+ first : string
84+ last : string | null
85+ full : string
86+ modified_at : string
87+ }
88+ people_same_first_name : {
89+ id : number
90+ first_name : string
91+ last_name : string | null
92+ age : number
93+ gender : 'male' | 'female' | 'other'
94+ marital_status : 'single' | 'married' | 'divorced' | 'widowed' | null
95+ modified_at : string
96+ deleted_at : string | null
97+ } [ ]
98+ potential_match : {
99+ id : number
100+ first_name : string
101+ last_name : string | null
102+ age : number
103+ gender : 'male' | 'female' | 'other'
104+ marital_status : 'single' | 'married' | 'divorced' | 'widowed' | null
105+ modified_at : string
106+ deleted_at : string | null
107+ } | null
56108 } [ ]
57109 > ( r1 )
58110}
0 commit comments