Skip to content

Instantly share code, notes, and snippets.

@dncrews
Created July 28, 2022 00:59
Show Gist options
  • Save dncrews/3a3a47be1d55a9a3fe7504e91511c014 to your computer and use it in GitHub Desktop.
Save dncrews/3a3a47be1d55a9a3fe7504e91511c014 to your computer and use it in GitHub Desktop.
Add parameters to query from TypeORM
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