DB - Retrieving data

<button id="btn">Load data</button><br> <output id="out"></output> <script src="src/db.js"></script> <script> async function loadData() { let rows = await DB.select(` SELECT U.name, SUM(A.credit) AS total, COUNT(A.credit) AS accounts FROM users U left JOIN accounts A ON A.user=U.rowid WHERE A.credit>? GROUP BY A.user ORDER BY A.credit DESC `, [20]); return rows.map(row => `${row.name} has total ${row.total} credits in ${row.accounts} accounts.`).join('<br>'); } btn.onclick = async _ => out.innerHTML = await loadData(); </script>

DB.select syntax

(array of objects) DB.select(string query, [array params])

The only mandatory argument is the SQL SELECT query, which can contain ? tokens. If it does contain them, the second argument must be present in form of array with the same amount of values, which are (properly escaped) placed one by one into the query. The return value is array of objects, where its attributes correspond to the names of selected columns. If the query doesn't return any row, the return value is an empty array.

There is also similar method for retrieving a single row:

object DB.selectRow(string query, [array params])

Here the first row is returned as an object. If the query doesn't return any row, undefined is returned.