Registry / database / postgres-tree

postgres-tree

JSON →
library1.1.3jsnpmunverified

A lightweight library for implementing hierarchical tree structures in PostgreSQL using closure tables. Current stable version 1.1.3 (last updated 2020). It provides an intuitive API for building, querying, and manipulating tree data, including adding nodes, inserting in specific positions, moving subtrees, and viewing the tree hierarchy. Differentiators include automatic table creation, a chainable builder API, and support for flexible tree operations without requiring raw SQL. Unlike ORM-based solutions, it offers a focused, minimal interface for tree structures, but it ties directly to pg Pool configuration via environment variables. Release cadence is low (sporadic); development appears to be in maintenance mode.

npm install postgres-tree
INSTALL
IMPORT
SIG · POSTGRES-TREE
P
postgres-tree
databasejavascriptv1.1.3
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.

PostgresTree
✓ import PostgresTree from 'postgres-tree'
✗ const PostgresTree = require('postgres-tree').default
ESM import works from v1.0.0+. CJS require needs .default.
default import
✓ const PostgresTree = require('postgres-tree').default
✗ const PostgresTree = require('postgres-tree')
CommonJS users must append .default to get the class constructor.
PostgresTree
✓ import { PostgresTree } from 'postgres-tree'
✗ import PostgresTree from 'postgres-tree'
Named export was added in v1.1.0; default export remains.

Creates a tree table, adds a root and child node, retrieves leaf nodes, then destroys the table.

import PostgresTree from 'postgres-tree'; async function main() { const tree = await new PostgresTree('mytree').build(); await tree.addNode({ id: 1, parent: null, name: 'Root', offset: 5 }); await tree.addNode({ id: 2, parent: 1, name: 'Child', offset: 0 }); const leaves = await tree.getLeaves(); console.log('Leaves:', leaves); await tree.destroy(); } main().catch(console.error);
Debug
Known issues
breakingThe 'build()' method will silently drop and recreate the table if it already exists.
fix
Check if table exists before calling build() if you want to preserve data.
affects: >=0.0.0
gotchaEnvironment variables PGUSER, PGHOST, PGPASS, PGDB, PGPORT must be set; constructor ignores explicit connection parameters.
fix
Set required environment variables or wrap the constructor with your own Pool instance.
affects: >=0.0.0
gotchaThe package uses require('pg').Pool internally without customizable configuration; does not support connection strings or SSL.
fix
Fork the package or use a different library if custom connection parameters are needed.
affects: >=0.0.0
deprecatedDefault export via require('postgres-tree').default is fragile with some bundlers.
fix
Use ESM import or named import { PostgresTree }.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: PostgresTree is not a constructor
Using require('postgres-tree') without .default in CJS environment.
fix
Change to: const PostgresTree = require('postgres-tree').default;
error: relation "mytree" does not exist
Calling tree methods before calling build() to create the table.
fix
Ensure await tree.build() is called before performing any tree operations.
Cannot find module 'pg'
The peer dependency pg is not installed.
fix
Run: npm install pg
Upgrade
Version history
1.1.3latest on npm
Audit
Dependencies
pgrequiredPeer dependency for PostgreSQL client (Pool). Must be installed separately.
Agent activity
7 hits · last 30 days
node
6
Resources
postgres-tree — npm install postgres-tree · libregistry