# pg-structure

# Type aliases

# BuiltinRelationNameFunction

Ƭ BuiltinRelationNameFunction: "short" | "descriptive"

Defined in types/index.ts:50

Name of the builtin relation name function.


# CollisionsByTable

Ƭ CollisionsByTable: object

Defined in types/index.ts:76

Type to store relation name collisions by tables.

# Example

{
  'public.contact': {
    m2o: [],
    o2m: [
      {
        carts: [
          '[public.contact]――― cart_contact ――⥷ [public.cart]',
          '[public.contact]――― other_cart_contact ――⥷ [other_schema.cart]'
        ]
      }
    ],
    m2m: []
   }
}

# Type declaration:


# M2MWithout

Ƭ M2MWithout: "any" | "source" | "join" | "target"

Defined in pg-structure/relation/m2m-relation.ts:13

Table type to exclude it's name from generated name, alias or adjectives.

# Example

const sourceAlias = m2m.getSourceAliasWithout({ target }); // Generates source alias and replace target table name from it.

# RelationNameCollision

Ƭ RelationNameCollision: object

Defined in types/index.ts:55

Type to store a relation name collision. Keys are relation names and values are information about relations with that name.

# Type declaration:

  • [ relationName: string]: string[]

# RelationNameFunction

Ƭ RelationNameFunction: function

Defined in types/index.ts:45

Type for functions to generate names for relations. All necessary information such as table names, columns, foreign key, comment data can be accessed via passed relation parameter.

# Example

const config = {
  relationNameFunction: (relation) => inflection.pluralize(relation.targetTable.name),
}

# Type declaration:

▸ (relation: Relation): string

Parameters:

Name Type
relation Relation

# RelationWithout

Ƭ RelationWithout: "any" | "source" | "target"

Defined in pg-structure/base/relation.ts:14

Table type to exclude it's name from generated name, alias or adjectives.

# Example

const sourceAlias = relation.getSourceAliasWithout({ target }); // Generates source alias and replace target table name from it.

# TypeCategory

Ƭ TypeCategory: "A" | "B" | "C" | "D" | "E" | "G" | "I" | "N" | "P" | "R" | "S" | "T" | "U" | "V" | "X"

Defined in types/index.ts:34

PostgreSQL system-defined values of typcategory. See pg_type in PostgreSQL docs.

# Variables

# Const packageJson

packageJson: any = JSON.parse(readFileSync(join(__dirname, "../../package.json"), { encoding: "utf8" }))

Defined in pg-structure/db.ts:16


# readFile

readFile: readFile

Defined in util/helper.ts:15

# Functions

# deserialize

deserialize(serializedData: string): Db

Defined in index.ts:411

Deserializes given data to create Db object.

# Example

import pgStructure, { deserialize } from "pg-structure";
const db = await pgStructure({ database: "db", user: "u", password: "pass" });
const serialized = db.serialize();
const otherDb = deserialize(serialized);

Parameters:

Name Type Description
serializedData string is serialized data of the Db object.

Returns: Db

Db object for given serialized data.


# getRelationNameFunction

getRelationNameFunction(relationNameFunction: RelationNameFunction | BuiltinRelationNameFunction): RelationNameFunction

Defined in util/naming-function/index.ts:11

Parameters:

Name Type
relationNameFunction RelationNameFunction | BuiltinRelationNameFunction

Returns: RelationNameFunction


# pgStructure

pgStructure(pgClientOrConfig: Client | ClientConfig | string, __namedParameters: object): Promise‹Db

Defined in index.ts:357

Creates and returns Db object which represents given database's structure. It is possible to include or exclude some schemas using options. Please note that if included schemas contain references (i.e. foreign key to other schema or type in other schema) to non-included schema, throws exception.

Parameters:

pgClientOrConfig: Client | ClientConfig | string

is connection string or node-postgres client or node-postgres client configuration.

Default value __namedParameters: object= {}

Name Type Default Description
commentDataToken string "pg-structure" is tag name to extract JSON data from from database object's comments. For example by default JSON data between [pg-structure][/pg-structure] is available imn database objects. Data can be retrieved with commentData method.
excludeSchemas undefined | string | string[] - is pattern similar to SQL LIKE (i.e public_%) or list of schemas to exclude.
foreignKeyAliasSeparator string "," is character to separate {@link ForeignKey.sourceAlias source alias} and {@link ForeignKey.targetAlias target alias} in foreign key name. For example: prime_color,product.
foreignKeyAliasTargetFirst boolean false is whether first part of the foreign key aliases contains target alias (i.e company_employees) or source alias (i.e. employee_company).
includeSchemas undefined | string | string[] - is pattern similar to SQL LIKE (i.e public_%) or list of schemas to include.
includeSystemSchemas undefined | false | true - is whether to include PostgreSQL system schemas (i.e. pg_catalog) from database.
name undefined | string - is name of the database. This is inferred if possible from client or connection string.
relationNameFunction function | "short" | "descriptive" "short" Optional function to generate names for relationships. If not provided, default naming functions are used. All necessary information such as table names, columns, foreign key, comment data can be accessed via passed relation parameter. * It is also possible to use one of the builtin naming functions such as short, descriptive.

Returns: Promise‹Db

Db object which represents given database's structure.