pg-structure / O2MRelation
# Class: O2MRelation
Class which represent one to many relationship which resembles hasMany
relation in ORMs (Object Relational Mappers).
Provides attributes and methods for details of the relationship.
Below is a database schema as an example:
Some definitions used in descriptions for O2MRelation.
- ** Source Table: ** Table which this relationship belongs to.
- ** Target Table: ** Table that is related to base table.
# Example
// Example tables have single primary key and examples first relation. So zero index ([0]) is used. Use all array elements if necessary.
// product ----< line_item
// (source) (target)
const relation = product.o2mRelations[0]; // RELATION: product ---< line_item
const foreignKey = relation.foreignKey; // FOREIGN KEY: ^-- product_has_carts
const sourceTable = relation.sourceTable; // TABLE: product
const targetTable = relation.targetTable; // TABLE: line_item
const FKColumn = relation.foreignKey.columns[0]; // COLUMN: product_id (from line_item table)
const sourcePKColumn = relation.sourceTable.primaryKeys[0]; // COLUMN: id (from product table)
# Hierarchy
-
↳ O2MRelation
# Properties
# foreignKey
• foreignKey: ForeignKey
Foreign key between source table and target table.
# Example
const relation = product.O2MRelationRelations[0]; // RELATION: product ---< line_item
const foreignKey = relation.foreignKey; // CONSTRAINT: ^-- product_has_carts
const FKColumn = relation.foreignKey.columns[0]; // COLUMN: product_id (from line_item table)
Defined in: pg-structure/relation/o2m-relation.ts:116
# toMany
• Readonly
toMany: boolean= true
Whether the relation targets to many. Since, one to many relations targets many, this is true
.
Defined in: pg-structure/relation/o2m-relation.ts:53
# type
• Readonly
type: O2M
Type of the relation, which is o2m
for O2MRelation.
For TypeScript it is enum of RelationType.O2M
.
Defined in: pg-structure/relation/o2m-relation.ts:48
# Accessors
# info
• get info(): string
Informational text representation of the relation.
# Example
const info = relation.info; // [public.contact]――― cart_contact ――⥷ [public.cart]
Returns: string
Defined in: pg-structure/relation/o2m-relation.ts:82
# name
• get name(): string
Suggested name for relation.
see
{@link ../relation-names.md Relation Names}
Returns: string
Defined in: pg-structure/relation/o2m-relation.ts:61
# sourceAdjective
• get sourceAdjective(): undefined | string
Source table's adjective extracted from foreign key name.
Returns: undefined | string
Defined in: pg-structure/base/relation.ts:86
# sourceAlias
• get sourceAlias(): string
Source table alias
Returns: string
Defined in: pg-structure/relation/o2m-relation.ts:139
# sourceName
• get sourceName(): string
Source table name.
Returns: string
Defined in: pg-structure/base/relation.ts:76
# sourceTable
• get sourceTable(): Table
Table which this relation belongs to.
# Example
const relation = product.O2MRelationRelations[0]; // RELATION: product ---< line_item
const sourceTable = relation.sourceTable; // TABLE: product
Returns: Table
Defined in: pg-structure/relation/o2m-relation.ts:93
# targetAdjective
• get targetAdjective(): undefined | string
Source table's adjective extracted from foreign key name.
Returns: undefined | string
Defined in: pg-structure/base/relation.ts:91
# targetAlias
• get targetAlias(): string
Target table alias
Returns: string
Defined in: pg-structure/relation/o2m-relation.ts:144
# targetName
• get targetName(): string
Target table name
Returns: string
Defined in: pg-structure/base/relation.ts:81
# targetTable
• get targetTable(): Table
Table which this relation is referring to.
# Example
const relation = product.O2MRelationRelations[0]; // RELATION: product ---< line_item
const targetTable = relation.targetTable; // TABLE: line_item
Returns: Table
Defined in: pg-structure/relation/o2m-relation.ts:104
# Methods
# getName
▸ getName(relationNameFunctions
: string | RelationNameFunctions): string
Retunrs name for the relation using given naming function.
# Parameters:
Name | Type | Description |
---|---|---|
relationNameFunctions | string | RelationNameFunctions | are custom functions or name of the modulethat 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/o2m-relation.ts:72
# getSourceAliasWithout
▸ getSourceAliasWithout(without
: any | source | target | RelationWithout[]): string
Returns source table alias after replacing given tables' names from it.
# Parameters:
Name | Type | Description |
---|---|---|
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.
Defined in: pg-structure/relation/o2m-relation.ts:124
# getSourceNameWithout
▸ getSourceNameWithout(without
: any | source | target | RelationWithout[]): string
Returns source table name after replacing given tables' names from it.
# Parameters:
Name | Type | Description |
---|---|---|
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.
Inherited from: Relation
Defined in: pg-structure/base/relation.ts:61
# getTargetAliasWithout
▸ getTargetAliasWithout(without
: any | source | target | RelationWithout[]): string
Returns target table alias after replacing given tables' names from it.
# Parameters:
Name | Type | Description |
---|---|---|
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.
Defined in: pg-structure/relation/o2m-relation.ts:134
# getTargetNameWithout
▸ getTargetNameWithout(without
: any | source | target | RelationWithout[]): string
Returns target table name after replacing given tables' names from it.
# Parameters:
Name | Type | Description |
---|---|---|
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.
Inherited from: Relation
Defined in: pg-structure/base/relation.ts:71