pg-structure / M2MRelation

# Class: M2MRelation

Class which represent a many to many relationship which resembles belongsToMany or hasManyThrough relations in ORMs (Object Relational Mappers). Provides attributes and methods for details of the relationship.

Actually there isn't such a thing called many to many relationship or through constraint in the database engine. They are concepts to describe records which may be related more than one record on both sides. For example an invoice may contain more than one product and a product may related to more than one invoice. Those relationships are solved using a join table.

Since those relations are not present in database engine, they are extracted by estimation/interpretation. Many non-join tables in a database could have more than one foreign keys, and they may not meant to be join tables, but they still appear to have through relationships.

Below is a database schema as an example: Database Schema

Some definitions used in descriptions for M2MRelation.

  • ** Source Table: ** Table which this relationship belongs to.
  • ** Join Table: ** Table that contains common fields from two or more other tables.
  • ** Target Table: ** Table that is related to base table through a join table.

    Product table has 3 foreign keys. Product table is not meant to be a many to many join table. However product could have been join table for size & vendor, color & vendor and size & color. As a result size, color and vendor tables would have many to many relationships.

# Example

// Example tables have single primary key and and examples first relation. So zero index ([0]) is used. Use all array elements if necessary.
// product ----< line_item >---- cart
// (source)        (join)       (target)

const relation             = product.m2mRelations[0];              // RELATION:    product ---< line_item >--- cart
const foreignKey           = relation.foreignKey;                  // FOREIGNKEY:           ^-- product_has_carts
const targetForeignKey     = relation.targetForeignKey;            // FOREIGNKEY:       cart_has_products --^
const sourceTable          = relation.sourceTable;                 // TABLE:       product
const targetTable          = relation.targetTable;                 // TABLE:       cart
const sourceJoinFKColumn   = relation.foreignKey.columns[0];       // COLUMN:      product_id  (from line_item table)
const targetJoinFKColumn   = relation.targetForeignKey.columns[0]; // COLUMN:      cart_id     (from line_item table)
const sourcePKColumn       = relation.sourceTable.primaryKeys[0];  // COLUMN:      id          (from product table)
const targetPKColumn       = relation.targetTable.primaryKeys[0];  // COLUMN:      id          (from cart table)

# Hierarchy

# Properties

# foreignKey

Readonly foreignKey: ForeignKey

Foreign key between source table and join table.

# Example

const relation             = product.M2MRelationRelations[0];        // RELATION:    product ---< line_item >--- cart
const foreignKey           = relation.sourceForeignKey;              // CONSTRAINT:           ^-- product_has_carts
const sourceJoinFKColumn   = relation.sourceForeignKey.columns[0];   // COLUMN:      product_id (from line_item table)

Defined in: pg-structure/relation/m2m-relation.ts:157


# targetForeignKey

Readonly targetForeignKey: ForeignKey

Foreign key between join table and target table.

# Example

const relation             = product.M2MRelationRelations[0];      // RELATION:    product ---< line_item >--- cart
const targetForeignKey     = relation.targetForeignKey;            // CONSTRAINT:       cart_has_products --^
const targetJoinFKColumn   = relation.targetForeignKey.columns[0]; // COLUMN:      cart_id (from line_item table)

Defined in: pg-structure/relation/m2m-relation.ts:167


# toMany

Readonly toMany: boolean= true

Whether the relation targets to many. Since, many to many relations targets many, this is true.

Defined in: pg-structure/relation/m2m-relation.ts:82


# type

Readonly type: M2M

Type of the relation, which is m2m for M2MRelation. For TypeScript it is enum of RelationType.M2M.

Defined in: pg-structure/relation/m2m-relation.ts:77

# Accessors

# info

• get info(): string

Informational text representation of the relation.

# Example

const info = relation.info; // [public.product]――― line_item_product ――⥷ [public.line_item] ⭃―― line_item_cart ―――[public.cart]

Returns: string

Defined in: pg-structure/relation/m2m-relation.ts:111


# joinAdjective

• get joinAdjective(): undefined | string

Join table adjective

Returns: undefined | string

Defined in: pg-structure/relation/m2m-relation.ts:280


# joinAlias

• get joinAlias(): string

Join table alias

Returns: string

Defined in: pg-structure/relation/m2m-relation.ts:265


# joinName

• get joinName(): string

Join table name.

Returns: string

Defined in: pg-structure/relation/m2m-relation.ts:250


# joinTable

• get joinTable(): Table

Join Table of this relationship. This table contains foreign key columns referring both sourceTable and targetTable.

# Example

const relation  = product.M2MRelationRelations[0]; // RELATION:    product ---< line_item >--- cart
const joinTable = relation.joinTable;              // TABLE:       line_item

Returns: Table

Defined in: pg-structure/relation/m2m-relation.ts:134


# name

• get name(): string

Suggested name for relation.

see {@link ../relation-names.md Relation Names}

Returns: string

Defined in: pg-structure/relation/m2m-relation.ts:90


# sourceAdjective

• get sourceAdjective(): undefined | string

Source table adjective

Returns: undefined | string

Defined in: pg-structure/relation/m2m-relation.ts:275


# sourceAlias

• get sourceAlias(): string

Source table alias

Returns: string

Defined in: pg-structure/relation/m2m-relation.ts:260


# sourceName

• get sourceName(): string

Source table name

Returns: string

Defined in: pg-structure/relation/m2m-relation.ts:245


# sourceTable

• get sourceTable(): Table

Table which this relation belongs to.

# Example

const relation = product.M2MRelationRelations[0];  // RELATION:    product ---< line_item >--- cart
const source   = relation.sourceTable;             // TABLE:       product

Returns: Table

Defined in: pg-structure/relation/m2m-relation.ts:122


# targetAdjective

• get targetAdjective(): undefined | string

Target table adjective

Returns: undefined | string

Defined in: pg-structure/relation/m2m-relation.ts:285


# targetAlias

• get targetAlias(): string

Target table alias

Returns: string

Defined in: pg-structure/relation/m2m-relation.ts:270


# targetName

• get targetName(): string

Target table name

Returns: string

Defined in: pg-structure/relation/m2m-relation.ts:255


# targetTable

• get targetTable(): Table

Table which this relation is referring to (Through a join table).

# Example

const relation = product.M2MRelationRelations[0];  // RELATION:    product ---< line_item >--- cart
const target   = relation.targetTable;             // TABLE:       cart

Returns: Table

Defined in: pg-structure/relation/m2m-relation.ts:145

# Methods

# getJoinAliasWithout

getJoinAliasWithout(without: any | source | target | join | M2MWithout[]): string

Returns join table alias after replacing given tables' names from it.

# Parameters:

Name Type Description
without any | source | target | join | M2MWithout[] is type or types of tables to exclude names of.

Returns: string

join table alias after given tables' names replaced.

Defined in: pg-structure/relation/m2m-relation.ts:230


# getJoinNameWithout

getJoinNameWithout(without: any | source | target | join | M2MWithout[]): string

Returns join table name after replacing given tables' names from it.

# Parameters:

Name Type Description
without any | source | target | join | M2MWithout[] is type or types of tables to exclude names of.

Returns: string

join table name after given tables' names replaced.

Defined in: pg-structure/relation/m2m-relation.ts:200


# getName

getName(relationNameFunctions: string | RelationNameFunctions): string

Returns name for the relation using given naming function.

# Parameters:

Name Type Description
relationNameFunctions string | RelationNameFunctions are custom functions or name of the module that exports relation name functions to generate names with. pg-structure provides some builtin modules (short, optimal and descriptive), but you can use your own.

Returns: string

name for the relation using naming function.

Defined in: pg-structure/relation/m2m-relation.ts:101


# getSourceAliasWithout

getSourceAliasWithout(without: any | source | target | join | M2MWithout[]): string

Returns source table alias after replacing given tables' names from it.

# Parameters:

Name Type Description
without any | source | target | join | M2MWithout[] is type or types of tables to exclude names of.

Returns: string

source table alias after given tables' names replaced.

Defined in: pg-structure/relation/m2m-relation.ts:220


# getSourceNameWithout

getSourceNameWithout(without: any | source | target | join | M2MWithout[]): string

Returns source table name after replacing given tables' names from it.

# Parameters:

Name Type Description
without any | source | target | join | M2MWithout[] is type or types of tables to exclude names of.

Returns: string

source table name after given tables' names replaced.

Overrides: Relation

Defined in: pg-structure/relation/m2m-relation.ts:190


# getTargetAliasWithout

getTargetAliasWithout(without: any | source | target | join | M2MWithout[]): string

Returns target table alias after replacing given tables' names from it.

# Parameters:

Name Type Description
without any | source | target | join | M2MWithout[] is type or types of tables to exclude names of.

Returns: string

target table alias after given tables' names replaced.

Defined in: pg-structure/relation/m2m-relation.ts:240


# getTargetNameWithout

getTargetNameWithout(without: any | source | target | join | M2MWithout[]): string

Returns target table name after replacing given tables' names from it.

# Parameters:

Name Type Description
without any | source | target | join | M2MWithout[] is type or types of tables to exclude names of.

Returns: string

target table name after given tables' names replaced.

Overrides: Relation

Defined in: pg-structure/relation/m2m-relation.ts:210