pg-structure
# pg-structure
# Table of contents
# Enumerations
# Classes
- AggregateFunction
- BaseType
- CheckConstraint
- Column
- CompositeType
- Constraint
- Db
- DbObject
- Domain
- Entity
- EnumType
- ExclusionConstraint
- ForeignKey
- Func
- FunctionArgument
- Index
- M2MRelation
- M2ORelation
- MaterializedView
- NormalFunction
- O2MRelation
- PrimaryKey
- Procedure
- PseudoType
- RangeType
- Relation
- Schema
- Sequence
- Table
- Trigger
- Type
- UniqueConstraint
- View
- WindowFunction
# Interfaces
# Type aliases
# ArgumentMode
Ƭ ArgumentMode: in | inout | out | variadic | table
Modes of the PostgreSQL function arguments.
Defined in: types/index.ts:111
# CollisionsByTable
Ƭ CollisionsByTable: object
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:
Defined in: types/index.ts:100
# ParallelSafety
Ƭ ParallelSafety: safe | unsafe | restricted
Parallel safety of the PostgreSQL function.
Defined in: types/index.ts:108
# RelationNameCollision
Ƭ RelationNameCollision: object
Type to store a relation name collision. Keys are relation names and values are information about relations with that name.
# Type declaration:
Defined in: types/index.ts:79
# RelationNameFunctions
Ƭ RelationNameFunctions: object
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:
Name | Type |
---|---|
m2m | (relation : M2MRelation) => string |
m2o | (relation : M2ORelation) => string |
o2m | (relation : O2MRelation) => string |
Defined in: types/index.ts:70
# TriggerEnabled
Ƭ TriggerEnabled: origin | disabled | replica | always
In which session_replication_role modes the trigger fires.
Defined in: types/index.ts:123
# TriggerEvent
Ƭ TriggerEvent: insert | delete | update | truncate
Event that fires the trigger
Defined in: types/index.ts:120
# TriggerOrientation
Ƭ TriggerOrientation: row | statement
whether the trigger fires once for each processed row or once for each statement.
Defined in: types/index.ts:114
# TriggerTiming
Ƭ TriggerTiming: before | after | insteadOf
Time at which the trigger fires
Defined in: types/index.ts:117
# 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 (opens new window) in PostgreSQL docs.
Defined in: types/index.ts:55
# Volatility
Ƭ Volatility: immutable | stable | volatile
Volatility of the PostgreSQL function.
Defined in: types/index.ts:105
# Functions
# default
▸ default(client?
: Client | ClientConfig | string, 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:
Name | Type | Description |
---|---|---|
client? | Client | ClientConfig | string | is either a node-postgres client (opens new window) or a configuration object or a connection string to create a node-postgres client (opens new window). |
options? | Options | are preferences to modify reverse engineering process. |
Returns: Promise<Db>
Db object which represents given database's structure.
Defined in: main.ts:415
▸ default(options?
: Options): Promise<Db>
Reads configuration details from environment variables to create node-postgres client (opens new window).
Keys are upper case environment variables prefixed with options.envPrefix
(default is DB
).
Environment Varibale | ClientConfig (opens new window) 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:
Name | Type | Description |
---|---|---|
options? | Options | are preferences to modify reverse engineering process. |
Returns: Promise<Db>
Db object which represents given database's structure.
Defined in: main.ts:436
# 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:
Name | Type | Description |
---|---|---|
serializedData | string | is serialized data of the Db object. |
Returns: Db
Db object for given serialized data.
Defined in: main.ts:481