1.20.1 • Published 4 months ago

@wmfs/pg-info v1.20.1

Weekly downloads
272
License
MIT
Repository
github
Last release
4 months ago

pg-info

Tymly Package npm (scoped) CircleCI codecov CodeFactor Dependabot badge Commitizen friendly JavaScript Style Guide license

Grabs metadata from a PostgreSQL database

Install

$ npm install pg-info --save

Usage

const pg = require('pg')
const pgInfo = require('@wmfs/pg-info')

// Make a new Postgres client
const client = new pg.Client('postgres://postgres:postgres@localhost:5432/my_test_db')
client.connect()

const info = await pgInfo({
    client: client,
    schemas: [
      'pginfo_people_test',
      'pginfo_planets_test',
      'pginfo_not_exists'
    ]    
})

API

pgInfo(options, callback)

Options:

OptionNotes
clientAn already-connected pg client or pg pool instance to your database
schemasAn array of schema names you'd like information about

Output

Taking a look at the tests is a good place to get a feel for what pg-info will produce.

In summary, the structure of the output is:

  • info object

    • schema object(s)
      • table object(s)
        • column object(s)
        • index object(s)
        • trigger object(s)
        • fkConstraint object(s)
      • view object(s)
        • column object(s)
        • trigger object(s)

info object

Example

{
  generated: '2017-05-21T21:53:42.594Z',
  schemas: {} // Keys are schema names
}

Properties

propertyTypeNotes
generatedstringTimestamp of when pg-info interrogated the database
schemasobjectAn object where the key refers to a schema name provided via the options, and the value is a schema object

schema object

Example

{
  schemaExistsInDatabase: true,
  comment: 'Simple schema created to support testing of the pg-info package!',
  tables: {...} // Keys are table names
}

Properties

propertyTypeNotes
commentstringThe database comment added for this schema, if available
schemaExistsInDatabasebooleanIndicates if this schema is present in the database or not
tablesobjectAn object where the key refers to a table name within this schema, and the value is a table object
viewsobjectAn object where the key refers to a view name within this schema, and the value is a view object

table object

Example

{
  comment: 'For storing a list of planets',
  pkColumnNames: [
    'planet_name'
  ],
  columns: {...}, // Keys are column names
  triggers: {...}, // Keys are trigger names
  indexes: {...}, // Keys are index names
  fkConstraints: {...} // Keys are foreign key constraint names
}

Properties

propertyTypeNotes
commentstringThe database comment added for this table, if available
pkColumnNames[string]An array of column names that define this table's primary key
columnsobjectAn object where the key refers to a column name within this table, and the value is a column object
indexesobjectAn object where the key refers to an index name defined for this table, and the value is an index object
triggersobjectAn object where the key refers to a trigger name within this table, and the value is a trigger object
fkConstraintsobjectAn object where the key refers to a foreign-key constraint name defined for this table, and the value is a fkConstraint object

view object

Example

{
  columns: {...}, // Keys are column names
  triggers: {...}, // Keys are trigger names
  comment: 'View of just those planets in our solar system',
  sql: 'SELECT * FROM my_schema.planets WHERE solar_system = true;'
}

Properties

propertyTypeNotes
columnsobjectAn object where the key refers to a column name within this view, and the value is a column object
triggersobjectAn object where the key refers to a trigger name within this view, and the value is a trigger object
sqlstringThe underlying SQL query of this view
commentstringThe database comment added for this view, if available

column object

Example

{
  array: false,
  columnDefault: null,
  isNullable: 'YES',
  dataType: 'integer',
  characterMaximumLength: null,
  numericScale: 0,
  comment: 'Age in years'
}

Properties

propertyTypeNotes
arraybooleanIs the column an array?
commentstringThe database comment added for this comment, if available
columnDefaultstringThe value used to default the value of this column
isNullablestringIndicates if null values are allowed in this column (YES) or not (NO)
dataTypestringThe PostgreSQL data type assigned to this column
characterMaximumLengthintegerThe maximum length of a string stored in this column
numericScaleintegerFor numeric columns, this refers to the number of digits permitted after the decimal point

index object

Example

{
  columns: [
    [
      'moons_id'
    ]
  ],
  unique: false,
  method: 'btree'
}

Properties

propertyTypeNotes
columns[string]An array that contains the column names of the table that are covered by this index
uniquebooleanIndicates whether this is a unique index or not
methodstringThe index method used, one of btree, hash, gist or gin)

trigger object

Example

{
  triggers: {
    someInsertTriggerName: {
      eventManipulation: 'INSERT',      
      actionCondition: null,
      actionStatement: 'EXECUTE PROCEDURE append_inserted_craters_row()',
      actionOrientation: 'STATEMENT',
      actionTiming: 'BEFORE'
    }
  }
}

Properties

propertyTypeNotes
eventManipulationstringEvent that fires the trigger (INSERT, UPDATE, or DELETE)
actionConditionstringWHEN condition of the trigger, null if none (also null if the table is not owned by a currently enabled role)
actionStatementstringStatement that is executed by the trigger (currently always EXECUTE PROCEDURE function(...))
actionOrientationstringIdentifies whether the trigger fires once for each processed row or once for each statement (ROW or STATEMENT)
actionTimingstringTime at which the trigger fires (BEFORE, AFTER, or INSTEAD OF)

fkConstraint object

Example

{
  targetTable: 'pginfo_planets_test.moons',
  sourceColumns: [
    'moons_id'
  ],
  targetColumns: [
    'id'
  ],
  updateAction: 'NO_ACTION',
  deleteAction: 'CASCADE',
  matchType: 'SIMPLE'
}

Properties

propertyTypeNotes
targetTablestringThe 'child' table that is related to this table - of the form [schemaName].[tableName]
sourceColumns[string]An array of foreign-key column names on this table
targetColumns[string]And this is an array of column names found the target table (often relating to its primary key)
updateActionstringIdentifies the update action, either NO ACTION, RESTRICT, CASCADE, SET NULL or SET DEFAULT
deleteActionstringIdentifies the delete action, either NO ACTION, RESTRICT, CASCADE, SET NULL or SET DEFAULT
matchTypestringIdentifies the match type, either FULL, PARTIAL, or SIMPLE
  • Note the order of the columns provided in sourceColumns and targetColumns arrays correlate with each other.

Testing

Before running these tests, you'll need a test PostgreSQL database available and set a PG_CONNECTION_STRING environment variable to point to it, for example:

PG_CONNECTION_STRING=postgres://postgres:postgres@localhost:5432/my_test_db

$ npm test

License

MIT

1.20.1

4 months ago

1.20.0

11 months ago

1.19.0

3 years ago

1.18.0

3 years ago

1.17.0

4 years ago

1.16.0

4 years ago

1.15.0

4 years ago

1.14.0

4 years ago

1.13.0

5 years ago

1.12.0

5 years ago

1.11.0

5 years ago

1.10.0

5 years ago

1.9.0

6 years ago

1.8.0

6 years ago

1.7.0

6 years ago

1.6.0

6 years ago

1.5.0

6 years ago

1.4.0

6 years ago

1.3.0

6 years ago

1.2.0

6 years ago

1.1.0

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago