Added foreignKeyTarget parameters for bookshelf.js library.

This commit is contained in:
Artur Pelczar 2017-03-05 15:30:23 +01:00
parent 14cfa9f41c
commit e52501f54f
2 changed files with 50 additions and 3 deletions

View File

@ -1330,3 +1330,50 @@ new User({name: 'John'}).fetchAll({require: true})
/* events.trigger(), see http://bookshelfjs.org/#Events-instance-trigger */
/* events.triggerThen(), see http://bookshelfjs.org/#Events-instance-triggerThen */
/* model - foreignKey and foreignKeyTarget, see http://bookshelfjs.org/#Model-instance-hasOne */
{
class Capital extends bookshelf.Model<Capital> {
get tableName() { return 'capitals'; }
}
class City extends bookshelf.Model<City> {
get tableName() { return 'cities'; }
country(): Country {
return this.belongsTo(Country, 'key1', 'key2');
}
}
class Language extends bookshelf.Model<Language> {
get tableName() { return 'languages'; }
countries(): Bookshelf.Collection<Country> {
return this.belongsToMany(Country, 'languages_countries', 'lang_id', 'country_id');
}
}
class Country extends bookshelf.Model<Country> {
get tableName() { return 'countries'; }
capital(): Capital {
return this.hasOne(Capital, 'id', 'capital_id');
}
cities(): Bookshelf.Collection<City> {
return this.hasMany(City, 'key2', 'key1');
}
}
// select * from `health_records` where `patient_id` = 1;
const capital = <Capital> new Country({id: 1}).related('capital');
capital.fetch().then(model => {
// ...
});
// alternatively, if you don't need the relation loaded on the patient's relations hash:
new Country({id: 1}).capital().fetch().then(model => {
// ...
});
}

View File

@ -92,14 +92,14 @@ declare namespace Bookshelf {
static where<T>(properties: { [key: string]: any }): T;
static where<T>(key: string, operatorOrValue: string | number | boolean, valueIfOperator?: string | number | boolean): T;
belongsTo<R extends Model<any>>(target: { new (...args: any[]): R }, foreignKey?: string): R;
belongsTo<R extends Model<any>>(target: { new (...args: any[]): R }, foreignKey?: string, foreignKeyTarget?: string): R;
belongsToMany<R extends Model<any>>(target: { new (...args: any[]): R }, table?: string, foreignKey?: string, otherKey?: string): Collection<R>;
count(column?: string, options?: SyncOptions): BlueBird<number>;
destroy(options?: DestroyOptions): BlueBird<T>;
fetch(options?: FetchOptions): BlueBird<T>;
fetchAll(options?: FetchAllOptions): BlueBird<Collection<T>>;
hasMany<R extends Model<any>>(target: { new (...args: any[]): R }, foreignKey?: string): Collection<R>;
hasOne<R extends Model<any>>(target: { new (...args: any[]): R }, foreignKey?: string): R;
hasMany<R extends Model<any>>(target: { new (...args: any[]): R }, foreignKey?: string, foreignKeyTarget?: string): Collection<R>;
hasOne<R extends Model<any>>(target: { new (...args: any[]): R }, foreignKey?: string, foreignKeyTarget?: string): R;
load(relations: string | string[], options?: LoadOptions): BlueBird<T>;
morphMany<R extends Model<any>>(target: { new (...args: any[]): R }, name?: string, columnNames?: string[], morphValue?: string): Collection<R>;
morphOne<R extends Model<any>>(target: { new (...args: any[]): R }, name?: string, columnNames?: string[], morphValue?: string): R;