New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

objection-unique

Package Overview
Dependencies
Maintainers
3
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

objection-unique

objection-unique

  • 1.2.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
173
decreased by-88.35%
Maintainers
3
Weekly downloads
 
Created
Source

Unique validation for Objection.js

npm node Build Status Coverage Status

This plugin adds a unique validation for Objection.js models.

NOTE: Unique validation at update only works with $query methods.

Installation

NPM

npm i objection-unique --save

Yarn

yarn add objection-unique

Usage

Mixin the plugin

// 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';
  }
}

Validate insert

/**
 * 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.'
    //   }
    // }
}

Validate update/patch

/**
 * 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.'
  //   }]
  // }
}

Options

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']
});

Tests

Run the tests from the root directory:

npm test

Contributing & Development

Contributing

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.

Develop

It will be awesome if you can help us evolve objection-unique. Want to help?

  1. Fork it.
  2. npm install.
  3. Hack away.
  4. Run the tests: npm test.
  5. Create a Pull Request.

Keywords

FAQs

Package last updated on 26 May 2021

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc