Options
All
  • Public
  • Public/Protected
  • All
Menu

pg-structure

Index

Type aliases

ArgumentMode

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

Modes of the PostgreSQL function arguments.

CollisionsByTable

CollisionsByTable: {}

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

ParallelSafety

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

Parallel safety of the PostgreSQL function.

RelationNameCollision

RelationNameCollision: {}

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: { m2m: (relation: M2MRelation) => string; m2o: (relation: M2ORelation) => string; o2m: (relation: O2MRelation) => string }

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

TriggerEnabled

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

In which session_replication_role modes the trigger fires.

TriggerEvent

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

Event that fires the trigger

TriggerOrientation

TriggerOrientation: "row" | "statement"

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

TriggerTiming

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

Time at which the trigger fires

TypeCategory

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

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

Volatility

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

Volatility of the PostgreSQL function.

Functions

default

  • default(client?: Client | ClientConfig | string, options?: Options): Promise<Db>
  • default(options?: Options): Promise<Db>
  • Reverse engineers a PostgreSQL database and creates Db instance which represents given database's structure. There are several options such as to include or exclude schemas, provide custom names to relations. Please refer to Options for detailed explanations.

    IMPORTANT: Please note that if included schemas contain references to a non-included schema, this function throws exception. (e.g. a foreign key to another schema or a type in another schema which is not included)

    Example

    const db = await pgStructure({ database: "db", user: "u", password: "pass" }, { includeSchemas: ["public"] });
    

    Parameters

    • Optional client: Client | ClientConfig | string

      is either a node-postgres client or a configuration object or a connection string to create a node-postgres client.

    • Optional options: Options

      are preferences to modify reverse engineering process.

    Returns Promise<Db>

    Db object which represents given database's structure.

  • Reads configuration details from environment variables to create node-postgres client. Keys are upper case environment variables prefixed with options.envPrefix (default is DB).

    Environment Varibale ClientConfig Key
    DB_DATABASE database
    DB_USER user
    DB_PASSWORD password
    ... ...

    Example 1

    const db = await pgStructure({ includeSchemas: ["public"] });
    

    Example 2

    const db = await pgStructure(); // Read connection details from environmet variables.
    

    Parameters

    • Optional options: Options

      are preferences to modify reverse engineering process.

    Returns Promise<Db>

    Db object which represents given database's structure.

deserialize

  • deserialize(serializedData: string): Db
  • Deserializes given data to create Db object. Please note that custom relation name functions are not serialized. To serialize, provide functions as a module and use them with { relationNameFunctions: "my-module" }.

    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

    • serializedData: string

      is serialized data of the Db object.

    Returns Db

    Db object for given serialized data.

Generated using TypeDoc