不要怂,就是干,撸起袖子干!

Commit 5a2c6a62 by Benoit MERIAUX Committed by Sushant

fix(typings): add missing sourceKey option in HasOneOptions association type (#10651)

1 parent bb952e89
...@@ -7,16 +7,21 @@ import { ...@@ -7,16 +7,21 @@ import {
Model, Model,
ModelCtor, ModelCtor,
Transactionable, Transactionable,
WhereOptions,
} from '../model'; } from '../model';
import { Promise } from '../promise'; import { Promise } from '../promise';
import { Transaction } from '../transaction';
import { Association, ManyToManyOptions, MultiAssociationAccessors } from './base'; import { Association, ManyToManyOptions, MultiAssociationAccessors } from './base';
/** /**
* Options provided when associating models with hasMany relationship * Options provided when associating models with hasMany relationship
*/ */
export interface HasManyOptions extends ManyToManyOptions { export interface HasManyOptions extends ManyToManyOptions {
/**
* The name of the field to use as the key for the association in the source table. Defaults to the primary
* key of the source table
*/
sourceKey?: string;
/** /**
* A string or a data type to represent the identifier in the table * A string or a data type to represent the identifier in the table
*/ */
......
...@@ -7,6 +7,13 @@ import { Association, AssociationOptions, SingleAssociationAccessors } from './b ...@@ -7,6 +7,13 @@ import { Association, AssociationOptions, SingleAssociationAccessors } from './b
* Options provided when associating models with hasOne relationship * Options provided when associating models with hasOne relationship
*/ */
export interface HasOneOptions extends AssociationOptions { export interface HasOneOptions extends AssociationOptions {
/**
* The name of the field to use as the key for the association in the source table. Defaults to the primary
* key of the source table
*/
sourceKey?: string;
/** /**
* A string or a data type to represent the identifier in the table * A string or a data type to represent the identifier in the table
*/ */
......
...@@ -56,6 +56,12 @@ export interface QueryOptions extends Logging, Transactionable { ...@@ -56,6 +56,12 @@ export interface QueryOptions extends Logging, Transactionable {
*/ */
instance?: Model; instance?: Model;
/**
* Map returned fields to model's fields if `options.model` or `options.instance` is present.
* Mapping will occur before building the model instance.
*/
mapToModel?: boolean;
retry?: RetryOptions; retry?: RetryOptions;
} }
......
// This file is used as example. // This file is used as example.
import { Sequelize, Model, DataTypes, BuildOptions } from 'sequelize'; import {BuildOptions, DataTypes, Model, Sequelize} from 'sequelize';
import { HasManyGetAssociationsMixin, HasManyAddAssociationMixin, HasManyHasAssociationMixin, Association, HasManyCountAssociationsMixin, HasManyCreateAssociationMixin } from '../../lib/associations'; import {
Association,
HasManyAddAssociationMixin,
HasManyCountAssociationsMixin,
HasManyCreateAssociationMixin,
HasManyGetAssociationsMixin,
HasManyHasAssociationMixin
} from '../../lib/associations';
import QueryTypes = require("../../lib/query-types");
class User extends Model { class User extends Model {
public id!: number; // Note that the `null assertion` `!` is required in strict mode. public id!: number; // Note that the `null assertion` `!` is required in strict mode.
...@@ -42,6 +50,14 @@ class Project extends Model { ...@@ -42,6 +50,14 @@ class Project extends Model {
public readonly updatedAt!: Date; public readonly updatedAt!: Date;
} }
class Address extends Model {
public userId!: number;
public address!: string;
public readonly createdAt!: Date;
public readonly updatedAt!: Date;
}
Project.init({ Project.init({
id: { id: {
type: new DataTypes.INTEGER.UNSIGNED(), // you can omit the `new` but this is discouraged type: new DataTypes.INTEGER.UNSIGNED(), // you can omit the `new` but this is discouraged
...@@ -80,12 +96,29 @@ User.init({ ...@@ -80,12 +96,29 @@ User.init({
sequelize: sequelize, // this bit is important sequelize: sequelize, // this bit is important
}); });
Address.init({
userId: {
type: new DataTypes.INTEGER.UNSIGNED(),
},
address: {
type: new DataTypes.STRING(128),
allowNull: false,
}
}, {
tableName: 'users',
sequelize: sequelize, // this bit is important
});
// Here we associate which actually populates out pre-declared `association` static and other methods. // Here we associate which actually populates out pre-declared `association` static and other methods.
User.hasMany(Project, { User.hasMany(Project, {
sourceKey: 'id',
foreignKey: 'ownerId', foreignKey: 'ownerId',
as: 'projects' // this determines the name in `associations`! as: 'projects' // this determines the name in `associations`!
}); });
Address.belongsTo(User, {targetKey: 'id'});
User.hasOne(Address,{sourceKey: 'id'});
async function stuff() { async function stuff() {
// Please note that when using async/await you lose the `bluebird` promise context // Please note that when using async/await you lose the `bluebird` promise context
// and you fall back to native // and you fall back to native
...@@ -105,6 +138,15 @@ async function stuff() { ...@@ -105,6 +138,15 @@ async function stuff() {
}); });
console.log(ourUser.projects![0].name); // Note the `!` null assertion since TS can't know if we included console.log(ourUser.projects![0].name); // Note the `!` null assertion since TS can't know if we included
// the model or not // the model or not
const user = await sequelize.query('SELECT * FROM users WHERE name = :userName',{
type: QueryTypes.SELECT,
replacements: {
userName: 'Johnny'
},
mapToModel: true,
model: User
})
} }
// Legacy models // Legacy models
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!