pg-structure / Table

# Class: Table

Class which represent a table. Provides attributes and methods for details of the table. Tables may have relationships with other tables.

Below is a database schema which is used in code examples. Database Schema

# Hierarchy

# Properties

# columns

Readonly columns: default<Column, name, attributeNumber, true>

All columns of the entity as an {@link IndexableArray indexable array} ordered by same order they are defined in database entity.

# Example

const isAvailable  = table.columns.has('id');
const columnNames  = table.columns.map(column => column.name);
const column       = table.columns.get('user_id');
const name         = column.name;

table.columns.forEach(column => console.log(column.name));

name Entity#columns

Inherited from: Entity.columns

Defined in: pg-structure/base/entity.ts:53


# comment

Optional Readonly comment: undefined | string

Comment of the database object defined in database including {@link DbObject#commentData comment data}.

Inherited from: Entity.comment

Defined in: pg-structure/base/db-object.ts:75


# constraints

Readonly constraints: default<Constraint, name, never, true>

All constraints in the table as an [[IndexableArray]] ordered by name.

Defined in: pg-structure/entity/table.ts:35


# foreignKeysToThis

Readonly foreignKeysToThis: default<ForeignKey, name, never, true>

All foreign keys which are referring to this table as an [[IndexableArray]].

see Table.o2mRelations, Table.m2oRelations, Table.m2mRelations to get more details about relations.

Defined in: pg-structure/entity/table.ts:42


# indexes

Readonly indexes: default<Index, name, never, true>

All indexes in the table as an [[IndexableArray]], ordered by name.

Defined in: pg-structure/entity/table.ts:47


# name

Readonly name: string

Name of the database object.

Inherited from: Entity.name

Defined in: pg-structure/base/db-object.ts:42


# oid

Readonly oid: number

Object identifier for the Entity

Inherited from: Entity.oid

Defined in: pg-structure/base/entity.ts:25


# schema

Readonly schema: Schema

Schema of the object.

Inherited from: Entity.schema

Defined in: pg-structure/base/entity.ts:38


# triggers

Readonly triggers: default<Trigger, name, never, true>

All triggers in the table as an [[IndexableArray]] ordered by name.

Defined in: pg-structure/entity/table.ts:30

# Accessors

# belongsToManyTables

• get belongsToManyTables(): default<Table, name, never, true>

Tables which this table has many to many relationship, ordered by table name Please note that same table will appear more than once if there are more than one relationship between tables or same named tables from different schemas (i.e. public.account may have relations with public.contact and vip.contact).

# Example

// Cart (id) has many products (id) through line_item join table.
cartTable.belongsToManyTables.forEach(table => console.log(table.name));
cartTable.belongsToManyTables.getAll("contact"); // All related tables named contact.

see Example schema

Returns: default<Table, name, never, true>

Defined in: pg-structure/entity/table.ts:167


# belongsToManyTablesPk

• get belongsToManyTablesPk(): default<Table, name, never, true>

Tables which this table has many to many relationship joined by primary keys only in join table, ordered by table name Please note that same table will appear more than once if there are more than one relationship between tables or same named tables from different schemas (i.e. public.account may have relations with public.contact and vip.contact).

# Example

// Cart (id) has many products (id) through line_item join table.
cartTable.belongsToManyTablesPk.forEach(table => console.log(table.name));
cartTable.belongsToManyTablesPk.getAll("contact"); // All related tables named contact.

see Example schema, {@link IndexableArray}

Returns: default<Table, name, never, true>

Defined in: pg-structure/entity/table.ts:184


# belongsToTables

• get belongsToTables(): default<Table, name, never, true>

Tables, which this table has belongs to relationship (a.k.a. many to one) which is reverse direction of one to many relationship (a.k.a one to many), ordered by table name. Please note that same table will appear more than once if there are more than one relationship between tables or same named tables from different schemas (i.e. public.account may have relations with public.contact and vip.contact).

# Example

productTable.belongsToTables.forEach(table => console.log(table.name));
productTable.belongsToTables.getAll("contact"); // All related tables named contact.

see Example schema, {@link IndexableArray}

Returns: default<Table, name, never, true>

Defined in: pg-structure/entity/table.ts:151


# checkConstraints

• get checkConstraints(): default<CheckConstraint, name, never, true>

All check constraints in the table as an [[IndexableArray]] ordered by name.

Returns: default<CheckConstraint, name, never, true>

Defined in: pg-structure/entity/table.ts:104


# commentData

• get commentData(): undefined | null | string | number | boolean | JSONObject | JSONArray

Data which is extracted from database object's comment. Data is extracted from text between special case-insensitive tag (default: [pg-structure][/pg-structure]) and converted to JavaScript object using JSON5 (opens new window). Token name can be specified by using commentDataToken arguments. For details of JSON5 (opens new window), see it's web site: https://json5.org (opens new window).

# Example

// "Account details. [pg-structure]{ extraData: 2 }[/pg-structure] Also used for logging."
table.comment;               // "Account details. [pg-structure]{ extraData: 2 }[/pg-structure] Also used for logging."
table.commentWithoutData;    // "Account details.  Also used for logging."
table.commentData;           // { extraData: 2 }
table.commentData.extraData; // 2

Returns: undefined | null | string | number | boolean | JSONObject | JSONArray

Defined in: pg-structure/base/db-object.ts:102


# commentWithoutData

• get commentWithoutData(): undefined | string

Description or comment of the database object defined in database. If comment contains {@link DbObject#commentData comment data}, it is removed.

# Example

// "Account details. [pg-structure]{ extraData: 2 }[/pg-structure] Also used for logging."
table.commentWithoutData;    // "Account details.  Also used for logging."

Returns: undefined | string

Defined in: pg-structure/base/db-object.ts:85


# db

• get db(): Db

Database of the database object.

Returns: Db

Defined in: pg-structure/base/db-object.ts:68


# exclusionConstraints

• get exclusionConstraints(): default<ExclusionConstraint, name, never, true>

All exclusion constraints in the table as an [[IndexableArray]] ordered by name.

Returns: default<ExclusionConstraint, name, never, true>

Defined in: pg-structure/entity/table.ts:111


# foreignKeys

• get foreignKeys(): default<ForeignKey, name, never, true>

All foreign keys in the table as an [[IndexableArray]] ordered by name.

see o2mRelations, m2oRelations, m2mRelations to get more details about relations.

Returns: default<ForeignKey, name, never, true>

Defined in: pg-structure/entity/table.ts:90


# fullCatalogName

• get fullCatalogName(): string

Full name of the database object including database name.

Returns: string

Defined in: pg-structure/base/db-object.ts:35


# fullName

• get fullName(): string

Full name of the object with '.' notation including Schema name.

# Example

const fullName = entity.fullName; // public.member

Returns: string

Defined in: pg-structure/base/entity.ts:33


# hasManyTables

• get hasManyTables(): default<Table, name, never, true>

Tables, which this table has one to many relationship, ordered by table name. Please note that same table will appear more than once if there are more than one relationship between tables or same named tables from different schemas (i.e. public.account may have relations with public.contact and vip.contact).

# Example

vendorTable.hasManyTables.forEach(table => console.log(table.name));
vendorTable.hasManyTables.getAll("contact"); // All related tables named contact.

see Example schema, {@link IndexableArray}

Returns: default<Table, name, never, true>

Defined in: pg-structure/entity/table.ts:136


# m2mRelations

• get m2mRelations(): default<M2MRelation, name, never, true>

Array of many to many relationships of the table. Many to many relationships resembles has many through and belongs to many relations in ORMs. It has some useful methods and information for generating ORM classes.

Returns: default<M2MRelation, name, never, true>

Defined in: pg-structure/entity/table.ts:194


# m2mRelationsPk

• get m2mRelationsPk(): default<M2MRelation, name, never, true>

Array of many to many relationships of the table. Different from m2mRelations, this only includes relations joined by Primary Foreign Keys in join table. Primary Foreign Key means foreign key of join table which are also primary key of join table at the same time. M2MRelation resembles has many through and belongs to many relations in ORMs. It has some useful methods and information for generating ORM classes.

Returns: default<M2MRelation, name, never, true>

Defined in: pg-structure/entity/table.ts:206


# m2oRelations

• get m2oRelations(): default<M2ORelation, name, never, true>

Array of many to one relationships of the table. M2ORelation resembles belongs to relations in ORMs. It has some useful methods and information for generating ORM classes.

Returns: default<M2ORelation, name, never, true>

Defined in: pg-structure/entity/table.ts:224


# nameCaseType

• get nameCaseType(): CaseType

Letter casing (i.e snakeCase or camelCase) of the database object name.

# Example

const name = entity.name;                        // ProductDetail
const caseType = entity.nameCaseType;            // camelCase

const otherEntity = otherEntity.name;            // member_protocol
const otherCaseType = otherEntity.nameCaseType;  // snakeCase

Returns: CaseType

Defined in: pg-structure/base/db-object.ts:54


# o2mRelations

• get o2mRelations(): default<O2MRelation, name, never, true>

Array of one to many relationships of the table. O2MRelation resembles has many relations in ORMs. It has some useful methods and information for generating ORM classes.

Returns: default<O2MRelation, name, never, true>

Defined in: pg-structure/entity/table.ts:215


# primaryKey

• get primaryKey(): undefined | PrimaryKey

Primary key of this table.

# Example

table.primaryKey.columns.forEach(column => console.log(column.name));

see {@link Table.primaryKeyColumns primaryKeyColumns} to get primary key columns directly.

Returns: undefined | PrimaryKey

Defined in: pg-structure/entity/table.ts:122


# relations

• get relations(): default<M2MRelation | O2MRelation | M2ORelation, name, never, true>

List of all relationships of the table. They are sort by type (O2MRelation, M2ORelation, M2MRelation).

Returns: default<M2MRelation | O2MRelation | M2ORelation, name, never, true>

Defined in: pg-structure/entity/table.ts:232


# separator

• get separator(): string

Separator used in database object name. Empty string for came case and underscore for (_) snake case.

Returns: string

Defined in: pg-structure/base/db-object.ts:61


# uniqueConstraints

• get uniqueConstraints(): default<UniqueConstraint, name, never, true>

All unique constraints in the table as an [[IndexableArray]] ordered by name.

Returns: default<UniqueConstraint, name, never, true>

Defined in: pg-structure/entity/table.ts:97

# Methods

# get

get(column: string): Column

Returns column with given name from table.

# Example

const column = entity.get('contact'),  // Returns contact column from entity.

# Parameters:

Name Type
column string

Returns: Column

requested columns.

Overrides: Entity

Defined in: pg-structure/entity/table.ts:280


# getForeignKeysFrom

getForeignKeysFrom(from: string | Table): default<ForeignKey, name, never, true>

Returns foreign keys from given table to this table.

# Parameters:

Name Type Description
from string | Table is table to get foreign keys targeting this table. It could be name, full name or table object.

Returns: default<ForeignKey, name, never, true>

foreign keys from given table to this table.

Defined in: pg-structure/entity/table.ts:66


# getForeignKeysTo

getForeignKeysTo(target: string | Table): default<ForeignKey, name, never, true>

Returns foreign keys from this table to target table.

# Parameters:

Name Type Description
target string | Table is target table to get foreign keys for. It could be name, full name or table object.

Returns: default<ForeignKey, name, never, true>

foreign keys from this table to target table.

Defined in: pg-structure/entity/table.ts:55


# getJoinTablesTo

getJoinTablesTo(target: string | Table): default<Table, name, never, true>

Returns join tables between this table and target table.

# Parameters:

Name Type Description
target string | Table is target table to get join tables for. It could be name, full name or table object.

Returns: default<Table, name, never, true>

Defined in: pg-structure/entity/table.ts:76