ltree is a Postgres extension that can give you and your application a lot of options around complex hierarchal relationships.
Knex.js is a SQL query builder based on Laravel Query Builder. It supports a handful of different databases, most importantly Postgres. It has been my first choice for database interaction in JavaScript for years.