Database Queries

Before making a database query in your application, you must import the database model and initialise the connection in the file where you methods are contained.

const db = require('/model/database');

db.init opens a new database connection pool if it doesn't already exist. You can configure the connection limit inside your config file to match your MySQL server specification.

Performing Database Queries

You can perform a query using the db.query method and passing the query string and array of parameters.

const userId = '0dda8121-c0d8-42b6-aedc-6c0cc4e97ef2';
const sql = 'SELECT name, email, last_login FROM user WHERE id = ?'
const data = await db.query(sql, [userId]);

You can pass in variable values using ? and these will be automatically escaped.