Skip to content

Commit 52fbf46

Browse files
Merge branch 'master' into issue-204-scope-typings
2 parents 04edce6 + a8cac9d commit 52fbf46

File tree

4 files changed

+103
-4
lines changed

4 files changed

+103
-4
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Decorators and some other extras for sequelize (v3 + v4).
1111
- [`@Column` API](#column-api)
1212
- [Usage](#usage)
1313
- [Configuration](#configuration)
14+
- [globs](#globs)
1415
- [Model-path resolving](#model-path-resolving)
1516
- [Model association](#model-association)
1617
- [One-to-many](#one-to-many)
@@ -63,6 +64,8 @@ class Person extends Model<Person> {
6364
```
6465
The model needs to extend the `Model` class and has to be annotated with the `@Table` decorator. All properties that
6566
should appear as a column in the database require the `@Column` annotation.
67+
68+
See more advanced example [here](https://github.com/RobinBuschmann/sequelize-typescript-example).
6669

6770
### `@Table`
6871
The `@Table` annotation can be used without passing any parameters. To specify some more define options, use
@@ -209,6 +212,18 @@ sequelize config or add the required models later on by calling `sequelize.addMo
209212
sequelize.addModels([Person]);
210213
sequelize.addModels(['path/to/models']);
211214
```
215+
### globs
216+
```typescript
217+
import {Sequelize} from 'sequelize-typescript';
218+
219+
const sequelize = new Sequelize({
220+
...
221+
modelPaths: [__dirname + '/**/*.model.ts']
222+
});
223+
// or
224+
sequelize.addModels([__dirname + '/**/*.model.ts']);
225+
```
226+
212227
#### Model-path resolving
213228
When using a path to resolve the required models, either the class has to be exported as default or if not exported
214229
as default, the file should have the same name as the corresponding class:

lib/utils/object.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,17 @@ export function deepAssign(target: any, ...sources: any[]): any {
1515
.getOwnPropertyNames(source)
1616
.forEach(key => assign(key, target, source))
1717
;
18+
/* istanbul ignore next */
19+
if (Object.getOwnPropertySymbols) {
20+
Object
21+
.getOwnPropertySymbols(source)
22+
.forEach(key => assign(key, target, source))
23+
;
24+
}
1825
});
19-
2026
return target;
2127

22-
function assign(key: string | number, _target: any, _source: any): void {
28+
function assign(key: string | number | symbol, _target: any, _source: any): void {
2329
const sourceValue = _source[key];
2430

2531
if (sourceValue !== void 0) {

test/specs/scopes.spec.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
11
import {expect, use} from 'chai';
22
import * as chaiAsPromised from 'chai-as-promised';
3+
import {useFakeTimers} from 'sinon';
4+
import {Op} from 'sequelize';
35
import {createSequelize} from "../utils/sequelize";
46
import {getScopeOptions} from "../../lib/services/scopes";
57
import {ShoeWithScopes, SHOE_DEFAULT_SCOPE, SHOE_SCOPES} from "../models/ShoeWithScopes";
68
import {Manufacturer} from "../models/Manufacturer";
79
import {Person} from "../models/Person";
10+
import {Model} from '../../lib/models/Model';
11+
import {Table} from '../../lib/annotations/Table';
12+
import {Scopes} from '../../lib/annotations/Scopes';
13+
import {majorVersion} from '../../lib/utils/versioning';
14+
import {Column} from '../../lib/annotations/Column';
15+
import {UpdatedAt} from '../../lib/annotations/UpdatedAt';
16+
import chaiDatetime = require('chai-datetime');
817

918
use(chaiAsPromised);
19+
use(chaiDatetime);
1020

1121
describe('scopes', () => {
1222

@@ -219,6 +229,55 @@ describe('scopes', () => {
219229

220230
});
221231

232+
if (majorVersion > 3) {
233+
234+
describe('with symbols', () => {
235+
const _sequelize = createSequelize(false);
236+
237+
@Scopes({
238+
bob: {where: {name: {[Op.like]: '%bob%'}}},
239+
updated: {where: {updated: {[Op.gt]: new Date(2000, 1)}}},
240+
})
241+
@Table
242+
class Person extends Model<Person> {
243+
244+
@Column
245+
name: string;
246+
247+
@UpdatedAt
248+
updated: Date;
249+
}
250+
251+
_sequelize.addModels([Person]);
252+
253+
beforeEach(() => _sequelize.sync({force: true}));
254+
255+
it('should consider symbols while finding elements', () => {
256+
return Person
257+
.create({name: '1bob2'})
258+
.then(() => Person.create({name: 'bob'}))
259+
.then(() => Person.create({name: 'bobby'}))
260+
.then(() => Person.create({name: 'robert'}))
261+
.then(() => (Person.scope('bob') as typeof Person).findAll())
262+
.then(persons => expect(persons).to.have.property('length', 3))
263+
;
264+
});
265+
266+
it('should consider symbols on timestamp column while finding elements', () => {
267+
const clock = useFakeTimers(+new Date());
268+
return Person
269+
.create({name: 'test'})
270+
.then(() => (Person.scope('updated') as typeof Person).findAll())
271+
.then(() => Person.findAll())
272+
.then(persons => expect(persons).to.have.property('length', 1))
273+
.then(() => clock.restore())
274+
;
275+
});
276+
277+
});
278+
}
279+
280+
222281
});
223282

224283
});

test/specs/utils/object.spec.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,14 @@ describe('utils', () => {
1313
const childSourceA = {f: childSourceF};
1414
const childSourceB = {};
1515
const source1 = {a: childSourceA, b: childSourceB, c: 1, d: 'd', over: 'ride', regex: /reg/gim, notNull: null};
16-
const source2 = {e: 'für elisa', g: () => null, arr: [{h: 1}, {}, 'e'], over: 'ridden', nullable: null, notNull: 'notNull'};
16+
const source2 = {
17+
e: 'für elisa',
18+
g: () => null,
19+
arr: [{h: 1}, {}, 'e'],
20+
over: 'ridden',
21+
nullable: null,
22+
notNull: 'notNull'
23+
};
1724
const sourceKeys = [].concat(Object.keys(source1), Object.keys(source2));
1825

1926
it('should not be undefined', () => {
@@ -110,15 +117,27 @@ describe('utils', () => {
110117

111118
it('should keep prototype chain', () => {
112119
class Test {
113-
protoFn(): any {}
120+
protoFn(): any {
121+
}
114122
}
123+
115124
const copy = deepAssign({}, {test: new Test()});
116125

117126
expect(copy.test)
118127
.to.have.property('protoFn')
119128
.that.is.a('function');
120129
});
121130

131+
if (Object.getOwnPropertySymbols) {
132+
it('should copy symbol based objects', () => {
133+
const symbol = Symbol('test');
134+
const value = 'test';
135+
const copy = deepAssign({}, {[symbol]: value});
136+
137+
expect(copy[symbol]).to.equal(value);
138+
});
139+
}
140+
122141
});
123142

124143
});

0 commit comments

Comments
 (0)