Skip to content

Instantly share code, notes, and snippets.

@charliemday
Created March 17, 2022 12:18
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save charliemday/b0668f1564afb8c8a264972777420683 to your computer and use it in GitHub Desktop.
Save charliemday/b0668f1564afb8c8a264972777420683 to your computer and use it in GitHub Desktop.
Print TypeORM SQL with Variables
let [sql, params] = q.getQueryAndParameters();
params.forEach((value, index) => {
if (typeof value === 'string') {
sql = sql.replace(`$${index + 1}`, `'${value}'`);
}
if (typeof value === 'object') {
if (Array.isArray(value)) {
sql = sql.replace(
`$${index + 1}`,
value.map((element) => (typeof element === 'string' ? `'${element}'` : element)).join(','),
);
} else {
sql = sql.replace(`$${index + 1}`, value);
}
}
if (['number', 'boolean'].includes(typeof value)) {
sql = sql.replace(`$${index + 1}`, value.toString());
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment