This plugin enables class table inheritance .

Consider the following table model. @code employee - id - name (varchar) - kind (varchar) / \ staff manager - id (fk employee) - id (fk employee) - manager_id (fk manger) - numStaff (number) | executive - id (fk manager)
  • employee: This is the parent table of all employee instances.
  • staff: Table that inherits from employee where and represents.
  • manager: Another subclass of employee.
  • executive: Subclass of manager that also inherits from employee through inhertiance

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

  1.  
  2. var Employee = (exports.Employee = patio.addModel("employee", {
  3. plugins : [patio.plugins.ClassTableInheritancePlugin],
  4. static:{
  5. init:function () {
  6. this._super(arguments);
  7. this.configure({key : "kind"});
  8. }
  9. }
  10. }));
  11.  
All sub classes should just inherit their super class
  1.  
  2. var Staff = (exports.Staff = patio.addModel("staff", Employee, {
  3. static:{
  4. init:function () {
  5. this._super(arguments);
  6. this.manyToOne("manager", {key : "managerId", fetchType : this.fetchType.EAGER});
  7. }
  8. }
  9. }));
  10. var Manager = (exports.Manager = patio.addModel("manager", Employee, {
  11. static:{
  12. init:function () {
  13. this._super(arguments);
  14. this.oneToMany("staff", {key : "managerId", fetchType : this.fetchType.EAGER});
  15. }
  16. }
  17. }));
  18.  

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

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

Working with models

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

Constructor

Defined plugins/inheritance.js

configure Static Function Public


Defined plugins/inheritance.js

Configures the plugins with the provided options. Note: This should only be called in the initial parent class.

Arguments Source
  1. function (opts){
  2. this.__configureOpts = opts;
  3. return this;
  4. }

inherits Static Function Public


Defined plugins/inheritance.js

Not typically called by user code. Sets up subclass inheritance.

Arguments Source
  1. function (model){
  2. this._super(arguments);
  3. var ctiKey = this.__ctiKey = model.__ctiKey;
  4. this.__ctiTables = model.__ctiTables.slice();
  5. this.__ctiModels = model.__ctiModels.slice();
  6. this.__ctiModels.push(this);
  7. this.__ctiTables.push(this.tableName);
  8. this.__ctiColumns = comb.merge({}, model.__ctiColumns);
  9. this.__ctiColumns[this.tableName] = this.columns;
  10. this.__ctiBaseModel = model.__ctiBaseModel;
  11. //copy over our schema
  12. var newSchema = comb.merge({}, this.__schema);
  13. var schemas = model.__ctiModels.map(
  14. function (m) {
  15. return m.schema;
  16. }).reverse();
  17. schemas.forEach(function (s) {
  18. for (var i in s) {
  19. newSchema[i] = s[i];
  20. }
  21. }, this);
  22. this._setSchema(newSchema);
  23. this._setDataset(model.dataset.join(this.tableName, this.primaryKey));
  24. this._setPrimaryKey(this.__ctiBaseModel.primaryKey);
  25. this.pre("save", function (next) {
  26. if (ctiKey) {
  27. this[ctiKey] = this.tableName.toString();
  28. }
  29. next();
  30. });
  31. }

Documentation generated using coddoc.