Skip to content

Commit

Permalink
fix(types): include 'as' in IncludeThroughOptions definition (#11624)
Browse files Browse the repository at this point in the history
  • Loading branch information
mmena1 authored and sushantdhiman committed Nov 1, 2019
1 parent 21d743a commit cbef15f
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 15 deletions.
16 changes: 16 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,22 @@ Now launch the docker mysql and postgres servers with this command (you can add
$ docker-compose up postgres-95 mysql-57 mssql
```

> **_NOTE:_** If you get the following output:
>```
>...
>Creating mysql-57 ... error
>
>ERROR: for mysql-57 Cannot create container for service mysql-57: b'create .: volume name is too short, names should be at least two alphanumeric characters'
>
>ERROR: for mysql-57 Cannot create container for service mysql-57: b'create .: volume name is too short, names should be at least two alphanumeric characters'
>ERROR: Encountered errors while bringing up the project.
>```
>You need to set the variables `MARIADB_ENTRYPOINT` and `MYSQLDB_ENTRYPOINT` accordingly:
>```sh
>$ export MARIADB_ENTRYPOINT="$PATH_TO_PROJECT/test/config/mariadb"
>$ export MYSQLDB_ENTRYPOINT="$PATH_TO_PROJECT/test/config/mysql"
>```
**MSSQL:** Please run `npm run setup-mssql` to create the test database.
**POSTGRES:** Sequelize uses [special](https://github.com/sushantdhiman/sequelize-postgres) Docker image for PostgreSQL, which install all the extensions required by tests.
Expand Down
1 change: 1 addition & 0 deletions lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -1654,6 +1654,7 @@ class Model {
* @param {boolean} [options.include[].right] If true, converts to a right join if dialect support it. Ignored if `include.required` is true.
* @param {boolean} [options.include[].separate] If true, runs a separate query to fetch the associated instances, only supported for hasMany associations
* @param {number} [options.include[].limit] Limit the joined rows, only supported with include.separate=true
* @param {string} [options.include[].through.as] The alias for the join model, in case you want to give it a different name than the default one.
* @param {Object} [options.include[].through.where] Filter on the join model for belongsToMany relations
* @param {Array} [options.include[].through.attributes] A list of attributes to select from the join model for belongsToMany relations
* @param {Array<Object|Model|string>} [options.include[].include] Load further nested related models
Expand Down
26 changes: 26 additions & 0 deletions test/integration/associations/belongs-to-many.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2569,6 +2569,32 @@ describe(Support.getTestDialectTeaser('BelongsToMany'), () => {
});
});

it('should be able to alias the default name of the join table', function() {
return Promise.all([
this.User.create(),
this.Project.create()
]).then(([user, project]) => {
return user.addProject(project, { through: { status: 'active', data: 42 } }).return(user);
}).then(() => {
return this.User.findAll({
include: [{
model: this.Project,
through: {
as: 'myProject'
}
}]
});
}).then(users => {
const project = users[0].Projects[0];

expect(project.UserProjects).not.to.exist;
expect(project.status).not.to.exist;
expect(project.myProject).to.be.ok;
expect(project.myProject.status).to.equal('active');
expect(project.myProject.data).to.equal(42);
});
});

it('should be able to limit the join table attributes returned', function() {
return Promise.all([
this.User.create(),
Expand Down
25 changes: 10 additions & 15 deletions types/lib/model.d.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,15 @@
import {
Association,
BelongsTo,
BelongsToMany,
BelongsToManyOptions,
BelongsToOptions,
HasMany,
HasManyOptions,
HasOne,
HasOneOptions,
} from './associations/index';
import { IndexHints } from '..';
import { Association, BelongsTo, BelongsToMany, BelongsToManyOptions, BelongsToOptions, HasMany, HasManyOptions, HasOne, HasOneOptions } from './associations/index';
import { DataType } from './data-types';
import { Deferrable } from './deferrable';
import { HookReturn, Hooks, ModelHooks } from './hooks';
import { ValidationOptions } from './instance-validator';
import { ModelManager } from './model-manager';
import Op = require('./operators');
import { Promise } from './promise';
import { QueryOptions, IndexesOptions } from './query-interface';
import { Config, Options, Sequelize, SyncOptions } from './sequelize';
import { Transaction, LOCK } from './transaction';
import { Col, Fn, Literal, Where } from './utils';
import { IndexHints } from '..';
import Op = require('./operators');

export interface Logging {
/**
Expand Down Expand Up @@ -373,7 +362,13 @@ export interface WhereAttributeHash {
/**
* Through options for Include Options
*/
export interface IncludeThroughOptions extends Filterable, Projectable {}
export interface IncludeThroughOptions extends Filterable, Projectable {
/**
* The alias of the relation, in case the model you want to eagerly load is aliassed. For `hasOne` /
* `belongsTo`, this should be the singular name, and for `hasMany`, it should be the plural
*/
as?: string;
}

/**
* Options for eager-loading associated models, also allowing for all associations to be loaded at once
Expand Down
11 changes: 11 additions & 0 deletions types/test/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,17 @@ const assoc: Association = MyModel.associations.other;
const Instance: MyModel = new MyModel({ int: 10 });
const num: number = Instance.get('num');

MyModel.findOne({
include: [
{
through: {
as: "OtherModel",
attributes: ['num']
}
}
]
});

MyModel.findOne({
include: [
{ model: OtherModel, paranoid: true }
Expand Down

0 comments on commit cbef15f

Please sign in to comment.