|
| 1 | +import { expectType } from 'tsd' |
| 2 | +import { PostgrestClient } from '../src/index' |
| 3 | +import type { MergeDeep } from 'type-fest' |
| 4 | + |
| 5 | +export type Json = string | number | boolean | null | { [key: string]: Json | undefined } | Json[] |
| 6 | + |
| 7 | +export type Database = { |
| 8 | + public: { |
| 9 | + Tables: { |
| 10 | + foo: { |
| 11 | + Row: { |
| 12 | + created_at: string | null |
| 13 | + bar: Json |
| 14 | + id: string |
| 15 | + baz: Json |
| 16 | + game_id: string |
| 17 | + updated_at: string | null |
| 18 | + user_id: string | null |
| 19 | + } |
| 20 | + Insert: { |
| 21 | + created_at?: string | null |
| 22 | + bar: Json |
| 23 | + id?: string |
| 24 | + baz: Json |
| 25 | + game_id: string |
| 26 | + updated_at?: string | null |
| 27 | + user_id?: string | null |
| 28 | + } |
| 29 | + Update: { |
| 30 | + created_at?: string | null |
| 31 | + bar?: Json |
| 32 | + id?: string |
| 33 | + baz?: Json |
| 34 | + game_id?: string |
| 35 | + updated_at?: string | null |
| 36 | + user_id?: string | null |
| 37 | + } |
| 38 | + Relationships: [] |
| 39 | + } |
| 40 | + } |
| 41 | + Views: {} |
| 42 | + Functions: {} |
| 43 | + Enums: {} |
| 44 | + CompositeTypes: { |
| 45 | + [_ in never]: never |
| 46 | + } |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +type PublicSchema = Database[Extract<keyof Database, 'public'>] |
| 51 | + |
| 52 | +export type Tables< |
| 53 | + PublicTableNameOrOptions extends |
| 54 | + | keyof (PublicSchema['Tables'] & PublicSchema['Views']) |
| 55 | + | { schema: keyof Database }, |
| 56 | + TableName extends PublicTableNameOrOptions extends { schema: keyof Database } |
| 57 | + ? keyof (Database[PublicTableNameOrOptions['schema']]['Tables'] & |
| 58 | + Database[PublicTableNameOrOptions['schema']]['Views']) |
| 59 | + : never = never |
| 60 | +> = PublicTableNameOrOptions extends { schema: keyof Database } |
| 61 | + ? (Database[PublicTableNameOrOptions['schema']]['Tables'] & |
| 62 | + Database[PublicTableNameOrOptions['schema']]['Views'])[TableName] extends { |
| 63 | + Row: infer R |
| 64 | + } |
| 65 | + ? R |
| 66 | + : never |
| 67 | + : PublicTableNameOrOptions extends keyof (PublicSchema['Tables'] & PublicSchema['Views']) |
| 68 | + ? (PublicSchema['Tables'] & PublicSchema['Views'])[PublicTableNameOrOptions] extends { |
| 69 | + Row: infer R |
| 70 | + } |
| 71 | + ? R |
| 72 | + : never |
| 73 | + : never |
| 74 | + |
| 75 | +export type TablesInsert< |
| 76 | + PublicTableNameOrOptions extends keyof PublicSchema['Tables'] | { schema: keyof Database }, |
| 77 | + TableName extends PublicTableNameOrOptions extends { schema: keyof Database } |
| 78 | + ? keyof Database[PublicTableNameOrOptions['schema']]['Tables'] |
| 79 | + : never = never |
| 80 | +> = PublicTableNameOrOptions extends { schema: keyof Database } |
| 81 | + ? Database[PublicTableNameOrOptions['schema']]['Tables'][TableName] extends { |
| 82 | + Insert: infer I |
| 83 | + } |
| 84 | + ? I |
| 85 | + : never |
| 86 | + : PublicTableNameOrOptions extends keyof PublicSchema['Tables'] |
| 87 | + ? PublicSchema['Tables'][PublicTableNameOrOptions] extends { |
| 88 | + Insert: infer I |
| 89 | + } |
| 90 | + ? I |
| 91 | + : never |
| 92 | + : never |
| 93 | + |
| 94 | +export type TablesUpdate< |
| 95 | + PublicTableNameOrOptions extends keyof PublicSchema['Tables'] | { schema: keyof Database }, |
| 96 | + TableName extends PublicTableNameOrOptions extends { schema: keyof Database } |
| 97 | + ? keyof Database[PublicTableNameOrOptions['schema']]['Tables'] |
| 98 | + : never = never |
| 99 | +> = PublicTableNameOrOptions extends { schema: keyof Database } |
| 100 | + ? Database[PublicTableNameOrOptions['schema']]['Tables'][TableName] extends { |
| 101 | + Update: infer U |
| 102 | + } |
| 103 | + ? U |
| 104 | + : never |
| 105 | + : PublicTableNameOrOptions extends keyof PublicSchema['Tables'] |
| 106 | + ? PublicSchema['Tables'][PublicTableNameOrOptions] extends { |
| 107 | + Update: infer U |
| 108 | + } |
| 109 | + ? U |
| 110 | + : never |
| 111 | + : never |
| 112 | + |
| 113 | +export type Enums< |
| 114 | + PublicEnumNameOrOptions extends keyof PublicSchema['Enums'] | { schema: keyof Database }, |
| 115 | + EnumName extends PublicEnumNameOrOptions extends { schema: keyof Database } |
| 116 | + ? keyof Database[PublicEnumNameOrOptions['schema']]['Enums'] |
| 117 | + : never = never |
| 118 | +> = PublicEnumNameOrOptions extends { schema: keyof Database } |
| 119 | + ? Database[PublicEnumNameOrOptions['schema']]['Enums'][EnumName] |
| 120 | + : PublicEnumNameOrOptions extends keyof PublicSchema['Enums'] |
| 121 | + ? PublicSchema['Enums'][PublicEnumNameOrOptions] |
| 122 | + : never |
| 123 | + |
| 124 | +export type CompositeTypes< |
| 125 | + PublicCompositeTypeNameOrOptions extends |
| 126 | + | keyof PublicSchema['CompositeTypes'] |
| 127 | + | { schema: keyof Database }, |
| 128 | + CompositeTypeName extends PublicCompositeTypeNameOrOptions extends { |
| 129 | + schema: keyof Database |
| 130 | + } |
| 131 | + ? keyof Database[PublicCompositeTypeNameOrOptions['schema']]['CompositeTypes'] |
| 132 | + : never = never |
| 133 | +> = PublicCompositeTypeNameOrOptions extends { schema: keyof Database } |
| 134 | + ? Database[PublicCompositeTypeNameOrOptions['schema']]['CompositeTypes'][CompositeTypeName] |
| 135 | + : PublicCompositeTypeNameOrOptions extends keyof PublicSchema['CompositeTypes'] |
| 136 | + ? PublicSchema['CompositeTypes'][PublicCompositeTypeNameOrOptions] |
| 137 | + : never |
| 138 | + |
| 139 | +type Custom = { |
| 140 | + version: number |
| 141 | + events: Array<{ |
| 142 | + type: string |
| 143 | + [x: string]: any |
| 144 | + }> |
| 145 | +} |
| 146 | + |
| 147 | +export type DatabaseOverride = MergeDeep< |
| 148 | + Database, |
| 149 | + { |
| 150 | + public: { |
| 151 | + Tables: { |
| 152 | + foo: { |
| 153 | + Row: { |
| 154 | + bar: Custom |
| 155 | + baz: Custom |
| 156 | + } |
| 157 | + Insert: { |
| 158 | + bar: Custom |
| 159 | + baz: Custom |
| 160 | + } |
| 161 | + Update: { |
| 162 | + bar?: Custom |
| 163 | + baz?: Custom |
| 164 | + } |
| 165 | + } |
| 166 | + } |
| 167 | + } |
| 168 | + } |
| 169 | +> |
| 170 | + |
| 171 | +const postgrest = new PostgrestClient<Database>('http://localhost:3000') |
| 172 | + |
| 173 | +const postgrestOverrideTypes = new PostgrestClient<DatabaseOverride>('http://localhost:3000') |
| 174 | + |
| 175 | +// Basic types |
| 176 | +{ |
| 177 | + const res = await postgrest.from('foo').select('id').eq('id', '...').single() |
| 178 | + |
| 179 | + const bar = {} as Custom |
| 180 | + const baz = {} as Custom |
| 181 | + if (res.error) { |
| 182 | + throw new Error(res.error.message) |
| 183 | + } |
| 184 | + const result = await postgrest |
| 185 | + .from('foo') |
| 186 | + .update({ |
| 187 | + bar, |
| 188 | + baz, |
| 189 | + }) |
| 190 | + .eq('id', res.data.id) |
| 191 | + expectType<null>(result.data) |
| 192 | +} |
| 193 | + |
| 194 | +// extended types |
| 195 | +{ |
| 196 | + const res = await postgrestOverrideTypes |
| 197 | + .from('foo') |
| 198 | + .select('id, bar, baz') |
| 199 | + .eq('id', '...') |
| 200 | + .single() |
| 201 | + |
| 202 | + const bar = {} as Custom |
| 203 | + const baz = {} as Custom |
| 204 | + if (res.error) { |
| 205 | + throw new Error(res.error.message) |
| 206 | + } |
| 207 | + const result = await postgrestOverrideTypes |
| 208 | + .from('foo') |
| 209 | + .update({ |
| 210 | + bar, |
| 211 | + baz, |
| 212 | + }) |
| 213 | + .eq('id', res.data.id) |
| 214 | + expectType<null>(result.data) |
| 215 | +} |
0 commit comments