https://github.com/fvilers/mongoose-shortid
A Mongoose plugin that adds a virtual property with a shorter document ID
https://github.com/fvilers/mongoose-shortid
mongodb mongoose mongoose-plugin
Last synced: 4 months ago
JSON representation
A Mongoose plugin that adds a virtual property with a shorter document ID
- Host: GitHub
- URL: https://github.com/fvilers/mongoose-shortid
- Owner: fvilers
- License: mit
- Created: 2019-02-11T10:44:06.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2022-01-16T09:18:57.000Z (over 3 years ago)
- Last Synced: 2025-02-04T09:38:52.287Z (5 months ago)
- Topics: mongodb, mongoose, mongoose-plugin
- Language: JavaScript
- Size: 6.84 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# mongoose-shortid
A Mongoose plugin that adds a virtual property with a shorter document ID
## Support
If you use and like this library, feel free to support my Open Source projects.
[](https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=JZ26X897M9V9L¤cy_code=EUR)
## How to install
```
npm install @fvilers/mongoose-shortid
```or
```
yarn add @fvilers/mongoose-shortid
```## How to use
```
const mongoose = require('mongoose');
const shortId = require('@fvilers/mongoose-shortid');// This will add a virtual property `shortId` to all schemas
mongoose.plugin(shortId);
```## Options
You can choose the name of the virtual property by passing an option object and specifying the `propertyName` key.
```
const mongoose = require('mongoose');
const shortId = require('@fvilers/mongoose-shortid');// This will add a virtual property `myProperty` to all schemas
mongoose.plugin(shortId, { propertyName: 'myProperty' });
```## How does it work
The generated ObjectId will be transformed into a base62 representation which is url-safe as it doesn't contain characters like `+` or `/`. For example, a value of `5c614931a1f4b30021fa212b` will be transformed to a shortId value of `BbkLuPjY756F4VMT`.