What works (in javascript):

const GetUser = async () => {
  try {
    const connection = await pool.getConnection();
    const [data] = await connection.query("SELECT * FROM product");

    connection.release();
    return data;
  } catch (error) {
    console.error("Failed query: ", error);
    throw error;
  }
};

What works in typescript (from a github demo):

import { ResultSetHeader } from "mysql2";
import connection from "../db";
import Tutorial from "../models/tutorial.model";

interface ITutorialRepository {
  save(tutorial: Tutorial): Promise<Tutorial>;
  retrieveAll(searchParams: {title: string, published: boolean}): Promise<Tutorial[]>;
  retrieveById(tutorialId: number): Promise<Tutorial | undefined>;
  update(tutorial: Tutorial): Promise<number>;
  delete(tutorialId: number): Promise<number>;
  deleteAll(): Promise<number>;
}

class TutorialRepository implements ITutorialRepository {
  save(tutorial: Tutorial): Promise<Tutorial> {
    return new Promise((resolve, reject) => {
      connection.query<ResultSetHeader>(
        "INSERT INTO tutorials (title, description, published) VALUES(?,?,?)",
        [tutorial.title, tutorial.description, tutorial.published ? tutorial.published : false],
        (err, res) => {
          if (err) reject(err);
          else
            this.retrieveById(res.insertId)
              .then((tutorial) => resolve(tutorial!))
              .catch(reject);
        }
      );
    });
  }

  retrieveAll(searchParams: {title?: string, published?: boolean}): Promise<Tutorial[]> {
    let query: string = "SELECT * FROM tutorials";

My attempt converting from js (Next 14 flavor) to ts:

interface IUser {
  save(applicationUser: ApplicationUser): Promise<ApplicationUser>;
  fetch(email: string): Promise<string>;
  // retrieveAll(searchParams: {title: string, published: boolean}): Promise<Tutorial[]>;
  // retrieveById(tutorialId: number): Promise<Tutorial | undefined>;
  // get(userID: number): Promise<number>;
  // update(applicationUser: ApplicationUser): Promise<number>;
  // delete(applicationUserId: number): Promise<number>;
  // deleteAll(): Promise<number>;
}

class User implements IUser {
  // connection: Connection;
  save(applicationUser: ApplicationUser): Promise<ApplicationUser> {
    const connection = await pool.getConnection();
    return new Promise((resolve, reject) => {
      connection.query<ResultSetHeader>(
        "INSERT INTO application_user (first_name, last_name, password, phone, email, image_url) VALUES(?,?,?,?,?,?)",
        [
          applicationUser.firstName,
          applicationUser.lastName, 
          applicationUser.passWord, 
          applicationUser.phoneNo, 
          applicationUser.eMail, 
          applicationUser.imageUrl ? applicationUser.imageUrl : "./users/noavatar.png",
        ],
        (err, res) => {
          if (err) reject(err);
          else
            this.retrieveById(res.insertId)
              .then((applicationUser) => resolve(applicationUser!))
              .catch(reject);
        }
      );
    });
  }

  fetch(eMail: string): Promise<string> {
    try {
      const connection = await pool.getConnection();

And my results are in the screenshot:

Lot of red lines and ts errors

Edit: Found the issue - but still have no solution. It's a Promise v. Execute thing. The mySQL2 docs are lagging by boatloads and don't show adequate support for typescript models.

Promise v. Execute

Edit: Making progress? :-/

class User implements IUser {
  async save(user: ApplicationUser): Promise<ApplicationUser> {
    const connection = await pool.getConnection();
    const sql = "INSERT INTO application_user (first_name, last_name, password, phone, email, image_url) VALUES(?,?,?,?,?,?)";

    return (new Promise((resolve, reject) => {
      await connection.query(sql,
        [
          user.firstName,
          user.lastName, 
          user.passWord, 
          user.phoneNo, 
          user.eMail, 
          user.imageUrl ? user.imageUrl : "./users/noavatar.png",
        ]).then((res) =>{
          resolve(user);
          console.log(res);
        }).catch((err) => {
          reject(err);
          console.log(err)
        });
    }));
  }
0

There are 0 best solutions below