SQL template tag for PostgreSQL done well
Done well because:
sql
tag for valid SQL statements, sqlPart
for subparts that ain't necessarily validimport sql, {
escapeSQLIdentifier,
sqlPart,
emptySQLPart,
joinSQLValues,
} from 'pgsqwell';
const limit = 10;
const query = sql`SELECT id FROM users WHERE name=${'toto'} ${
limit ? sqlPart`LIMIT ${limit}` : emptySQLPart
}`;
const query2 = sql`SELECT id FROM ${escapeSQLIdentifier('table')}`;
const query3 = sql`SELECT id FROM users WHERE id IN ${joinSQLValues([1, 2])}}`;
const mergedQuery = sql`
${query}
UNION
${query2}
UNION
${query3}
`;
To print any query built with pgsqwell
use the DEBUG=pgsqwell
environment variable:
DEBUG=pgsqwell npm t
Use pgsqwell-mock
to check you queries in your tests.