mirror of
https://github.com/knex/knex.git
synced 2026-01-05 19:47:55 +00:00
Move transaction type tests to TSD (#4208)
This commit is contained in:
parent
b766dbce2c
commit
000fde3f78
@ -229,8 +229,9 @@
|
||||
"tsd": {
|
||||
"directory": "test-tsd",
|
||||
"compilerOptions": {
|
||||
"esModuleInterop": true,
|
||||
"module": "commonjs"
|
||||
"esModuleInterop": false,
|
||||
"module": "commonjs",
|
||||
"target": "ES2017"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
96
test-tsd/transaction.test-d.ts
Normal file
96
test-tsd/transaction.test-d.ts
Normal file
@ -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);
|
||||
}
|
||||
));
|
||||
}
|
||||
@ -2667,88 +2667,6 @@ const main = async () => {
|
||||
.select('id')
|
||||
.modify<User, Pick<User, 'id' | 'age'>>(withAge);
|
||||
|
||||
// Transactions:
|
||||
|
||||
// $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 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);
|
||||
}
|
||||
);
|
||||
|
||||
// With:
|
||||
|
||||
// $ExpectType any[]
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user