8
8
import { expectType } from 'tsd'
9
9
import { TypeEqual } from 'ts-expect'
10
10
import {
11
- FindMatchingRelationships ,
12
11
FindMatchingTableRelationships ,
13
12
IsRelationNullable ,
14
13
} from '../../src/select-query-parser/utils'
@@ -17,58 +16,37 @@ import { ParseQuery } from '../../src/select-query-parser/parser/parser'
17
16
18
17
// This test file is here to ensure some of our helpers behave as expected for ease of development
19
18
// and debugging purposes
20
- // Searching for an non-existing relationship should return never
21
- {
22
- let result : FindMatchingRelationships <
23
- 'test' ,
24
- Database [ 'public' ] [ 'Tables' ] [ 'best_friends' ] [ 'Relationships' ]
25
- >
26
- let expected : false
27
- expectType < TypeEqual < typeof result , typeof expected > > ( true )
28
- }
19
+
29
20
// Searching for a relationship by direct foreignkey name
30
21
{
31
- let result : FindMatchingRelationships <
32
- 'best_friends_first_user_fkey' ,
33
- Database [ 'public' ] [ 'Tables' ] [ 'best_friends' ] [ 'Relationships' ]
22
+ let result : FindMatchingTableRelationships <
23
+ Database [ 'public' ] ,
24
+ Database [ 'public' ] [ 'Tables' ] [ 'best_friends' ] [ 'Relationships' ] ,
25
+ 'best_friends_first_user_fkey'
34
26
>
35
27
let expected : {
36
28
foreignKeyName : 'best_friends_first_user_fkey'
37
29
columns : [ 'first_user' ]
38
30
isOneToOne : false
39
31
referencedRelation : 'users'
40
32
referencedColumns : [ 'username' ]
41
- }
33
+ } & { match : 'fkname' }
42
34
expectType < TypeEqual < typeof result , typeof expected > > ( true )
43
35
}
44
36
// Searching for a relationship by column hoding the value reference
45
37
{
46
- let result : FindMatchingRelationships <
47
- 'first_user' ,
48
- Database [ 'public' ] [ 'Tables' ] [ 'best_friends' ] [ 'Relationships' ]
38
+ let result : FindMatchingTableRelationships <
39
+ Database [ 'public' ] ,
40
+ Database [ 'public' ] [ 'Tables' ] [ 'best_friends' ] [ 'Relationships' ] ,
41
+ 'first_user'
49
42
>
50
43
let expected : {
51
44
foreignKeyName : 'best_friends_first_user_fkey'
52
45
columns : [ 'first_user' ]
53
46
isOneToOne : false
54
47
referencedRelation : 'users'
55
48
referencedColumns : [ 'username' ]
56
- }
57
- expectType < TypeEqual < typeof result , typeof expected > > ( true )
58
- }
59
- // Will find the first matching relationship
60
- {
61
- let result : FindMatchingRelationships <
62
- 'username' ,
63
- Database [ 'public' ] [ 'Tables' ] [ 'user_profiles' ] [ 'Relationships' ]
64
- >
65
- let expected : {
66
- foreignKeyName : 'user_profiles_username_fkey'
67
- columns : [ 'username' ]
68
- isOneToOne : false
69
- referencedRelation : 'non_updatable_view'
70
- referencedColumns : [ 'username' ]
71
- }
49
+ } & { match : 'col' }
72
50
expectType < TypeEqual < typeof result , typeof expected > > ( true )
73
51
}
74
52
// should return the relation matching the "Tables" references
@@ -89,44 +67,48 @@ import { ParseQuery } from '../../src/select-query-parser/parser/parser'
89
67
}
90
68
// Searching for a relationship by referenced table name
91
69
{
92
- let result : FindMatchingRelationships <
93
- 'users' ,
94
- Database [ 'public' ] [ 'Tables' ] [ 'messages' ] [ 'Relationships' ]
70
+ let result : FindMatchingTableRelationships <
71
+ Database [ 'public' ] ,
72
+ Database [ 'public' ] [ 'Tables' ] [ 'messages' ] [ 'Relationships' ] ,
73
+ 'users'
95
74
>
96
75
let expected : {
97
76
foreignKeyName : 'messages_username_fkey'
98
77
columns : [ 'username' ]
99
78
isOneToOne : false
100
79
referencedRelation : 'users'
101
80
referencedColumns : [ 'username' ]
102
- }
81
+ } & { match : 'refrel' }
103
82
expectType < TypeEqual < typeof result , typeof expected > > ( true )
104
83
}
105
84
{
106
- let result : FindMatchingRelationships <
107
- 'channels' ,
108
- Database [ 'public' ] [ 'Tables' ] [ 'messages' ] [ 'Relationships' ]
85
+ let result : FindMatchingTableRelationships <
86
+ Database [ 'public' ] ,
87
+ Database [ 'public' ] [ 'Tables' ] [ 'messages' ] [ 'Relationships' ] ,
88
+ 'channels'
109
89
>
110
90
let expected : {
111
91
foreignKeyName : 'messages_channel_id_fkey'
112
92
columns : [ 'channel_id' ]
113
93
isOneToOne : false
114
94
referencedRelation : 'channels'
115
95
referencedColumns : [ 'id' ]
116
- }
96
+ } & { match : 'refrel' }
117
97
expectType < TypeEqual < typeof result , typeof expected > > ( true )
118
98
}
119
99
120
100
// IsRelationNullable
121
101
{
122
102
type BestFriendsTable = Database [ 'public' ] [ 'Tables' ] [ 'best_friends' ]
123
- type NonNullableRelation = FindMatchingRelationships <
124
- 'best_friends_first_user_fkey' ,
125
- BestFriendsTable [ 'Relationships' ]
103
+ type NonNullableRelation = FindMatchingTableRelationships <
104
+ Database [ 'public' ] ,
105
+ BestFriendsTable [ 'Relationships' ] ,
106
+ 'best_friends_first_user_fkey'
126
107
>
127
- type NullableRelation = FindMatchingRelationships <
128
- 'best_friends_third_wheel_fkey' ,
129
- BestFriendsTable [ 'Relationships' ]
108
+ type NullableRelation = FindMatchingTableRelationships <
109
+ Database [ 'public' ] ,
110
+ BestFriendsTable [ 'Relationships' ] ,
111
+ 'best_friends_third_wheel_fkey'
130
112
>
131
113
let nonNullableResult : IsRelationNullable < BestFriendsTable , NonNullableRelation >
132
114
let nullableResult : IsRelationNullable < BestFriendsTable , NullableRelation >
0 commit comments