
Security News
npm Adopts OIDC for Trusted Publishing in CI/CD Workflows
npm now supports Trusted Publishing with OIDC, enabling secure package publishing directly from CI/CD workflows without relying on long-lived tokens.
mongoose-unique-shard
Advanced tools
When sharding unique indexes are only allowed for keys matching the shard index. This module allows to still define other properties to be unique which will then be modelled using an additional collection to keep track of unique fields.
When sharding unique indexes are only allowed for keys matching the shard index. This module allows to still define other properties to be unique which will then be modelled using an additional collection to keep track of unique fields.
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var mongooseUniqueShard = require('mongoose-unique-shard');
var testSchema = new Schema({
uniqueKey: { type: String, uniqueShardIndex: true },
subdoc: {
uniqueSubKey: { type: String, uniqueShardIndex: true }
},
combinedKey1: String,
combinedKey2: { type: String }
});
testSchema.plugin(mongooseUniqueShard, { mongoose: mongoose }); // make sure that you pass your local mongoose to the plugin so that we create our collection in the correct database
testSchema.addUniqueShardIndex(['combinedKey1', 'combinedKey2']);
var TestModel = mongoose.model('test', testSchema);
var tm1 = new TestModel();
tm1.uniqueKey = 'foo';
tm1.save(function() { // will work
var tm2 = new TestModel();
tm2.uniqueKey = 'foo';
tm2.save(); // will fail as there is already a model holding this unique key in the database
});
As unique keys are not allowed in a sharded environment we have to use a separate collection to check for already existing unique key values. If you make use of this model an additional mongo collection will 'mongooseuniqueshards' will be created in your database holding the information used to do the unique checks.
When validating an object we check the collection whether it contains an object that would violate the uniqueness of the current object. In case such an object is present validation of the object will fail. Make sure that your objects are validated prior to saving them.
If you use the document.delete function we will cleanup old indexes for you. Since it might happen that the original document is deleted by other means we keep a back reference to the _id of the document and its collection with each lock. When a lock for a value pair for a certain document is found, we double check that this document does still exist. In case it has been deleted we will also clean up the lock object. Beware: So far we do not double check the values in the reread document. Make sure that you do not modify documents outside of this applications as the lock objects will not be updated.
Because of the bummers you might want to have a batch job double checking your collection that it still ensures the uniqueness completely.
Feel free to contact me or add an issue to the issue backlog on github.
Run npm test
to run the test suite. But make sure you have a mongo database running at mongodb://localhost:27017
FAQs
When sharding unique indexes are only allowed for keys matching the shard index. This module allows to still define other properties to be unique which will then be modelled using an additional collection to keep track of unique fields.
The npm package mongoose-unique-shard receives a total of 1 weekly downloads. As such, mongoose-unique-shard popularity was classified as not popular.
We found that mongoose-unique-shard 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
npm now supports Trusted Publishing with OIDC, enabling secure package publishing directly from CI/CD workflows without relying on long-lived tokens.
Research
/Security News
A RubyGems malware campaign used 60 malicious packages posing as automation tools to steal credentials from social media and marketing tool users.
Security News
The CNA Scorecard ranks CVE issuers by data completeness, revealing major gaps in patch info and software identifiers across thousands of vulnerabilities.