Options
All
  • Public
  • Public/Protected
  • All
Menu

Class M2ORelation

Class which represent many to one relationship which resembles belongsTo relation in ORMs (Object Relational Mappers). Provides attributes and methods for details of the relationship.

Actually there is no many to one relation in database engine. It is basically one to many relation in reverse direction.

Below is a database schema as an example: Database Schema

Some definitions used in descriptions for M2ORelation.

  • ** Source Table: ** Table which this relationship belongs to.
  • ** Target Table: ** Table that is related to base table.

    Example

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

const relation = line_item.m2oRelations[0]; // RELATION: line_item >---- product const foreignKey = relation.foreignKey; // CONSTRAINT: ^-- product_has_carts const sourceTable = relation.sourceTable; // TABLE: line_item const targetTable = relation.targetTable; // TABLE: product const FKColumn = relation.foreignKey.columns[0]; // COLUMN: product_id (from line_item table) const PKColumn = relation.targetTable.primaryKeys[0]; // COLUMN: id (from product table) ```

Hierarchy

Index

Properties

foreignKey

foreignKey: ForeignKey

Example

const relation     = product.M2ORelationRelations[0];  // RELATION:    line_item >---- product
const foreignKey   = relation.foreignKey;              // CONSTRAINT:               ^-- product_has_carts
const FKColumn     = relation.foreignKey.columns[0];   // COLUMN:      product_id (from line_item table)

Readonly toMany

toMany: boolean = false

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

Readonly type

type: M2O = ...

Type of the relation, which is m2o for M2ORelation. For TypeScript it is enum of RelationType.M2O.

Accessors

info

  • get info(): string
  • Informational text representation of the relation.

    Example

    const info = relation.info; // [public.cart] ⭃―― cart_contact ―――[public.contact]
    

    Returns string

name

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

    see

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

    Returns string

sourceAdjective

  • get sourceAdjective(): undefined | string
  • Source table's adjective extracted from foreign key name.

    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.M2ORelationRelations[0];  // RELATION:    line_item >---- product
    const sourceTable  = relation.sourceTable;             // TABLE:       line_item
    

    Returns Table

targetAdjective

  • get targetAdjective(): undefined | string
  • Source table's adjective extracted from foreign key name.

    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 referred by.

    Example

    const relation     = product.M2ORelationRelations[0];  // RELATION:    line_item >---- product
    const targetTable  = relation.targetTable;             // TABLE:       product
    

    Returns Table

Methods

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" | RelationWithout[]): string
  • Returns source table alias after replacing given tables' names from it.

    Parameters

    • without: "any" | "source" | "target" | RelationWithout[]

      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" | RelationWithout[]): string
  • Returns source table name after replacing given tables' names from it.

    Parameters

    • without: "any" | "source" | "target" | RelationWithout[]

      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" | RelationWithout[]): string
  • Returns target table alias after replacing given tables' names from it.

    Parameters

    • without: "any" | "source" | "target" | RelationWithout[]

      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" | RelationWithout[]): string
  • Returns target table name after replacing given tables' names from it.

    Parameters

    • without: "any" | "source" | "target" | RelationWithout[]

      is type or types of tables to exclude names of.

    Returns string

    target table name after given tables' names replaced.

Generated using TypeDoc