Skip to content
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
18 changes: 17 additions & 1 deletion lib/models/v3/Model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,26 @@ export const Model: any = (() => {
if (typeof SeqModelProto[key] === 'function') {

_Model[key] = function(...args: any[]): any {
return SeqModelProto[key].call(this.Model || this, ...args);

let targetModel = this.Model;

if (this.scoped !== undefined) {
// Adds scope info to 'this' context
targetModel = Object.create(targetModel);
targetModel.$scope = this.$scope;
targetModel.scoped = this.scoped;
}

return SeqModelProto[key].call(targetModel, ...args);
};
}
});

// 'scope' and 'unscoped' need to be called with 'this' context
// instead of 'this.Model' context
_Model['scope'] = _Model['unscoped'] = function(...args: any[]): any {
return SeqModelProto.scope.call(this, ...args);
};

return _Model;
})();
2 changes: 1 addition & 1 deletion lib/models/v3/Sequelize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export class Sequelize extends SequelizeOrigin implements BaseSequelize {
// this initializes some stuff for Instance
model['refreshAttributes']();

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

// the class needs to know its sequelize model
Expand Down
8 changes: 8 additions & 0 deletions test/models/ShoeWithScopes.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {Table, Model, Column, ForeignKey, BelongsTo,
DefaultScope, Scopes} from "../../index";
import {Manufacturer} from "./Manufacturer";
import {Person} from "./Person";

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

@ForeignKey(() => Person)
@Column
ownerId: number;

@BelongsTo(() => Person)
owner: Person;

}
82 changes: 80 additions & 2 deletions test/specs/scopes.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import {expect} from 'chai';
import {expect, use} from 'chai';
import * as chaiAsPromised from 'chai-as-promised';
import {createSequelize} from "../utils/sequelize";
import {getScopeOptions} from "../../lib/services/models";
import {ShoeWithScopes, SHOE_DEFAULT_SCOPE, SHOE_SCOPES} from "../models/ShoeWithScopes";
import {Manufacturer} from "../models/Manufacturer";
import {Person} from "../models/Person";

use(chaiAsPromised);

describe('scopes', () => {

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

const BRAND = 'adiwas';
const OWNER = 'bob';

beforeEach(() => ShoeWithScopes
.create<ShoeWithScopes>({
Expand All @@ -37,8 +42,11 @@ describe('scopes', () => {
producedAt: new Date(),
manufacturer: {
brand: BRAND
},
owner: {
name: OWNER
}
}, {include: [Manufacturer]}));
}, {include: [Manufacturer, Person]}));

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

Expand All @@ -64,6 +72,76 @@ describe('scopes', () => {
})
);

it('should not consider default scope due to unscoped call', () =>

ShoeWithScopes
.unscoped()
.findOne()
.then(shoe => {
expect(shoe).to.have.property('secretKey').which.is.not.null;
})
);

describe('with include options', () => {

it('should consider scopes and additional included model (object)', () =>
expect(ShoeWithScopes
.scope('full')
.findOne<ShoeWithScopes>({
include: [{
model: Person,
}]
})
.then(shoe => {
expect(shoe).to.have.property('manufacturer').which.is.not.null;
expect(shoe).to.have.property('manufacturer').which.have.property('brand', BRAND);
expect(shoe).to.have.property('owner').which.is.not.null;
})
).not.to.be.rejected
);

it('should consider scopes and additional included model (model)', () =>
expect(ShoeWithScopes
.scope('full')
.findOne({
include: [Person]
})
.then(shoe => {
expect(shoe).to.have.property('manufacturer').which.is.not.null;
expect(shoe).to.have.property('manufacturer').which.have.property('brand', BRAND);
expect(shoe).to.have.property('owner').which.is.not.null;
})
).not.to.be.rejected
);

it('should not consider default scope due to unscoped call, but additonal includes (object)', () =>

ShoeWithScopes
.unscoped()
.findOne({
include: [{model: Person}]
})
.then(shoe => {
expect(shoe).to.have.property('secretKey').which.is.not.null;
expect(shoe).to.have.property('owner').which.is.not.null;
})
);

it('should not consider default scope due to unscoped call, but additonal includes (model)', () =>

ShoeWithScopes
.unscoped()
.findOne({
include: [Person]
})
.then(shoe => {
expect(shoe).to.have.property('secretKey').which.is.not.null;
expect(shoe).to.have.property('owner').which.is.not.null;
})
);

});

});

});