diff --git a/bookshelf/bookshelf-tests.ts b/bookshelf/bookshelf-tests.ts index 5b743bef71..98fd0f4599 100644 --- a/bookshelf/bookshelf-tests.ts +++ b/bookshelf/bookshelf-tests.ts @@ -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 { + get tableName() { return 'capitals'; } + } + + class City extends bookshelf.Model { + get tableName() { return 'cities'; } + + country(): Country { + return this.belongsTo(Country, 'key1', 'key2'); + } + } + + class Language extends bookshelf.Model { + get tableName() { return 'languages'; } + + countries(): Bookshelf.Collection { + return this.belongsToMany(Country, 'languages_countries', 'lang_id', 'country_id'); + } + } + + class Country extends bookshelf.Model { + get tableName() { return 'countries'; } + capital(): Capital { + return this.hasOne(Capital, 'id', 'capital_id'); + } + + cities(): Bookshelf.Collection { + return this.hasMany(City, 'key2', 'key1'); + } + } + + // select * from `health_records` where `patient_id` = 1; + const 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 => { + // ... + }); +} \ No newline at end of file diff --git a/bookshelf/index.d.ts b/bookshelf/index.d.ts index 56472a3213..4aa6fd38b5 100644 --- a/bookshelf/index.d.ts +++ b/bookshelf/index.d.ts @@ -92,14 +92,14 @@ declare namespace Bookshelf { static where(properties: { [key: string]: any }): T; static where(key: string, operatorOrValue: string | number | boolean, valueIfOperator?: string | number | boolean): T; - belongsTo>(target: { new (...args: any[]): R }, foreignKey?: string): R; + belongsTo>(target: { new (...args: any[]): R }, foreignKey?: string, foreignKeyTarget?: string): R; belongsToMany>(target: { new (...args: any[]): R }, table?: string, foreignKey?: string, otherKey?: string): Collection; count(column?: string, options?: SyncOptions): BlueBird; destroy(options?: DestroyOptions): BlueBird; fetch(options?: FetchOptions): BlueBird; fetchAll(options?: FetchAllOptions): BlueBird>; - hasMany>(target: { new (...args: any[]): R }, foreignKey?: string): Collection; - hasOne>(target: { new (...args: any[]): R }, foreignKey?: string): R; + hasMany>(target: { new (...args: any[]): R }, foreignKey?: string, foreignKeyTarget?: string): Collection; + hasOne>(target: { new (...args: any[]): R }, foreignKey?: string, foreignKeyTarget?: string): R; load(relations: string | string[], options?: LoadOptions): BlueBird; morphMany>(target: { new (...args: any[]): R }, name?: string, columnNames?: string[], morphValue?: string): Collection; morphOne>(target: { new (...args: any[]): R }, name?: string, columnNames?: string[], morphValue?: string): R;