npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mongoose-locks

v0.3.0

Published

document specific locks for fields

Downloads

159

Readme

mongoose-locks

Document specific locks for fields. Works like immutable fields but you can customize them for each document and modify them on the air.

Motivation

Mongoose's immutable fields have same behavior for each document in the collection but in some cases you might need locks for different fields for different documents.

Let's say we have a schema for Users with roles. That means, we'll have users with different roles in the collection. In a real world application, you might need locks on different fields by roles of those users.

For example; administrators have nothing locked on their document but sales people might have their allowed modules locked. In a collection for products, all products might have lock on SKU field but some of them might need locks on different fields by the need of the category which they belong to.

Install

npm i --save mongoose-locks

Configuration

  • name (string) : field name to store locked fields. it's "locks" by default.
  • default (array) : default value for locked fields. it's empty array by default.
  • helpers (boolean) : flag for adding helper methods. it's false by default.
  • throw (boolean) : flag for throwing errors instead of preventing silently process. it's false by default.

Method

If you want to access these methods, you need to set helpers option as true.

  • lockAll(): locks all fields except storage field
  • lockField(field): locks a field. you can't lock storage field
  • unlockAll(): unlocks all fields
  • unlockField(field): unlocks a field.

Examples

const locks = require('mongoose-locks');
const schema = mongoose.Schema({ name: String, job: String });
schema.plugin(locks, { name: 'locks', default: [ 'name' ], helpers: true });
UserModel = mongoose.model('User', schema);

const user = new UserModel({ name: 'John', job: 'Developer' });
await user.save(); // That's fine.
user.name = 'Michael';
await user.save(); // Throws error for name field!
user.unlockField('name');
user.lockField('job');
user.name = 'Michael';
user.job = 'Teacher';
await user.save(); // Throws error for job field!