node-mysql2
node-mysql2 copied to clipboard
non-ascii named placeholders
I wish the requiremets for placeholders names matched the ones for js identifiers.
My project uses the local language in domain model identifiers, including some local non-ascii letters (æøå). This works fine with with js/ts generally, but does not work with named placeholders.
This means I either have to let requirements from a specific persistence library pollute my core model, maintain an anti-corruption layer with redundant DTOs and mappers just for the spelling, or generate modified param objects on the fly (vulnerable to naming conflicts).
None of them are very tempting.
I would like to be able to do this:
const person:Person = {
...
født: 1972,
...
}
query (
`
UPDATE person
SET født=:født,
...
WHERE id = :id
`,
person
)
-but mysql2 fails to bind placeholders with non-ascii characters: "Error: Bind parameters must not contain undefined. To pass SQL NULL specify JS null" If I 'normalize' the field name/placeholder to "foedt", everything works fine
mysql2 uses mysqljs/named-placeholders for this. The placeholders are identified using a regex.
I think you can close this issue and open new one in linked project.
I probably want to phase out "named placeholder" in favour of external or build in tagged literal based placeholder generation.
PR to support built in api: https://github.com/sidorares/node-mysql2/pull/1539
External modules:
- https://github.com/nearform/sql
- https://github.com/blakeembrey/sql-template-tag
With 'template literal client' your example would look like
sql`
UPDATE person
SET født=${person.født},
...
WHERE id = ${person.id}
`;