Registry / database / postgres-partial

postgres-partial

JSON →
library0.2.0jsnpmunverified

A lightweight wrapper around postgres.js (the porsager/postgres library) for building partial and dynamic SQL queries. Version 0.2.0 is the latest and only release. Unlike query builders like Knex or raw template strings, postgres-partial allows composing SQL fragments (e.g., WHERE clauses, SET clauses) that are merged into a final statement while preserving parameter safety. Designed for projects that use postgres.js and need a minimal, typed way to conditionally build queries without a full ORM. Types included.

npm install postgres-partial
INSTALL
IMPORT
SIG · POSTGRES-PARTIAL
P
postgres-partial
databasejavascriptv0.2.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.

sql, and, or, not, eq, neq, gt, gte, lt, lte, like, ilike, in, notIn, isNull, isNotNull, between, notBetween
import { sql, eq, and } from 'postgres-partial'
import postgresPartial from 'postgres-partial'
The module exports named functions, not a default export. All helpers are top-level exports.
PartialQuery
import { PartialQuery } from 'postgres-partial'
TypeScript type for a partial query object; also exported as a type.
Context
import { Context } from 'postgres-partial'
Type for the context object passed to `sql` to build the final query.

Shows building a SELECT query with dynamic WHERE conditions using `and`, `eq`, and the `PartialQuery` object.

import postgres from 'postgres'; import { sql, eq, and } from 'postgres-partial'; const db = postgres(process.env.DATABASE_URL ?? ''); // Build a partial query const partial = sql`SELECT * FROM users WHERE ${and( eq('name', 'Alice'), eq('age', 30) )}`; // Apply to a full query using the context const context = partial.getContext(); sql`${sql('')}${context.query}`; // Unused here; instead: const finalSql = sql`${partial}`; const result = await db.unsafe(finalSql);
Debug
Known issues
gotchaDo not interpolate user input directly into partial queries; always use the provided helpers like eq() to avoid SQL injection.
fix
Use e.g. eq('name', userInput) instead of sql`name = ${userInput}`.
affects: >=0.0.0
breakingNo breaking changes yet as this is the initial release (0.2.0).
fix
None.
affects: 0.2.0
deprecatedNo deprecations yet.
fix
None.
affects: 0.2.0
Errors
Common errors & fixes
TypeError: sql(...).getContext is not a function
Calling getContext on a template literal result that hasn't been converted to a PartialQuery object properly.
fix
Ensure you have imported `sql` from 'postgres-partial' (not from 'postgres') and that the template literal uses the imported `sql` function.
ReferenceError: eq is not defined
Forgetting to import `eq` from 'postgres-partial'.
fix
Add `import { eq } from 'postgres-partial'`.
Upgrade
Version history
0.2.0latest on npm
Audit
Dependencies
postgres.jsrequiredPeer dependency; postgres-partial is a wrapper around postgres.js and requires it to be installed.
Agent activity
7 hits · last 30 days
node
6
Resources
postgres-partial — npm install postgres-partial · libregistry