Skip to content

Commit 0663ee2

Browse files
fixes #70
1 parent 16593a9 commit 0663ee2

File tree

3 files changed

+34
-14
lines changed

3 files changed

+34
-14
lines changed

lib/services/scopes.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,14 @@ export function getScopeOptions(target: any): IScopeOptions | undefined {
4646
/**
4747
* Resolves scope
4848
*/
49-
function resolveScope(scopeName: string, model: typeof Model, options: IScopeFindOptions | undefined): void {
49+
function resolveScope(scopeName: string, model: typeof Model, options: IScopeFindOptions | Function | undefined): void {
5050
resolveModelGetter(options);
51-
options = inferAlias(options, model);
51+
if (typeof options === 'function') {
52+
const fn: Function = options;
53+
options = (...args: any[]) => inferAlias(fn(...args), model);
54+
} else {
55+
options = inferAlias(options, model);
56+
}
5257
model.addScope(scopeName, options as IFindOptions, {override: true});
5358
}
5459

test/models/ShoeWithScopes.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ export const SHOE_SCOPES = {
2222
primaryColor: primaryColor => ({
2323
where: {primaryColor}
2424
}
25+
),
26+
primaryColorWithManufacturer: primaryColor => ({
27+
include: [Manufacturer],
28+
where: {primaryColor},
29+
}
2530
)
2631
};
2732

test/specs/scopes.spec.ts

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -198,18 +198,28 @@ describe('scopes', () => {
198198

199199
});
200200

201-
// describe('with scope function', () => {
202-
//
203-
// it('should consider nested scope', () =>
204-
// ShoeWithScopes
205-
// .scope({method: ['primaryColor', 'yellow']})
206-
// .findOne()
207-
// .then(shoe => {
208-
// expect(shoe).to.have.property('primaryColor', 'yellow');
209-
// })
210-
// );
211-
//
212-
// });
201+
describe('with scope function', () => {
202+
203+
it('should find appropriate shoe due to correctly passed scope function param', () =>
204+
ShoeWithScopes
205+
.scope({method: ['primaryColor', 'red']})
206+
.findOne()
207+
.then(shoe => {
208+
expect(shoe).to.have.property('primaryColor', 'red');
209+
})
210+
);
211+
212+
it('should find appropriate shoe due to correctly passed scope function param including associated model', () =>
213+
ShoeWithScopes
214+
.scope({method: ['primaryColorWithManufacturer', 'red']})
215+
.findOne()
216+
.then(shoe => {
217+
expect(shoe).to.have.property('primaryColor', 'red');
218+
expect(shoe).to.have.property('manufacturer').that.is.an('object');
219+
})
220+
);
221+
222+
});
213223

214224
});
215225

0 commit comments

Comments
 (0)