Options
All
  • Public
  • Public/Protected
  • All
Menu

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

    ```typescript // 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

Index

Properties

Readonly foreignKey

foreignKey: ForeignKey

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)

Readonly targetForeignKey

targetForeignKey: ForeignKey

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)

Readonly toMany

toMany: boolean = true

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

Readonly type

type: M2M = ...

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

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

joinAdjective

  • get joinAdjective(): undefined | string
  • Join table adjective

    Returns undefined | string

joinAlias

  • get joinAlias(): string
  • Join table alias

    Returns string

joinName

  • get joinName(): string
  • Join table name.

    Returns string

joinTable

  • 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

name

  • get name(): string
  • Suggested name for relation.

    see

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

    Returns string

sourceAdjective

  • get sourceAdjective(): undefined | string
  • Source table adjective

    Returns undefined | string

sourceAlias

  • get sourceAlias(): string
  • Source table alias

    Returns string

sourceName

  • get sourceName(): string
  • Source table name

    Returns string

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

targetAdjective

  • get targetAdjective(): undefined | string
  • Target table adjective

    Returns undefined | string

targetAlias

  • get targetAlias(): string
  • Target table alias

    Returns string

targetName

  • get targetName(): string
  • Target table name

    Returns string

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

Methods

getJoinAliasWithout

  • getJoinAliasWithout(without: "any" | "source" | "target" | "join" | M2MWithout[]): string
  • Returns join table alias after replacing given tables' names from it.

    Parameters

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

getJoinNameWithout

  • getJoinNameWithout(without: "any" | "source" | "target" | "join" | M2MWithout[]): string
  • Returns join table name after replacing given tables' names from it.

    Parameters

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

getName

  • Returns name for the relation using given naming function.

    Parameters

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

getSourceAliasWithout

  • getSourceAliasWithout(without: "any" | "source" | "target" | "join" | M2MWithout[]): string
  • Returns source table alias after replacing given tables' names from it.

    Parameters

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

getSourceNameWithout

  • getSourceNameWithout(without: "any" | "source" | "target" | "join" | M2MWithout[]): string
  • Returns source table name after replacing given tables' names from it.

    Parameters

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

getTargetAliasWithout

  • getTargetAliasWithout(without: "any" | "source" | "target" | "join" | M2MWithout[]): string
  • Returns target table alias after replacing given tables' names from it.

    Parameters

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

getTargetNameWithout

  • getTargetNameWithout(without: "any" | "source" | "target" | "join" | M2MWithout[]): string
  • Returns target table name after replacing given tables' names from it.

    Parameters

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

Generated using TypeDoc