Table

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

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

size -------------------
id (PK)                |  ---------------------------< line_item >------------ cart
name                   |  |                            product_id (PFK)        id (PK)
                       |  |                            cart_id    (PFK)        name
                       ^  |
color -------------< product >------------- vendor
id (PK)              id        (PK)         id (PK)
name                 name                   name
                     color_id  (FK)
                     size_id   (FK)
                     vendor_id (FK)

Below is the same schema as image: Database Schema

Kind: global class

new Table(args)

Constructor function. You don't need to call constructor manually. pg-structure handles this.

Param Type Description
args Object Attributes of the Table instance to be created.
args.parent Schema Parent Schema of the Table.
args.name string Name of the Table.
args.description string Description of the Table.
args.descriptionData Object Extra data to store in object.
args.kind string Kind of table. (table or view)

table.name : string

Name of the table.

Kind: instance property of Table
Read only: true

table.fullName : string

Full name of the Table with (.) notation.

Kind: instance property of Table
Read only: true
Example

var fullName = table.fullName; // public.account

table.fullCatalogName : string

Full name of the Table with (.) notation including catalog name.

Kind: instance property of Table
Read only: true
Example

var fullName = table.fullName; // crm.public.account

table.schema : Schema

Schema this table belongs to.

Kind: instance property of Table
Read only: true
See: Aliases parent
Example

var schema = table.schema; // Schema instance

table.parent : Schema

Schema this table belongs to.

Kind: instance property of Table
Read only: true
See: Aliases schema
Example

var schema = table.parent; // Schema instance

table.comment : string

Comment of the table.

Kind: instance property of Table
Read only: true
See: Aliases description

table.commentData : Object

JS Object extracted from table description. Object is expected as JSON data between [PG-STRUCTURE] and [/PG-STRUCTURE] tags in description. Tags are case-insensitive. For maximum comfort JSON parsing is made by jsonic. It is a non-strict JSON parser. It is possible to ommit quotes etc. Please see jsonic for details.

Kind: instance property of Table
Read only: true
See: Aliases descriptionData
Example

let description = table.comment;             // -> 'This table holds account details. [PG-STRUCTURE]{ extraData: 2 }[/PGEN]'
let extra = table.commentData;               // -> { extraData: 2 }
console.log(table.commentData.extraData);    // -> 2

table.description : string

Comment of the table.

Kind: instance property of Table
Read only: true
See: Aliases comment

table.descriptionData : Object

JS Object extracted from table description. Object is expected as JSON data between [PG-STRUCTURE] and [/PG-STRUCTURE] tags in description. Tags are case-insensitive. For maximum comfort JSON parsing is made by jsonic. It is a non-strict JSON parser. It is possible to ommit quotes etc. You don't need to quote property names: { foo:"bar baz", red:255 } You don't need the top level braces: foo:"bar baz", red:255 You don't need to quote strings with spaces: foo:bar baz, red:255 You do need to quote strings if they contain a comma or closing brace or square bracket: icky:",}]" You can use single quotes for strings: Jules:'Cry "Havoc," and let slip the dogs of war!' You can have trailing commas: foo:bar, red:255, For details, please see jsonic.

Kind: instance property of Table
Read only: true
See: Aliases commentData
Example

let description = table.description;             // -> 'This table holds account details. [PG-STRUCTURE]{ "extraData": 2 }[/PGEN]'
let extra = table.descriptionData;               // -> { extraData: 2 }
console.log(table.descriptionData.extraData);    // -> 2

table.kind : string

Kind of table. One of the values of table or view.

Kind: instance property of Table
Read only: true

table.columns : Map.<Column>

All Column instances in the table as a Map. They are ordered same order as they are defined in database table.

Kind: instance property of Table
Read only: true
See: Map
Example

var isAvailable  = table.columns.has('id');
var columnNames  = Array.from(schema.columns.keys());       // Use spread operator to get column names as an array.
var column       = table.columns.get('user_id');
var name         = column.name;

for (let column of table.columns.values()) {
    console.log(column.name);
}

for (let [name, column] of table.columns) {
    console.log(name, column.name);
}

table.constraints : Map.<Constraint>

All Constraint instances in the table as a Map. They are ordered by name.

Kind: instance property of Table
Read only: true

table.db : Db

Db this table belongs to.

Kind: instance property of Table
Read only: true

table.foreignKeyConstraints : Map.<Constraint>

All Constraint instances which are foreign key constraints in the table as a Map.

Kind: instance property of Table
Read only: true
See: o2mRelations, m2oRelations, m2mRelations to get more details about relations.

table.foreignKeyColumns : Map.<Column>

All foreign key columns of all foreignKeyConstraints as a Map. Foreign key constraints may contain more than one column. To get foreign key columns of a specific foreign key constraint use foreignKeyConstraints.columns

Kind: instance property of Table
Read only: true

table.foreignKeyConstraintsToThis : Map.<Constraint>

All foreign key Constraint instances which are referring to this table as a Map.

Kind: instance property of Table
Read only: true
See: o2mRelations, m2oRelations, m2mRelations to get more details about relations.

table.primaryKeyConstraint : Constraint | undefined

Primary key constraint instance of this table.

Kind: instance property of Table
Read only: true
See: primaryKeyColumns to get primary key columns directly.
Example

let pkConstraint = table.primaryKeyConstraint;
let pkColumns    = Array.from(pkConstraint.columns.values());   // As an array

for (let [name, column] of pkConstraint.columns) {
    console.log(column.name);
}

table.primaryKeyColumns : Map.<Column>

Primary key columns of this table as a Map.

Kind: instance property of Table
Read only: true
See: primaryKeyConstraint to get primary key constraint.
Example

let pkColumns  = Array.from(table.primaryKeyColumns.values());  // As an array
for (let [name, column] of pkConstraint.columns) {
    console.log(column.name);
}

table.hasManyTables : Map.<Table>

Tables sorted by name, which this table has relationship of type one to many.

Kind: instance property of Table
Read only: true
See: Example schema, Map
Example

for (let [name, table] of vendorTable.hasManyTables) {
    console.log(table.name);
}

table.belongsToTables : Map.<Table>

Tables sorted by name, which this table has relationship of type belongs to which is reverse direction of one to many.

Kind: instance property of Table
Read only: true
See: Example schema, Map
Example

for (let [name, table] of productTable.belongsToTables) {
    console.log(table.name);
}

table.belongsToManyTables : Map.<Table>

Tables sorted by name, which this table has relationship of type many to many.

Kind: instance property of Table
Read only: true
See: Example schema, Map
Example

// Cart (id) has many products (id) through line_item join table.
for (let [name, table] of cartTable.belongsToManyTables) {
    console.log(table.name);
}

table.belongsToManyTablesPk : Map.<Table>

Tables sorted by name, which this table has relationship of type many to many. Includes only tables joined by primary keys in join table.

Kind: instance property of Table
Read only: true
See: Example schema, Map
Example

// Cart (id) has many products (id) through line_item join table.
for (let [name, table] of cartTable.belongsToManyTables) {
    console.log(table.name);
}

table.m2mRelations : Set.<M2MRelation>

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

Kind: instance property of Table
Read only: true

table.m2mRelationsPk : Set.<M2MRelation>

Set 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 Keys means foreign keys of join table which are also Primary Keys 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.

Kind: instance property of Table
Read only: true

table.o2mRelations : Set.<O2MRelation>

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

Kind: instance property of Table
Read only: true

table.m2oRelations : Set.<M2ORelation>

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

Kind: instance property of Table
Read only: true

table.relations : Array.<(O2MRelation|M2ORelation|M2MRelation)>

List of all relationships of the table.

Kind: instance property of Table
Read only: true

table.get(path) ⇒ Column | undefined

Returns Column on given path relative to Table.

Kind: instance method of Table
Returns: Column | undefined - - Requested item.

Param Type Description
path string Path of the requested item in dot (.) notation such as 'public.contact'

Example

var column = table.get('contact'),      // Returns contact column in public table.