DbReference - do-/node-doix-db GitHub Wiki

DbReference is an object representing the fact that values stored in some DbColumn are unambiguously tied to records in some relation in the same database.

For regular tables, it can be used to build a corresponding foreign key.

For other table like objects (SQL views etc.) foreign keys may not be available, but DbReference still can be used to perform automatic query building and similar tasks.

Properties

Name Type Description Note
column DbColumn parent to this reference
targetSchemaName String Name of the schema targeted by this reference. null for default model's schema, undefined for same schema (local reference)
targetSchema DbSchema The schema targeted by this reference fetched by targetSchemaName
targetRelationName String Name of the relation targeted by this reference
targetRelation DbRelation The relation targeted by this reference fetched by targetRelationName
targetColumnName String Name of the column targeted by this reference inferred by targetRelation.pk
targetColumn DbColumn The column targeted by this reference fetched by targetColumnName
on Object Actions to use by the foreign key
on.DELETE String Action for the the foreign key's ON DELETE clause

Domain-specific language

The class implements a tiny DSL to build DbReference objects from formatted strings

reference ::= "(" on_delete? relation ")"
relation ::= (targetSchemaName ".")? targetRelationName
on_delete ::= ("-" | "~")

where

  • targetSchemaName is either a schema name or an empty string (for the default schema);
  • targetRelationName is the name of some relation in the corresponding schema;
  • on_delete is translated to on.DELETE as
    • CASCADE for "-"
    • SET DEFAULT for "~" when the parent column has a default value;
    • SET NULL for "~" otherwise.

Constructor

Normally, called by the DbColumn constructor. Can accept the 1st parameter in two forms:

  • an Object to copy to this
  • or a String to parse
const ref = new DbReference ({relation: 'users', on: {DELETE: 'CASCADE'}}, parentColumn)
// or
const ref = new DbReference ('(-users)', parentColumn)

Methods

parse ()

Parses this.src (where the constructor puts the 1st string argument) into local properties. Used by the constructor itself.