I am using @mysql/xdevapi
in my Express.js application for creating REST web APIs.
My current way of using @mysql/xdevapi is
const mysqlx = require('@mysql/xdevapi')
const options = {
host: 'mysql',
port: 33060,
password: 'password',
user: 'root',
schema: 'DB_name'
}
const session = await mysqlx.getSession(options)
var getData = async function (req, res) {
try {
let { id } = req.body
var data = (await session.sql(`SELECT * FROM data_table WHERE id = ${id}`).execute()).fetchAll()
res.json({ success: true, data: data })
} catch (error) {
res.json({ success: false, message: "Error in getData" })
}
}
var app = express()
app.post('/getData', getData)
var server = app.listen(config.express.bindPort, config.express.bindIp, function () {
console.log(`Express listening on port ${config.express.bindIp}:${config.express.bindPort}`)
})
This setup give me error after few hours
Error: This session was closed because the connection has been idle too long. Use "mysqlx.getSession()" or "mysqlx.getClient()" to create a new one.
What should be right way to use @mysql/xdevapi
with Express.js? Any good Boilerplate code?