{"id":43685,"library":"postgres-tree","title":"Postgres Tree","description":"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.","status":"maintenance","version":"1.1.3","language":"javascript","source_language":"en","source_url":null,"tags":["javascript","postgres","pg","tree","trees","hierarchy","closuretables","graph"],"install":[{"cmd":"npm install postgres-tree","lang":"bash","label":"npm"},{"cmd":"yarn add postgres-tree","lang":"bash","label":"yarn"},{"cmd":"pnpm add postgres-tree","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Peer dependency for PostgreSQL client (Pool). Must be installed separately.","package":"pg","optional":false}],"imports":[{"note":"ESM import works from v1.0.0+. CJS require needs .default.","wrong":"const PostgresTree = require('postgres-tree').default","symbol":"PostgresTree","correct":"import PostgresTree from 'postgres-tree'"},{"note":"CommonJS users must append .default to get the class constructor.","wrong":"const PostgresTree = require('postgres-tree')","symbol":"default import","correct":"const PostgresTree = require('postgres-tree').default"},{"note":"Named export was added in v1.1.0; default export remains.","wrong":"import PostgresTree from 'postgres-tree'","symbol":"PostgresTree","correct":"import { PostgresTree } from 'postgres-tree'"}],"quickstart":{"code":"import PostgresTree from 'postgres-tree';\n\nasync function main() {\n  const tree = await new PostgresTree('mytree').build();\n  await tree.addNode({ id: 1, parent: null, name: 'Root', offset: 5 });\n  await tree.addNode({ id: 2, parent: 1, name: 'Child', offset: 0 });\n  const leaves = await tree.getLeaves();\n  console.log('Leaves:', leaves);\n  await tree.destroy();\n}\nmain().catch(console.error);","lang":"javascript","description":"Creates a tree table, adds a root and child node, retrieves leaf nodes, then destroys the table."},"warnings":[{"fix":"Check if table exists before calling build() if you want to preserve data.","message":"The 'build()' method will silently drop and recreate the table if it already exists.","severity":"breaking","affected_versions":">=0.0.0"},{"fix":"Set required environment variables or wrap the constructor with your own Pool instance.","message":"Environment variables PGUSER, PGHOST, PGPASS, PGDB, PGPORT must be set; constructor ignores explicit connection parameters.","severity":"gotcha","affected_versions":">=0.0.0"},{"fix":"Fork the package or use a different library if custom connection parameters are needed.","message":"The package uses require('pg').Pool internally without customizable configuration; does not support connection strings or SSL.","severity":"gotcha","affected_versions":">=0.0.0"},{"fix":"Use ESM import or named import { PostgresTree }.","message":"Default export via require('postgres-tree').default is fragile with some bundlers.","severity":"deprecated","affected_versions":">=1.0.0"}],"env_vars":null,"search_vec":"'1.1.3':19 '2020':22 'ad':36 'api':27,57 'appear':99 'automat':51 'base':71 'build':29 'builder':56 'cadenc':94 'chainabl':55 'closur':14 'closuret':111 'configur':89 'creation':53 'current':16 'data':34 'develop':98 'differenti':49 'direct':85 'environ':91 'flexibl':61 'focus':76 'graph':112 'hierarch':8 'hierarchi':48,110 'implement':7 'includ':35,50 'insert':38 'interfac':78 'intuit':26 'javascript':105 'last':20 'librari':5 'lightweight':4 'low':96 'mainten':103 'manipul':32 'minim':77 'mode':104 'move':42 'node':37 'offer':74 'oper':63 'orm':70 'orm-bas':69 'pg':87,107 'pool':88 'posit':41 'postgr':1,106 'postgresql':12 'provid':24 'queri':30 'raw':66 'releas':93 'requir':65 'solut':72 'specif':40 'sporad':97 'sql':67 'stabl':17 'structur':10,81 'subtre':43 'support':59 'tabl':15,52 'tie':84 'tree':2,9,33,47,62,80,108,109 'unlik':68 'updat':21 'use':13 'variabl':92 'version':18 'via':90 'view':45 'without':64","created_at":"2026-06-05T17:00:59.257415+00:00","updated_at":"2026-06-05T17:00:59.257415+00:00","problems":[{"fix":"Change to: const PostgresTree = require('postgres-tree').default;","cause":"Using require('postgres-tree') without .default in CJS environment.","error":"TypeError: PostgresTree is not a constructor"},{"fix":"Ensure await tree.build() is called before performing any tree operations.","cause":"Calling tree methods before calling build() to create the table.","error":"error: relation \"mytree\" does not exist"},{"fix":"Run: npm install pg","cause":"The peer dependency pg is not installed.","error":"Cannot find module 'pg'"}],"ecosystem":"npm","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":null,"cli_version":null,"type":"library","homepage":null,"github":null,"docs":null,"changelog":null,"pypi":null,"npm":"postgres-tree","openapi_spec":null,"status_page":null,"smithery":null,"categories":["database"],"base_url":null,"auth_type":null,"provenance":{"verified_status":null,"verified_at":null,"last_verified":"2026-06-05","next_check":"2026-09-03","install_tag":null}}