# pg-structure

# Type aliases

# ArgumentMode

Ƭ ArgumentMode: "in" | "inout" | "out" | "variadic" | "table"

Defined in src/types/index.ts:97

Modes of the PostgreSQL function arguments.


# 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.

# ParallelSafety

Ƭ ParallelSafety: "safe" | "unsafe" | "restricted"

Defined in src/types/index.ts:94

Parallel safety of the PostgreSQL function.


# RelationKindLetter

Ƭ RelationKindLetter: "r" | "i" | "s" | "t" | "v" | "m" | "c" | "f" | "p" | "I"

Defined in src/types/query-result.ts:13

ignore


# 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.

# TriggerEnabled

Ƭ TriggerEnabled: "origin" | "disabled" | "replica" | "always"

Defined in src/types/index.ts:109

In which session_replication_role modes the trigger fires.


# TriggerEvent

Ƭ TriggerEvent: "insert" | "delete" | "update" | "truncate"

Defined in src/types/index.ts:106

Event that fires the trigger


# TriggerOrientation

Ƭ TriggerOrientation: "row" | "statement"

Defined in src/types/index.ts:100

whether the trigger fires once for each processed row or once for each statement.


# TriggerTiming

Ƭ TriggerTiming: "before" | "after" | "insteadOf"

Defined in src/types/index.ts:103

Time at which the trigger fires


# 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.


# Volatility

Ƭ Volatility: "immutable" | "stable" | "volatile"

Defined in src/types/index.ts:91

Volatility of the PostgreSQL function.

# Variables

# Const packageJson

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

Defined in src/pg-structure/db.ts:17


# readFile

readFile: readFile

Defined in src/util/helper.ts:16


# readdir

readdir: readdir

Defined in src/util/helper.ts:16

# Functions

# deserialize

deserialize(serializedData: string): Db

Defined in src/index.ts:522

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.


# 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:170

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:472

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.

# Object literals

# Const builtinTypeAliases

#builtinTypeAliases: object

Defined in src/index.ts:221

bit: object

Defined in src/index.ts:236

  • hasLength: true = true

bool: object

Defined in src/index.ts:235

  • name: string = "boolean"

char: object

Defined in src/index.ts:229

  • hasLength: true = true

  • name: string = "character"

float4: object

Defined in src/index.ts:226

  • name: string = "real"

float8: object

Defined in src/index.ts:227

  • name: string = "double precision"

int2: object

Defined in src/index.ts:222

  • name: string = "smallint"

int4: object

Defined in src/index.ts:223

  • name: string = "integer"

  • shortName: string = "int"

int8: object

Defined in src/index.ts:224

  • name: string = "bigint"

interval: object

Defined in src/index.ts:234

  • hasPrecision: true = true

numeric: object

Defined in src/index.ts:225

  • hasPrecision: true = true

  • hasScale: true = true

  • internalName: string = "decimal"

time: object

Defined in src/index.ts:232

  • hasPrecision: true = true

  • name: string = "time without time zone"

timestamp: object

Defined in src/index.ts:230

  • hasPrecision: true = true

  • name: string = "timestamp without time zone"

timestamptz: object

Defined in src/index.ts:231

  • hasPrecision: true = true

  • name: string = "timestamp with time zone"

timetz: object

Defined in src/index.ts:233

  • hasPrecision: true = true

  • name: string = "time with time zone"

varbit: object

Defined in src/index.ts:237

  • hasLength: true = true

  • name: string = "bit varying"

varchar: object

Defined in src/index.ts:228

  • hasLength: true = true

  • name: string = "character varying"