Skip to content
This repository has been archived by the owner on Aug 27, 2020. It is now read-only.

offlinehacker/bookshelf-check-duplicates

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Bookshelf check duplicates

Bookshelf plugin that checks for duplicates in a database.

Installation

npm install bookshelf-check-duplicates

Then in your bookshelf configuration:

var bookshelf = require('bookshelf')(knex);
bookshelf.plugin(require('bookshelf-check-duplicates');

Usage

Define bookshelf model with additional attributes

var User = bookshelf.Model.extend({
  tableName: 'users',
  duplicates: ['name']
});

User.forge({name: 'admin'}).save().then(function(admin) {
  User.forge({name: 'user'}).save().catch(function(err) {
    assert(err instanceof bookshelf.Model.DuplicateError);
  });
});

Whenever model is saved validateDuplicates will check if there's already a saved model with same value of specified fields, and if there is bookshelf.Model.DuplicateError error will be thrown.

License

MIT

Author

offlinehacker

About

Bookshelf plugin that checks for duplicates in a database

Resources

Stars

Watchers

Forks

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •