Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/wmfs/pg-info

Grabs metadata from a PostgreSQL database.
https://github.com/wmfs/pg-info

package postgresql tymly

Last synced: 3 months ago
JSON representation

Grabs metadata from a PostgreSQL database.

Awesome Lists containing this project

README

        

# pg-info
[![Tymly Package](https://img.shields.io/badge/tymly-package-blue.svg)](https://tymly.io/)
[![npm (scoped)](https://img.shields.io/npm/v/@wmfs/pg-info.svg)](https://www.npmjs.com/package/@wmfs/pg-info)
[![CircleCI](https://circleci.com/gh/wmfs/pg-info.svg?style=svg)](https://circleci.com/gh/wmfs/pg-info)
[![codecov](https://codecov.io/gh/wmfs/pg-info/branch/master/graph/badge.svg)](https://codecov.io/gh/wmfs/pg-info)
[![CodeFactor](https://www.codefactor.io/repository/github/wmfs/pg-info/badge)](https://www.codefactor.io/repository/github/wmfs/pg-info)
[![Dependabot badge](https://img.shields.io/badge/Dependabot-active-brightgreen.svg)](https://dependabot.com/)
[![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg)](http://commitizen.github.io/cz-cli/)
[![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)
[![license](https://img.shields.io/github/license/mashape/apistatus.svg)](https://github.com/wmfs/tymly/blob/master/packages/pg-concat/LICENSE)


> Grabs metadata from a PostgreSQL database

## Install
```bash
$ npm install pg-info --save
```

## Usage

```javascript
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:
| Option | Notes |
| ------ | ----- |
| `client` | An already-connected [pg client](https://github.com/brianc/node-postgres/wiki/Client) or [pg pool](https://github.com/brianc/node-postgres/wiki) instance to your database |
| `schemas` | An array of schema names you'd like information about |

## Output

> Taking a look at [the tests](https://github.com/wmfs/pg-info/blob/master/test/tests.js) 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__

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

__Properties__

| property | Type | Notes |
| -------- | ----- | ----- |
| `generated` | `string` | Timestamp of when __pg-info__ interrogated the database |
| `schemas` | `object` | An object where the key refers to a schema name provided via the options, and the value is a `schema` object |

### `schema` object

__Example__

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

__Properties__

| property | Type | Notes |
| -------- | ---- | ------- |
| `comment` | `string` | The database comment added for this schema, if available |
| `schemaExistsInDatabase` | `boolean` | Indicates if this schema is present in the database or not |
| `tables` | `object` | An object where the key refers to a table name within this schema, and the value is a `table` object |
| `views` | `object` | An object where the key refers to a view name within this schema, and the value is a `view` object |

### `table` object

__Example__

```javascript
{
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__

| property | Type | Notes |
| -------- | ---- | ----- |
| `comment` | `string` | The database comment added for this table, if available |
| `pkColumnNames` | `[string]` | An array of column names that define this table's primary key |
| `columns` | `object` | An object where the key refers to a column name within this table, and the value is a `column` object |
| `indexes` | `object` | An object where the key refers to an index name defined for this table, and the value is an `index` object |
| `triggers` | `object` | An object where the key refers to a trigger name within this table, and the value is a `trigger` object |
| `fkConstraints` | `object` | An 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__

```javascript
{
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__

| property | Type | Notes |
| -------- | ---- | ----- |
| `columns` | `object` | An object where the key refers to a column name within this view, and the value is a `column` object |
| `triggers` | `object` | An object where the key refers to a trigger name within this view, and the value is a `trigger` object |
| `sql` | `string` | The underlying SQL query of this view |
| `comment` | `string` | The database comment added for this view, if available |

### `column` object

__Example__

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

__Properties__

| property | Type | Notes |
| -------- | ---- | ----- |
| `array` | `boolean` | Is the column an array? |
| `comment` | `string` | The database comment added for this comment, if available |
| `columnDefault` | `string` | The value used to default the value of this column |
| `isNullable` | `string` | Indicates if null values are allowed in this column (`YES`) or not (`NO`) |
| `dataType` | `string` | The PostgreSQL data type assigned to this column |
| `characterMaximumLength` | `integer` | The maximum length of a string stored in this column |
| `numericScale` | `integer` | For numeric columns, this refers to the number of digits permitted after the decimal point |

### `index` object

__Example__

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

__Properties__

| property | Type | Notes |
| -------- | ---- | ----- |
| `columns` | `[string]` | An array that contains the column names of the table that are covered by this index |
| `unique` | `boolean` | Indicates whether this is a unique index or not |
| `method` | `string` | The index method used, one of `btree`, `hash`, `gist` or `gin`) |

### `trigger` object

__Example__

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

__Properties__

| property | Type | Notes |
| -------- | ---- | ----- |
| `eventManipulation` | `string` | Event that fires the trigger (`INSERT`, `UPDATE`, or `DELETE`) |
| `actionCondition` | `string` | `WHEN` condition of the trigger, null if none (also null if the table is not owned by a currently enabled role) |
| `actionStatement` | `string` | Statement that is executed by the trigger (currently always `EXECUTE PROCEDURE function(...)`) |
| `actionOrientation` | `string` | Identifies whether the trigger fires once for each processed row or once for each statement (`ROW` or `STATEMENT`) |
| `actionTiming` | `string` | Time at which the trigger fires (`BEFORE`, `AFTER`, or `INSTEAD OF`) |

### `fkConstraint` object

__Example__

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

__Properties__

| property | Type | Notes |
| -------- | ---- | ----- |
| `targetTable` | `string` | The '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) |
| `updateAction` | `string` | Identifies the update action, either `NO ACTION`, `RESTRICT`, `CASCADE`, `SET NULL` or `SET DEFAULT` |
| `deleteAction` | `string` | Identifies the delete action, either `NO ACTION`, `RESTRICT`, `CASCADE`, `SET NULL` or `SET DEFAULT` |
| `matchType` | `string` | Identifies 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```

```bash
$ npm test
```

## License
[MIT](https://github.com/wmfs/pg-info/blob/master/LICENSE)