diff --git a/types/lib/model.d.ts b/types/lib/model.d.ts index 99280c70433b..5589662ca1ce 100644 --- a/types/lib/model.d.ts +++ b/types/lib/model.d.ts @@ -715,12 +715,20 @@ export interface Hookable { * Options for Model.findOrCreate method */ export interface FindOrCreateOptions - extends FindOptions + extends FindOptions, CreateOptions { /** - * 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 + extends FindOptions, BuildOptions +{ /** * Default values to use if building a new instance */ @@ -2033,7 +2041,7 @@ export abstract class Model( this: ModelStatic, - options: FindOrCreateOptions + options: FindOrBuildOptions ): Promise<[M, boolean]>; /** diff --git a/types/test/model.ts b/types/test/model.ts index 870d8be2dc76..53294e19f1f3 100644 --- a/types/test/model.ts +++ b/types/test/model.ts @@ -104,14 +104,21 @@ const model: typeof MyModel = MyModel.init({ } }); +type UserModelAttributes = { + username: string, + beta_user: boolean, +}; + +type UserCreationAttributes = Optional; + /** * Tests for findCreateFind() type. */ -class UserModel extends Model {} +class UserModel extends Model {} 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 }) @@ -121,7 +128,8 @@ UserModel.findCreateFind({ username: "new user username" }, defaults: { - beta_user: true + beta_user: true, + username: "new user username" } }) @@ -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; /**