IONIC 4 SQLITE get ID of last insert | SQLITE get id after insert

Cheers, I’ve seen around a couple of posts about doing this but I can’t figure out how to get it done in ionic and typescript

Here is my insert

   /**
   * Inserir na DB
   */
  public insert(appointment: Appointment){
    return this.databaseService.getDB()
    .then((db: SQLiteObject) => {
      db.sqlBatch([
        ["INSERT INTO Appointment (dia, horaInicial, horaFinal, description, area) values (?,?,?,?,?)", [appointment.dia, appointment.horaInicial, appointment.horaFinal, appointment.description, appointment.area]]
      ])
        .then(() => console.log('Appointment inserido com sucesso.'))
        .catch(e => console.error('Erro ao inserir o Appointment', e));
    })
    .catch((e) => console.error(e));
  }

From what I know I should be getting a variable in then and accessing the last inserted id from that variable, i tried some things but nothing worked, anyone has any idea how to do it?

EDIT: now that I think about it, optimally it would be amazing to get the ID exactly when I insert, while doing the insert, that way there will be no mistakes, imagine a database getting manyyy inserts at the same time, that thing of getting last inserted ID might be wrong, but yeah if that’s not possible to get ID when we insert, then I’ll do with get last inserted ID

I’ll provide an example of how you would go about doing it, I wanna document this so it stays clear for anyone in the future with the same problem

I ended up trying a lot of random queries and sintaxes myself and eventually randomly got it
Instead of sqlbatch gotta use execute sql to return an object with that id

  public insert(appointment: Appointment){
    return this.databaseService.getDB()
    .then((db: SQLiteObject) => {
      let sql = "INSERT INTO Appointment (dia, horaInicial, horaFinal, description, area) values (?,?,?,?,?)";
      let data = [appointment.dia, appointment.horaInicial, appointment.horaFinal, appointment.description, appointment.area];

      return db.executeSql(sql, data)
      .then((row: any) => {
        console.log('Appointment inserido com sucesso. Id:', row);
        return row.insertId;
      })
      .catch((e) => console.error(e));
    })
    .catch((e) => console.error(e));
  }

image