-
-
Notifications
You must be signed in to change notification settings - Fork 158
/
Copy pathbasic.ts
368 lines (308 loc) · 11.4 KB
/
basic.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
import { PostgrestClient } from '../src/index'
const REST_URL = 'http://localhost:3000'
const postgrest = new PostgrestClient(REST_URL)
test('basic select table', async () => {
const res = await postgrest.from('users').select()
expect(res).toMatchSnapshot()
})
test('rpc', async () => {
const res = await postgrest.rpc('get_status', { name_param: 'supabot' })
expect(res).toMatchSnapshot()
})
test('rpc returns void', async () => {
const res = await postgrest.rpc('void_func')
expect(res).toMatchSnapshot()
})
test('custom headers', async () => {
const postgrest = new PostgrestClient(REST_URL, { headers: { apikey: 'foo' } })
expect((postgrest.from('users').select() as any).headers['apikey']).toEqual('foo')
})
describe('custom prefer headers with ', () => {
test('insert', async () => {
const postgrest = new PostgrestClient(REST_URL, { headers: { Prefer: 'tx=rollback' } })
const postgrestFilterBuilder = postgrest.from('users').insert({ username: 'dragarcia' }) as any
expect(postgrestFilterBuilder.headers['Prefer']).toContain('tx=rollback')
expect(postgrestFilterBuilder.headers['Prefer']).toContain('return=')
})
test('update', async () => {
const postgrest = new PostgrestClient(REST_URL, { headers: { Prefer: 'tx=rollback' } })
const postgrestFilterBuilder = postgrest.from('users').update({ username: 'dragarcia' }) as any
expect(postgrestFilterBuilder.headers['Prefer']).toContain('tx=rollback')
expect(postgrestFilterBuilder.headers['Prefer']).toContain('return=')
})
test('upsert', async () => {
const postgrest = new PostgrestClient(REST_URL, { headers: { Prefer: 'tx=rollback' } })
const postgrestFilterBuilder = postgrest.from('users').upsert({ username: 'dragarcia' }) as any
expect(postgrestFilterBuilder.headers['Prefer']).toContain('tx=rollback')
expect(postgrestFilterBuilder.headers['Prefer']).toContain('return=')
})
test('delete', async () => {
const postgrest = new PostgrestClient(REST_URL, { headers: { Prefer: 'tx=rollback' } })
const postgrestFilterBuilder = postgrest.from('users').delete() as any
expect(postgrestFilterBuilder.headers['Prefer']).toContain('tx=rollback')
expect(postgrestFilterBuilder.headers['Prefer']).toContain('return=')
})
})
test('auth', async () => {
const postgrest = new PostgrestClient(REST_URL).auth('foo')
expect((postgrest.from('users').select() as any).headers['Authorization']).toEqual('Bearer foo')
})
test('switch schema', async () => {
const postgrest = new PostgrestClient(REST_URL, { schema: 'personal' })
const res = await postgrest.from('users').select()
expect(res).toMatchSnapshot()
})
test('on_conflict insert', async () => {
const res = await postgrest
.from('users')
.upsert({ username: 'dragarcia' }, { onConflict: 'username' })
expect(res).toMatchSnapshot()
})
test('ignoreDuplicates upsert', async () => {
const res = await postgrest
.from('users')
.upsert({ username: 'dragarcia' }, { onConflict: 'username', ignoreDuplicates: true })
expect(res).toMatchSnapshot()
})
describe('basic insert, update, delete', () => {
test('basic insert', async () => {
let res = await postgrest
.from('messages')
.insert({ message: 'foo', username: 'supabot', channel_id: 1 })
expect(res).toMatchSnapshot()
res = await postgrest.from('messages').select()
expect(res).toMatchSnapshot()
})
test('upsert', async () => {
let res = await postgrest
.from('messages')
.upsert({ id: 3, message: 'foo', username: 'supabot', channel_id: 2 })
expect(res).toMatchSnapshot()
res = await postgrest.from('messages').select()
expect(res).toMatchSnapshot()
})
test('bulk insert', async () => {
let res = await postgrest.from('messages').insert([
{ message: 'foo', username: 'supabot', channel_id: 1 },
{ message: 'foo', username: 'supabot', channel_id: 1 },
])
expect(res).toMatchSnapshot()
res = await postgrest.from('messages').select()
expect(res).toMatchSnapshot()
})
test('basic update', async () => {
let res = await postgrest.from('messages').update({ channel_id: 2 }).eq('message', 'foo')
expect(res).toMatchSnapshot()
res = await postgrest.from('messages').select()
expect(res).toMatchSnapshot()
})
test('basic delete', async () => {
let res = await postgrest.from('messages').delete().eq('message', 'foo')
expect(res).toMatchSnapshot()
res = await postgrest.from('messages').select()
expect(res).toMatchSnapshot()
})
})
test('missing table', async () => {
const res = await postgrest.from('missing_table').select()
expect(res).toMatchSnapshot()
})
test('throwOnError throws errors instead of returning them', async () => {
let isErrorCaught = false
try {
await postgrest.from('missing_table').throwOnError().select()
} catch (error) {
expect(error).toMatchSnapshot()
isErrorCaught = true
}
expect(isErrorCaught).toBe(true)
})
test('throwOnError setting at the client level - query', async () => {
let isErrorCaught = false
const postgrest_ = new PostgrestClient(REST_URL, { throwOnError: true })
try {
await postgrest_.from('missing_table').select()
} catch (error) {
expect(error).toMatchSnapshot()
isErrorCaught = true
}
expect(isErrorCaught).toBe(true)
})
test('throwOnError setting at the client level - rpc', async () => {
let isErrorCaught = false
const postgrest_ = new PostgrestClient(REST_URL, { throwOnError: true })
try {
await postgrest_.rpc('missing_fn').select()
} catch (error) {
expect(error).toMatchSnapshot()
isErrorCaught = true
}
expect(isErrorCaught).toBe(true)
})
test('throwOnError can be disabled per call', async () => {
let isErrorCaught = false
const postgrest_ = new PostgrestClient(REST_URL, { throwOnError: true })
const { error } = await postgrest_.from('missing_table').select().throwOnError(false)
expect(error).toMatchSnapshot()
expect(isErrorCaught).toBe(false)
})
test('connection error w/o throwing', async () => {
const postgrest = new PostgrestClient('http://foo.invalid')
let isErrorCaught = false
await postgrest
.from('user')
.select()
.then(undefined, () => {
isErrorCaught = true
})
expect(isErrorCaught).toBe(false)
})
test('connection error w/ throwOnError', async () => {
const postgrest = new PostgrestClient('http://foo.invalid')
let isErrorCaught = false
await postgrest
.from('user')
.select()
.throwOnError()
.then(undefined, () => {
isErrorCaught = true
})
expect(isErrorCaught).toBe(true)
})
test('custom type', async () => {
interface User {
username: string
data: object | null
age_range: string | null
status: 'ONLINE' | 'OFFLINE'
catchphrase: 'string' | null
}
// TODO: Find a cleaner way to weave a custom type
// eq should show User's properties in LSP/IntelliSense
const { data: users } = <{ data: User[] }>(
await postgrest.from<User>('users').select().eq('username', 'supabot')
)
const user = users[0]
// Autocomplete should show properties of user after '.'
user.username
})
test("don't mutate PostgrestClient.headers", async () => {
await postgrest.from('users').select().limit(1).single()
const { error } = await postgrest.from('users').select()
expect(error).toMatchSnapshot()
})
test('allow ordering on JSON column', async () => {
const { data } = await postgrest.from('users').select().order('data->something')
expect(data).toMatchSnapshot()
})
test('Prefer: return=minimal', async () => {
const { data } = await postgrest
.from('users')
.insert({ username: 'bar' }, { returning: 'minimal' })
expect(data).toMatchSnapshot()
await postgrest.from('users').delete().eq('username', 'bar')
})
test('select with head:true', async () => {
const res = await postgrest.from('users').select('*', { head: true })
expect(res).toMatchSnapshot()
})
test('select with head:true, count:exact', async () => {
const res = await postgrest.from('users').select('*', { head: true, count: 'exact' })
expect(res).toMatchSnapshot()
})
test('select with head:true, count:planned', async () => {
const res = await postgrest.from('users').select('*', { head: true, count: 'planned' })
expect(res).toMatchSnapshot({
count: expect.any(Number),
})
})
test('select with head:true, count:estimated', async () => {
const res = await postgrest.from('users').select('*', { head: true, count: 'estimated' })
expect(res).toMatchSnapshot({
count: expect.any(Number),
})
})
test('select with count:exact', async () => {
const res = await postgrest.from('users').select('*', { count: 'exact' })
expect(res).toMatchSnapshot()
})
test("rpc with count: 'exact'", async () => {
const res = await postgrest.rpc('get_status', { name_param: 'supabot' }, { count: 'exact' })
expect(res).toMatchSnapshot()
})
test('rpc with head:true, count:exact', async () => {
const res = await postgrest.rpc(
'get_status',
{ name_param: 'supabot' },
{ head: true, count: 'exact' }
)
expect(res).toMatchSnapshot()
})
describe("insert, update, delete with count: 'exact'", () => {
test("insert with count: 'exact'", async () => {
let res = await postgrest
.from('messages')
.insert({ message: 'foo', username: 'supabot', channel_id: 1 }, { count: 'exact' })
expect(res).toMatchSnapshot()
res = await postgrest.from('messages').select()
expect(res).toMatchSnapshot()
})
test("upsert with count: 'exact'", async () => {
let res = await postgrest
.from('messages')
.upsert({ id: 3, message: 'foo', username: 'supabot', channel_id: 2 }, { count: 'exact' })
expect(res).toMatchSnapshot()
res = await postgrest.from('messages').select()
expect(res).toMatchSnapshot()
})
test("bulk insert with count: 'exact'", async () => {
let res = await postgrest.from('messages').insert(
[
{ message: 'foo', username: 'supabot', channel_id: 1 },
{ message: 'foo', username: 'supabot', channel_id: 1 },
],
{ count: 'exact' }
)
expect(res).toMatchSnapshot()
res = await postgrest.from('messages').select()
expect(res).toMatchSnapshot()
})
test("update with count: 'exact'", async () => {
let res = await postgrest
.from('messages')
.update({ channel_id: 2 }, { count: 'exact' })
.eq('message', 'foo')
expect(res).toMatchSnapshot()
res = await postgrest.from('messages').select()
expect(res).toMatchSnapshot()
})
test("basic delete count: 'exact'", async () => {
let res = await postgrest.from('messages').delete({ count: 'exact' }).eq('message', 'foo')
expect(res).toMatchSnapshot()
res = await postgrest.from('messages').select()
expect(res).toMatchSnapshot()
})
})
test('insert includes columns param', async () => {
const client = postgrest.from('users').insert([{ foo: 1 }, { bar: 2 }])
expect((client as any).url.searchParams.get('columns')).toMatchInlineSnapshot(
`"\\"foo\\",\\"bar\\""`
)
})
test('insert w/ empty body has no columns param', async () => {
const client = postgrest.from('users').insert([{}, {}])
expect((client as any).url.searchParams.has('columns')).toBeFalsy()
})
test('select with no match', async () => {
const res = await postgrest.from('users').select().eq('username', 'missing')
expect(res).toMatchInlineSnapshot(`
Object {
"body": Array [],
"count": null,
"data": Array [],
"error": null,
"status": 200,
"statusText": "OK",
}
`)
})