
Product
Rust Support Now in Beta
Socket's Rust support is moving to Beta: all users can scan Cargo projects and generate SBOMs, including Cargo.toml-only crates, with Rust-aware supply chain checks.
@hoist/mongoose-shortid
Advanced tools
This plugin provides a new Schema Type, ShortId, that can be used in place of ObjectId. Typically you will want to use this instead of the default id
field, but you can also use ShortId for any other field. To make sure however the ShortId is unique, you should add an index on the field.
The generated IDs are random url-safe strings of configurable length, represented in a configurable base (10, 16, 32, 36, 62, 64 only).
This plugin will automatically retry inserts on a collision.
var mongoose = require('mongoose');
var ShortId = require('mongoose-shortid');
var personSchema = mongoose.Schema({
_id: ShortId,
name: String
});
// or
var anotherSchema = mongoose.Schema({
alternativeId: { type: ShortId, index: true},
name : String
});
The default options are:
var personSchema = mongoose.Schema({
_id: {
type: ShortId,
len: 7, // Length 7 characters
base: 64, // Web-safe base 64 encoded string
alphabet: undefined // Use default alphabet for base
retries: 4 // Four retries on collision
},
name: String
});
A custom alphabet can be provided using the alphabet
option. This takes priority over the base
argument.
var personSchema = mongoose.Schema({
_id: {
type: ShortId,
len: 9,
alphabet: 'fubar'
}
});
The generated IDs will be 9 characters long with only the characters f
u
b
a
and r
.
A custom ID generator function can be provided by setting the generator
option. This function will be called with two arguments: generatorOptions
and callback
.
The generatorOptions
is made up from the generatorOptions
object in the field options (if set), with len
, base
and alphabet
overriden if set on the field options.
The callback
function expects to be called with err
and id
parameters.
Here's an example:
var mongoose = require('mongoose');
var ShortId = require('mongoose-shortid');
function customIdGenerator(options, callback) {
var desiredLength = options.len || 7;
var base = options.base || 64;
var alphabet = options.alphabet || alphabetForBase(base);
var customOption = options.customOption;
// do Id generation
var generatedId = ...;
if (generatedId) {
callback(null, generatedId);
} else {
callback(err);
}
}
var exampleSchema = mongoose.Schema({
_id: {
type: ShortId,
len:4,
generator: customIdGenerator,
generatorOptions: { customOption: 'foo' }
},
name: String
});
FAQs
Short Ids for mongoose
We found that @hoist/mongoose-shortid 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.
Product
Socket's Rust support is moving to Beta: all users can scan Cargo projects and generate SBOMs, including Cargo.toml-only crates, with Rust-aware supply chain checks.
Product
Socket Fix 2.0 brings targeted CVE remediation, smarter upgrade planning, and broader ecosystem support to help developers get to zero alerts.
Security News
Socket CEO Feross Aboukhadijeh joins Risky Business Weekly to unpack recent npm phishing attacks, their limited impact, and the risks if attackers get smarter.