I have been playing with the TypeScript MVC framework TypeFramework and am trying to figure out how to create relationships between models. I know it uses the Waterline ORM. An example of a Waterline model with a relationship is:
// A user may have many pet
var User = Waterline.Collection.extend({
attributes: {
firstName: 'string',
lastName: 'string',
// Add a reference to Pets
pets: {
collection: 'pet',
via: 'owner'
}
}
});
// A pet may only belong to a single user
var Pet = Waterline.Collection.extend({
attributes: {
breed: 'string',
type: 'string',
name: 'string',
// Add a reference to User
owner: {
model: 'user'
}
}
});
An example of a TypeFramework model looks like:
class User extends TF.Model{
firstName: string,
lastName: string
}
The code for TF.Model is:
class Model {
public id: number;
public createdAt: Date;
public updatedAt: Date;
static collectionName: string;
static schema: boolean;
static adapter: string;
static attributes: any;
private static collection;
public save: <T>(callback: ISingleResultCallback<T>) => void;
public destroy: <T>(callback: INoResultCallback<T>) => void;
static save<T>(model: T, callback: ISingleResultCallback<T>): void;
static destroy<T>(model: T, callback: INoResultCallback<T>): void;
static all<T>(): DbQueryEnd<T>;
static where(query: {}): WL.IQuery;
static get(id: number): DbQueryUnique<any>;
static first<T>(query: {}): DbQueryUnique<T>;
static find<T>(): DbQuery<T>;
static query<T>(query: string): DbQueryRaw<T>;
static validate(attr: string, options: ModelValidation): any;
static validate(attrs: string[], definition: ModelValidation): any;
private static extend(from, to);
}
I'm not sure how to integrate a relationship into a strongly-typed TypeFramework model. Any suggestions or help would be greatly appreciated.