Skip to content

Commit

Permalink
typings test inserts.
Browse files Browse the repository at this point in the history
  • Loading branch information
igalklebanov committed Oct 3, 2024
1 parent 2ccbc1e commit 4883c88
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 2 deletions.
2 changes: 1 addition & 1 deletion test/typings/shared.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,5 @@ export interface PersonMetadata {
>
schedule: JSONColumnType<{ name: string; time: string }[][][]>
record: JSONColumnType<Record<string, string>>
array: JSONColumnType<Array<string>>
array: JSONColumnType<Array<string> | null>
}
125 changes: 124 additions & 1 deletion test/typings/test-d/insert.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expectError, expectType } from 'tsd'
import { InsertResult, Kysely, sql } from '..'
import { ExpressionBuilder, InsertObject, InsertResult, Kysely, sql } from '..'
import { Database } from '../shared'

async function testInsert(db: Kysely<Database>) {
Expand Down Expand Up @@ -268,3 +268,126 @@ async function testOutput(db: Kysely<Database>) {
expectError(db.insertInto('person').output('deleted.age').values(person))
expectError(db.insertInto('person').outputAll('deleted').values(person))
}

async function testValJson(db: Kysely<Database>) {
const getValues = <
O extends Partial<InsertObject<Database, 'person_metadata'>>,
>(
{ valJson }: ExpressionBuilder<Database, 'person_metadata'>,
overrides?: O,
) => ({
array: valJson(['123']),
experience: valJson([{ establishment: 'New York Times' }]),
nicknames: valJson(['Jenny']),
person_id: 1,
profile: valJson({
auth: {
roles: ['admin'],
},
tags: ['important'],
}),
website: valJson({ url: 'http://example.com' }),
record: valJson({ key: 'value' }),
schedule: valJson([
[
[
{
name: 'foo',
time: '2024-01-01T00:00:00.000Z',
},
],
],
]),
...overrides,
})

db.insertInto('person_metadata').values(getValues).execute()

db.insertInto('person_metadata').values((eb) =>
getValues(eb, {
array: null,
}),
)

db.insertInto('person_metadata').values((eb) =>
getValues(eb, {
array: eb.valJson(null),
}),
)

db.insertInto('person_metadata').values((eb) =>
getValues(eb, {
array: sql.valJson(null),
}),
)

db.insertInto('person_metadata').values((eb) =>
getValues(eb, {
website: sql.valJson({ url: 'http://example.com' }),
}),
)

expectError(
db.insertInto('person_metadata').values((eb) =>
getValues(eb, {
array: ['123'], // expects `valJson(Array<string> | null)`
}),
),
)

expectError(
db.insertInto('person_metadata').values((eb) =>
getValues(eb, {
array: eb.val(['123']), // expects `valJson(Array<string> | null)`
}),
),
)

expectError(
db.insertInto('person_metadata').values((eb) =>
getValues(eb, {
array: eb.valJson({}), // expects `valJson(Array<string> | null)`
}),
),
)

expectError(
db.insertInto('person_metadata').values((eb) =>
getValues(eb, {
array: eb.valJson([123]), // expects `valJson(Array<string> | null)`
}),
),
)

expectError(
db.insertInto('person_metadata').values((eb) =>
getValues(eb, {
experience: [{ establishment: 'New York Times' }], // expects `valJson({ establishment: string }[])`
}),
),
)

expectError(
db.insertInto('person_metadata').values((eb) =>
getValues(eb, {
experience: eb.valJson({ establishment: 'New York Times' }), // expects `valJson({ establishment: string }[])`
}),
),
)

expectError(
db.insertInto('person_metadata').values((eb) =>
getValues(eb, {
experience: eb.valJson([{}]), // expects `valJson({ establishment: string }[])`
}),
),
)

expectError(
db.insertInto('person_metadata').values((eb) =>
getValues(eb, {
experience: eb.valJson([{ establishment: 2 }]), // expects `valJson({ establishment: string }[])`
}),
),
)
}

0 comments on commit 4883c88

Please sign in to comment.