Skip to content

Commit

Permalink
feat(types): add InferAttributes utility type (sequelize#13909)
Browse files Browse the repository at this point in the history
Co-authored-by: Rik Smale <13023439+WikiRik@users.noreply.github.com>
  • Loading branch information
2 people authored and maramizo committed Jun 2, 2022
1 parent 298fb8d commit f7dd991
Show file tree
Hide file tree
Showing 17 changed files with 4,310 additions and 642 deletions.
357 changes: 254 additions & 103 deletions docs/manual/other-topics/typescript.md

Large diffs are not rendered by default.

41 changes: 26 additions & 15 deletions src/associations/belongs-to-many.d.ts
Expand Up @@ -8,7 +8,8 @@ import {
InstanceDestroyOptions,
InstanceUpdateOptions,
Model,
ModelStatic,
ModelCtor,
ModelType,
Transactionable,
} from '../model';
import { Association, AssociationScope, ForeignKeyOptions, ManyToManyOptions, MultiAssociationAccessors } from './base';
Expand All @@ -21,7 +22,7 @@ export interface ThroughOptions {
* The model used to join both sides of the N:M association.
* Can be a string if you want the model to be generated by sequelize.
*/
model: ModelStatic | string;
model: ModelType | string;

/**
* If true the generated join table will be paranoid
Expand Down Expand Up @@ -52,14 +53,14 @@ export interface JoinTableAttributes {
}

/**
* Options provided when associating models with {@link Model.belongsToMany} relationship
* Options provided when associating models with belongsToMany relationship
*/
export interface BelongsToManyOptions extends ManyToManyOptions {
/**
* The name of the table that is used to join source and target in n:m associations. Can also be a
* sequelize model if you want to define the junction table yourself and add extra attributes to it.
*/
through: ModelStatic | string | ThroughOptions;
through: ModelType | string | ThroughOptions;

/**
* The name of the foreign key in the join table (representing the target model) or an object representing
Expand Down Expand Up @@ -97,7 +98,7 @@ export class BelongsToMany<S extends Model = Model, T extends Model = Model> ext
public sourceKey: string;
public targetKey: string;
public accessors: MultiAssociationAccessors;
constructor(source: ModelStatic<S>, target: ModelStatic<T>, options: BelongsToManyOptions);
constructor(source: ModelCtor<S>, target: ModelCtor<T>, options: BelongsToManyOptions);
}

/**
Expand All @@ -116,7 +117,7 @@ export interface BelongsToManyGetAssociationsMixinOptions extends FindOptions<an
}

/**
* The getAssociations mixin applied to models with {@link Model.belongsToMany}.
* The getAssociations mixin applied to models with belongsToMany.
* An example of usage is as follows:
*
* ```js
Expand All @@ -137,6 +138,7 @@ export interface BelongsToManyGetAssociationsMixinOptions extends FindOptions<an
* }
* ```
*
* @see https://sequelize.org/master/class/lib/associations/belongs-to-many.js~BelongsToMany.html
* @see Instance
*/
export type BelongsToManyGetAssociationsMixin<TModel> = (
Expand All @@ -156,7 +158,7 @@ export interface BelongsToManySetAssociationsMixinOptions
}

/**
* The setAssociations mixin applied to models with {@link Model.belongsToMany}.
* The setAssociations mixin applied to models with belongsToMany.
* An example of usage is as follows:
*
* ```js
Expand All @@ -177,6 +179,7 @@ export interface BelongsToManySetAssociationsMixinOptions
* }
* ```
*
* @see https://sequelize.org/master/class/lib/associations/belongs-to-many.js~BelongsToMany.html
* @see Instance
*/
export type BelongsToManySetAssociationsMixin<TModel, TModelPrimaryKey> = (
Expand All @@ -197,7 +200,7 @@ export interface BelongsToManyAddAssociationsMixinOptions
}

/**
* The addAssociations mixin applied to models with {@link Model.belongsToMany}.
* The addAssociations mixin applied to models with belongsToMany.
* An example of usage is as follows:
*
* ```js
Expand All @@ -218,6 +221,7 @@ export interface BelongsToManyAddAssociationsMixinOptions
* }
* ```
*
* @see https://sequelize.org/master/class/lib/associations/belongs-to-many.js~BelongsToMany.html
* @see Instance
*/
export type BelongsToManyAddAssociationsMixin<TModel, TModelPrimaryKey> = (
Expand All @@ -238,7 +242,7 @@ export interface BelongsToManyAddAssociationMixinOptions
}

/**
* The addAssociation mixin applied to models with {@link Model.belongsToMany}.
* The addAssociation mixin applied to models with belongsToMany.
* An example of usage is as follows:
*
* ```js
Expand All @@ -259,6 +263,7 @@ export interface BelongsToManyAddAssociationMixinOptions
* }
* ```
*
* @see https://sequelize.org/master/class/lib/associations/belongs-to-many.js~BelongsToMany.html
* @see Instance
*/
export type BelongsToManyAddAssociationMixin<TModel, TModelPrimaryKey> = (
Expand All @@ -274,7 +279,7 @@ export interface BelongsToManyCreateAssociationMixinOptions extends CreateOption
through?: JoinTableAttributes;
}
/**
* The createAssociation mixin applied to models with {@link Model.belongsToMany}.
* The createAssociation mixin applied to models with belongsToMany.
* An example of usage is as follows:
*
* ```js
Expand All @@ -295,6 +300,7 @@ export interface BelongsToManyCreateAssociationMixinOptions extends CreateOption
* }
* ```
*
* @see https://sequelize.org/master/class/lib/associations/belongs-to-many.js~BelongsToMany.html
* @see Instance
*/
export type BelongsToManyCreateAssociationMixin<TModel extends Model> = (
Expand All @@ -309,7 +315,7 @@ export type BelongsToManyCreateAssociationMixin<TModel extends Model> = (
export interface BelongsToManyRemoveAssociationMixinOptions extends InstanceDestroyOptions {}

/**
* The removeAssociation mixin applied to models with {@link Model.belongsToMany}.
* The removeAssociation mixin applied to models with belongsToMany.
* An example of usage is as follows:
*
* ```js
Expand All @@ -330,6 +336,7 @@ export interface BelongsToManyRemoveAssociationMixinOptions extends InstanceDest
* }
* ```
*
* @see https://sequelize.org/master/class/lib/associations/belongs-to-many.js~BelongsToMany.html
* @see Instance
*/
export type BelongsToManyRemoveAssociationMixin<TModel, TModelPrimaryKey> = (
Expand All @@ -344,7 +351,7 @@ export type BelongsToManyRemoveAssociationMixin<TModel, TModelPrimaryKey> = (
export interface BelongsToManyRemoveAssociationsMixinOptions extends InstanceDestroyOptions, InstanceDestroyOptions {}

/**
* The removeAssociations mixin applied to models with {@link Model.belongsToMany}.
* The removeAssociations mixin applied to models with belongsToMany.
* An example of usage is as follows:
*
* ```js
Expand All @@ -365,6 +372,7 @@ export interface BelongsToManyRemoveAssociationsMixinOptions extends InstanceDes
* }
* ```
*
* @see https://sequelize.org/master/class/lib/associations/belongs-to-many.js~BelongsToMany.html
* @see Instance
*/
export type BelongsToManyRemoveAssociationsMixin<TModel, TModelPrimaryKey> = (
Expand All @@ -379,7 +387,7 @@ export type BelongsToManyRemoveAssociationsMixin<TModel, TModelPrimaryKey> = (
export interface BelongsToManyHasAssociationMixinOptions extends BelongsToManyGetAssociationsMixinOptions {}

/**
* The hasAssociation mixin applied to models with {@link Model.belongsToMany}.
* The hasAssociation mixin applied to models with belongsToMany.
* An example of usage is as follows:
*
* ```js
Expand All @@ -400,6 +408,7 @@ export interface BelongsToManyHasAssociationMixinOptions extends BelongsToManyGe
* }
* ```
*
* @see https://sequelize.org/master/class/lib/associations/belongs-to-many.js~BelongsToMany.html
* @see Instance
*/
export type BelongsToManyHasAssociationMixin<TModel, TModelPrimaryKey> = (
Expand All @@ -414,7 +423,7 @@ export type BelongsToManyHasAssociationMixin<TModel, TModelPrimaryKey> = (
export interface BelongsToManyHasAssociationsMixinOptions extends BelongsToManyGetAssociationsMixinOptions {}

/**
* The removeAssociations mixin applied to models with {@link Model.belongsToMany}.
* The removeAssociations mixin applied to models with belongsToMany.
* An example of usage is as follows:
*
* ```js
Expand All @@ -435,6 +444,7 @@ export interface BelongsToManyHasAssociationsMixinOptions extends BelongsToManyG
* }
* ```
*
* @see https://sequelize.org/master/class/lib/associations/belongs-to-many.js~BelongsToMany.html
* @see Instance
*/
export type BelongsToManyHasAssociationsMixin<TModel, TModelPrimaryKey> = (
Expand All @@ -454,7 +464,7 @@ export interface BelongsToManyCountAssociationsMixinOptions extends Transactiona
}

/**
* The countAssociations mixin applied to models with {@link Model.belongsToMany}.
* The countAssociations mixin applied to models with belongsToMany.
* An example of usage is as follows:
*
* ```js
Expand All @@ -475,6 +485,7 @@ export interface BelongsToManyCountAssociationsMixinOptions extends Transactiona
* }
* ```
*
* @see https://sequelize.org/master/class/lib/associations/belongs-to-many.js~BelongsToMany.html
* @see Instance
*/
export type BelongsToManyCountAssociationsMixin = (
Expand Down
19 changes: 12 additions & 7 deletions src/associations/belongs-to.d.ts
@@ -1,10 +1,10 @@
import { DataType } from '../data-types';
import { CreateOptions, CreationAttributes, FindOptions, Model, ModelStatic, SaveOptions } from '../model';
import { CreateOptions, CreationAttributes, FindOptions, Model, ModelCtor, SaveOptions } from '../model';
import { Association, AssociationOptions, SingleAssociationAccessors } from './base';

// type ModelStatic<M extends Model> = InstanceType<typeof M>;
// type ModelCtor<M extends Model> = InstanceType<typeof M>;
/**
* Options provided when associating models with {@link Model.belongsTo} relationship
* Options provided when associating models with belongsTo relationship
*
* @see Association class belongsTo method
*/
Expand All @@ -23,7 +23,7 @@ export interface BelongsToOptions extends AssociationOptions {

export class BelongsTo<S extends Model = Model, T extends Model = Model> extends Association<S, T> {
public accessors: SingleAssociationAccessors;
constructor(source: ModelStatic<S>, target: ModelStatic<T>, options: BelongsToOptions);
constructor(source: ModelCtor<S>, target: ModelCtor<T>, options: BelongsToOptions);
}

/**
Expand All @@ -38,7 +38,7 @@ export interface BelongsToGetAssociationMixinOptions extends FindOptions<any> {
}

/**
* The getAssociation mixin applied to models with {@link Model.belongsTo}.
* The getAssociation mixin applied to models with belongsTo.
* An example of usage is as follows:
*
* ```js
Expand All @@ -52,6 +52,7 @@ export interface BelongsToGetAssociationMixinOptions extends FindOptions<any> {
* }
* ```
*
* @see https://sequelize.org/master/class/lib/associations/belongs-to.js~BelongsTo.html
* @see Instance
*/
export type BelongsToGetAssociationMixin<TModel> = (options?: BelongsToGetAssociationMixinOptions) => Promise<TModel>;
Expand All @@ -68,7 +69,7 @@ export interface BelongsToSetAssociationMixinOptions extends SaveOptions<any> {
}

/**
* The setAssociation mixin applied to models with {@link Model.belongsTo}.
* The setAssociation mixin applied to models with belongsTo.
* An example of usage is as follows:
*
* ```js
Expand All @@ -82,6 +83,7 @@ export interface BelongsToSetAssociationMixinOptions extends SaveOptions<any> {
* }
* ```
*
* @see https://sequelize.org/master/class/lib/associations/belongs-to.js~BelongsTo.html
* @see Instance
*/
export type BelongsToSetAssociationMixin<TModel, TPrimaryKey> = (
Expand All @@ -97,7 +99,7 @@ export interface BelongsToCreateAssociationMixinOptions
extends CreateOptions<any>, BelongsToSetAssociationMixinOptions {}

/**
* The createAssociation mixin applied to models with {@link Model.belongsTo}.
* The createAssociation mixin applied to models with belongsTo.
* An example of usage is as follows:
*
* ```js
Expand All @@ -111,9 +113,12 @@ export interface BelongsToCreateAssociationMixinOptions
* }
* ```
*
* @see https://sequelize.org/master/class/lib/associations/belongs-to.js~BelongsTo.html
* @see Instance
*/
export type BelongsToCreateAssociationMixin<TModel extends Model> = (
values?: CreationAttributes<TModel>,
options?: BelongsToCreateAssociationMixinOptions
) => Promise<TModel>;

export default BelongsTo;

0 comments on commit f7dd991

Please sign in to comment.