# pg-structure

# Type aliases

# BuiltinRelationNameFunctions

Ƭ BuiltinRelationNameFunctions: "short" | "descriptive"

Defined in src/types/index.ts:60

Name of the builtin relation name function.


# CollisionsByTable

Ƭ CollisionsByTable: object

Defined in src/types/index.ts:86

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 src/pg-structure/relation/m2m-relation.ts:17

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 src/types/index.ts:65

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[]

# RelationNameFunctions

Ƭ RelationNameFunctions: object

Defined in src/types/index.ts:51

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 = {
  relationNameFunctions: {
    o2m: (relation) => some_function(relation.targetTable.name),
    m2o: (relation) => some_function(relation.targetTable.name),
    m2m: (relation) => some_function(relation.targetTable.name),
  },
}

# Type declaration:


# RelationWithout

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

Defined in src/pg-structure/base/relation.ts:11

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 src/types/index.ts:36

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 src/pg-structure/db.ts:16


# readFile

readFile: readFile

Defined in src/util/helper.ts:16

# Functions

# deserialize

deserialize(serializedData: string): Db

Defined in src/index.ts:424

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.


# executeSqlFile

executeSqlFile(file: "entity.sql", client: Client, schemas: any[]): Promise‹EntityQueryResult[]›

Defined in src/util/helper.ts:117

Parameters:

Name Type
file "entity.sql"
client Client
schemas any[]

Returns: Promise‹EntityQueryResult[]›

executeSqlFile(file: "column.sql", client: Client, schemas: any[]): Promise‹ColumnQueryResult[]›

Defined in src/util/helper.ts:118

Parameters:

Name Type
file "column.sql"
client Client
schemas any[]

Returns: Promise‹ColumnQueryResult[]›

executeSqlFile(file: "index.sql", client: Client, schemas: any[]): Promise‹IndexQueryResult[]›

Defined in src/util/helper.ts:119

Parameters:

Name Type
file "index.sql"
client Client
schemas any[]

Returns: Promise‹IndexQueryResult[]›

executeSqlFile(file: "constraint.sql", client: Client, schemas: any[]): Promise‹ConstraintQueryResult[]›

Defined in src/util/helper.ts:120

Parameters:

Name Type
file "constraint.sql"
client Client
schemas any[]

Returns: Promise‹ConstraintQueryResult[]›


# getRelationNameFunctions

getRelationNameFunctions(relationNameFunctions: RelationNameFunctions | BuiltinRelationNameFunctions): RelationNameFunctions

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

Parameters:

Name Type
relationNameFunctions RelationNameFunctions | BuiltinRelationNameFunctions

Returns: RelationNameFunctions


# isPgClient

isPgClient(pgClientOrConfig: any): pgClientOrConfig is Client

Defined in src/util/helper.ts:132

Returns whether given argument is a PostgreSQL client or pool.

Parameters:

Name Type
pgClientOrConfig any

Returns: pgClientOrConfig is Client

boolean


# pgStructure

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

Defined in src/index.ts:369

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.
keepConnection boolean false -
name undefined | string - is name of the database. This is inferred if possible from client or connection string.
relationNameFunctions object | "short" | "descriptive" "short" Optional functions 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.