|
| 1 | +import { PostgrestClient } from '../src/index' |
| 2 | +import { Database } from './types.override' |
| 3 | +import { expectType } from 'tsd' |
| 4 | +import { TypeEqual } from 'ts-expect' |
| 5 | + |
| 6 | +const REST_URL = 'http://localhost:3000' |
| 7 | +export const postgrest = new PostgrestClient<Database>(REST_URL) |
| 8 | + |
| 9 | +test('select with aggregate count function', async () => { |
| 10 | + const res = await postgrest.from('users').select('username, messages(count)').limit(1).single() |
| 11 | + expect(res).toMatchInlineSnapshot(` |
| 12 | + Object { |
| 13 | + "count": null, |
| 14 | + "data": Object { |
| 15 | + "messages": Array [ |
| 16 | + Object { |
| 17 | + "count": 3, |
| 18 | + }, |
| 19 | + ], |
| 20 | + "username": "supabot", |
| 21 | + }, |
| 22 | + "error": null, |
| 23 | + "status": 200, |
| 24 | + "statusText": "OK", |
| 25 | + } |
| 26 | + `) |
| 27 | + let result: Exclude<typeof res.data, null> |
| 28 | + let expected: { |
| 29 | + username: string |
| 30 | + messages: Array<{ |
| 31 | + count: number |
| 32 | + }> |
| 33 | + } |
| 34 | + expectType<TypeEqual<typeof result, typeof expected>>(true) |
| 35 | +}) |
| 36 | + |
| 37 | +test('select with aggregate count on a column function', async () => { |
| 38 | + const res = await postgrest |
| 39 | + .from('users') |
| 40 | + .select('username, messages(id.count())') |
| 41 | + .limit(1) |
| 42 | + .single() |
| 43 | + expect(res).toMatchInlineSnapshot(` |
| 44 | + Object { |
| 45 | + "count": null, |
| 46 | + "data": Object { |
| 47 | + "messages": Array [ |
| 48 | + Object { |
| 49 | + "count": 3, |
| 50 | + }, |
| 51 | + ], |
| 52 | + "username": "supabot", |
| 53 | + }, |
| 54 | + "error": null, |
| 55 | + "status": 200, |
| 56 | + "statusText": "OK", |
| 57 | + } |
| 58 | + `) |
| 59 | + let result: Exclude<typeof res.data, null> |
| 60 | + let expected: { |
| 61 | + username: string |
| 62 | + messages: Array<{ |
| 63 | + count: number |
| 64 | + }> |
| 65 | + } |
| 66 | + expectType<TypeEqual<typeof result, typeof expected>>(true) |
| 67 | +}) |
| 68 | + |
| 69 | +test('select with aggregate count function and alias', async () => { |
| 70 | + const res = await postgrest |
| 71 | + .from('users') |
| 72 | + .select('username, messages(message_count:count())') |
| 73 | + .limit(1) |
| 74 | + .single() |
| 75 | + expect(res).toMatchInlineSnapshot(` |
| 76 | + Object { |
| 77 | + "count": null, |
| 78 | + "data": Object { |
| 79 | + "messages": Array [ |
| 80 | + Object { |
| 81 | + "message_count": 3, |
| 82 | + }, |
| 83 | + ], |
| 84 | + "username": "supabot", |
| 85 | + }, |
| 86 | + "error": null, |
| 87 | + "status": 200, |
| 88 | + "statusText": "OK", |
| 89 | + } |
| 90 | + `) |
| 91 | + let result: Exclude<typeof res.data, null> |
| 92 | + let expected: { |
| 93 | + username: string |
| 94 | + messages: Array<{ |
| 95 | + message_count: number |
| 96 | + }> |
| 97 | + } |
| 98 | + expectType<TypeEqual<typeof result, typeof expected>>(true) |
| 99 | +}) |
| 100 | + |
| 101 | +test('select with aggregate nested count function', async () => { |
| 102 | + const res = await postgrest |
| 103 | + .from('users') |
| 104 | + .select('username, messages(channels(count))') |
| 105 | + .limit(1) |
| 106 | + .single() |
| 107 | + expect(res).toMatchInlineSnapshot(` |
| 108 | + Object { |
| 109 | + "count": null, |
| 110 | + "data": Object { |
| 111 | + "messages": Array [ |
| 112 | + Object { |
| 113 | + "channels": Object { |
| 114 | + "count": 1, |
| 115 | + }, |
| 116 | + }, |
| 117 | + Object { |
| 118 | + "channels": Object { |
| 119 | + "count": 1, |
| 120 | + }, |
| 121 | + }, |
| 122 | + Object { |
| 123 | + "channels": Object { |
| 124 | + "count": 1, |
| 125 | + }, |
| 126 | + }, |
| 127 | + ], |
| 128 | + "username": "supabot", |
| 129 | + }, |
| 130 | + "error": null, |
| 131 | + "status": 200, |
| 132 | + "statusText": "OK", |
| 133 | + } |
| 134 | + `) |
| 135 | + let result: Exclude<typeof res.data, null> |
| 136 | + let expected: { |
| 137 | + username: string |
| 138 | + messages: Array<{ |
| 139 | + channels: { |
| 140 | + count: number |
| 141 | + } |
| 142 | + }> |
| 143 | + } |
| 144 | + expectType<TypeEqual<typeof result, typeof expected>>(true) |
| 145 | +}) |
| 146 | + |
| 147 | +test('select with aggregate nested count function and alias', async () => { |
| 148 | + const res = await postgrest |
| 149 | + .from('users') |
| 150 | + .select('username, messages(channels(channel_count:count()))') |
| 151 | + .limit(1) |
| 152 | + .single() |
| 153 | + expect(res).toMatchInlineSnapshot(` |
| 154 | + Object { |
| 155 | + "count": null, |
| 156 | + "data": Object { |
| 157 | + "messages": Array [ |
| 158 | + Object { |
| 159 | + "channels": Object { |
| 160 | + "channel_count": 1, |
| 161 | + }, |
| 162 | + }, |
| 163 | + Object { |
| 164 | + "channels": Object { |
| 165 | + "channel_count": 1, |
| 166 | + }, |
| 167 | + }, |
| 168 | + Object { |
| 169 | + "channels": Object { |
| 170 | + "channel_count": 1, |
| 171 | + }, |
| 172 | + }, |
| 173 | + ], |
| 174 | + "username": "supabot", |
| 175 | + }, |
| 176 | + "error": null, |
| 177 | + "status": 200, |
| 178 | + "statusText": "OK", |
| 179 | + } |
| 180 | + `) |
| 181 | + let result: Exclude<typeof res.data, null> |
| 182 | + let expected: { |
| 183 | + username: string |
| 184 | + messages: Array<{ |
| 185 | + channels: { |
| 186 | + channel_count: number |
| 187 | + } |
| 188 | + }> |
| 189 | + } |
| 190 | + expectType<TypeEqual<typeof result, typeof expected>>(true) |
| 191 | +}) |
| 192 | + |
| 193 | +test('select with aggregate sum function', async () => { |
| 194 | + const res = await postgrest.from('users').select('username, messages(id.sum())').limit(1).single() |
| 195 | + expect(res).toMatchInlineSnapshot(` |
| 196 | + Object { |
| 197 | + "count": null, |
| 198 | + "data": Object { |
| 199 | + "messages": Array [ |
| 200 | + Object { |
| 201 | + "sum": 7, |
| 202 | + }, |
| 203 | + ], |
| 204 | + "username": "supabot", |
| 205 | + }, |
| 206 | + "error": null, |
| 207 | + "status": 200, |
| 208 | + "statusText": "OK", |
| 209 | + } |
| 210 | + `) |
| 211 | + let result: Exclude<typeof res.data, null> |
| 212 | + let expected: { |
| 213 | + username: string |
| 214 | + messages: Array<{ |
| 215 | + sum: number |
| 216 | + }> |
| 217 | + } |
| 218 | + expectType<TypeEqual<typeof result, typeof expected>>(true) |
| 219 | +}) |
| 220 | + |
| 221 | +test('select with aggregate aliased sum function', async () => { |
| 222 | + const res = await postgrest |
| 223 | + .from('users') |
| 224 | + .select('username, messages(sum_id:id.sum())') |
| 225 | + .limit(1) |
| 226 | + .single() |
| 227 | + expect(res).toMatchInlineSnapshot(` |
| 228 | + Object { |
| 229 | + "count": null, |
| 230 | + "data": Object { |
| 231 | + "messages": Array [ |
| 232 | + Object { |
| 233 | + "sum_id": 7, |
| 234 | + }, |
| 235 | + ], |
| 236 | + "username": "supabot", |
| 237 | + }, |
| 238 | + "error": null, |
| 239 | + "status": 200, |
| 240 | + "statusText": "OK", |
| 241 | + } |
| 242 | + `) |
| 243 | + let result: Exclude<typeof res.data, null> |
| 244 | + let expected: { |
| 245 | + username: string |
| 246 | + messages: Array<{ |
| 247 | + sum_id: number |
| 248 | + }> |
| 249 | + } |
| 250 | + expectType<TypeEqual<typeof result, typeof expected>>(true) |
| 251 | +}) |
| 252 | + |
| 253 | +test('select with aggregate sum function on nested relation', async () => { |
| 254 | + const res = await postgrest |
| 255 | + .from('users') |
| 256 | + .select('username, messages(channels(id.sum()))') |
| 257 | + .limit(1) |
| 258 | + .single() |
| 259 | + expect(res).toMatchInlineSnapshot(` |
| 260 | + Object { |
| 261 | + "count": null, |
| 262 | + "data": Object { |
| 263 | + "messages": Array [ |
| 264 | + Object { |
| 265 | + "channels": Object { |
| 266 | + "sum": 1, |
| 267 | + }, |
| 268 | + }, |
| 269 | + Object { |
| 270 | + "channels": Object { |
| 271 | + "sum": 2, |
| 272 | + }, |
| 273 | + }, |
| 274 | + Object { |
| 275 | + "channels": Object { |
| 276 | + "sum": 3, |
| 277 | + }, |
| 278 | + }, |
| 279 | + ], |
| 280 | + "username": "supabot", |
| 281 | + }, |
| 282 | + "error": null, |
| 283 | + "status": 200, |
| 284 | + "statusText": "OK", |
| 285 | + } |
| 286 | + `) |
| 287 | + let result: Exclude<typeof res.data, null> |
| 288 | + let expected: { |
| 289 | + username: string |
| 290 | + messages: Array<{ |
| 291 | + channels: { |
| 292 | + sum: number |
| 293 | + } |
| 294 | + }> |
| 295 | + } |
| 296 | + expectType<TypeEqual<typeof result, typeof expected>>(true) |
| 297 | +}) |
0 commit comments