Last active
June 17, 2021 04:22
-
-
Save anztrax/1eb43a62cc0ba9fa6209dbdc352ad45d to your computer and use it in GitHub Desktop.
maria DB with nextJS API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import mariadb from 'mariadb'; | |
const pool = mariadb.createPool({ | |
host: process.env.DB_HOST, | |
user: process.env.DB_USER, | |
password: process.env.DB_PASS, | |
database: 'my_db', | |
connectionLimit: 5 | |
}); | |
console.log('is this called ?'); | |
export default async function handler(req, res) { | |
if (req.method === 'POST') { | |
let conn; | |
try { | |
conn = await pool.getConnection(); | |
const rows = await conn.query("SELECT 1 as val"); | |
// rows: [ {val: 1}, meta: ... ] | |
const rowsData = await conn.query('SELECT 1 + 1 AS solution'); | |
console.log('row data: ',rowsData); | |
res.status(200).json(rowsData); | |
// const dbResponse = await conn.query(`INSERT INTO myTable value (?, ?)`, [1, "mariadb"]); | |
// res: { affectedRows: 1, insertId: 1, warningStatus: 0 } | |
// res.status(200).json(dbResponse); | |
} catch (err) { | |
console.log('error here : ', err); | |
throw err; | |
} finally { | |
if (conn) { | |
conn.release(); //release to pool | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment