Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/tarquas/mongoose-hook-custom-id
Mongoose plugin to use String _id and by default will generate 16-char base64 string representation of ObjectId.
https://github.com/tarquas/mongoose-hook-custom-id
Last synced: 8 days ago
JSON representation
Mongoose plugin to use String _id and by default will generate 16-char base64 string representation of ObjectId.
- Host: GitHub
- URL: https://github.com/tarquas/mongoose-hook-custom-id
- Owner: tarquas
- License: mit
- Created: 2015-03-20T08:13:20.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2015-05-12T09:42:09.000Z (over 9 years ago)
- Last Synced: 2025-01-03T16:39:04.863Z (14 days ago)
- Language: JavaScript
- Size: 156 KB
- Stars: 0
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# mongoose-hook-custom-id
Mongoose plugin to use String _id and by default will generate 16-char base64 string representation of ObjectId.# Installation
```shell
git clone [email protected]:tarquas/mongoose-hook-custom-id.git mongoose-hook-custom-id
```# Package
```js
{
"mongoose-hook-custom-id": "0.1.4"
}
```# Usage
Example:
```js
var
mongoose = require('mongoose'),
customId = require('mongoose-hook-custom-id'),
PersonSchema;PersonSchema = {
name: String,
email: String
};PersonSchema.plugin(customId, {mongoose: mongoose}); // _id will look like 'VQvnBImPTGAoqeVY'
//PersonSchema.plugin(customId, {mongoose: mongoose,
generator: function() {return Math.random();}}); // _id will look like '0.45921047893352807'
//PersonSchema.plugin(customId, {mongoose: mongoose,
generatorAsync: function(callback) {callback(Math.random());}}); // same as above, but using async generatormongoose.model('Person', PersonSchema);
```# Notes
* This plugin must be provided with an exact instance of `mongoose`, where the processing models expected to be processed, in `opts` parameter.
* This plugin forces _id field on Schema to be of type `String`. New documents will get it generated, if it wasn't specified.
* By default (if no specific generator option is provided to plugin), plugin will generate URI-compatible base64-representation of generated ObjectId, which is 16 characters long.
* If `generator` option is specified, it's a function, taking no arguments and returning `String` result, which will be called every time the new _id needs to be generated.
* If `generatorAsync` option is specified, it's a function, taking a callback argument, which accepts `String` parameter. Async generator will be called every time the new _id needs to be generated.