Skip to content

Commit

Permalink
add type checker
Browse files Browse the repository at this point in the history
  • Loading branch information
zfben committed Feb 1, 2025
1 parent 7bd5c53 commit 14d286f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
25 changes: 21 additions & 4 deletions src/__tests__/query-builder/query.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { describe, it, expect, beforeAll, afterAll } from 'vitest'
import { describe, it, expect, beforeAll, afterAll, expectTypeOf } from 'vitest'
import { type Client, createClient } from '../../client'
import { QueryBuilder } from '../../query-builder'
import { createTestingPostgres } from '../utils'
import type { User } from '../types.test'

describe('QueryBuilder/query', () => {
let client: Client
Expand Down Expand Up @@ -42,13 +43,24 @@ describe('QueryBuilder/query', () => {
},
{ id: 2, name: 'Bob', metadata: {} },
])
expectTypeOf(result).toEqualTypeOf<User[]>()
})

it('selects specific columns', async () => {
const result = await new QueryBuilder(client, 'query')
.select('name')
const result = await new QueryBuilder(client, 'query').select('name')

expect(result).toEqual([{ name: 'Alice' }, { name: 'Bob' }])
expectTypeOf(result).toEqualTypeOf<Pick<User, 'name'>>()
})

it('selects multiple columns', async () => {
const result = await new QueryBuilder(client, 'query').select('name', 'metadata')

expect(result).toEqual([
{ name: 'Alice', metadata: { age: 100 } },
{ name: 'Bob', metadata: {} },
])
expectTypeOf(result).toEqualTypeOf<Pick<User, 'name' | 'metadata'>>()
})
})

Expand Down Expand Up @@ -194,6 +206,7 @@ describe('QueryBuilder/query', () => {
const result = await new QueryBuilder(client, 'query').pluck('name')

expect(result).toEqual(['Alice', 'Bob'])
expectTypeOf(result).toEqualTypeOf<string[]>()
})
})

Expand All @@ -210,12 +223,16 @@ describe('QueryBuilder/query', () => {
const result = await new QueryBuilder(client, 'query').first()

expect(result).toEqual({ id: 1, name: 'Alice', metadata: { age: 100 } })
expectTypeOf(result).toEqualTypeOf<User | null>()
})

it('returns the first row with column', async () => {
const result = await new QueryBuilder(client, 'query').select('name').first()
const result = await new QueryBuilder(client, 'query')
.select('name')
.first()

expect(result).toEqual({ name: 'Alice' })
expectTypeOf(result).toEqualTypeOf<Pick<User, 'name'> | null>()
})
})

Expand Down
3 changes: 2 additions & 1 deletion src/__tests__/types.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { describe, it, expectTypeOf } from 'vitest'
import type { ColumnName, ColumnValue, TableName, Tables, TableType } from '../types'

type User = {
// biome-ignore lint/suspicious/noExportsInTest: <explanation>
export type User = {
id: number
name: string
metadata: {
Expand Down

0 comments on commit 14d286f

Please sign in to comment.