Skip to content

Commit f3e4826

Browse files
fixes 37
1 parent 065dc29 commit f3e4826

File tree

4 files changed

+70
-4
lines changed

4 files changed

+70
-4
lines changed

lib/models/v3/Model.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,26 @@ export const Model: any = (() => {
3434
if (typeof SeqModelProto[key] === 'function') {
3535

3636
_Model[key] = function(...args: any[]): any {
37-
return SeqModelProto[key].call(this.Model || this, ...args);
37+
38+
let targetModel = this.Model;
39+
40+
if (this.scoped) {
41+
// Adds scope info to 'this' context
42+
targetModel = Object.create(targetModel);
43+
targetModel.$scope = this.$scope;
44+
targetModel.scoped = this.scoped;
45+
}
46+
47+
return SeqModelProto[key].call(targetModel, ...args);
3848
};
3949
}
4050
});
4151

52+
// 'scope' need to be called with 'this'context
53+
// instead of 'this.Model' context
54+
_Model['scope'] = function(...args: any[]): any {
55+
return SeqModelProto.scope.call(this, ...args);
56+
};
57+
4258
return _Model;
4359
})();

lib/models/v3/Sequelize.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ export class Sequelize extends SequelizeOrigin implements BaseSequelize {
103103
// this initializes some stuff for Instance
104104
model['refreshAttributes']();
105105

106-
// copy static fields to class
106+
// copy own static fields to class
107107
Object.keys(model).forEach(key => key !== 'name' && (_class[key] = model[key]));
108108

109109
// the class needs to know its sequelize model

test/models/ShoeWithScopes.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {Table, Model, Column, ForeignKey, BelongsTo,
22
DefaultScope, Scopes} from "../../index";
33
import {Manufacturer} from "./Manufacturer";
4+
import {Person} from "./Person";
45

56
export const SHOE_DEFAULT_SCOPE = {
67
attributes: ['id', 'primaryColor', 'secondaryColor', 'producedAt']
@@ -41,4 +42,11 @@ export class ShoeWithScopes extends Model<ShoeWithScopes> {
4142
@BelongsTo(() => Manufacturer)
4243
manufacturer: Manufacturer;
4344

45+
@ForeignKey(() => Person)
46+
@Column
47+
ownerId: number;
48+
49+
@BelongsTo(() => Person)
50+
owner: Person;
51+
4452
}

test/specs/scopes.spec.ts

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1-
import {expect} from 'chai';
1+
import {expect, use} from 'chai';
2+
import * as chaiAsPromised from 'chai-as-promised';
23
import {createSequelize} from "../utils/sequelize";
34
import {getScopeOptions} from "../../lib/services/models";
45
import {ShoeWithScopes, SHOE_DEFAULT_SCOPE, SHOE_SCOPES} from "../models/ShoeWithScopes";
56
import {Manufacturer} from "../models/Manufacturer";
7+
import {Person} from "../models/Person";
8+
9+
use(chaiAsPromised);
610

711
describe('scopes', () => {
812

@@ -28,6 +32,7 @@ describe('scopes', () => {
2832
describe('find', () => {
2933

3034
const BRAND = 'adiwas';
35+
const OWNER = 'bob';
3136

3237
beforeEach(() => ShoeWithScopes
3338
.create<ShoeWithScopes>({
@@ -37,8 +42,11 @@ describe('scopes', () => {
3742
producedAt: new Date(),
3843
manufacturer: {
3944
brand: BRAND
45+
},
46+
owner: {
47+
name: OWNER
4048
}
41-
}, {include: [Manufacturer]}));
49+
}, {include: [Manufacturer, Person]}));
4250

4351
it('should consider default scope', () =>
4452

@@ -64,6 +72,40 @@ describe('scopes', () => {
6472
})
6573
);
6674

75+
describe('with include options', () => {
76+
77+
it('should consider scopes and additional included model (object)', () =>
78+
expect(ShoeWithScopes
79+
.scope('full')
80+
.findOne<ShoeWithScopes>({
81+
include: [{
82+
model: Person,
83+
}]
84+
})
85+
.then(shoe => {
86+
expect(shoe).to.have.property('manufacturer').which.is.not.null;
87+
expect(shoe).to.have.property('manufacturer').which.have.property('brand', BRAND);
88+
expect(shoe).to.have.property('owner').which.is.not.null;
89+
})
90+
).not.to.be.rejected
91+
);
92+
93+
it('should consider scopes and additional included model (model)', () =>
94+
expect(ShoeWithScopes
95+
.scope('full')
96+
.findOne({
97+
include: [Person]
98+
})
99+
.then(shoe => {
100+
expect(shoe).to.have.property('manufacturer').which.is.not.null;
101+
expect(shoe).to.have.property('manufacturer').which.have.property('brand', BRAND);
102+
expect(shoe).to.have.property('owner').which.is.not.null;
103+
})
104+
).not.to.be.rejected
105+
);
106+
107+
});
108+
67109
});
68110

69111
});

0 commit comments

Comments
 (0)