diff --git a/src/associations/belongs-to-many/belongs-to-many-association.ts b/src/associations/belongs-to-many/belongs-to-many-association.ts index 8924c367..9dfacb5a 100644 --- a/src/associations/belongs-to-many/belongs-to-many-association.ts +++ b/src/associations/belongs-to-many/belongs-to-many-association.ts @@ -1,5 +1,3 @@ -import {BelongsToManyOptions as OriginBelongsToManyOptions, Model, ThroughOptions} from "sequelize"; - import {BaseAssociation} from '../shared/base-association'; import {BelongsToManyOptions} from './belongs-to-many-options'; import {ModelNotInitializedError} from '../../model/shared/model-not-initialized-error'; @@ -8,11 +6,13 @@ import {ModelClassGetter} from "../../model/shared/model-class-getter"; import {Association} from "../shared/association"; import {Sequelize} from "../../sequelize/sequelize/sequelize"; import {UnionAssociationOptions} from "../shared/union-association-options"; +import {ModelType} from "../../model/model/model"; +import {ThroughOptions} from "../through/through-options"; -export class BelongsToManyAssociation extends BaseAssociation { +export class BelongsToManyAssociation extends BaseAssociation { - constructor(associatedClassGetter: ModelClassGetter, - protected options: BelongsToManyOptions) { + constructor(associatedClassGetter: ModelClassGetter, + protected options: BelongsToManyOptions) { super(associatedClassGetter, options); } @@ -20,24 +20,24 @@ export class BelongsToManyAssociation extends BaseAssociation { return Association.BelongsToMany; } - getSequelizeOptions(model: typeof Model, + getSequelizeOptions(model: ModelType, sequelize: Sequelize): UnionAssociationOptions { - const options: OriginBelongsToManyOptions = {...this.options as any}; + const options: BelongsToManyOptions = {...this.options}; const associatedClass = this.getAssociatedClass(); const throughOptions = this.getThroughOptions(sequelize); const throughModel = typeof throughOptions === 'object' && typeof throughOptions.model !== "string" ? throughOptions.model : undefined; options.through = throughOptions; - options.foreignKey = getForeignKeyOptions(model, throughModel, this.options.foreignKey); - options.otherKey = getForeignKeyOptions(associatedClass, throughModel, this.options.otherKey); + options.foreignKey = getForeignKeyOptions(model, throughModel as any, this.options.foreignKey); + options.otherKey = getForeignKeyOptions(associatedClass, throughModel as any, this.options.otherKey); return options; } - private getThroughOptions(sequelize: Sequelize): ThroughOptions | string { + private getThroughOptions(sequelize: Sequelize): ThroughOptions | string { const through = this.options.through; const throughModel = typeof through === 'object' ? through.model : through; - const throughOptions: ThroughOptions = + const throughOptions: ThroughOptions = typeof through === 'object' ? {...through} : {} as any; if (typeof throughModel === 'function') { @@ -45,7 +45,7 @@ export class BelongsToManyAssociation extends BaseAssociation { if (!throughModelClass.isInitialized) { throw new ModelNotInitializedError(throughModelClass, 'Association cannot be resolved.'); } - throughOptions.model = throughModelClass; + throughOptions.model = throughModelClass as any; } else { return throughModel; } diff --git a/src/associations/belongs-to-many/belongs-to-many-options.ts b/src/associations/belongs-to-many/belongs-to-many-options.ts index 248e4930..cdecb506 100644 --- a/src/associations/belongs-to-many/belongs-to-many-options.ts +++ b/src/associations/belongs-to-many/belongs-to-many-options.ts @@ -3,8 +3,8 @@ import {ModelClassGetter} from "../../model/shared/model-class-getter"; import {ThroughOptions} from "../through/through-options"; -export type BelongsToManyOptions = { +export type BelongsToManyOptions = { [K in keyof OriginBelongsToManyOptions]: K extends 'through' - ? ModelClassGetter | string | ThroughOptions + ? ModelClassGetter | string | ThroughOptions : OriginBelongsToManyOptions[K] }; diff --git a/src/associations/belongs-to-many/belongs-to-many.ts b/src/associations/belongs-to-many/belongs-to-many.ts index 0323cf33..a9f826e9 100644 --- a/src/associations/belongs-to-many/belongs-to-many.ts +++ b/src/associations/belongs-to-many/belongs-to-many.ts @@ -3,19 +3,20 @@ import {BelongsToManyAssociation} from './belongs-to-many-association'; import {ModelClassGetter} from "../../model/shared/model-class-getter"; import {addAssociation} from "../shared/association-service"; -export function BelongsToMany(associatedClassGetter: ModelClassGetter, - through: ModelClassGetter | string, - foreignKey?: string, - otherKey?: string): Function; -export function BelongsToMany(associatedClassGetter: ModelClassGetter, - options: BelongsToManyOptions): Function; -export function BelongsToMany(associatedClassGetter: ModelClassGetter, - throughOrOptions: ModelClassGetter | string | BelongsToManyOptions, - foreignKey?: string, - otherKey?: string): Function { +export function BelongsToMany< + TCreationAttributes, TModelAttributes, TCreationAttributesThrough, TModelAttributesThrough>(associatedClassGetter: ModelClassGetter, + through: ModelClassGetter | string, + foreignKey?: string, + otherKey?: string): Function; +export function BelongsToMany(associatedClassGetter: ModelClassGetter, + options: BelongsToManyOptions): Function; +export function BelongsToMany(associatedClassGetter: ModelClassGetter, + throughOrOptions: ModelClassGetter | string | BelongsToManyOptions, + foreignKey?: string, + otherKey?: string): Function { return (target: any, propertyName: string) => { - let options: Partial = {foreignKey, otherKey}; + let options: Partial> = {foreignKey, otherKey}; if (typeof throughOrOptions === 'string' || typeof throughOrOptions === 'function') { @@ -28,7 +29,7 @@ export function BelongsToMany(associatedClassGetter: ModelClassGetter, addAssociation(target, new BelongsToManyAssociation( associatedClassGetter, - options as BelongsToManyOptions, + options as BelongsToManyOptions, ) ); }; diff --git a/src/associations/belongs-to/belongs-to-association.ts b/src/associations/belongs-to/belongs-to-association.ts index 73212441..b26035e1 100644 --- a/src/associations/belongs-to/belongs-to-association.ts +++ b/src/associations/belongs-to/belongs-to-association.ts @@ -4,12 +4,12 @@ import {BaseAssociation} from '../shared/base-association'; import {getForeignKeyOptions} from "../foreign-key/foreign-key-service"; import {ModelClassGetter} from "../../model/shared/model-class-getter"; import {Association} from "../shared/association"; -import {Model} from "../../model/model/model"; +import {ModelType} from "../../model/model/model"; import {UnionAssociationOptions} from "../shared/union-association-options"; -export class BelongsToAssociation extends BaseAssociation { +export class BelongsToAssociation extends BaseAssociation { - constructor(associatedClassGetter: ModelClassGetter, + constructor(associatedClassGetter: ModelClassGetter, protected options: BelongsToOptions) { super(associatedClassGetter, options); } @@ -18,7 +18,7 @@ export class BelongsToAssociation extends BaseAssociation { return Association.BelongsTo; } - getSequelizeOptions(model: typeof Model): UnionAssociationOptions { + getSequelizeOptions(model: ModelType): UnionAssociationOptions { const associatedClass = this.getAssociatedClass(); const foreignKey = getForeignKeyOptions(associatedClass, model, this.options.foreignKey); diff --git a/src/associations/belongs-to/belongs-to.ts b/src/associations/belongs-to/belongs-to.ts index a09a391a..5ac50971 100644 --- a/src/associations/belongs-to/belongs-to.ts +++ b/src/associations/belongs-to/belongs-to.ts @@ -4,11 +4,11 @@ import {BelongsToAssociation} from './belongs-to-association'; import {ModelClassGetter} from "../../model/shared/model-class-getter"; import {addAssociation, getPreparedAssociationOptions} from "../shared/association-service"; -export function BelongsTo(associatedClassGetter: ModelClassGetter, foreignKey?: string): Function; +export function BelongsTo(associatedClassGetter: ModelClassGetter, foreignKey?: string): Function; -export function BelongsTo(associatedClassGetter: ModelClassGetter, options?: BelongsToOptions): Function; +export function BelongsTo(associatedClassGetter: ModelClassGetter, options?: BelongsToOptions): Function; -export function BelongsTo(associatedClassGetter: ModelClassGetter, optionsOrForeignKey?: string | BelongsToOptions): Function { +export function BelongsTo(associatedClassGetter: ModelClassGetter, optionsOrForeignKey?: string | BelongsToOptions): Function { return (target: any, propertyName: string) => { const options: BelongsToOptions = getPreparedAssociationOptions(optionsOrForeignKey); diff --git a/src/associations/foreign-key/foreign-key-meta.ts b/src/associations/foreign-key/foreign-key-meta.ts index 768ce922..5ed86fd2 100644 --- a/src/associations/foreign-key/foreign-key-meta.ts +++ b/src/associations/foreign-key/foreign-key-meta.ts @@ -1,7 +1,7 @@ import {ModelClassGetter} from "../../model/shared/model-class-getter"; -export interface ForeignKeyMeta { +export interface ForeignKeyMeta { - relatedClassGetter: ModelClassGetter; + relatedClassGetter: ModelClassGetter; foreignKey: string; } diff --git a/src/associations/foreign-key/foreign-key-service.ts b/src/associations/foreign-key/foreign-key-service.ts index aeccf881..f630268f 100644 --- a/src/associations/foreign-key/foreign-key-service.ts +++ b/src/associations/foreign-key/foreign-key-service.ts @@ -1,13 +1,14 @@ -import {ForeignKeyOptions, Model} from "sequelize"; +import {ForeignKeyOptions} from "sequelize"; import {ForeignKeyMeta} from './foreign-key-meta'; import {ModelClassGetter} from "../../model/shared/model-class-getter"; +import {ModelType} from "../../model/model/model"; const FOREIGN_KEYS_KEY = 'sequelize:foreignKeys'; -export function getForeignKeyOptions(relatedClass: typeof Model, - classWithForeignKey?: typeof Model, - foreignKey?: string | ForeignKeyOptions): ForeignKeyOptions { +export function getForeignKeyOptions(relatedClass: ModelType, + classWithForeignKey?: ModelType, + foreignKey?: string | ForeignKeyOptions): ForeignKeyOptions { let foreignKeyOptions: ForeignKeyOptions = {}; if (typeof foreignKey === 'string') { @@ -36,9 +37,9 @@ export function getForeignKeyOptions(relatedClass: typeof Model, /** * Adds foreign key meta data for specified class */ -export function addForeignKey(target: any, - relatedClassGetter: ModelClassGetter, - foreignKey: string): void { +export function addForeignKey(target: any, + relatedClassGetter: ModelClassGetter, + foreignKey: string): void { let foreignKeys = getForeignKeys(target); if (!foreignKeys) { foreignKeys = []; @@ -53,7 +54,7 @@ export function addForeignKey(target: any, /** * Returns foreign key meta data from specified class */ -export function getForeignKeys(target: any): ForeignKeyMeta[] | undefined { +export function getForeignKeys(target: any): ForeignKeyMeta[] | undefined { const foreignKeys = Reflect.getMetadata(FOREIGN_KEYS_KEY, target); if (foreignKeys) { return [...foreignKeys]; diff --git a/src/associations/foreign-key/foreign-key.ts b/src/associations/foreign-key/foreign-key.ts index f7f00057..789040bc 100644 --- a/src/associations/foreign-key/foreign-key.ts +++ b/src/associations/foreign-key/foreign-key.ts @@ -1,7 +1,7 @@ import {addForeignKey} from "./foreign-key-service"; import {ModelClassGetter} from "../../model/shared/model-class-getter"; -export function ForeignKey(relatedClassGetter: ModelClassGetter): Function { +export function ForeignKey(relatedClassGetter: ModelClassGetter): Function { return (target: any, propertyName: string) => { addForeignKey(target, relatedClassGetter, propertyName); }; diff --git a/src/associations/has/has-association.ts b/src/associations/has/has-association.ts index 4a3379c3..b8145e76 100644 --- a/src/associations/has/has-association.ts +++ b/src/associations/has/has-association.ts @@ -1,14 +1,15 @@ -import {HasManyOptions, HasOneOptions, Model} from 'sequelize'; +import {HasManyOptions, HasOneOptions} from 'sequelize'; import {BaseAssociation} from '../shared/base-association'; import {getForeignKeyOptions} from "../foreign-key/foreign-key-service"; import {ModelClassGetter} from "../../model/shared/model-class-getter"; import {Association} from "../shared/association"; import {UnionAssociationOptions} from "../shared/union-association-options"; +import {ModelType} from '../../model/model/model'; -export class HasAssociation extends BaseAssociation { +export class HasAssociation extends BaseAssociation { - constructor(associatedClassGetter: ModelClassGetter, + constructor(associatedClassGetter: ModelClassGetter, protected options: HasManyOptions | HasOneOptions, private association: Association) { super(associatedClassGetter, options); @@ -18,7 +19,7 @@ export class HasAssociation extends BaseAssociation { return this.association; } - getSequelizeOptions(model: typeof Model): UnionAssociationOptions { + getSequelizeOptions(model: ModelType): UnionAssociationOptions { const options = {...this.options}; const associatedClass = this.getAssociatedClass(); diff --git a/src/associations/has/has-many.ts b/src/associations/has/has-many.ts index 53f6d1e2..fc8c218f 100644 --- a/src/associations/has/has-many.ts +++ b/src/associations/has/has-many.ts @@ -5,11 +5,11 @@ import {ModelClassGetter} from "../../model/shared/model-class-getter"; import {addAssociation, getPreparedAssociationOptions} from "../shared/association-service"; import {Association} from "../shared/association"; -export function HasMany(associatedClassGetter: ModelClassGetter, foreignKey?: string): Function; +export function HasMany(associatedClassGetter: ModelClassGetter, foreignKey?: string): Function; -export function HasMany(associatedClassGetter: ModelClassGetter, options?: HasManyOptions): Function; +export function HasMany(associatedClassGetter: ModelClassGetter, options?: HasManyOptions): Function; -export function HasMany(associatedClassGetter: ModelClassGetter, optionsOrForeignKey?: string | HasManyOptions): Function { +export function HasMany(associatedClassGetter: ModelClassGetter, optionsOrForeignKey?: string | HasManyOptions): Function { return (target: any, propertyName: string) => { const options: HasManyOptions = getPreparedAssociationOptions(optionsOrForeignKey); diff --git a/src/associations/has/has-one.ts b/src/associations/has/has-one.ts index c25f0b8f..e45fa0e5 100644 --- a/src/associations/has/has-one.ts +++ b/src/associations/has/has-one.ts @@ -5,11 +5,11 @@ import {ModelClassGetter} from "../../model/shared/model-class-getter"; import {addAssociation, getPreparedAssociationOptions} from "../shared/association-service"; import {Association} from "../shared/association"; -export function HasOne(associatedClassGetter: ModelClassGetter, foreignKey?: string): Function; +export function HasOne(associatedClassGetter: ModelClassGetter, foreignKey?: string): Function; -export function HasOne(associatedClassGetter: ModelClassGetter, options?: HasOneOptions): Function; +export function HasOne(associatedClassGetter: ModelClassGetter, options?: HasOneOptions): Function; -export function HasOne(associatedClassGetter: ModelClassGetter, optionsOrForeignKey?: string | HasOneOptions): Function { +export function HasOne(associatedClassGetter: ModelClassGetter, optionsOrForeignKey?: string | HasOneOptions): Function { return (target: any, propertyName: string) => { const options: HasOneOptions = getPreparedAssociationOptions(optionsOrForeignKey); diff --git a/src/associations/shared/association-service.ts b/src/associations/shared/association-service.ts index 361124e9..c5743443 100644 --- a/src/associations/shared/association-service.ts +++ b/src/associations/shared/association-service.ts @@ -27,8 +27,8 @@ export function getPreparedAssociationOptions(optionsOrForeignKey?: string | Non /** * Stores association meta data for specified class */ -export function addAssociation(target: any, - association: BaseAssociation) { +export function addAssociation(target: any, + association: BaseAssociation) { let associations = getAssociations(target); @@ -42,20 +42,20 @@ export function addAssociation(target: any, /** * Returns association meta data from specified class */ -export function getAssociations(target: any): BaseAssociation[] | undefined { +export function getAssociations(target: any): BaseAssociation[] | undefined { const associations = Reflect.getMetadata(ASSOCIATIONS_KEY, target); if (associations) { return [...associations]; } } -export function setAssociations(target: any, associations: BaseAssociation[]) { +export function setAssociations(target: any, associations: BaseAssociation[]) { Reflect.defineMetadata(ASSOCIATIONS_KEY, associations, target); } -export function getAssociationsByRelation(target: any, - relatedClass: any): BaseAssociation[] { - const associations = getAssociations(target); +export function getAssociationsByRelation(target: any, + relatedClass: any): BaseAssociation[] { + const associations = getAssociations(target); return (associations || []).filter(association => { const _relatedClass = association.getAssociatedClass(); return ( diff --git a/src/associations/shared/base-association.ts b/src/associations/shared/base-association.ts index 1f6143fe..ae902dcd 100644 --- a/src/associations/shared/base-association.ts +++ b/src/associations/shared/base-association.ts @@ -1,20 +1,20 @@ import {UnionAssociationOptions} from './union-association-options'; import {Association} from './association'; import {ModelClassGetter} from "../../model/shared/model-class-getter"; -import {Model} from "../../model/model/model"; +import {ModelType} from "../../model/model/model"; import {Sequelize} from "../../sequelize/sequelize/sequelize"; -export abstract class BaseAssociation { +export abstract class BaseAssociation { - constructor(private associatedClassGetter: ModelClassGetter, + constructor(private associatedClassGetter: ModelClassGetter, protected options: UnionAssociationOptions) { } abstract getAssociation(): Association; - abstract getSequelizeOptions(model: typeof Model, + abstract getSequelizeOptions(model: ModelType, sequelize: Sequelize): UnionAssociationOptions; - getAssociatedClass(): typeof Model { + getAssociatedClass(): ModelType { return this.associatedClassGetter(); } diff --git a/src/associations/through/through-options.ts b/src/associations/through/through-options.ts index 6e1c2075..bf572385 100644 --- a/src/associations/through/through-options.ts +++ b/src/associations/through/through-options.ts @@ -1,8 +1,8 @@ import {ThroughOptions as OriginThroughOptions} from 'sequelize'; import {ModelClassGetter} from "../../model/shared/model-class-getter"; -export type ThroughOptions = { +export type ThroughOptions = { [K in keyof OriginThroughOptions]: K extends 'model' - ? ModelClassGetter | string + ? ModelClassGetter | string : OriginThroughOptions[K] }; diff --git a/src/model/model/model.ts b/src/model/model/model.ts index 7f2a855b..63be8d12 100644 --- a/src/model/model/model.ts +++ b/src/model/model/model.ts @@ -7,9 +7,10 @@ import {AssociationGetOptions} from "./association/association-get-options"; import {AssociationCountOptions} from "./association/association-count-options"; import {AssociationActionOptions} from "./association/association-action-options"; import {AssociationCreateOptions} from "./association/association-create-options"; +import {Repository} from '../../sequelize/repository/repository'; -export type ModelType = typeof Model; -export type ModelCtor = (new () => M) & ModelType; +export type ModelType = new (values?: TCreationAttributes, options?: any) => Model; +export type ModelCtor = Repository; export type $GetType = NonNullable extends any[] ? NonNullable : (NonNullable | null); diff --git a/src/model/shared/model-class-getter.ts b/src/model/shared/model-class-getter.ts index 29200d8b..84ef5148 100644 --- a/src/model/shared/model-class-getter.ts +++ b/src/model/shared/model-class-getter.ts @@ -1,3 +1,3 @@ -import {Model} from "../model/model"; +import {ModelType} from "../model/model"; -export type ModelClassGetter = (returns?: void) => typeof Model; +export type ModelClassGetter = (returns?: void) => ModelType; diff --git a/src/scopes/default-scope.ts b/src/scopes/default-scope.ts index 19186570..5f498d3c 100644 --- a/src/scopes/default-scope.ts +++ b/src/scopes/default-scope.ts @@ -11,12 +11,12 @@ export function DefaultScope(scopeGetter: DefaultScopeGetter): Function; * Decorator for defining default Model scope * @deprecated */ -export function DefaultScope(scope: ScopeFindOptions): Function; +export function DefaultScope(scope: ScopeFindOptions): Function; /** * Decorator for defining default Model scope */ -export function DefaultScope(scopeOrSsopeGetter: ScopeFindOptions | DefaultScopeGetter): Function { +export function DefaultScope(scopeOrSsopeGetter: ScopeFindOptions | DefaultScopeGetter): Function { return (target: any) => { if (typeof scopeOrSsopeGetter === 'function') { addScopeOptionsGetter(target.prototype, {getDefaultScope: scopeOrSsopeGetter}); diff --git a/src/scopes/scope-find-options.ts b/src/scopes/scope-find-options.ts index 4902a9d0..f23dc624 100644 --- a/src/scopes/scope-find-options.ts +++ b/src/scopes/scope-find-options.ts @@ -1,16 +1,16 @@ import {Association, FindOptions, IncludeOptions} from "sequelize"; import {ModelClassGetter} from "../model/shared/model-class-getter"; -export type ScopeIncludeOptions = { +export type ScopeIncludeOptions = { [K in keyof IncludeOptions]: K extends 'model' - ? ModelClassGetter + ? ModelClassGetter : (K extends 'include' - ? ScopeIncludeOptions + ? ScopeIncludeOptions : IncludeOptions[K]) }; -export type ScopeFindOptions = { +export type ScopeFindOptions = { [K in keyof FindOptions]: K extends 'include' - ? ModelClassGetter[] | Association[] | ScopeIncludeOptions[] | { all: true } + ? ModelClassGetter[] | Association[] | ScopeIncludeOptions[] | { all: true } : FindOptions[K] }; diff --git a/src/scopes/scope-options.ts b/src/scopes/scope-options.ts index 962cdee4..5fef3700 100644 --- a/src/scopes/scope-options.ts +++ b/src/scopes/scope-options.ts @@ -2,8 +2,8 @@ import {ScopeTableOptions} from './scope-table-options'; import {ScopeFindOptions} from "./scope-find-options"; import {FindOptions} from "sequelize"; -export interface ScopeOptions extends ScopeTableOptions { - defaultScope?: ScopeFindOptions; +export interface ScopeOptions extends ScopeTableOptions { + defaultScope?: ScopeFindOptions; } export interface ScopeOptionsGetters { diff --git a/src/scopes/scope-service.ts b/src/scopes/scope-service.ts index 9012a338..b5d25193 100644 --- a/src/scopes/scope-service.ts +++ b/src/scopes/scope-service.ts @@ -70,7 +70,7 @@ export const resolvesDeprecatedScopes = (model: ModelCtor) => { * Adds scope option meta data for specified prototype * @deprecated */ -export function addScopeOptions(target: any, options: ScopeOptions): void { +export function addScopeOptions(target: any, options: ScopeOptions): void { const _options = getScopeOptions(target) || {}; setScopeOptions(target, deepAssign({}, _options, options)); } @@ -79,7 +79,7 @@ export function addScopeOptions(target: any, options: ScopeOptions): void { * Returns scope option meta data from specified target * @deprecated */ -export function getScopeOptions(target: any): ScopeOptions | undefined { +export function getScopeOptions(target: any): ScopeOptions | undefined { const options = Reflect.getMetadata(SCOPES_KEY, target); if (options) { return deepAssign({}, options); @@ -89,7 +89,7 @@ export function getScopeOptions(target: any): ScopeOptions | undefined { /** * @deprecated */ -function resolveDeprecatedScope(scopeName: string, model: ModelCtor, 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); @@ -103,6 +103,6 @@ function resolveDeprecatedScope(scopeName: string, model: ModelCtor, options: Sc * Set scope option meta data for specified prototype * @deprecated */ -function setScopeOptions(target: any, options: ScopeOptions): void { +function setScopeOptions(target: any, options: ScopeOptions): void { Reflect.defineMetadata(SCOPES_KEY, options, target); } diff --git a/src/scopes/scope-table-options.ts b/src/scopes/scope-table-options.ts index 052a220b..54b0dff2 100644 --- a/src/scopes/scope-table-options.ts +++ b/src/scopes/scope-table-options.ts @@ -1,5 +1,5 @@ import {ScopeFindOptions} from "./scope-find-options"; -export interface ScopeTableOptions { - [scopeName: string]: ScopeFindOptions | Function | undefined; +export interface ScopeTableOptions { + [scopeName: string]: ScopeFindOptions | Function | undefined; } diff --git a/src/scopes/scopes.ts b/src/scopes/scopes.ts index 6cff170b..3ad04d41 100644 --- a/src/scopes/scopes.ts +++ b/src/scopes/scopes.ts @@ -11,12 +11,12 @@ export function Scopes(scopesGetter: ScopesOptionsGetter): Function; * Decorator for defining Model scopes * @deprecated */ -export function Scopes(scopes: ScopeTableOptions): Function; +export function Scopes(scopes: ScopeTableOptions): Function; /** * Decorator for defining Model scopes */ -export function Scopes(scopesOrScopesGetter: ScopeTableOptions | ScopesOptionsGetter): Function { +export function Scopes(scopesOrScopesGetter: ScopeTableOptions | ScopesOptionsGetter): Function { return (target: any) => { if (typeof scopesOrScopesGetter === 'function') { addScopeOptionsGetter(target.prototype, { diff --git a/src/sequelize/sequelize/sequelize.ts b/src/sequelize/sequelize/sequelize.ts index acc18fa3..868f4fea 100644 --- a/src/sequelize/sequelize/sequelize.ts +++ b/src/sequelize/sequelize/sequelize.ts @@ -2,7 +2,7 @@ import {InitOptions, Sequelize as OriginSequelize} from 'sequelize'; import {ModelNotInitializedError} from "../../model/shared/model-not-initialized-error"; import {ModelMatch, SequelizeOptions} from "./sequelize-options"; import {getModels, prepareArgs} from "./sequelize-service"; -import {Model, ModelCtor} from "../../model/model/model"; +import {Model, ModelCtor, ModelType} from "../../model/model/model"; import {getModelName, getOptions} from "../../model/shared/model-service"; import {resolveScopes} from "../../scopes/scope-service"; import {installHooks} from "../../hooks/shared/hooks-service"; @@ -33,7 +33,7 @@ export class Sequelize extends OriginSequelize { } } - model(model: string | typeof Model): ModelCtor { + model(model: string | ModelType): ModelCtor { if (typeof model !== 'string') { return super.model(getModelName(model.prototype)) as ModelCtor; } @@ -110,7 +110,7 @@ export class Sequelize extends OriginSequelize { } private createRepositoryModel(modelClass: ModelCtor): ModelCtor { - return class extends modelClass { + return class extends modelClass { }; } diff --git a/test/types/attributes.spec.ts b/test/types/attributes.spec.ts new file mode 100644 index 00000000..155859f1 --- /dev/null +++ b/test/types/attributes.spec.ts @@ -0,0 +1,82 @@ +// Types only test. This should compile successfully. + +import { Optional } from "sequelize"; +import { + AutoIncrement, + BelongsTo, + BelongsToMany, + ForeignKey, + PrimaryKey, + Sequelize, +} from "../../src/index"; +import { Column } from "../../src/model/column/column"; +import { Model } from "../../src/model/model/model"; +import { Table } from "../../src/model/table/table"; +import { DataType } from "../../src/sequelize/data-type/data-type"; + +interface PetPersonAttributes { + petId: number; + personId: number; +} + +@Table +class PetPerson extends Model { + @ForeignKey(() => Pet) + @Column + petId: number; + + @ForeignKey(() => Person) + @Column + personId: number; +} + +interface PersonAttributes { + id: number; + name: string; +} +type PersonCreationAttributes = Optional; + +@Table +export class Person extends Model { + @PrimaryKey + @AutoIncrement + @Column(DataType.INTEGER) + id: number; + + @Column(DataType.STRING) + name: string; +} + +@Table +export class Pet extends Model { + @PrimaryKey + @AutoIncrement + @Column(DataType.INTEGER) + petId: number; + + @Column(DataType.STRING) + name: string; + + // model with attributes + @BelongsToMany(() => Person, () => PetPerson) + owners: Person[]; +} + +@Table +export class Toy extends Model { + @ForeignKey(() => Pet) + @Column(DataType.INTEGER) + petId: number; + + @Column(DataType.STRING) + name: string; + + // model without attributes + @BelongsTo(() => Pet) + pet: Pet; +} + +function testTypes() { + // all models should be accepted + new Sequelize().addModels([Person, Pet, PetPerson]); +}