Skip to content

Commit

Permalink
add TypeScript benchmarks. (#1314)
Browse files Browse the repository at this point in the history
  • Loading branch information
igalklebanov authored Jan 11, 2025
1 parent a619971 commit b9b80bc
Show file tree
Hide file tree
Showing 7 changed files with 330 additions and 21 deletions.
19 changes: 19 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -247,3 +247,22 @@ jobs:

- name: Type-check JSDocs code blocks
run: npm run test:jsdocs

typescript-benchmarks:
name: TypeScript Benchmarks
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4

- name: Use Node.js
uses: actions/setup-node@v4
with:
node-version: 22.x
cache: 'npm'

- name: Install dependencies
run: npm ci

- name: Run benchmarks
run: npm run bench:ts
196 changes: 191 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
},
"scripts": {
"clean": "rm -rf dist & rm -rf test/node/dist & rm -rf test/browser/bundle.js & rm -rf helpers",
"bench:ts": "npm run build && cd ./test/ts-benchmarks && tsx ./index.ts",
"test": "npm run build && npm run test:node:build && npm run test:node:run && npm run test:typings && npm run test:esmimports && npm run test:exports",
"test:node:build": "tsc -p test/node",
"test:node": "npm run build && npm run test:node:build && npm run test:node:run",
Expand Down Expand Up @@ -82,6 +83,7 @@
],
"devDependencies": {
"@arethetypeswrong/cli": "^0.17.0",
"@ark/attest": "^0.36.0",
"@types/better-sqlite3": "^7.6.11",
"@types/chai": "^4.3.17",
"@types/chai-as-promised": "^7.1.8",
Expand Down
31 changes: 15 additions & 16 deletions src/parser/select-from-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,18 @@ export type SelectFrom<
DB,
TB extends keyof DB,
TE extends TableExpressionOrList<DB, TB>,
> =
TE extends ReadonlyArray<infer T>
? SelectQueryBuilder<From<DB, T>, FromTables<DB, TB, T>, {}>
: TE extends keyof DB & string
? // This branch creates a good-looking type for the most common case:
// selectFrom('person') --> SelectQueryBuilder<DB, 'person', {}>.
// ExtractTableAlias is needed for the case where DB == any. Without it:
// selectFrom('person as p') --> SelectQueryBuilder<DB, 'person as p', {}>
SelectQueryBuilder<DB, TB | ExtractTableAlias<DB, TE>, {}>
: // This branch creates a good-looking type for common aliased case:
// selectFrom('person as p') --> SelectQueryBuilder<DB & { p: Person }, 'p', {}>.
TE extends `${infer T} as ${infer A}`
? T extends keyof DB
? SelectQueryBuilder<DB & ShallowRecord<A, DB[T]>, TB | A, {}>
: never
: SelectQueryBuilder<From<DB, TE>, FromTables<DB, TB, TE>, {}>
> = TE extends keyof DB & string
? // This branch creates a good-looking type for the most common case:
// selectFrom('person') --> SelectQueryBuilder<DB, 'person', {}>.
// ExtractTableAlias is needed for the case where DB == any. Without it:
// selectFrom('person as p') --> SelectQueryBuilder<DB, 'person as p', {}>
SelectQueryBuilder<DB, TB | ExtractTableAlias<DB, TE>, {}>
: // This branch creates a good-looking type for common aliased case:
// selectFrom('person as p') --> SelectQueryBuilder<DB & { p: Person }, 'p', {}>.
TE extends `${infer T} as ${infer A}`
? T extends keyof DB
? SelectQueryBuilder<DB & ShallowRecord<A, DB[T]>, TB | A, {}>
: never
: TE extends ReadonlyArray<infer T>
? SelectQueryBuilder<From<DB, T>, FromTables<DB, TB, T>, {}>
: SelectQueryBuilder<From<DB, TE>, FromTables<DB, TB, TE>, {}>
1 change: 1 addition & 0 deletions test/ts-benchmarks/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import './selectFrom.bench.js'
Loading

0 comments on commit b9b80bc

Please sign in to comment.