Skip to content

Commit 05a467e

Browse files
Merge branch 'master' of github.com:RobinBuschmann/sequelize-typescript
2 parents 4684da7 + 7e77fc1 commit 05a467e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+239
-245
lines changed

README.md

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ With `sequelize-typescript@1` comes a repository mode. See [docs](#repository-mo
102102
import {Table, Column, Model, HasMany} from 'sequelize-typescript';
103103

104104
@Table
105-
class Person extends Model<Person> {
105+
class Person extends Model {
106106

107107
@Column
108108
name: string;
@@ -128,7 +128,7 @@ from sequelize are valid):
128128
timestamps: true,
129129
...
130130
})
131-
class Person extends Model<Person> {}
131+
class Person extends Model {}
132132
```
133133
#### Table API
134134

@@ -223,7 +223,7 @@ Design type | Sequelize data type
223223
Get/set accessors do work as well
224224
```typescript
225225
@Table
226-
class Person extends Model<Person> {
226+
class Person extends Model {
227227

228228
@Column
229229
get name(): string {
@@ -277,7 +277,7 @@ sequelize.addModels([__dirname + '/**/*.model.ts']);
277277
A model is matched to a file by its filename. E.g.
278278
```typescript
279279
// File User.ts matches the following exported model.
280-
export class User extends Model<User> {}
280+
export class User extends Model {}
281281
```
282282
This is done by comparison of the filename against all exported members. The
283283
matching can be customized by specifying the `modelMatch` function in the
@@ -308,7 +308,7 @@ export const UserN = 'Not a model';
308308
export const NUser = 'Not a model';
309309

310310
@Table
311-
export class User extends Model<User> {
311+
export class User extends Model {
312312

313313
@Column
314314
nickname: string;
@@ -325,7 +325,7 @@ user.model User -> true (User will be added as model)
325325
Another way to match model to file is to make your model the default export.
326326

327327
```TypeScript
328-
export default class User extends Model<User> {}
328+
export default class User extends Model {}
329329
```
330330

331331
> ⚠️ When using paths to add models, keep in mind that they will be loaded during runtime. This means that the path
@@ -374,7 +374,7 @@ and `@ForeignKey` annotations.
374374
### One-to-many
375375
```typescript
376376
@Table
377-
class Player extends Model<Player> {
377+
class Player extends Model {
378378

379379
@Column
380380
name: string;
@@ -391,7 +391,7 @@ class Player extends Model<Player> {
391391
}
392392

393393
@Table
394-
class Team extends Model<Team> {
394+
class Team extends Model {
395395

396396
@Column
397397
name: string;
@@ -415,20 +415,20 @@ the players will also be resolved (when passing `include: Player` to the find op
415415
### Many-to-many
416416
```typescript
417417
@Table
418-
class Book extends Model<Book> {
418+
class Book extends Model {
419419
@BelongsToMany(() => Author, () => BookAuthor)
420420
authors: Author[];
421421
}
422422

423423
@Table
424-
class Author extends Model<Author> {
424+
class Author extends Model {
425425

426426
@BelongsToMany(() => Book, () => BookAuthor)
427427
books: Book[];
428428
}
429429

430430
@Table
431-
class BookAuthor extends Model<BookAuthor> {
431+
class BookAuthor extends Model {
432432

433433
@ForeignKey(() => Book)
434434
@Column
@@ -478,7 +478,7 @@ Note that when using AssociationOptions, certain properties will be overwritten
478478
So if you define a model with multiple relations like
479479
```typescript
480480
@Table
481-
class Book extends Model<Book> {
481+
class Book extends Model {
482482

483483
@ForeignKey(() => Person)
484484
@Column
@@ -496,7 +496,7 @@ class Book extends Model<Book> {
496496
}
497497

498498
@Table
499-
class Person extends Model<Person> {
499+
class Person extends Model {
500500

501501
@HasMany(() => Book)
502502
writtenBooks: Book[];
@@ -533,14 +533,14 @@ But TypeScript wont recognize them and will complain if you try to access `getMo
533533
functions.
534534
```typescript
535535
@Table
536-
class ModelA extends Model<ModelA> {
536+
class ModelA extends Model {
537537

538538
@HasMany(() => ModelB)
539539
bs: ModelB[];
540540
}
541541

542542
@Table
543-
class ModelB extends Model<ModelB> {
543+
class ModelB extends Model {
544544

545545
@BelongsTo(() => ModelA)
546546
a: ModelA;
@@ -565,7 +565,7 @@ modelA.$create('bs', /* value */ ).then( /* ... */);
565565
The `@Index` annotation can be used without passing any parameters.
566566
```typescript
567567
@Table
568-
class Person extends Model<Person> {
568+
class Person extends Model {
569569
@Index // Define an index with default name
570570
@Column
571571
name: string;
@@ -580,7 +580,7 @@ To specify index and index field options, use
580580
an object literal (see [indexes define option](https://sequelize.org/v5/manual/models-definition.html#indexes)):
581581
```typescript
582582
@Table
583-
class Person extends Model<Person> {
583+
class Person extends Model {
584584
@Index('my-index') // Define a multi-field index on name and birthday
585585
@Column
586586
name: string;
@@ -639,7 +639,7 @@ const JobIndex = createIndexDecorator({
639639
});
640640

641641
@Table
642-
class Person extends Model<Person> {
642+
class Person extends Model {
643643
@SomeIndex // Add name to SomeIndex
644644
@Column
645645
name: string;
@@ -707,7 +707,7 @@ Nested scopes and includes in general won't work when using `@Scope` annotation
707707
}
708708
}))
709709
@Table
710-
class User extends Model<User> {}
710+
class User extends Model {}
711711
```
712712
> ⚠️ This will change in the future: Simple includes will be implemented.
713713
@@ -733,7 +733,7 @@ Validator | Annotation
733733
const HEX_REGEX = /^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/;
734734

735735
@Table
736-
export class Shoe extends Model<Shoe> {
736+
export class Shoe extends Model {
737737

738738
@IsUUID(4)
739739
@PrimaryKey
@@ -801,7 +801,7 @@ sequelize (See sequelize [docs](https://docs.sequelizejs.com/manual/tutorial/sco
801801
}
802802
}))
803803
@Table
804-
export class ShoeWithScopes extends Model<ShoeWithScopes> {
804+
export class ShoeWithScopes extends Model {
805805

806806
@Column
807807
readonly secretKey: string;
@@ -833,7 +833,7 @@ The name of the method cannot be the same as the name of the hook (for example,
833833

834834
```typescript
835835
@Table
836-
export class Person extends Model<Person> {
836+
export class Person extends Model {
837837
@Column
838838
name: string;
839839

examples/repository-mode/lib/posts/Post.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {Model, Table, Column, ForeignKey, BelongsTo} from 'sequelize-typescript'
33
import {User} from '../users/User';
44

55
@Table
6-
export class Post extends Model<Post> {
6+
export class Post extends Model {
77

88
@Column text!: string;
99
@ForeignKey(() => User) @Column userId!: number;

examples/repository-mode/lib/users/User.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {Model, Table, Column, HasMany} from 'sequelize-typescript';
33
import {Post} from '../posts/Post';
44

55
@Table
6-
export class User extends Model<User> {
6+
export class User extends Model {
77

88
@Column name!: string;
99
@HasMany(() => Post) posts: Post[];

examples/simple/lib/posts/Post.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {Model, Table, Column, ForeignKey, BelongsTo} from 'sequelize-typescript'
33
import {User} from '../users/User';
44

55
@Table
6-
export class Post extends Model<Post> {
6+
export class Post extends Model {
77

88
@Column text!: string;
99
@ForeignKey(() => User) @Column userId!: number;

examples/simple/lib/users/User.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {Model, Table, Column, HasMany} from 'sequelize-typescript';
33
import {Post} from '../posts/Post';
44

55
@Table
6-
export class User extends Model<User> {
6+
export class User extends Model {
77

88
@Column name!: string;
99
@HasMany(() => Post) posts: Post[];

package-lock.json

Lines changed: 9 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
"nyc": "15.1.0",
6161
"prettyjson": "1.2.1",
6262
"reflect-metadata": "0.1.9",
63-
"sequelize": "6.1.1",
63+
"sequelize": "6.3.5",
6464
"sinon": "1.17.7",
6565
"sinon-chai": "2.8.0",
6666
"source-map-support": "0.4.14",

src/hooks/shared/hooks-service.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ import 'reflect-metadata';
22
import {HookMeta} from "./hook-meta";
33
import {HookOptions} from "./hook-options";
44
import {SequelizeHooks} from "sequelize/types/lib/hooks";
5-
import {Model} from "../../model/model/model";
5+
import {ModelCtor} from "../../model/model/model";
66

77
const HOOKS_KEY = 'sequelize:hooks';
88

99
/**
1010
* Installs hooks on the specified models
1111
*/
12-
export function installHooks(models: Array<typeof Model>): void {
12+
export function installHooks(models: Array<ModelCtor>): void {
1313
models.forEach(model => {
1414
const hooks = getHooks(model);
1515

@@ -74,7 +74,7 @@ export function addHook(target: any, hookType: keyof SequelizeHooks, methodName:
7474
/**
7575
* Install a hook
7676
*/
77-
function installHook(model: typeof Model, hook: HookMeta): void {
77+
function installHook(model: ModelCtor, hook: HookMeta): void {
7878
if (hook.options && hook.options.name) {
7979
model.addHook(hook.hookType, hook.options.name, model[hook.methodName]);
8080
return;

src/model/model/model.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export type ModelCtor<M extends Model = Model> = (new () => M) & ModelType;
1313

1414
export type $GetType<T> = NonNullable<T> extends any[] ? NonNullable<T> : (NonNullable<T> | null);
1515

16-
export abstract class Model<T = any, T2 = any> extends OriginModel<T, T2> {
16+
export abstract class Model<TModelAttributes extends {} = any, TCreationAttributes extends {} = TModelAttributes> extends OriginModel<TModelAttributes, TCreationAttributes> {
1717

1818
// TODO Consider moving the following props to OriginModel
1919
id?: number | any;
@@ -30,7 +30,7 @@ export abstract class Model<T = any, T2 = any> extends OriginModel<T, T2> {
3030
return super.init(attributes, options);
3131
}
3232

33-
constructor(values?: object, options?: BuildOptions) {
33+
constructor(values?: TCreationAttributes, options?: BuildOptions) {
3434
if (!new.target.isInitialized) {
3535
throw new ModelNotInitializedError(
3636
new.target,

src/scopes/scope-service.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {FindOptions} from "sequelize";
22

3-
import {Model} from "../model/model/model";
3+
import {ModelCtor} from "../model/model/model";
44
import {deepAssign} from "../shared/object";
55
import {ScopeOptions, ScopeOptionsGetters, ScopesOptions} from "./scope-options";
66
import {resolveModelGetter} from '../model/shared/model-service';
@@ -13,7 +13,7 @@ const SCOPES_OPTIONS_KEY = 'sequelize:scopes-options';
1313
/**
1414
* Resolves scopes and adds them to the specified models
1515
*/
16-
export function resolveScopes(models: Array<typeof Model>): void {
16+
export function resolveScopes(models: Array<ModelCtor>): void {
1717
models.forEach(model => {
1818
resolvesDeprecatedScopes(model);
1919
const {getDefaultScope, getScopes} = getScopeOptionsGetters(model.prototype);
@@ -29,7 +29,7 @@ export function resolveScopes(models: Array<typeof Model>): void {
2929
.forEach(key => resolveScope(key, model, options[key]));
3030
});
3131
}
32-
export const resolveScope = (scopeName: string, model: typeof Model, options: ScopesOptions) => {
32+
export const resolveScope = (scopeName: string, model: ModelCtor, options: ScopesOptions) => {
3333
if (typeof options === 'function') {
3434
const fn = options;
3535
options = (...args: any[]) => inferAlias(fn(...args), model);
@@ -59,7 +59,7 @@ export const setScopeOptionsGetters = (target: any, options: ScopeOptionsGetters
5959
/**
6060
* @deprecated
6161
*/
62-
export const resolvesDeprecatedScopes = (model: typeof Model) => {
62+
export const resolvesDeprecatedScopes = (model: ModelCtor) => {
6363
const options = getScopeOptions(model.prototype) || {};
6464
Object
6565
.keys(options)
@@ -89,7 +89,7 @@ export function getScopeOptions(target: any): ScopeOptions | undefined {
8989
/**
9090
* @deprecated
9191
*/
92-
function resolveDeprecatedScope(scopeName: string, model: typeof Model, options: ScopeFindOptions | Function | undefined): void {
92+
function resolveDeprecatedScope(scopeName: string, model: ModelCtor, options: ScopeFindOptions | Function | undefined): void {
9393
if (typeof options === 'function') {
9494
const fn: Function = options;
9595
options = (...args: any[]) => inferAlias(fn(...args), model);

test/models/Book.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {Table, Model, Column, HasMany} from "../../src";
22
import {Page} from "./Page";
33

44
@Table
5-
export class Book extends Model<Book> {
5+
export class Book extends Model {
66

77
@Column
88
title: string;

test/models/Box.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {Table, Model, Column} from "../../src";
22

33
@Table
4-
export class Box extends Model<Box> {
4+
export class Box extends Model {
55

66
@Column
77
length: number;

test/models/Hook.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ import {
4444
* Model used to test hook decorators. Defined hooks are mocked out for testing.
4545
*/
4646
@Table
47-
export class Hook extends Model<Hook> {
47+
export class Hook extends Model {
4848

4949
@Column
5050
name: string;

0 commit comments

Comments
 (0)