Skip to content
This repository was archived by the owner on Jul 1, 2026. It is now read-only.

Add generic type annotations for Statement and Database get/all/each methods callback rows - #1686

Merged
daniellockyer merged 2 commits into
TryGhost:masterfrom
stevescruz:stevescruz/add-generic-type-definitions-for-callback-rows
Mar 13, 2023
Merged

Add generic type annotations for Statement and Database get/all/each methods callback rows#1686
daniellockyer merged 2 commits into
TryGhost:masterfrom
stevescruz:stevescruz/add-generic-type-definitions-for-callback-rows

Conversation

@stevescruz

@stevescruzstevescruz commented Feb 24, 2023

Copy link
Copy Markdown
Contributor

Description

Small change, but some developers enjoy working with generics over the any type when using TypeScript.

Added Generics type annotations for the following methods:

  • Statement.get
  • Statement.all
  • Statement.each
  • Database.get
  • Database.all
  • Database.each

Backwards Compatibility

This will not break existing TypeScript code. It will still be possible to directly add type annotations to the row/rows.

Examples:

Take into consideration the following TypeScript code:

importsqlite3from"sqlite3";interfaceIUser{id: string;name: string;email: string;}constdb=newsqlite3.Database("./test.db");constcreateTableQuery=`CREATE TABLE IF NOT EXISTS users ( id TEXT PRIMARY KEY NOT NULL, name TEXT NOT NULL, email TEXT NOT NULL, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP)`;

How type annotations currently works:
NOTE: this will still work after the code changes.

constquery="SELECT * FROM users";db.all(query,[],(err,rows: IUser[])=>{if(err){console.error(err.message);}else{console.log(rows[1].email);}});

Now you will be able to:

constquery="SELECT * FROM users";db.all<IUser>(query,[],(err,rows)=>{if(err){console.error(err.message);}else{console.log(rows[1].email);}});

…ent methods
-For get method callback's row
-For all method callback's rows
-For each method callback's row
…se methods
-For get method callback's row
-For all method callback's rows
-For each method callback's row
@stevescruzstevescruz changed the title Add generic type definitions for Statement and Database get/all/each methods callback rowsAdd generic type annotations for Statement and Database get/all/each methods callback rowsFeb 24, 2023
@stevescruz

Copy link
Copy Markdown
ContributorAuthor

@daniellockyer thanks for running the CI pipeline.
We can add the "types" label to this PR.

@daniellockyer
daniellockyer merged commit 46da1ab into TryGhost:masterMar 13, 2023
@daniellockyer

Copy link
Copy Markdown
Contributor

@stevescruz Nice, thanks!

@stevescruz
stevescruz deleted the stevescruz/add-generic-type-definitions-for-callback-rows branch March 22, 2023 06:48
Sign up for freeto subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

@stevescruz@daniellockyer