Created
July 28, 2022 00:59
-
-
Save dncrews/3a3a47be1d55a9a3fe7504e91511c014 to your computer and use it in GitHub Desktop.
Add parameters to query from TypeORM
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
import { readFile, writeFile } from 'fs/promises'; | |
const inputFile = 'input.txt'; | |
const main = async () => { | |
const input = await readFile(inputFile, 'utf8'); | |
const [queryRaw, parametersRaw] = input.split(' -- PARAMETERS: '); | |
// console.info({ queryRaw, parametersRaw }); | |
const parameters: string[] = JSON.parse(parametersRaw); | |
// console.info({ parameters }); | |
const query = queryRaw.replace(/\$\d+/g, (_, __): string => { | |
const value = parameters.shift(); | |
if (value === undefined) { | |
throw new Error('Not enough parameters'); | |
} | |
// console.info({ value }); | |
return `'${value}'`; | |
}); | |
// console.info({ query }); | |
await writeFile('output.txt', query, 'utf8'); | |
}; | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment