Skip to content

Commit

Permalink
fix(types): add missing fields to FindOr{Create,Build}Options (#13389)
Browse files Browse the repository at this point in the history
Co-authored-by: Zoé <zoe@ephys.dev>
  • Loading branch information
devagrawal09 and ephys committed Jan 12, 2022
1 parent 6d4b553 commit 81b0b9d
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 10 deletions.
16 changes: 12 additions & 4 deletions types/lib/model.d.ts
Expand Up @@ -715,12 +715,20 @@ export interface Hookable {
* Options for Model.findOrCreate method
*/
export interface FindOrCreateOptions<TAttributes = any, TCreationAttributes = TAttributes>
extends FindOptions<TAttributes>
extends FindOptions<TAttributes>, CreateOptions<TAttributes>
{
/**
* The fields to insert / update. Defaults to all fields
* Default values to use if building a new instance
*/
fields?: (keyof TAttributes)[];
defaults?: TCreationAttributes;
}

/**
* Options for Model.findOrBuild method
*/
export interface FindOrBuildOptions<TAttributes = any, TCreationAttributes = TAttributes>
extends FindOptions<TAttributes>, BuildOptions
{
/**
* Default values to use if building a new instance
*/
Expand Down Expand Up @@ -2033,7 +2041,7 @@ export abstract class Model<TModelAttributes extends {} = any, TCreationAttribut
*/
public static findOrBuild<M extends Model>(
this: ModelStatic<M>,
options: FindOrCreateOptions<M['_attributes'], M['_creationAttributes']>
options: FindOrBuildOptions<M['_attributes'], M['_creationAttributes']>
): Promise<[M, boolean]>;

/**
Expand Down
55 changes: 49 additions & 6 deletions types/test/model.ts
Expand Up @@ -104,14 +104,21 @@ const model: typeof MyModel = MyModel.init({
}
});

type UserModelAttributes = {
username: string,
beta_user: boolean,
};

type UserCreationAttributes = Optional<UserModelAttributes, 'beta_user'>;

/**
* Tests for findCreateFind() type.
*/
class UserModel extends Model {}
class UserModel extends Model<UserModelAttributes, UserCreationAttributes> {}

UserModel.init({
username: { type: DataTypes.STRING, allowNull: false },
beta_user: { type: DataTypes.BOOLEAN, allowNull: false }
beta_user: { type: DataTypes.BOOLEAN, allowNull: false, defaultValue: false }
}, {
sequelize: sequelize
})
Expand All @@ -121,7 +128,8 @@ UserModel.findCreateFind({
username: "new user username"
},
defaults: {
beta_user: true
beta_user: true,
username: "new user username"
}
})

Expand All @@ -132,19 +140,54 @@ UserModel.getAttributes();
*/

UserModel.findOrCreate({
fields: [ "jane.doe" ],
// 'create' options
hooks: true,
fields: ['username'],
ignoreDuplicates: true,
returning: true,
validate: true,
raw: true,
isNewRecord: true,
include: [],

// 'find' options
paranoid: true,
where: {
username: "jane.doe"
},

// 'findOrCreate' options
defaults: {
username: "jane.doe"
}
})
});

/**
* Tests for findOrBuild() type.
*/

UserModel.findOrBuild({
// 'build' options
raw: true,
isNewRecord: true,
include: [],

// 'find' options
paranoid: true,
where: {
username: "jane.doe"
},

// 'findOrCreate' options
defaults: {
username: "jane.doe"
}
});

/**
* Test for primaryKeyAttributes.
*/
class TestModel extends Model {};
class TestModel extends Model {}
TestModel.primaryKeyAttributes;

/**
Expand Down

0 comments on commit 81b0b9d

Please sign in to comment.