Skip to content

Commit a06cc3e

Browse files
key access to added models implemented #17 ; code coverage increased
1 parent ed0ca42 commit a06cc3e

File tree

9 files changed

+39
-8
lines changed

9 files changed

+39
-8
lines changed

example/models/Post.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {Comment} from "./Comment";
66
import {PostTopic} from "./PostTopic";
77
import {Topic} from "./Topic";
88
import {Author} from "./Author";
9-
import {Length} from "../../lib/annotations/validation/Length";
109

1110
@Scopes({
1211
full: {
@@ -48,5 +47,3 @@ export class Post extends Model<Post> {
4847
author?: Author;
4948

5049
}
51-
52-
Post.beforeCreate(post => {});

lib/models/BaseSequelize.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {getForeignKey} from "../services/association";
1616
export abstract class BaseSequelize {
1717

1818
thoughMap: {[through: string]: any} = {};
19+
_: {[modelName: string]: (typeof Model)} = {};
1920

2021
static extend(target: any): void {
2122

@@ -67,6 +68,7 @@ export abstract class BaseSequelize {
6768
models.forEach(model => model.isInitialized = true);
6869
this.associateModels(models);
6970
resolveScopes(models);
71+
models.forEach(model => this._[model.name] = model);
7072
}
7173

7274
init(config: ISequelizeConfig): void {

lib/models/Sequelize.d.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
/// <reference types="sequelize" />
22
import 'reflect-metadata';
33
import * as SequelizeOrigin from 'sequelize';
4-
import { Model } from "./Model";
5-
import { ISequelizeConfig } from "../interfaces/ISequelizeConfig";
4+
import {Model} from "./Model";
5+
import {ISequelizeConfig} from "../interfaces/ISequelizeConfig";
66
import {ISequelizeValidationOnlyConfig} from "../interfaces/ISequelizeValidationOnlyConfig";
77

88
export declare class Sequelize extends SequelizeOrigin {
99

10-
constructor(config: ISequelizeConfig|ISequelizeValidationOnlyConfig);
10+
_: {[modelName: string]: (typeof Model)};
1111

12-
addModels(models: Array<typeof Model>): void;
13-
addModels(modelPaths: string[]): void;
12+
constructor(config: ISequelizeConfig | ISequelizeValidationOnlyConfig);
13+
14+
addModels(models: Array<typeof Model>): void;
15+
addModels(modelPaths: string[]): void;
1416
}

lib/models/v3/Sequelize.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export class Sequelize extends SequelizeOrigin implements BaseSequelize {
1515
Model: any = Function;
1616

1717
thoughMap: {[through: string]: any} = {};
18+
_: {[modelName: string]: typeof Model} = {};
1819
init: (config: ISequelizeConfig) => void;
1920
addModels: (models: Array<typeof Model>|string[]) => void;
2021
associateModels: (models: Array<typeof Model>) => void;

lib/models/v4/Sequelize.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ let preparedConfig;
1111
export class Sequelize extends OriginSequelize implements BaseSequelize {
1212

1313
thoughMap: {[through: string]: any} = {};
14+
_: {[modelName: string]: typeof Model} = {};
1415
init: (config: ISequelizeConfig) => void;
1516
addModels: (models: Array<typeof Model>|string[]) => void;
1617
associateModels: (models: Array<typeof Model>) => void;

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
"sinon-chai": "2.8.0",
6060
"source-map-support": "0.4.14",
6161
"sqlite3": "3.1.8",
62+
"ts-node": "3.0.4",
6263
"tslint": "4.3.1",
6364
"typescript": "2.2.1",
6465
"uuid-validate": "0.0.2"

test/models/ShoeWithValidation.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ export class ShoeWithValidation extends Model<ShoeWithValidation> {
5151
brandUrl: string;
5252

5353
@Is('HexColor', hexColor)
54+
@Length({min: 4, msg: 'too short'})
5455
@Column
5556
primaryColor: string;
5657

test/specs/models/sequelize.spec.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,15 @@ describe('sequelize', () => {
6060

6161
});
6262

63+
describe('model', () => {
64+
65+
it('should make class references of loaded models available', () => {
66+
67+
sequelize.addModels([__dirname + '/../../models/exports/']);
68+
69+
expect(sequelize._).to.have.property('Game', Game);
70+
expect(sequelize._).to.have.property('Gamer', Gamer);
71+
});
72+
73+
});
6374
});

test/specs/validation.spec.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
hexColor, HEX_REGEX, PRODUCED_AT_IS_AFTER, PRODUCED_AT_IS_BEFORE, UUID_VERSION, MAX, MIN, NOT, IS_IN, NOT_CONTAINS
88
} from "../models/ShoeWithValidation";
99
import {majorVersion} from "../../lib/utils/versioning";
10+
import {Is} from "../../lib/annotations/validation/Is";
1011

1112
use(chaiAsPromised);
1213

@@ -184,6 +185,20 @@ describe('validation', () => {
184185

185186
});
186187

188+
describe('decorators', () => {
189+
190+
describe('Is', () => {
191+
192+
it('Should throw due to missing name of function', () => {
193+
194+
expect(() => Is(() => null)).to.throw(/Passed validator function must have a name/);
195+
196+
});
197+
198+
});
199+
200+
});
201+
187202
describe('only', () => {
188203

189204
it('should not throw', () => {

0 commit comments

Comments
 (0)