dresende / node-orm2

Object Relational Mapping
http://github.com/dresende/node-orm2
MIT License
3.07k stars 379 forks source link

Unicode in query is improperly escaped #784

Closed jacgrady1 closed 3 years ago

jacgrady1 commented 7 years ago
const updateQuery = 'UPDATE table1 SET state = ?  where id = 1';
const state = 'abc\u008Fdef';
db.drive.execQuery(updateQuery, [state], (err, data) => {
});

orm truncate the state from the unicode escape sequence, after executing the query,
state = 'abc';

dresende commented 7 years ago

I found nodejs does not properly handle emojis properly (or didn't, I don't know the latest version). A year ago I wrote this:

function encode_emojis(s) {
    return s.replace(/[\u007f-\uffff]/g, (c) => ('\\u'+('0000'+c.charCodeAt(0).toString(16)).slice(-4)));
};

Try it before passing the string to orm.

dxg commented 3 years ago

https://stackoverflow.com/questions/60827366/node-js-emoji-in-utf8-request-returning-characters may help

lawrencebensaid commented 3 years ago

I too am experiencing a lot of trouble implementing support for emojis.

@dxg How would I apply this charset option in orm2?

dxg commented 3 years ago

@lawrencebensaid add ?charset=utf8mb4 to your connection string or query: { charset: utf8mb4' } to your connection object.

I've added a test for this as well as examples in the doco.

This will only work if your database was created with utf8mb4 encoding. This is the default encoding in MySQL 8 & newer.