Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(types): add missing fields to FindOr{Create,Build}Options #13389

Merged
merged 6 commits into from Jan 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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