Skip to content

Commit dff0039

Browse files
Robin BuschmannRobin Buschmann
authored andcommitted
added modelName to DefineOptions; added minfication hint in readme #7
1 parent 773e1d1 commit dff0039

File tree

7 files changed

+27
-17
lines changed

7 files changed

+27
-17
lines changed

README.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Decorators and some other extras for sequelize (v3 + v4).
2222
- [Model valiation](#model-validation)
2323
- [Scopes](#scopes)
2424
- [Why `() => Model`?](#user-content-why---model)
25-
- [Limitations and recommendations](#limitations-and-recommendations)
25+
- [Recommendations and limitations](#recommendations-and-limitations)
2626

2727
### Installation
2828
*sequelize-typescript* requires [sequelize](https://github.com/sequelize/sequelize)
@@ -552,8 +552,8 @@ comes to circular-dependencies (which are in general solved by node for you) `Mo
552552
when it gets passed to @ForeignKey. With the usage of a function, which returns the actual model, we prevent
553553
this issue.
554554

555-
## Limitations and recommendations
556-
### One connection per model
555+
## Recommendations and limitations
556+
### One Sequelize instance per model
557557
You cannot add one and the same model to multiple Sequelize instances with
558558
differently configured connections. So that one model will only work for one connection.
559559
### One model class per file
@@ -562,3 +562,9 @@ of execution. Since typescript creates a `__metadata("design:type", SomeModel)`
562562
compile option, in some cases `SomeModel` is probably not defined(not undefined!) and would throw a `ReferenceError`.
563563
When putting `SomeModel` in a separate file, it would look like `__metadata("design:type", SomeModel_1.SomeModel)`,
564564
which does not throw an error.
565+
### Minification
566+
If you need to minify your code, you need to set `tableName` and `modelName`
567+
in the `DefineOptions` for `@Table` annotation. sequelize-typescript
568+
uses the class name as default name for `tableName` and `modelName`.
569+
When the code is minified the class name will no longer be the originally
570+
defined one (So that `class User` will become `class b` for example).

lib/annotations/Table.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import 'reflect-metadata';
22
import {setModelName, addOptions} from "../services/models";
3-
import {DefineOptions} from "sequelize";
3+
import {IDefineOptions} from "../interfaces/IDefineOptions";
44

5-
export function Table(options: DefineOptions<any>): Function;
5+
export function Table(options: IDefineOptions): Function;
66
export function Table(target: any): void;
77
export function Table(arg: any): void|Function {
88

@@ -12,19 +12,19 @@ export function Table(arg: any): void|Function {
1212
annotate(target);
1313
} else {
1414

15-
const options: DefineOptions<any> = Object.assign({}, arg);
15+
const options: IDefineOptions = Object.assign({}, arg);
1616

1717
return (target: any) => annotate(target, options);
1818
}
1919
}
2020

21-
function annotate(target: any, options: DefineOptions<any> = {}): void {
21+
function annotate(target: any, options: IDefineOptions = {}): void {
2222

2323
if (!options.tableName) options.tableName = target.name;
2424

2525
options.instanceMethods = target.prototype;
2626
options.classMethods = target;
2727

28-
setModelName(target.prototype, options['modelName'] || target.name);
28+
setModelName(target.prototype, options.modelName || target.name);
2929
addOptions(target.prototype, options);
3030
}

lib/interfaces/IDefineOptions.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import {DefineOptions} from "sequelize";
2+
3+
export interface IDefineOptions extends DefineOptions<any> {
4+
modelName?: string;
5+
}

lib/models/v3/Sequelize.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import 'reflect-metadata';
22
import * as SequelizeOrigin from 'sequelize';
3-
import {DefineOptions} from 'sequelize';
43
import {Model} from "../Model";
54
import {ISequelizeConfig} from "../../interfaces/ISequelizeConfig";
65
import {getModelName, getAttributes, getOptions} from "../../services/models";
@@ -39,7 +38,7 @@ export class Sequelize extends SequelizeOrigin implements BaseSequelize {
3938
getThroughModel(through: string): typeof Model {
4039

4140
// tslint:disable:max-classes-per-file
42-
@Table({tableName: through, modelName: through} as DefineOptions<any>)
41+
@Table({tableName: through, modelName: through})
4342
class Through extends Model<Through> {
4443
}
4544

lib/models/v4/Sequelize.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import 'reflect-metadata';
22
import * as OriginSequelize from 'sequelize';
3-
import {DefineOptions} from 'sequelize';
43
import {Model} from "../Model";
54
import {ISequelizeConfig} from "../../interfaces/ISequelizeConfig";
65
import {getModelName, getAttributes, getOptions} from "../../services/models";
@@ -35,7 +34,7 @@ export class Sequelize extends OriginSequelize implements BaseSequelize {
3534
getThroughModel(through: string): typeof Model {
3635

3736
// tslint:disable:max-classes-per-file
38-
@Table({tableName: through, modelName: through} as DefineOptions<any>)
37+
@Table({tableName: through, modelName: through})
3938
class Through extends Model<Through> {
4039
}
4140

test/specs/instance.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import {useFakeTimers} from 'sinon';
33
import * as chaiAsPromised from 'chai-as-promised';
44
import * as validateUUID from 'uuid-validate';
55
import {createSequelize} from "../utils/sequelize";
6-
import {DefineOptions} from "sequelize";
76
import {
87
Sequelize, Model, Table, Column, AllowNull, PrimaryKey,
98
ForeignKey, HasMany, BelongsTo, HasOne, DataType, Default
@@ -22,6 +21,7 @@ import {UserWithNoAutoIncrementation} from "../models/UserWithNoAutoIncrementati
2221
import {UserWithCustomUpdatedAt} from "../models/UserWithCustomUpdatedAt";
2322
import {UserWithCreatedAtButWithoutUpdatedAt} from "../models/UserWithCreatedAtButWithoutUpdatedAt";
2423
import chaiDatetime = require('chai-datetime');
24+
import {IDefineOptions} from "../../lib/interfaces/IDefineOptions";
2525
// import {UserWithSwag} from "../models/UserWithSwag";
2626

2727
// TODO@robin create belongs to many with through options "add" test
@@ -2103,7 +2103,7 @@ describe('instance', () => {
21032103

21042104
it('returns null for null, undefined, and unset boolean values', () => {
21052105

2106-
@Table({logging: true} as DefineOptions<any>)
2106+
@Table({logging: true} as IDefineOptions)
21072107
class Setting extends Model<Setting> {
21082108

21092109
@Column
@@ -2212,7 +2212,7 @@ describe('instance', () => {
22122212

22132213
it('returns all values', () => {
22142214

2215-
@Table({logging: false} as DefineOptions<any>)
2215+
@Table({logging: false} as IDefineOptions)
22162216
class UserHelper extends Model<UserHelper> {
22172217

22182218
@Column

test/specs/model.spec.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import * as sinonChai from 'sinon-chai';
66
import * as _ from 'lodash';
77
import * as moment from 'moment';
88
import * as Promise from 'bluebird';
9-
import {UniqueConstraintError, DefineOptions} from 'sequelize';
9+
import {UniqueConstraintError} from 'sequelize';
1010
import * as chaiAsPromised from 'chai-as-promised';
1111
import {createSequelize} from "../utils/sequelize";
1212
import {
@@ -16,6 +16,7 @@ import {
1616
BelongsToMany
1717
} from "../../index";
1818
import chaiDatetime = require('chai-datetime');
19+
import {IDefineOptions} from "../../lib/interfaces/IDefineOptions";
1920

2021
use(sinonChai);
2122
use(chaiAsPromised);
@@ -494,7 +495,7 @@ describe('model', () => {
494495
fields: ['fieldD']
495496
}],
496497
engine: 'MyISAM'
497-
} as DefineOptions<any>)
498+
} as IDefineOptions)
498499
class ModelA extends Model<ModelA> {
499500
@Column
500501
fieldA: string;

0 commit comments

Comments
 (0)