Skip to content

Commit 19ec7d2

Browse files
fixes #43
1 parent f12e603 commit 19ec7d2

File tree

4 files changed

+77
-5
lines changed

4 files changed

+77
-5
lines changed

lib/services/association.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,8 @@ export function getAssociationsByRelation(target: any, relatedClass: any): ISequ
144144

145145
const associations = getAssociations(target);
146146

147-
return (associations || []).filter(association => association.relatedClassGetter() === relatedClass);
147+
return (associations || []).filter(association =>
148+
association.relatedClassGetter().prototype === relatedClass.prototype);
148149
}
149150

150151
/**

test/models/Manufacturer.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
1-
import {Table, Model, Column, HasMany} from "../../index";
1+
import {Table, Model, Column, HasMany, Scopes} from "../../index";
22
import {ShoeWithScopes} from "./ShoeWithScopes";
33

4-
4+
@Scopes({
5+
brandOnly: {
6+
attributes: {
7+
exclude: ['notInScopeBrandOnly']
8+
}
9+
}
10+
})
511
@Table
612
export class Manufacturer extends Model<Manufacturer> {
713

814
@Column
915
brand: string;
1016

17+
@Column
18+
notInScopeBrandOnly: string;
19+
1120
@HasMany(() => ShoeWithScopes)
1221
shoes: ShoeWithScopes[];
1322
}

test/models/ShoeWithScopes.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ export const SHOE_SCOPES = {
1212
},
1313
yellow: {
1414
where: {primaryColor: 'yellow'}
15+
},
16+
red: {
17+
where: {primaryColor: 'red'}
18+
},
19+
manufacturerWithScope: {
20+
include: [() => Manufacturer.scope('brandOnly')]
1521
}
1622
};
1723

test/specs/scopes.spec.ts

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ describe('scopes', () => {
4141
secondaryColor: 'blue',
4242
producedAt: new Date(),
4343
manufacturer: {
44-
brand: BRAND
44+
brand: BRAND,
45+
notInScopeBrandOnly: 'invisible :)',
4546
},
4647
owner: {
4748
name: OWNER
@@ -78,7 +79,7 @@ describe('scopes', () => {
7879
.unscoped()
7980
.findOne()
8081
.then(shoe => {
81-
expect(shoe).to.have.property('secretKey').which.is.not.null;
82+
expect(shoe).to.have.property('secretKey').which.is.a('string');
8283
})
8384
);
8485

@@ -140,6 +141,61 @@ describe('scopes', () => {
140141
})
141142
);
142143

144+
describe('with using scoped included model', () => {
145+
146+
it('should consider scope of included model (without own scope)', () =>
147+
ShoeWithScopes
148+
.findOne({
149+
include: [Manufacturer.scope('brandOnly')]
150+
})
151+
.then(shoe => {
152+
expect(shoe).to.have.property('manufacturer')
153+
.that.have.property('notInScopeBrandOnly')
154+
.which.is.undefined;
155+
})
156+
);
157+
158+
it('should consider scope of included model (with own scope)', () =>
159+
ShoeWithScopes
160+
.scope('red')
161+
.findOne({
162+
include: [Manufacturer.scope('brandOnly')]
163+
})
164+
.then(shoe => {
165+
expect(shoe).to.have.property('manufacturer')
166+
.that.have.property('notInScopeBrandOnly')
167+
.which.is.undefined;
168+
})
169+
);
170+
171+
});
172+
173+
});
174+
175+
describe('with nested scope', () => {
176+
177+
it('should consider nested scope', () =>
178+
ShoeWithScopes
179+
.scope('manufacturerWithScope')
180+
.findOne()
181+
.then(shoe => {
182+
expect(shoe).to.have.property('manufacturer')
183+
.that.have.property('notInScopeBrandOnly')
184+
.which.is.undefined;
185+
})
186+
);
187+
188+
it('should not consider nested scope', () =>
189+
ShoeWithScopes
190+
.scope('full')
191+
.findOne()
192+
.then(shoe => {
193+
expect(shoe).to.have.property('manufacturer')
194+
.that.have.property('notInScopeBrandOnly')
195+
.which.is.a('string');
196+
})
197+
);
198+
143199
});
144200

145201
});

0 commit comments

Comments
 (0)