Using MySQL v8 in Node.js

From Logic Wiki
Jump to: navigation, search


install driver

npm install @mysql/xdevapi --save --save-exact

Sample code

var mysqlx = require('@mysql/xdevapi');

const connectionOptions = {
    user: 'root',
    password: 'C1pralex',
    host: 'localhost',
    port: '33060'
}
const result = async function () {
    console.log('function called');
    // Connect to server on localhost
    var res = [];
    await mysqlx
        .getSession(connectionOptions)
        .then(s => { return s.getSchema('OrderTaker') })
        .then(t => { return t.getTable('AccUser') })
        .then(c => c.select('Id', 'FirstName', 'LastName')
            .execute(row => myHandler(row)));

    function myHandler(row) {
        console.log(row);
        res.push(row);
    }

    return res;
}

exports.result = result;