Registry / database / pg-require-sql

pg-require-sql

JSON →
library1.0.0jsnpmunverified

A Node.js module that loads SQL files from disk and exports them as parameterized query functions for use with the `pg` (node-postgres) library. v1.0.0, stable but no recent updates. Key differentiator: auto-generates named query functions from .sql files, reducing boilerplate when embedding SQL in Node.js applications. Not actively maintained; ESM imported with .js extension.

npm install pg-require-sql
INSTALL
IMPORT
SIG · PG-REQUIRE-SQL
P
pg-require-sql
databasejavascriptv1.0.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

default (named import from module)
import loadQueries from 'pg-require-sql';
const loadQueries = require('pg-require-sql');
Package is ESM-only since v1.0.0; top-level default export only
loadQueries function
const queries = loadQueries(pathToDir, pgClient);
Returns object with SQL filenames (camelCased) as keys bound to a pg client
require() usage
import loadQueries from 'pg-require-sql';
const { loadQueries } = require('pg-require-sql');
CommonJS require() does not apply; package has no named exports

Shows how to load SQL files from a directory, bind them to a pg Pool, and execute a named query.

import loadQueries from 'pg-require-sql'; import pg from 'pg'; import path from 'path'; const pool = new pg.Pool({ connectionString: process.env.DATABASE_URL ?? 'postgres://localhost/myapp' }); const sqlDir = path.resolve('./sql'); const queries = loadQueries(sqlDir, pool); // Assumes ./sql/users.sql exists with: SELECT * FROM users WHERE id = $1 const { users } = queries; const result = await users('123'); console.log(result.rows);
Debug
Known issues
gotchaSQL file names are converted to camelCase: 'getUser.sql' becomes queries.getUser.
fix
Ensure SQL filenames are lowercase with hyphens or underscores; avoid special characters.
affects: >=1.0.0
gotchaThe function returned for each SQL file expects you to pass query parameters (not client). It returns a standard pg result.
fix
Call the function, e.g., `queries.getUser(id)`, not `client.query(...)`.
affects: >=1.0.0
gotchaThe path to the SQL directory must be relative to the current working directory or absolute.
fix
Use `path.resolve()` to build absolute path.
affects: >=1.0.0
gotchaOn Windows, directory separators may cause issues; use forward slashes or `path.join`.
fix
Use `path.resolve('./sql')` for cross-platform compatibility.
affects: >=1.0.0
Errors
Common errors & fixes
Cannot find module 'pg-require-sql'
Node.js cannot locate the module; likely not installed or using wrong resolution.
fix
Run `npm install pg-require-sql` and ensure package.json includes 'type': 'module' or use .mjs extension.
require() of ES Module not supported
Package is ESM-only; using CommonJS require() fails.
fix
Use `import loadQueries from 'pg-require-sql'` and set 'type': 'module' in package.json.
TypeError: pool.query is not a function
Passed a pool incorrectly or used a client instead of a pool.
fix
Ensure you pass a `pg.Pool` instance (e.g., `new pg.Pool(...)`) as the second argument.
Upgrade
Version history
1.0.0latest on npm
Audit
Dependencies
pgrequiredPeer dependency: exports functions that take a pg client/pool instance
Agent activity
5 hits · last 30 days
node
4
Resources
pg-require-sql — npm install pg-require-sql · libregistry