Skip to content

relationships

waterfoul edited this page Aug 19, 2016 · 6 revisions
  • All foreign key relationships are just special fields on data contracts of the type foreignKeyType with a slightly different decorator
  • All of the foreign key objects have a static fetch method for reverse lookups when the other side of the relationship doesn't reciprocate the relationship
  • All relationships use join tables to simplify object -> database table reconciliation
    • These tables may not be created until an object with the relationship is acted upon
  • The following relationship types are supported
    • OneToOne
      • fetch()
        • Returns a promise to the related object
      • set(obj: T)
        • Returns a promise to update the record
    • OneToMany (Represents the one side of the relationship)
      • fetch()
        • Returns a promise to the related objects
    • ManyToOne (Represents the many side of the relationship)
      • fetch()
        • Returns a promise to the related object
      • set(obj: T)
        • Returns a promise to update the record
    • ManyToMany
      • fetch()
        • Returns a promise to the related objects

Example

Starting with two data contracts

    import {DataContract, field} from 'tanjentjs-ts-orm/node';

    export class ContractOne extends DataContract {
    	@field()
    	public data: string;
    }

    export class ContractTwo extends DataContract {
    	@field()
    	public data: string;
    }

Import the required relationship from the node library and add the field

    import {DataContract, field} from 'tanjentjs-ts-orm/node';
    import {OneToOne, relatedField} from 'tanjentjs-ts-orm/node/relationships';

    export class ContractOne extends DataContract {
    	@field()
    	public data: string;
    	@relatedField(ContractTwo)
    	public ContractTwo: OneToOne<ContractTwo>;
    }

    export class ContractTwo extends DataContract {
    	@field()
    	public data: string;
    	@relatedField(ContractOne)
    	public OtherContract: OneToOne<ContractOne>;
    }

Clone this wiki locally