Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move transaction type tests to TSD (#4208)
- Loading branch information
Showing
3 changed files
with
99 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import knexDefault, { Knex } from '../types'; | ||
import { clientConfig } from './common'; | ||
import { expectType } from 'tsd'; | ||
|
||
const knexInstance = knexDefault(clientConfig); | ||
|
||
interface Article { | ||
id: number; | ||
subject: string; | ||
body?: string; | ||
authorId?: string; | ||
} | ||
|
||
const main = async () => { | ||
// # Select: | ||
|
||
expectType<any[]>(await knexInstance.transaction((trx) => { | ||
return trx.insert({ name: 'Old Books' }, 'id').into('articles'); | ||
})); | ||
|
||
expectType<Pick<Article, "id" | "subject">[]>(await knexInstance.transaction((trx) => { | ||
const articles: Article[] = [ | ||
{ id: 1, subject: 'Canterbury Tales' }, | ||
{ id: 2, subject: 'Moby Dick' }, | ||
{ id: 3, subject: 'Hamlet' }, | ||
]; | ||
return trx | ||
.insert(articles) | ||
.into<Article>('articles') | ||
.returning(['id', 'subject']); | ||
})); | ||
|
||
expectType<Article[]>(await knexInstance.transaction((trx) => { | ||
return trx | ||
.select('*') | ||
.from<Article>('articles'); | ||
})); | ||
|
||
expectType<Pick<Article, "id" | "subject">[]> (await knexInstance.transaction((trx) => { | ||
const articles = [ | ||
{ id: 1, subject: 'Canterbury Tales' }, | ||
{ id: 2, subject: 'Moby Dick' }, | ||
{ id: 3, subject: 'Hamlet' }, | ||
] as const; | ||
return trx | ||
.insert(articles) | ||
.into<Article>('articles') | ||
.returning(['id', 'subject']); | ||
})); | ||
|
||
expectType<any[]> (await knexInstance.transaction(async (trx) => { | ||
const articles: Article[] = [ | ||
{ id: 1, subject: 'Canterbury Tales' }, | ||
{ id: 2, subject: 'Moby Dick' }, | ||
{ id: 3, subject: 'Hamlet' }, | ||
]; | ||
return knexInstance | ||
.insert(articles, ['id', 'subject']) | ||
.into<Article>('articles') | ||
.transacting(trx) | ||
.then(trx.commit) | ||
.catch(trx.rollback); | ||
})); | ||
|
||
expectType<any[]>(await knexInstance.transaction(async (trx) => { | ||
const articles: ReadonlyArray<Article> = [ | ||
{ id: 1, subject: 'Canterbury Tales' }, | ||
{ id: 2, subject: 'Moby Dick' }, | ||
{ id: 3, subject: 'Hamlet' }, | ||
]; | ||
return knexInstance | ||
.insert(articles, ['id', 'subject']) | ||
.into<Article>('articles') | ||
.transacting(trx) | ||
.then(trx.commit) | ||
.catch(trx.rollback); | ||
})); | ||
|
||
expectType<Pick<Article, "id" | "subject">[]> (await knexInstance.transaction( | ||
async ( | ||
trx: Knex.Transaction<Article, Pick<Article, 'id' | 'subject'>[]> | ||
) => { | ||
const articles: Article[] = [ | ||
{ id: 1, subject: 'Canterbury Tales' }, | ||
{ id: 2, subject: 'Moby Dick' }, | ||
{ id: 3, subject: 'Hamlet' }, | ||
]; | ||
return knexInstance | ||
.insert(articles, ['id', 'subject']) | ||
.into<Article>('articles') | ||
.transacting(trx) | ||
.then(trx.commit) | ||
.catch(trx.rollback); | ||
} | ||
)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters