Skip to content

Refactor queries: avoid type casts#240

Merged
ScriptRaccoon merged 2 commits into
mainfrom
refactor-queries
Jun 8, 2026
Merged

Refactor queries: avoid type casts#240
ScriptRaccoon merged 2 commits into
mainfrom
refactor-queries

Conversation

@ScriptRaccoon

@ScriptRaccoon ScriptRaccoon commented Jun 8, 2026

Copy link
Copy Markdown
Owner

This is a small refactoring of the queries in the application and the scripts.

  • allow passing no values, defaults to empty array
  • use built-in generic for db.prepare (from better-sqlite3) to avoid type casts (or rather: let the package do the type cast, there is no runtime check)
  • use uppercase AS for all aliases in SQL queries to distinguish them easily (in global searches) from type casts in TypeScript.

So instead of

const categories = db.prepare(`SELECT id FROM categories WHERE ...`).all() as Category[]

we can write

const categories = db.prepare<never[], Category>(`SELECT id FROM categories WHERE ...`).all()

Here, the first generic allows type checking of the passed arguments to .all() resp. .get(). When no arguments should be passed, never[] is appropriate.

@ScriptRaccoon ScriptRaccoon changed the title Refactor queries Refactor queries: avoid type casts Jun 8, 2026
@ScriptRaccoon ScriptRaccoon merged commit 370fa9a into main Jun 8, 2026
1 check passed
@ScriptRaccoon ScriptRaccoon deleted the refactor-queries branch June 8, 2026 23:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant