Model Inheritance

Class Table Inheritance

Consider the following table model.

            - id
            - name (varchar)
            - kind (varchar)
     /                          \
 staff                        manager
   - id (fk employee)           - id (fk employee)
   - manager_id (fk manger)     - numStaff (number)
                                - id (fk manager)

When setting up you tables the parent table should contain a String column that contains the "kind" of class it is. (i.e. employee, staff, manager, executive). This allows the plugin to return the proper instance type when querying the tables.

All other tables that inherit from employee should contain a foreign key to their direct super class that is the same name as the primary key of the parent table(employee). So, in the above example staff and manager both contain foreign keys to employee and executive contains a foreign key to manager and they are all named id.

To set up you models the base super class should contain the ClassTableInheritancePlugin plugin.

 var Employee = patio.addModel("employee", {
      plugins : [patio.plugins.ClassTableInheritancePlugin],
 }).configure({key : "kind"});

All sub classes should just inherit their super class

var Staff = patio.addModel("staff", Employee)
    .manyToOne("manager", {key : "managerId", fetchType : this.fetchType.EAGER});;

var Manager = patio.addModel("manager", Employee)
    .oneToMany("staff", {key : "managerId", fetchType : this.fetchType.EAGER});;

Executive inherits from Manager, and through inheritance will also receive the oneToMany relationship with staff

 var Executive = patio.addModel("executive",  Manager);

Working with models

var comb = require("comb"),
    when = comb.when;
    new Employee({name:"Bob"}).save(),
    new Staff({name:"Greg"}).save(),
    new Manager({name:"Jane"}).save(),
    new Executive({name:"Sue"}).save()
      return Employee.all().chain(function(emps){
          var bob = emps[0], greg = emps[1], jane = emps[2], sue = emps[3];
          console.log(bob instanceof Employee); //true
          console.log(greg instanceof Employee);  //true
          console.log(greg instanceof Staff);  //true
          console.log(jane instanceof Employee);  //true
          console.log(jane instanceof Manager);  //true
          console.log(sue instanceof Employee);  //true
          console.log(sue instanceof Manager);  //true
          console.log(sue instanceof Executive);  //true


When setting up the ClassTableInheritancePlugin plugin you should include it as a plugin on the base model, and call configure in the init block of the Model.


var Employee = patio.addModel("employee", {
    plugins : [patio.plugins.ClassTableInheritancePlugin]
}).configure({key : "kind"});
var Employee = patio.addModel("employee", {
    plugins : [patio.plugins.ClassTableInheritancePlugin],
}).configure({keyCb : function(key){
    return key.toLowerCase();

Documentation generated using coddoc.