Created
March 17, 2022 12:18
-
-
Save charliemday/b0668f1564afb8c8a264972777420683 to your computer and use it in GitHub Desktop.
Print TypeORM SQL with Variables
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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