Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

compatibility updates for sequelize >=6.2 #856

Merged
merged 1 commit into from
Dec 7, 2020
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
44 changes: 22 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ With `sequelize-typescript@1` comes a repository mode. See [docs](#repository-mo
import {Table, Column, Model, HasMany} from 'sequelize-typescript';

@Table
class Person extends Model<Person> {
class Person extends Model {

@Column
name: string;
Expand All @@ -128,7 +128,7 @@ from sequelize are valid):
timestamps: true,
...
})
class Person extends Model<Person> {}
class Person extends Model {}
```
#### Table API

Expand Down Expand Up @@ -223,7 +223,7 @@ Design type | Sequelize data type
Get/set accessors do work as well
```typescript
@Table
class Person extends Model<Person> {
class Person extends Model {

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

@Table
export class User extends Model<User> {
export class User extends Model {

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

```TypeScript
export default class User extends Model<User> {}
export default class User extends Model {}
```

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

@Column
name: string;
Expand All @@ -391,7 +391,7 @@ class Player extends Model<Player> {
}

@Table
class Team extends Model<Team> {
class Team extends Model {

@Column
name: string;
Expand All @@ -415,20 +415,20 @@ the players will also be resolved (when passing `include: Player` to the find op
### Many-to-many
```typescript
@Table
class Book extends Model<Book> {
class Book extends Model {
@BelongsToMany(() => Author, () => BookAuthor)
authors: Author[];
}

@Table
class Author extends Model<Author> {
class Author extends Model {

@BelongsToMany(() => Book, () => BookAuthor)
books: Book[];
}

@Table
class BookAuthor extends Model<BookAuthor> {
class BookAuthor extends Model {

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

@ForeignKey(() => Person)
@Column
Expand All @@ -496,7 +496,7 @@ class Book extends Model<Book> {
}

@Table
class Person extends Model<Person> {
class Person extends Model {

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

@HasMany(() => ModelB)
bs: ModelB[];
}

@Table
class ModelB extends Model<ModelB> {
class ModelB extends Model {

@BelongsTo(() => ModelA)
a: ModelA;
Expand All @@ -565,7 +565,7 @@ modelA.$create('bs', /* value */ ).then( /* ... */);
The `@Index` annotation can be used without passing any parameters.
```typescript
@Table
class Person extends Model<Person> {
class Person extends Model {
@Index // Define an index with default name
@Column
name: string;
Expand All @@ -580,7 +580,7 @@ To specify index and index field options, use
an object literal (see [indexes define option](https://sequelize.org/v5/manual/models-definition.html#indexes)):
```typescript
@Table
class Person extends Model<Person> {
class Person extends Model {
@Index('my-index') // Define a multi-field index on name and birthday
@Column
name: string;
Expand Down Expand Up @@ -639,7 +639,7 @@ const JobIndex = createIndexDecorator({
});

@Table
class Person extends Model<Person> {
class Person extends Model {
@SomeIndex // Add name to SomeIndex
@Column
name: string;
Expand Down Expand Up @@ -707,7 +707,7 @@ Nested scopes and includes in general won't work when using `@Scope` annotation
}
}))
@Table
class User extends Model<User> {}
class User extends Model {}
```
> ⚠️ This will change in the future: Simple includes will be implemented.

Expand All @@ -733,7 +733,7 @@ Validator | Annotation
const HEX_REGEX = /^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/;

@Table
export class Shoe extends Model<Shoe> {
export class Shoe extends Model {

@IsUUID(4)
@PrimaryKey
Expand Down Expand Up @@ -801,7 +801,7 @@ sequelize (See sequelize [docs](https://docs.sequelizejs.com/manual/tutorial/sco
}
}))
@Table
export class ShoeWithScopes extends Model<ShoeWithScopes> {
export class ShoeWithScopes extends Model {

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

```typescript
@Table
export class Person extends Model<Person> {
export class Person extends Model {
@Column
name: string;

Expand Down
2 changes: 1 addition & 1 deletion examples/repository-mode/lib/posts/Post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {Model, Table, Column, ForeignKey, BelongsTo} from 'sequelize-typescript'
import {User} from '../users/User';

@Table
export class Post extends Model<Post> {
export class Post extends Model {

@Column text!: string;
@ForeignKey(() => User) @Column userId!: number;
Expand Down
2 changes: 1 addition & 1 deletion examples/repository-mode/lib/users/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {Model, Table, Column, HasMany} from 'sequelize-typescript';
import {Post} from '../posts/Post';

@Table
export class User extends Model<User> {
export class User extends Model {

@Column name!: string;
@HasMany(() => Post) posts: Post[];
Expand Down
2 changes: 1 addition & 1 deletion examples/simple/lib/posts/Post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {Model, Table, Column, ForeignKey, BelongsTo} from 'sequelize-typescript'
import {User} from '../users/User';

@Table
export class Post extends Model<Post> {
export class Post extends Model {

@Column text!: string;
@ForeignKey(() => User) @Column userId!: number;
Expand Down
2 changes: 1 addition & 1 deletion examples/simple/lib/users/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {Model, Table, Column, HasMany} from 'sequelize-typescript';
import {Post} from '../posts/Post';

@Table
export class User extends Model<User> {
export class User extends Model {

@Column name!: string;
@HasMany(() => Post) posts: Post[];
Expand Down
24 changes: 9 additions & 15 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
"nyc": "15.1.0",
"prettyjson": "1.2.1",
"reflect-metadata": "0.1.9",
"sequelize": "6.1.1",
"sequelize": "6.3.5",
"sinon": "1.17.7",
"sinon-chai": "2.8.0",
"source-map-support": "0.4.14",
Expand Down
6 changes: 3 additions & 3 deletions src/hooks/shared/hooks-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import 'reflect-metadata';
import {HookMeta} from "./hook-meta";
import {HookOptions} from "./hook-options";
import {SequelizeHooks} from "sequelize/types/lib/hooks";
import {Model} from "../../model/model/model";
import {ModelCtor} from "../../model/model/model";

const HOOKS_KEY = 'sequelize:hooks';

/**
* Installs hooks on the specified models
*/
export function installHooks(models: Array<typeof Model>): void {
export function installHooks(models: Array<ModelCtor>): void {
models.forEach(model => {
const hooks = getHooks(model);

Expand Down Expand Up @@ -74,7 +74,7 @@ export function addHook(target: any, hookType: keyof SequelizeHooks, methodName:
/**
* Install a hook
*/
function installHook(model: typeof Model, hook: HookMeta): void {
function installHook(model: ModelCtor, hook: HookMeta): void {
if (hook.options && hook.options.name) {
model.addHook(hook.hookType, hook.options.name, model[hook.methodName]);
return;
Expand Down
4 changes: 2 additions & 2 deletions src/model/model/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export type ModelCtor<M extends Model = Model> = (new () => M) & ModelType;

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

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

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

constructor(values?: object, options?: BuildOptions) {
constructor(values?: TCreationAttributes, options?: BuildOptions) {
if (!new.target.isInitialized) {
throw new ModelNotInitializedError(
new.target,
Expand Down
10 changes: 5 additions & 5 deletions src/scopes/scope-service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {FindOptions} from "sequelize";

import {Model} from "../model/model/model";
import {ModelCtor} from "../model/model/model";
import {deepAssign} from "../shared/object";
import {ScopeOptions, ScopeOptionsGetters, ScopesOptions} from "./scope-options";
import {resolveModelGetter} from '../model/shared/model-service';
Expand All @@ -13,7 +13,7 @@ const SCOPES_OPTIONS_KEY = 'sequelize:scopes-options';
/**
* Resolves scopes and adds them to the specified models
*/
export function resolveScopes(models: Array<typeof Model>): void {
export function resolveScopes(models: Array<ModelCtor>): void {
models.forEach(model => {
resolvesDeprecatedScopes(model);
const {getDefaultScope, getScopes} = getScopeOptionsGetters(model.prototype);
Expand All @@ -29,7 +29,7 @@ export function resolveScopes(models: Array<typeof Model>): void {
.forEach(key => resolveScope(key, model, options[key]));
});
}
export const resolveScope = (scopeName: string, model: typeof Model, options: ScopesOptions) => {
export const resolveScope = (scopeName: string, model: ModelCtor, options: ScopesOptions) => {
if (typeof options === 'function') {
const fn = options;
options = (...args: any[]) => inferAlias(fn(...args), model);
Expand Down Expand Up @@ -59,7 +59,7 @@ export const setScopeOptionsGetters = (target: any, options: ScopeOptionsGetters
/**
* @deprecated
*/
export const resolvesDeprecatedScopes = (model: typeof Model) => {
export const resolvesDeprecatedScopes = (model: ModelCtor) => {
const options = getScopeOptions(model.prototype) || {};
Object
.keys(options)
Expand Down Expand Up @@ -89,7 +89,7 @@ export function getScopeOptions(target: any): ScopeOptions | undefined {
/**
* @deprecated
*/
function resolveDeprecatedScope(scopeName: string, model: typeof Model, options: ScopeFindOptions | Function | undefined): void {
function resolveDeprecatedScope(scopeName: string, model: ModelCtor, options: ScopeFindOptions | Function | undefined): void {
if (typeof options === 'function') {
const fn: Function = options;
options = (...args: any[]) => inferAlias(fn(...args), model);
Expand Down
2 changes: 1 addition & 1 deletion test/models/Book.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {Table, Model, Column, HasMany} from "../../src";
import {Page} from "./Page";

@Table
export class Book extends Model<Book> {
export class Book extends Model {

@Column
title: string;
Expand Down
2 changes: 1 addition & 1 deletion test/models/Box.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {Table, Model, Column} from "../../src";

@Table
export class Box extends Model<Box> {
export class Box extends Model {

@Column
length: number;
Expand Down
2 changes: 1 addition & 1 deletion test/models/Hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ import {
* Model used to test hook decorators. Defined hooks are mocked out for testing.
*/
@Table
export class Hook extends Model<Hook> {
export class Hook extends Model {

@Column
name: string;
Expand Down
Loading