{"library":"pg-structure","title":"PostgreSQL Database Structure Reverse Engineer","description":"pg-structure is a TypeScript-first library designed to reverse engineer a PostgreSQL database schema into a detailed JavaScript object structure. It provides an API to programmatically access and navigate details about databases, schemas, tables, columns, foreign keys, relations, indexes, and custom types. The current stable version is 7.15.3. Releases are somewhat infrequent, with several bug fix releases in 2024 and feature additions in 2023 and prior years, indicating active maintenance. Its key differentiator is offering a comprehensive, introspected object model of the entire PostgreSQL schema, which is useful for ORM generators, database documentation tools, schema analysis scripts, or custom code generation, rather than just basic table listings. It handles complex PostgreSQL features like custom types and generated columns.","language":"javascript","status":"active","last_verified":"Wed Apr 22","install":{"commands":["npm install pg-structure"],"cli":null},"imports":["import pgStructure from 'pg-structure';","import type { Db } from 'pg-structure';","import type { Table } from 'pg-structure';"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import pgStructure from \"pg-structure\";\nimport type { Db, Table } from \"pg-structure\";\n\nasync function demo() {\n  // It's highly recommended to use environment variables for sensitive credentials.\n  // Example uses process.env for security best practices.\n  const connectionConfig = {\n    host: process.env.PG_HOST ?? \"localhost\",\n    database: process.env.PG_DATABASE ?? \"your_db_name\",\n    user: process.env.PG_USER ?? \"your_username\",\n    password: process.env.PG_PASSWORD ?? \"your_password\",\n    port: parseInt(process.env.PG_PORT ?? \"5432\", 10),\n  };\n\n  try {\n    // Establish a connection and reverse engineer the database structure\n    const db: Db = await pgStructure(connectionConfig, { includeSchemas: [\"public\"] });\n\n    // Access a specific table by its name\n    const contactTable: Table | undefined = db.get(\"contact\");\n\n    if (contactTable) {\n      console.log(`Successfully introspected table: ${contactTable.name}`);\n\n      // Get column names for the 'contact' table\n      const columnNames = contactTable.columns.map((c) => c.name);\n      console.log(\"Column Names for 'contact' table:\", columnNames);\n\n      // Get the type name of a specific column (e.g., 'options' column)\n      const optionsColumn = contactTable.columns.get(\"options\");\n      if (optionsColumn) {\n        console.log(`Type name of 'options' column: ${optionsColumn.type.name}`);\n      }\n\n      // Get columns involved in a specific index (e.g., 'ix_mail' index)\n      const ixMailIndex = contactTable.indexes.get(\"ix_mail\");\n      if (ixMailIndex) {\n        const indexColumnNames = ixMailIndex.columns.map(c => c.name);\n        console.log(\"Columns in 'ix_mail' index:\", indexColumnNames);\n      }\n\n      // Get tables related to 'contact' via hasMany relationship\n      const relatedTables = contactTable.hasManyTables;\n      console.log(\"Tables related to 'contact' via hasMany:\", relatedTables.map(t => t.name));\n    } else {\n      console.log(\"Table 'contact' not found or not included in introspection.\");\n    }\n  } catch (error) {\n    console.error(\"Failed to connect or introspect database:\", error);\n  }\n}\n\ndemo();","lang":"typescript","description":"This example connects to a PostgreSQL database, reverse engineers its structure, and demonstrates how to access specific tables, columns, their types, indexes, and relationships using the programmatic API.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}