
Security News
Node.js TSC Votes to Stop Distributing Corepack
Corepack will be phased out from future Node.js releases following a TSC vote.
objection-unique
Advanced tools
This plugin adds a unique validation for Objection.js models.
NOTE: Unique validation at update only works with $query
methods.
npm i objection-unique --save
yarn add objection-unique
// Import objection model.
const Model = require('objection').Model;
// Import the plugin.
const unique = require('objection-unique')({
fields: ['email', 'username'],
identifiers: ['id']
});
// Mixin the plugin.
class User extends unique(Model) {
static get tableName() {
return 'User';
}
}
/**
* Insert.
*/
// Insert one user.
await User.query().insert({ email: 'foo', username: 'bar' });
try {
// Try to insert another user with the same data.
await User.query().insert({ email: 'foo', username: 'bar' });
} catch (e) {
// Exception with the invalid unique fields
//
// {
// email: [{
// keyword: 'unique',
// message: 'email already in use.'
// }],
// username: [{
// keyword: 'unique',
// message: 'username already in use.'
// }
// }
}
/**
* Update/Patch.
*/
// Insert one user.
await User.query().insert({ email: 'foo', username: 'bar' });
// Insert the user that we want to update.
const user = await User.query().insertAndFetch({ email: 'biz', username: 'buz' });
try {
user.$query().update({ email: 'foo', username: 'buz' });
// user.$query().patch({ email: 'foo' });
} catch (e) {
// Exception with the invalid unique fields
//
// {
// email: [{
// keyword: 'unique',
// message: 'email already in use.'
// }]
// }
}
fields: The unique fields.
identifiers: The fields that identifies the model. (Default: ['id'])
These options can be provided when instantiating the plugin:
const unique = require('objection-unique')({
fields: ['email', 'username'],
identifiers: ['id']
});
Run the tests from the root directory:
npm test
Found a bug or want to suggest something? Take a look first on the current and closed issues. If it is something new, please submit an issue.
It will be awesome if you can help us evolve objection-unique
. Want to help?
npm install
.npm test
.v1.0.1 (2018-03-26)
Merged pull requests:
FAQs
objection-unique
We found that objection-unique demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Corepack will be phased out from future Node.js releases following a TSC vote.
Research
Security News
Research uncovers Black Basta's plans to exploit package registries for ransomware delivery alongside evidence of similar attacks already targeting open source ecosystems.
Security News
Oxlint's beta release introduces 500+ built-in linting rules while delivering twice the speed of previous versions, with future support planned for custom plugins and improved IDE integration.