node-modulesnode-mysqlnode-mysql2

Using MySQL functions with Node mysql2 library


This is my current request to insert data:

let record = {
    ip : '127.0.0.1',
    ...
};
conn.query('INSERT INTO events_log SET ?', [record]);

Is it possible to apply INET6_ATON function to the IP without converting it to a plain query? There are tens of parameters in this object and I wouldn't want to disassemble it.

The IP field is varbinary(16) type.


Solution

  • Yes, it is possible:

    let INET6_ATON = { toSqlString: function() { return 'INET6_ATON(' + mysql.escape(ip) + ')'; } };
    
    let record = {
        ip : INET6_ATON,
        ...
    };
    conn.query('INSERT INTO events_log SET ?', [record]);