Registry / database / pg-slang

pg-slang

JSON →
library0.2.6jsnpmunverified

Converts informal SQL SELECT statements (e.g., "SELECT 'food name', 'calcium' FROM 'apples'") into formal SQL (e.g., "SELECT 'name', 'ca', 'ca_e' FROM 'compositions' WHERE ...") via a user-supplied mapping function. Version 0.2.6, low release cadence. Key differentiator: decouples user-facing informal queries from actual schema by delegating mapping to an external callback, enabling dynamic column/table resolution against a database or dictionary.

npm install pg-slang
INSTALL
IMPORT
SIG · PG-SLANG
P
pg-slang
databasejavascriptv0.2.6
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
✓ import slang from 'pg-slang'
✗ const { slang } = require('pg-slang')
Package exports a single default function. Default import works in both ESM and CommonJS.
require
✓ const slang = require('pg-slang')
✗ const { slang } = require('pg-slang')
CommonJS require returns the function directly; no named export.
default (ESM re-export)
✓ import slang from 'pg-slang'
✗ import { slang } from 'pg-slang'
No named export exists; use default import.

Converts informal SELECT with column mapping and table-based WHERE clause.

// Map function: resolves 'food name' -> ['name'], 'calcium' -> ['ca', 'ca_e'], table 'apples' -> search condition const slang = require('pg-slang'); const columns = { 'food name': ['name'], 'calcium': ['ca', 'ca_e'] }; function myMapper(text, type, hint, from) { if (type === 'column') return columns[text] || ['unknown']; if (text === 'apples') return [`"tsvector" @@ '${text}'`]; return [`"tsvector" @@ '${text}'`]; } slang(`SELECT "food name", "calcium" FROM "apples"`, myMapper, null, { from: 'compositions' }).then(console.log); // SELECT "name", "ca", "ca_e" FROM "compositions" WHERE TRUE AND (FALSE OR ("tsvector" @@ 'apples'))
Debug
Known issues
gotchaMap function must handle all four parameters (text, type, hint, from) and return an array of strings; returning a single string or non-array may cause errors.
fix
Ensure map function returns an array, e.g., return ['expression'].
affects: >=0.0.0
gotchaIf map function returns empty array for a column, it may be silently dropped or cause malformed SQL.
fix
Always return at least one expression per column, or handle gracefully.
affects: >=0.0.0
gotchaThe 'from' parameter is null for table lookups; only 'type' distinguishes table from column.
fix
Check type === 'from' or type === 'table' (whichever is documented) to identify table names.
affects: >=0.0.0
gotchaUsing CommonJS require with destructuring ({ slang }) fails because package exports a function directly.
fix
Use const slang = require('pg-slang'); (no destructuring) or import slang from 'pg-slang'
affects: >=0.0.0
Errors
Common errors & fixes
TypeError: slang is not a function
Attempting to destructure require result as `const { slang } = require('pg-slang')`
fix
Use `const slang = require('pg-slang');`
Map function must return an array
Returning a string or undefined instead of array
fix
Return an array, e.g., `return ['expression'];`
Cannot read properties of undefined (reading 'then')
Calling `slang(...).then(...)` before properly importing slang function
fix
Ensure slang is the function returned by `require('pg-slang')` or default import
Upgrade
Version history
0.2.6latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
5 hits · last 30 days
node
4
Resources
pg-slang — npm install pg-slang · libregistry