const { Sequelize } = require('sequelize')

const sequelize = new Sequelize('app_db', 'db_user', 'db_user_pass', {
  host: 'localhost',
  port: 6033,
  dialect: 'mysql'
})

const User = sequelize.define(
  'User',
  {
    firstName: { type: Sequelize.STRING, allowNull: false },
    lastName: { type: Sequelize.STRING, allowNull: false }
  },
  { timestamps: false }
)

;(async () => {
  try {
    await sequelize.authenticate()
    console.log('Connection has been established successfully.')

    // create table
    await sequelize.sync()

    const user = await User.create({ firstName: 'John', lastName: 'Doe' })
    console.log('Created user: ', user)

    const users = await User.findAll()
    console.log('Found all users: ', users)

    await sequelize.close()
  } catch (error) {
    console.error('Unable to connect to the database:', error)
  }
})()
