Model Inheritance

Class Table Inheritance

Consider the following table model.

  1. employee
  2. - id
  3. - name (varchar)
  4. - kind (varchar)
  5. / \
  6. staff manager
  7. - id (fk employee) - id (fk employee)
  8. - manager_id (fk manger) - numStaff (number)
  9. |
  10. executive
  11. - 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.

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

All sub classes should just inherit their super class

  1. var Staff = patio.addModel("staff", Employee)
  2. .manyToOne("manager", {key : "managerId", fetchType : this.fetchType.EAGER});;
  3. var Manager = patio.addModel("manager", Employee)
  4. .oneToMany("staff", {key : "managerId", fetchType : this.fetchType.EAGER});;

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

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

Working with models

  1. var comb = require("comb"),
  2. when = comb.when;
  3. when(
  4. new Employee({name:"Bob"}).save(),
  5. new Staff({name:"Greg"}).save(),
  6. new Manager({name:"Jane"}).save(),
  7. new Executive({name:"Sue"}).save()
  8. ).chain(function(){
  9. return Employee.all().chain(function(emps){
  10. var bob = emps[0], greg = emps[1], jane = emps[2], sue = emps[3];
  11. console.log(bob instanceof Employee); //true
  12. console.log(greg instanceof Employee); //true
  13. console.log(greg instanceof Staff); //true
  14. console.log(jane instanceof Employee); //true
  15. console.log(jane instanceof Manager); //true
  16. console.log(sue instanceof Employee); //true
  17. console.log(sue instanceof Manager); //true
  18. console.log(sue instanceof Executive); //true
  19. });
  20. });

Configuring

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.

Options

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

Documentation generated using coddoc.