How to use the UniqueConstraintError function from sequelize

Find comprehensive JavaScript sequelize.UniqueConstraintError code examples handpicked from public code repositorys.

149
150
151
152
153
154
155
156
157
158
const found = await User.findAll({ where, paranoid: false });
return Promise.map(users, userData => Promise.try(() => {
  const user = find(found, { email: userData.email });
  if (user && !user.deletedAt) {
    const message = this.rawAttributes.email.unique.msg;
    throw new UniqueConstraintError({ message });
  }
  if (user) {
    user.setDataValue('deletedAt', null);
    return user;
fork icon4
star icon0
watch icon0

34
35
36
37
38
39
40
41
42
43
register(null, {
    database: {
        db: sequelize,
        errors: {
            ValidationError: Sequelize.ValidationError,
            UniqueConstraintError: Sequelize.UniqueConstraintError
        },
        models: {
            Bid: Bid,
            Item: Item,
fork icon0
star icon12
watch icon3

149
150
151
152
153
154
155
156
157
158
    }
  } else {
    return exports.addUser(user);
  }
};
exports.addUser = async user => { // catch for Sequelize.UniqueConstraintError
    let newUser = null;
    try {
      newUser = await User.create(user);
      const newToken = jwtUtils.signToken({userId: newUser.userId, email: newUser.email});
fork icon1
star icon0
watch icon2

30
31
32
33
34
35
36
37
38
39
});

it('should throw an error if username is already taken', async () => {
  jest
    .spyOn(db, 'create')
    .mockRejectedValueOnce(new UniqueConstraintError());
  await expect(AuthService.createUser('test', 'password')).rejects.toEqual(
    expect.objectContaining({ message: 'Username already exists' })
  );
});
fork icon0
star icon0
watch icon1

14
15
16
17
18
19
20
21
22
23
// eslint-disable-next-line no-empty-function
exports.notifyErrorDatabase = (e, additionalMessages = () => {}) => {
  console.log("ERROR DB:", e);
  if (
    e instanceof Sequelize.ForeignKeyConstraintError ||
    e instanceof Sequelize.UniqueConstraintError
  ) {
    const message = additionalMessages(e);
    //return Promise.reject(errors.badRequest(message || defaultErrorMessage(e)));
    return Promise.reject(
fork icon0
star icon0
watch icon0