Registry / database / knex-postgres-transactional-upsert

knex-postgres-transactional-upsert

JSON →
library0.1.1jsnpmunverified

Utility for performing transactional upserts (INSERT ... ON CONFLICT DO UPDATE) in PostgreSQL via Knex. Current version 0.1.1, no recent updates. Provides two functions: upsert (single row, no transaction) and transactionalUpsert (batch rows with transaction). Lightweight wrapper; no active maintenance. Differentiated by focusing solely on PostgreSQL upsert with a simple API and configurable batch size.

npm install knex-postgres-transactional-upsert
INSTALL
IMPORT
SIG · KNEX-POSTGRES-TRAN
K
knex-postgres-transactional-upsert
databasejavascriptv0.1.1
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.

getUpserters
const getUpserters = require('knex-postgres-transactional-upsert');
import getUpserters from 'knex-postgres-transactional-upsert';
Package is CommonJS only; no ESM export. Use require().
upsert
const upserters = require('knex-postgres-transactional-upsert')(knex); upserters.upsert('table', id, data);
const { upsert } = require('knex-postgres-transactional-upsert')(knex);
The module is a factory function; destructure the returned object.
transactionalUpsert
upserters.transactionalUpsert('table', rows, typeMap, primaryKey);
require('knex-postgres-transactional-upsert').transactionalUpsert(...);
Must call factory first to get the object containing both methods.

Initializes Knex with PostgreSQL, creates the upsert helper, and demonstrates both single and batch transactional upserts.

const knex = require('knex')({ client: 'pg', connection: process.env.DATABASE_URL || 'postgres://localhost/mydb' }); const getUpserters = require('knex-postgres-transactional-upsert'); const upserters = getUpserters(knex, { batchSize: 100 }); // Upsert a single row (no transaction) upserters.upsert('users', 1, { name: 'Alice', email: 'alice@example.com' }); // Upsert many rows transactionally const rows = [ { id: 1, name: 'Alice', email: 'alice@example.com' }, { id: 2, name: 'Bob', email: 'bob@example.com' } ]; const fieldToTypeMap = { name: 'text', email: 'text' }; const primaryKey = 'id'; upserters.transactionalUpsert('users', rows, fieldToTypeMap, primaryKey);
Debug
Known issues
gotchaThe upsert function does not create a transaction; for atomicity use transactionalUpsert. Running many upsert calls without a transaction can lead to partial failures.
fix
Wrap manual upsert calls in a Knex transaction, or use transactionalUpsert for batch operations.
affects: <=0.1.1
gotchaRequires pass of 'fieldToTypeMap' containing only the columns to update, excluding the primary key; including primary key in the map may cause unexpected errors.
fix
Omit the primary key column from the fieldToTypeMap object.
affects: 0.1.1
breakingThe module is a factory function that must be invoked with Knex instance; calling require() does not directly return the methods.
fix
Call the required function with a Knex instance, e.g., const upserters = require('knex-postgres-transactional-upsert')(knex, opts);
affects: 0.1.0, 0.1.1
Errors
Common errors & fixes
TypeError: getUpserters is not a function
Using ES import syntax on a CommonJS module that exports a factory function.
fix
Use require() and call the result: const upserters = require('knex-postgres-transactional-upsert')(knex);
Error: relation "tablename" does not exist
Table name misspelled or schema not specified and not in search path.
fix
Use proper schema notation, e.g., 'public.tablename'.
TypeError: Cannot destructure property 'upsert' of 'require(...)(...)' as it is undefined.
Calling require and then destructuring without first invoking the factory.
fix
Assign the factory result to a variable: const upserters = require('knex-postgres-transactional-upsert')(knex); then use upserters.upsert(...);
Upgrade
Version history
0.1.1latest on npm
Audit
Dependencies
knexrequiredPeer dependency; requires a Knex instance connected to PostgreSQL.
Agent activity
8 hits · last 30 days
node
6
Resources
knex-postgres-transactional-upsert — npm install knex-postgres-transactional-upsert · libregistry