|
| 1 | +import { afterEach, beforeEach, describe, expect, it } from 'vitest'; |
| 2 | +import type { ClientContract } from '../../src/client'; |
| 3 | +import { schema } from '../test-schema'; |
| 4 | +import { createClientSpecs } from './client-specs'; |
| 5 | + |
| 6 | +const PG_DB_NAME = 'client-api-raw-query-tests'; |
| 7 | + |
| 8 | +describe.each(createClientSpecs(PG_DB_NAME, true))('Client raw query tests', ({ createClient, provider }) => { |
| 9 | + let client: ClientContract<typeof schema>; |
| 10 | + |
| 11 | + beforeEach(async () => { |
| 12 | + client = await createClient(); |
| 13 | + }); |
| 14 | + |
| 15 | + afterEach(async () => { |
| 16 | + await client?.$disconnect(); |
| 17 | + }); |
| 18 | + |
| 19 | + it('works with executeRaw', async () => { |
| 20 | + await client.user.create({ |
| 21 | + data: { |
| 22 | + id: '1', |
| 23 | + |
| 24 | + }, |
| 25 | + }); |
| 26 | + |
| 27 | + await expect( |
| 28 | + client.$executeRaw`UPDATE "User" SET "email" = ${'[email protected]'} WHERE "id" = ${'1'}`, |
| 29 | + ).resolves.toBe(1); |
| 30 | + await expect(client.user.findFirst()).resolves.toMatchObject({ email: '[email protected]' }); |
| 31 | + }); |
| 32 | + |
| 33 | + it('works with executeRawUnsafe', async () => { |
| 34 | + await client.user.create({ |
| 35 | + data: { |
| 36 | + id: '1', |
| 37 | + |
| 38 | + }, |
| 39 | + }); |
| 40 | + |
| 41 | + const sql = |
| 42 | + provider === 'postgresql' |
| 43 | + ? `UPDATE "User" SET "email" = $1 WHERE "id" = $2` |
| 44 | + : `UPDATE "User" SET "email" = ? WHERE "id" = ?`; |
| 45 | + await expect(client.$executeRawUnsafe(sql, '[email protected]', '1')).resolves.toBe(1); |
| 46 | + await expect(client.user.findFirst()).resolves.toMatchObject({ email: '[email protected]' }); |
| 47 | + }); |
| 48 | + |
| 49 | + it('works with queryRaw', async () => { |
| 50 | + await client.user.create({ |
| 51 | + data: { |
| 52 | + id: '1', |
| 53 | + |
| 54 | + }, |
| 55 | + }); |
| 56 | + |
| 57 | + const uid = '1'; |
| 58 | + const users = await client.$queryRaw< |
| 59 | + { id: string; email: string }[] |
| 60 | + >`SELECT "User"."id", "User"."email" FROM "User" WHERE "User"."id" = ${uid}`; |
| 61 | + expect(users).toEqual([{ id: '1', email: '[email protected]' }]); |
| 62 | + }); |
| 63 | + |
| 64 | + it('works with queryRawUnsafe', async () => { |
| 65 | + await client.user.create({ |
| 66 | + data: { |
| 67 | + id: '1', |
| 68 | + |
| 69 | + }, |
| 70 | + }); |
| 71 | + |
| 72 | + const sql = |
| 73 | + provider === 'postgresql' |
| 74 | + ? `SELECT "User"."id", "User"."email" FROM "User" WHERE "User"."id" = $1` |
| 75 | + : `SELECT "User"."id", "User"."email" FROM "User" WHERE "User"."id" = ?`; |
| 76 | + const users = await client.$queryRawUnsafe<{ id: string; email: string }[]>(sql, '1'); |
| 77 | + expect(users).toEqual([{ id: '1', email: '[email protected]' }]); |
| 78 | + }); |
| 79 | +}); |
0 commit comments