Security News
UK Officials Consider Banning Ransomware Payments from Public Entities
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.
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', ['phone_prefix','phone_number']],
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. Compound fields can be specified as an array
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', ['phone_prefix', 'phone_number']],
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.2.2 (2021-05-26)
Merged pull requests:
FAQs
objection-unique
The npm package objection-unique receives a total of 1,107 weekly downloads. As such, objection-unique popularity was classified as popular.
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
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.
Security News
Snyk's use of malicious npm packages for research raises ethical concerns, highlighting risks in public deployment, data exfiltration, and unauthorized testing.
Research
Security News
Socket researchers found several malicious npm packages typosquatting Chalk and Chokidar, targeting Node.js developers with kill switches and data theft.