Here the php module rewritten for nodejs.
const db = require('../../lib/core/db');
module.exports = {
// get user from database
getuser: async function(options) {
// connection option is required
const connection = this.parseRequired(options.connection, 'string', 'connection is required.');
// userid option is required
const userId = this.parseRequired(options.userId, 'number', 'userId is required.');
// get the database connection
const db = this.getDbConnection(connection);
// throw error if connection not found
if (!db) throw new Error(`Connection "${connection}" doesn't exist.`);
// get results from database
let results = await db.raw('SELECT * FROM Users WHERE UserId = ?', [userId]);
// raw method returns raw response from driver, needs some conversion (http://knexjs.org/#Raw-Queries)
if (db.client.config.client == 'mysql' || db.client.config.client == 'mysql2') {
results = results[0];
} else if (db.client.config.client == 'postgres' || db.client.config.client == 'redshift') {
results = results.rows;
}
return results;
}
// get user from database (using knex as query builder)
getuser2: async function(options) {
// connection option is required
const connection = this.parseRequired(options.connection, 'string', 'connection is required.');
// userid option is required
const userId = this.parseRequired(options.userId, 'number', 'userId is required.');
// get the database connection
const db = this.getDbConnection(connection);
// throw error if connection not found
if (!db) throw new Error(`Connection "${connection}" doesn't exist.`);
// get results from database
return db.select('*').from('Users').where('UserId', userId);
}
}