Skip to content

Bugfix 43 #53

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions lib/services/association.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,16 @@ export function setAssociations(target: any, associations: ISequelizeAssociation
}

export function getAssociationsByRelation(target: any, relatedClass: any): ISequelizeAssociation[] {

const associations = getAssociations(target);

return (associations || []).filter(association => association.relatedClassGetter() === relatedClass);
return (associations || []).filter(association => {
const _relatedClass = association.relatedClassGetter();
return (
_relatedClass.prototype === relatedClass.prototype || // v3 + v4
/* istanbul ignore next */
relatedClass.prototype instanceof _relatedClass // v4 (for child classes)
);
});
}

/**
Expand Down
13 changes: 11 additions & 2 deletions test/models/Manufacturer.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
import {Table, Model, Column, HasMany} from "../../index";
import {Table, Model, Column, HasMany, Scopes} from "../../index";
import {ShoeWithScopes} from "./ShoeWithScopes";


@Scopes({
brandOnly: {
attributes: {
exclude: ['notInScopeBrandOnly']
}
}
})
@Table
export class Manufacturer extends Model<Manufacturer> {

@Column
brand: string;

@Column
notInScopeBrandOnly: string;

@HasMany(() => ShoeWithScopes)
shoes: ShoeWithScopes[];
}
6 changes: 6 additions & 0 deletions test/models/ShoeWithScopes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ export const SHOE_SCOPES = {
},
yellow: {
where: {primaryColor: 'yellow'}
},
red: {
where: {primaryColor: 'red'}
},
manufacturerWithScope: {
include: [() => Manufacturer.scope('brandOnly')]
}
};

Expand Down
60 changes: 58 additions & 2 deletions test/specs/scopes.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ describe('scopes', () => {
secondaryColor: 'blue',
producedAt: new Date(),
manufacturer: {
brand: BRAND
brand: BRAND,
notInScopeBrandOnly: 'invisible :)',
},
owner: {
name: OWNER
Expand Down Expand Up @@ -78,7 +79,7 @@ describe('scopes', () => {
.unscoped()
.findOne()
.then(shoe => {
expect(shoe).to.have.property('secretKey').which.is.not.null;
expect(shoe).to.have.property('secretKey').which.is.a('string');
})
);

Expand Down Expand Up @@ -140,6 +141,61 @@ describe('scopes', () => {
})
);

describe('with using scoped included model', () => {

it('should consider scope of included model (without own scope)', () =>
ShoeWithScopes
.findOne({
include: [Manufacturer.scope('brandOnly')]
})
.then(shoe => {
expect(shoe).to.have.property('manufacturer')
.that.have.property('notInScopeBrandOnly')
.which.is.undefined;
})
);

it('should consider scope of included model (with own scope)', () =>
ShoeWithScopes
.scope('red')
.findOne({
include: [Manufacturer.scope('brandOnly')]
})
.then(shoe => {
expect(shoe).to.have.property('manufacturer')
.that.have.property('notInScopeBrandOnly')
.which.is.undefined;
})
);

});

});

describe('with nested scope', () => {

it('should consider nested scope', () =>
ShoeWithScopes
.scope('manufacturerWithScope')
.findOne()
.then(shoe => {
expect(shoe).to.have.property('manufacturer')
.that.have.property('notInScopeBrandOnly')
.which.is.undefined;
})
);

it('should not consider nested scope', () =>
ShoeWithScopes
.scope('full')
.findOne()
.then(shoe => {
expect(shoe).to.have.property('manufacturer')
.that.have.property('notInScopeBrandOnly')
.which.is.a('string');
})
);

});

});
Expand Down