|
| 1 | +import { expectType, TypeEqual } from './types' |
| 2 | +import { PostgrestClient } from '../src/index' |
| 3 | + |
| 4 | +export type Json = string | number | boolean | null | { [key: string]: Json | undefined } | Json[] |
| 5 | + |
| 6 | +export type Database = { |
| 7 | + public: { |
| 8 | + Tables: { |
| 9 | + countries: { |
| 10 | + Row: { |
| 11 | + id: number |
| 12 | + name: string |
| 13 | + country_alpha_locations: |
| 14 | + | Database['public']['Tables']['country_alpha_locations']['Row'] |
| 15 | + | null |
| 16 | + } |
| 17 | + Insert: { |
| 18 | + id?: never |
| 19 | + name: string |
| 20 | + } |
| 21 | + Update: { |
| 22 | + id?: never |
| 23 | + name?: string |
| 24 | + } |
| 25 | + Relationships: [] |
| 26 | + } |
| 27 | + country_alpha_locations: { |
| 28 | + Row: { |
| 29 | + alpha2: string |
| 30 | + id: number |
| 31 | + location: string |
| 32 | + } |
| 33 | + Insert: { |
| 34 | + alpha2: string |
| 35 | + id?: never |
| 36 | + location: string |
| 37 | + } |
| 38 | + Update: { |
| 39 | + alpha2?: string |
| 40 | + id?: never |
| 41 | + location?: string |
| 42 | + } |
| 43 | + Relationships: [] |
| 44 | + } |
| 45 | + country_alphas: { |
| 46 | + Row: { |
| 47 | + alpha2: string |
| 48 | + alpha3: string |
| 49 | + country_id: number |
| 50 | + id: number |
| 51 | + } |
| 52 | + Insert: { |
| 53 | + alpha2: string |
| 54 | + alpha3: string |
| 55 | + country_id: number |
| 56 | + id?: never |
| 57 | + } |
| 58 | + Update: { |
| 59 | + alpha2?: string |
| 60 | + alpha3?: string |
| 61 | + country_id?: number |
| 62 | + id?: never |
| 63 | + } |
| 64 | + Relationships: [ |
| 65 | + { |
| 66 | + foreignKeyName: 'country_alphas_country_id_fkey' |
| 67 | + columns: ['country_id'] |
| 68 | + isOneToOne: true |
| 69 | + referencedRelation: 'countries' |
| 70 | + referencedColumns: ['id'] |
| 71 | + } |
| 72 | + ] |
| 73 | + } |
| 74 | + } |
| 75 | + Views: { |
| 76 | + [_ in never]: never |
| 77 | + } |
| 78 | + Functions: { |
| 79 | + country_alpha_locations: { |
| 80 | + Args: { '': Database['public']['Tables']['countries']['Row'] } |
| 81 | + Returns: { |
| 82 | + alpha2: string |
| 83 | + id: number |
| 84 | + location: string |
| 85 | + }[] |
| 86 | + } |
| 87 | + } |
| 88 | + Enums: { |
| 89 | + [_ in never]: never |
| 90 | + } |
| 91 | + CompositeTypes: { |
| 92 | + [_ in never]: never |
| 93 | + } |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +type DatabaseWithoutInternals = Omit<Database, '__InternalSupabase'> |
| 98 | + |
| 99 | +type DefaultSchema = DatabaseWithoutInternals[Extract<keyof Database, 'public'>] |
| 100 | + |
| 101 | +export type Tables< |
| 102 | + DefaultSchemaTableNameOrOptions extends |
| 103 | + | keyof (DefaultSchema['Tables'] & DefaultSchema['Views']) |
| 104 | + | { schema: keyof DatabaseWithoutInternals }, |
| 105 | + TableName extends DefaultSchemaTableNameOrOptions extends { |
| 106 | + schema: keyof DatabaseWithoutInternals |
| 107 | + } |
| 108 | + ? keyof (DatabaseWithoutInternals[DefaultSchemaTableNameOrOptions['schema']]['Tables'] & |
| 109 | + DatabaseWithoutInternals[DefaultSchemaTableNameOrOptions['schema']]['Views']) |
| 110 | + : never = never |
| 111 | +> = DefaultSchemaTableNameOrOptions extends { |
| 112 | + schema: keyof DatabaseWithoutInternals |
| 113 | +} |
| 114 | + ? (DatabaseWithoutInternals[DefaultSchemaTableNameOrOptions['schema']]['Tables'] & |
| 115 | + DatabaseWithoutInternals[DefaultSchemaTableNameOrOptions['schema']]['Views'])[TableName] extends { |
| 116 | + Row: infer R |
| 117 | + } |
| 118 | + ? R |
| 119 | + : never |
| 120 | + : DefaultSchemaTableNameOrOptions extends keyof (DefaultSchema['Tables'] & DefaultSchema['Views']) |
| 121 | + ? (DefaultSchema['Tables'] & DefaultSchema['Views'])[DefaultSchemaTableNameOrOptions] extends { |
| 122 | + Row: infer R |
| 123 | + } |
| 124 | + ? R |
| 125 | + : never |
| 126 | + : never |
| 127 | + |
| 128 | +export type TablesInsert< |
| 129 | + DefaultSchemaTableNameOrOptions extends |
| 130 | + | keyof DefaultSchema['Tables'] |
| 131 | + | { schema: keyof DatabaseWithoutInternals }, |
| 132 | + TableName extends DefaultSchemaTableNameOrOptions extends { |
| 133 | + schema: keyof DatabaseWithoutInternals |
| 134 | + } |
| 135 | + ? keyof DatabaseWithoutInternals[DefaultSchemaTableNameOrOptions['schema']]['Tables'] |
| 136 | + : never = never |
| 137 | +> = DefaultSchemaTableNameOrOptions extends { |
| 138 | + schema: keyof DatabaseWithoutInternals |
| 139 | +} |
| 140 | + ? DatabaseWithoutInternals[DefaultSchemaTableNameOrOptions['schema']]['Tables'][TableName] extends { |
| 141 | + Insert: infer I |
| 142 | + } |
| 143 | + ? I |
| 144 | + : never |
| 145 | + : DefaultSchemaTableNameOrOptions extends keyof DefaultSchema['Tables'] |
| 146 | + ? DefaultSchema['Tables'][DefaultSchemaTableNameOrOptions] extends { |
| 147 | + Insert: infer I |
| 148 | + } |
| 149 | + ? I |
| 150 | + : never |
| 151 | + : never |
| 152 | + |
| 153 | +export type TablesUpdate< |
| 154 | + DefaultSchemaTableNameOrOptions extends |
| 155 | + | keyof DefaultSchema['Tables'] |
| 156 | + | { schema: keyof DatabaseWithoutInternals }, |
| 157 | + TableName extends DefaultSchemaTableNameOrOptions extends { |
| 158 | + schema: keyof DatabaseWithoutInternals |
| 159 | + } |
| 160 | + ? keyof DatabaseWithoutInternals[DefaultSchemaTableNameOrOptions['schema']]['Tables'] |
| 161 | + : never = never |
| 162 | +> = DefaultSchemaTableNameOrOptions extends { |
| 163 | + schema: keyof DatabaseWithoutInternals |
| 164 | +} |
| 165 | + ? DatabaseWithoutInternals[DefaultSchemaTableNameOrOptions['schema']]['Tables'][TableName] extends { |
| 166 | + Update: infer U |
| 167 | + } |
| 168 | + ? U |
| 169 | + : never |
| 170 | + : DefaultSchemaTableNameOrOptions extends keyof DefaultSchema['Tables'] |
| 171 | + ? DefaultSchema['Tables'][DefaultSchemaTableNameOrOptions] extends { |
| 172 | + Update: infer U |
| 173 | + } |
| 174 | + ? U |
| 175 | + : never |
| 176 | + : never |
| 177 | + |
| 178 | +export type Enums< |
| 179 | + DefaultSchemaEnumNameOrOptions extends |
| 180 | + | keyof DefaultSchema['Enums'] |
| 181 | + | { schema: keyof DatabaseWithoutInternals }, |
| 182 | + EnumName extends DefaultSchemaEnumNameOrOptions extends { |
| 183 | + schema: keyof DatabaseWithoutInternals |
| 184 | + } |
| 185 | + ? keyof DatabaseWithoutInternals[DefaultSchemaEnumNameOrOptions['schema']]['Enums'] |
| 186 | + : never = never |
| 187 | +> = DefaultSchemaEnumNameOrOptions extends { |
| 188 | + schema: keyof DatabaseWithoutInternals |
| 189 | +} |
| 190 | + ? DatabaseWithoutInternals[DefaultSchemaEnumNameOrOptions['schema']]['Enums'][EnumName] |
| 191 | + : DefaultSchemaEnumNameOrOptions extends keyof DefaultSchema['Enums'] |
| 192 | + ? DefaultSchema['Enums'][DefaultSchemaEnumNameOrOptions] |
| 193 | + : never |
| 194 | + |
| 195 | +export type CompositeTypes< |
| 196 | + PublicCompositeTypeNameOrOptions extends |
| 197 | + | keyof DefaultSchema['CompositeTypes'] |
| 198 | + | { schema: keyof DatabaseWithoutInternals }, |
| 199 | + CompositeTypeName extends PublicCompositeTypeNameOrOptions extends { |
| 200 | + schema: keyof DatabaseWithoutInternals |
| 201 | + } |
| 202 | + ? keyof DatabaseWithoutInternals[PublicCompositeTypeNameOrOptions['schema']]['CompositeTypes'] |
| 203 | + : never = never |
| 204 | +> = PublicCompositeTypeNameOrOptions extends { |
| 205 | + schema: keyof DatabaseWithoutInternals |
| 206 | +} |
| 207 | + ? DatabaseWithoutInternals[PublicCompositeTypeNameOrOptions['schema']]['CompositeTypes'][CompositeTypeName] |
| 208 | + : PublicCompositeTypeNameOrOptions extends keyof DefaultSchema['CompositeTypes'] |
| 209 | + ? DefaultSchema['CompositeTypes'][PublicCompositeTypeNameOrOptions] |
| 210 | + : never |
| 211 | + |
| 212 | +export const Constants = { |
| 213 | + graphql_public: { |
| 214 | + Enums: {}, |
| 215 | + }, |
| 216 | + public: { |
| 217 | + Enums: {}, |
| 218 | + }, |
| 219 | +} as const |
| 220 | + |
| 221 | +const postgrest = new PostgrestClient<Database>('http://localhost:3000') |
| 222 | + |
| 223 | +// Basic types |
| 224 | +{ |
| 225 | + const { data } = await postgrest.from('countries').select( |
| 226 | + `name, |
| 227 | + country_alphas!inner ( |
| 228 | + alpha2, |
| 229 | + alpha3 |
| 230 | + ), |
| 231 | + country_alpha_locations!inner(location) |
| 232 | + ` |
| 233 | + ) |
| 234 | + // .eq('name', 'Canada') |
| 235 | + // .single() |
| 236 | + |
| 237 | + let expected: { |
| 238 | + name: string |
| 239 | + country_alphas: { |
| 240 | + alpha2: string |
| 241 | + alpha3: string |
| 242 | + } |
| 243 | + country_alpha_locations: { |
| 244 | + location: string |
| 245 | + } | null |
| 246 | + } | null |
| 247 | + expectType<TypeEqual<typeof data, typeof expected>>(true) |
| 248 | +} |
0 commit comments