Skip to content

Commit

Permalink
feat(sequelize): add Association class defs and Model metadata field …
Browse files Browse the repository at this point in the history
…defs (flow-typed#1526)

* feat(sequelize): add Association class defs and Model metadata field defs

* fix(sequelize): fix association export structure

* fix(Association): export Association class
  • Loading branch information
jedwards1211 authored and gantoine committed Nov 14, 2017
1 parent 9c854fa commit cf7c3fa
Show file tree
Hide file tree
Showing 2 changed files with 343 additions and 39 deletions.
283 changes: 250 additions & 33 deletions definitions/npm/sequelize_v4.x.x/flow_v0.42.x-/sequelize_v4.x.x.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ declare module "sequelize" {

/**
* The getAssociation mixin applied to models with belongsTo.
* An example of usage is as follows:
* An example of usage is as follows
```js
Expand Down Expand Up @@ -1277,7 +1277,7 @@ declare module "sequelize" {
* Options provided when associating models with belongsToMany relationship
* @see Association class belongsToMany method
*/
declare export type AssociationOptionsBelongsToMany = AssociationOptionsManyToMany & {
declare export type AssociationOptionsBelongsToMany<Through: Model<any>> = AssociationOptionsManyToMany & {
/**
* The name of the table that is used to join source and target in n:m associations. Can also be a
* sequelize
Expand All @@ -1288,7 +1288,7 @@ declare module "sequelize" {
* @see https://github.com/sequelize/sequelize/blob/v3.4.1/lib/associations/has-many.js
* @see https://github.com/sequelize/sequelize/blob/v3.4.1/lib/associations/belongs-to-many.js
*/
through: Class<Model<any>> | string | ThroughOptions,
through: Class<Through> | string | ThroughOptions<Through>,

/**
* The name of the foreign key in the join table (representing the target model) or an object representing
Expand All @@ -1310,11 +1310,11 @@ declare module "sequelize" {
* Used for a association table in n:m associations.
* @see AssociationOptionsBelongsToMany
*/
declare export type ThroughOptions = {
declare export type ThroughOptions<TInstance: Model<any>> = {
/**
* The model used to join both sides of the N:M association.
*/
model: Class<Model<any>>,
model: Class<TInstance>,

/**
* A key/value set that will be used for association create and find defaults on the through model.
Expand All @@ -1331,6 +1331,160 @@ declare module "sequelize" {
unique?: boolean
}

declare type AssociationType = 'HasMany' | 'BelongsTo' | 'HasOne' | 'BelongsToMany'

declare export type Attribute = {
/**
* A string or a data type
*/
type: DataTypeAbstract,

values?: Array<any>,

/**
* If true, the column will get a unique constraint. If a string is provided, the column will be part of a
* composite unique index. If multiple columns have the same string, they will be part of the same unique
index
*/
unique?: boolean | string | {
name: string,
msg: string
},

/**
* Primary key flag
*/
primaryKey?: boolean,

/**
* Is this field an auto increment field
*/
autoIncrement?: boolean,

/**
* Comment for the database
*/
comment?: string,

/**
* An object with reference configurations
*/
references?: DefineAttributeColumnReferencesOptions,

Model: Model<any>,
_autoGenerated?: true,
fieldName: string,
field: string,
}

declare export class Association<Source: Model<any>, Target: Model<any>> {
constructor(source: Class<Source>, target: Class<Target>, options?: AssociationOptions): this;
static BelongsTo: typeof BelongsTo;
static HasOne: typeof HasOne;
static BelongsToMany: typeof BelongsToMany;
static HasMany: typeof HasMany;
source: Class<Source>;
target: Class<Target>;
sequelize: Sequelize;
options: AssociationOptions;
scope: ?AssociationScope;
isSingleAssociation: boolean;
isMultiAssociation: boolean;
isSelfAssociation: boolean;
as: string | {
singular: string,
plural: string
};
associationType: $Subtype<AssociationType>;
}

declare type ArrayOrElement<T> = T | Array<T>;

declare class BelongsTo<Source: Model<any>, TargetAttributes: Object, TargetInitAttributes: Object, Target: Model<TargetAttributes, TargetInitAttributes>> extends Association<Source, Target> {
associationType: 'BelongsTo';
foreignKey: string;
foreignKeyField: string;
foreignKeyAttribute: Attribute;
identifier: string;
targetKey: string;
targetKeyField: string;
targetKeyAttribute: string;
targetIdentifier: string;
targetKeyIsPrimary: boolean;
identifierField: string;
get(instance: Source, options?: FindOptions<TargetAttributes>): Promise<Target>;
get<PrimaryKey>(instances: Array<Source>, options?: FindOptions<TargetAttributes>): Promise<{[key: PrimaryKey]: Target}>;
set<PrimaryKey>(sourceInstance: Source, targetInstance: PrimaryKey | Target, options?: InstanceSaveOptions<TargetAttributes>): Promise<void>;
create(sourceInstance: Source, values: TargetInitAttributes, options?: CreateOptions<TargetAttributes>): Promise<Target>;
}

declare class HasOne<Source: Model<any>, TargetAttributes: Object, TargetInitAttributes: Object, Target: Model<TargetAttributes, TargetInitAttributes>> extends Association<Source, Target> {
associationType: 'HasOne';
foreignKey: string;
foreignKeyField: string;
foreignKeyAttribute: Attribute;
identifier: string;
sourceKey: string;
sourceKeyField: string;
sourceKeyAttribute: string;
sourceKeyIsPrimary: boolean;
sourceIdentifier: string;
identifierField: string;
get(instance: Source, options?: FindOptions<TargetAttributes>): Promise<Target>;
get<PrimaryKey>(instances: Array<Source>, options?: FindOptions<TargetAttributes>): Promise<{[key: PrimaryKey]: Target}>;
set<PrimaryKey>(sourceInstance: Source, targetInstance: PrimaryKey | Target, options?: InstanceSaveOptions<TargetAttributes>): Promise<void>;
create(sourceInstance: Source, values: TargetInitAttributes, options?: CreateOptions<TargetAttributes>): Promise<Target>;
}

declare class HasMany<Source: Model<any>, TargetAttributes: Object, TargetInitAttributes: Object, Target: Model<TargetAttributes, TargetInitAttributes>> extends Association<Source, Target> {
associationType: 'HasMany';
foreignKey: string;
foreignKeyField: string;
foreignKeyAttribute: Attribute;
sourceKey: string;
sourceKeyField: string;
sourceKeyAttribute: string;
identifierField: string;
get(instance: Source, options?: FindOptions<TargetAttributes>): Promise<Array<Target>>;
get<PrimaryKey>(instances: Array<Source>, options?: FindOptions<TargetAttributes>): Promise<{[key: PrimaryKey]: Target}>;
count(instance: Source, options?: FindOptions<TargetAttributes>): Promise<number>;
has<PrimaryKey>(sourceInstance: Source, targetInstances: ArrayOrElement<PrimaryKey | Target>, options?: FindOptions<TargetAttributes>): Promise<boolean>;
set<PrimaryKey>(sourceInstance: Source, targetInstances: ArrayOrElement<PrimaryKey | Target>, options?: FindOptions<TargetAttributes> & UpdateRelatedOptions<TargetAttributes>): Promise<Source>;
add<PrimaryKey>(sourceInstance: Source, targetInstances: ArrayOrElement<PrimaryKey | Target>, options?: UpdateRelatedOptions<TargetAttributes>): Promise<Source>;
remove<PrimaryKey>(sourceInstance: Source, targetInstances: ArrayOrElement<PrimaryKey | Target>, options?: UpdateRelatedOptions<TargetAttributes>): Promise<this>;
create(sourceInstance: Source, values: TargetInitAttributes, options?: CreateOptions<TargetAttributes>): Promise<Target>;
}

declare class BelongsToMany<
SourceAttributes: Object,
SourceInitAttributes: Object,
Source: Model<SourceAttributes, SourceInitAttributes>,
TargetAttributes: Object,
TargetInitAttributes: Object,
Target: Model<TargetAttributes, TargetInitAttributes>,
ThroughAttributes: Object,
Through: Model<ThroughAttributes>
> extends Association<Source, Target> {
associationType: 'BelongsToMany';
foreignKey: string;
foreignKeyField: string;
foreignKeyAttribute: Attribute;
otherKey: string;
otherKeyField: string;
otherKeyAttribute: string;
identifierField: string;
foreignIdentifierField?: string;
paired?: BelongsToMany<TargetAttributes, TargetInitAttributes, Target, SourceAttributes, SourceInitAttributes, Source, ThroughAttributes, Through>;
through: ThroughOptions<Through>;
get(instance: Source, options?: FindOptions<TargetAttributes>): Promise<Array<Target>>;
count(instance: Source, options?: FindOptions<TargetAttributes>): Promise<number>;
has<PrimaryKey>(sourceInstance: Source, targetInstances: ArrayOrElement<PrimaryKey | Target>, options?: FindOptions<TargetAttributes>): Promise<boolean>;
set<PrimaryKey>(sourceInstance: Source, targetInstances: ArrayOrElement<PrimaryKey | Target>, options?: FindOptions<TargetAttributes> & UpdateRelatedOptions<TargetAttributes> & DestroyOptions): Promise<Array<any>>;
add<PrimaryKey>(sourceInstance: Source, targetInstances: ArrayOrElement<PrimaryKey | Target>, options?: FindOptions<TargetAttributes> & UpdateRelatedOptions<TargetAttributes>): Promise<Array<any>>;
remove<PrimaryKey>(sourceInstance: Source, targetInstances: ArrayOrElement<PrimaryKey | Target>, options?: DestroyOptions): Promise<void>;
create(sourceInstance: Source, values: TargetInitAttributes, options?: CreateOptions<TargetAttributes>): Promise<Target>;
}

/**
* Abstract DataType interface. Use this if you want to create an interface that has a value any of the
* DataTypes that Sequelize supports.
Expand Down Expand Up @@ -2367,25 +2521,14 @@ declare module "sequelize" {
attributes?: string[]
}


/**
* Association Object for Include Options
*/
declare export type IncludeAssociation = {
source: Class<Model<any>>,
target: Class<Model<any>>,
identifier: string
}


/**
* Complex include options
*/
declare export type IncludeOptions<TAttributes, ModelClass: Class<Model<TAttributes>>> = {
declare export type IncludeOptions<TAttributes, TInstance: Model<TAttributes>> = {
/**
* The model you want to eagerly load
*/
model?: ModelClass,
model?: Class<TInstance>,

/**
* The alias of the relation, in case the model you want to eagerly load is aliassed. For `hasOne` /
Expand All @@ -2396,7 +2539,7 @@ declare module "sequelize" {
/**
* The association you want to eagerly load. (This can be used instead of providing a model/as pair)
*/
association?: IncludeAssociation,
association?: Association<TInstance, any>,

/**
* Where clauses to apply to the child models. Note that this converts the eager load to an inner join,
Expand Down Expand Up @@ -2795,6 +2938,52 @@ declare module "sequelize" {
transaction?: Transaction
} & LoggingOptions

/**
* Options used for HasMany.update, BelongsToMany.update
*/
declare export type UpdateRelatedOptions<TAttributes> = {
/**
* Options to describe the scope of the search.
*/
where?: WhereOptions,

/**
* Run before / after bulk update hooks?
*
Defaults to true
*/
hooks?: boolean,

/**
* Whether or not to update the side effects of any virtual setters.
*
Defaults to true
*/
sideEffects?: boolean,

/**
* Run before / after update hooks?. If true, this will execute a SELECT followed by individual UPDATEs.
* A select is needed, because the row data needs to be passed to the hooks
Defaults to false
*/
individualHooks?: boolean,

/**
* How many rows to update (only for mysql and mariadb)
*/
limit?: number,

/**
* Transaction to run query under
*/
transaction?: Transaction,

/**
* If true, the updatedAt timestamp will not be updated.
*/
silent?: boolean
} & FieldsOptions<TAttributes> & LoggingOptions & ReturningOptions


/**
Expand Down Expand Up @@ -3435,8 +3624,7 @@ declare module "sequelize" {
static beforeBulkDelete(name: string, fn: AsyncFn1<Object>): void,
static beforeBulkDelete(fn: AsyncFn1<Object>): void,

/**
* A hook that is run after destroying instances in bulk
/* A hook that is run after destroying instances in bulk
* @param name
* @param fn A callback function that is called with options
* @alias afterBulkDelete
Expand Down Expand Up @@ -3571,9 +3759,9 @@ declare module "sequelize" {
* @param target The model that will be associated with hasOne relationship
* @param options Options for the association
*/
static hasOne(
target: Class<Model<any>>,
options?: AssociationOptionsHasOne): void,
static hasOne<TargetAttributes: Object, TargetInitAttributes: Object, Target: Model<TargetAttributes, TargetInitAttributes>>(
target: Class<Target>,
options?: AssociationOptionsHasOne): HasOne<this, TargetAttributes, TargetInitAttributes, Target>,

/**
* Creates an association between this (the source) and the provided target. The foreign key is added on the
Expand All @@ -3583,9 +3771,9 @@ declare module "sequelize" {
* @param target The model that will be associated with hasOne relationship
* @param options Options for the association
*/
static belongsTo(
target: Class<Model<any>>,
options?: AssociationOptionsBelongsTo): void,
static belongsTo<TargetAttributes: Object, TargetInitAttributes: Object, Target: Model<TargetAttributes, TargetInitAttributes>>(
target: Class<Target>,
options?: AssociationOptionsBelongsTo): BelongsTo<this, TargetAttributes, TargetInitAttributes, Target>,

/**
* Create an association that is either 1:m or n:m.
Expand Down Expand Up @@ -3638,9 +3826,9 @@ declare module "sequelize" {
* @param target The model that will be associated with hasOne relationship
* @param options Options for the association
*/
static hasMany(
target: Class<Model<any>>,
options?: AssociationOptionsHasMany): void,
static hasMany<TargetAttributes: Object, TargetInitAttributes: Object, Target: Model<TargetAttributes, TargetInitAttributes>>(
target: Class<Target>,
options?: AssociationOptionsHasMany): HasMany<this, TargetAttributes, TargetInitAttributes, Target>,

/**
* Create an N:M association with a join table
Expand Down Expand Up @@ -3688,9 +3876,38 @@ declare module "sequelize" {
* @param target The model that will be associated with hasOne relationship
* @param options Options for the association
*/
static belongsToMany(
target: Class<Model<any>>,
options: AssociationOptionsBelongsToMany): void,
static belongsToMany<
TargetAttributes: Object,
TargetInitAttributes: Object,
Target: Model<TargetAttributes, TargetInitAttributes>,
ThroughAttributes: Object,
Through: Model<ThroughAttributes>
>(
target: Class<Target>,
options: AssociationOptionsBelongsToMany<Through>
): BelongsToMany<
TAttributes, TInitAttributes, this,
TargetAttributes, TargetInitAttributes, Target,
ThroughAttributes, Through
>,

static associations: {[name: string]: Association<this, any>},
static tableName: string,
static rawAttributes: {[name: string]: Attribute},
static tableAttributes: {[name: string]: Attribute},
static attributes: {[name: string]: Attribute},
static primaryKeys: {[name: string]: Attribute},
static primaryKeyAttributes: Array<string>,
static primaryKeyAttribute: ?string,
static primaryKeyField?: string,
static uniqueKeys: {[idxName: string | false]: {
name: string | false,
column: string | false,
msg: ?string,
fields: Array<string>,
}},
static fieldRawAttributesMap: {[name: string]: string},
static fieldAttributesMap: {[name: string]: string},

Model: Class<this>,

Expand Down
Loading

0 comments on commit cf7c3fa

Please sign in to comment.