pgsqwell
TypeScript icon, indicating that this package has built-in type declarations

4.1.0 • Public • Published

pgsqwell

SQL template tag for PostgreSQL done well

GitHub license Coverage Status

Done well because:

  • immutable
  • the sql tag for valid SQL statements sqlPart for subparts
  • allows syntax checking in your tests

Sample usage:

import 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 check queries in your Jest tests, simply add this to your tests files:

jest.mock('pgsqwell', () => require('pgsqwell/dist/mock'));

Known downsides:

Authors

License

MIT

Readme

Keywords

Package Sidebar

Install

npm i pgsqwell

Weekly Downloads

143

Version

4.1.0

License

MIT

Unpacked Size

57.4 kB

Total Files

24

Last publish

Collaborators

  • nfroidure