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

Commit cd8ee566 by Levi Bostian Committed by Simon Schick

fix(types): add missing findCreateFind type (#11068)

1 parent dcf079b2
Showing with 32 additions and 1 deletions
......@@ -1925,6 +1925,15 @@ export abstract class Model<T = any, T2 = any> extends Hooks {
): Promise<[M, boolean]>;
/**
* A more performant findOrCreate that will not work under a transaction (at least not in postgres)
* Will execute a find call, if empty then attempt to create, if unique constraint then attempt to find again
*/
public static findCreateFind<M extends Model>(
this: { new (): M } & typeof Model,
options: FindOrCreateOptions
): Promise<[M, boolean]>;
/**
* Insert or update a single row. An update will be executed if a row which matches the supplied values on
* either the primary key or a unique key is found. Note that the unique index must be defined in your
* sequelize model and not just in the table. Otherwise you may experience a unique constraint violation,
......
import { Association, HasOne, Model, Sequelize } from 'sequelize';
import { Association, HasOne, Model, Sequelize, DataTypes } from 'sequelize';
class MyModel extends Model {
public num!: number;
......@@ -36,3 +36,25 @@ MyModel.init({}, {
sequelize,
tableName: 'my_model'
});
/**
* Tests for findCreateFind() type.
*/
class UserModel extends Model {}
UserModel.init({
username: { type: DataTypes.STRING, allowNull: false },
beta_user: { type: DataTypes.BOOLEAN, allowNull: false }
}, {
sequelize: sequelize
})
UserModel.findCreateFind({
where: {
username: "new user username"
},
defaults: {
beta_user: true
}
})
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!