Simple mongoose plugin that auto convert ObjectId to string
npm install @cylution/mongoose-plugin-object-id-to-stringSimple mongoose plugin that auto convert ObjectId to string
npm i @cylution/mongoose-plugin-object-id-to-string
or
yarn add @cylution/mongoose-plugin-object-id-to-string
> This plugin will add toObject: { getters: true } to mongoose schema options
>
> If you don't want to modify a schema, just add toObject: {} to schema options
``js`
const demoSchema = new Schema({
name: String
}, {
toObject: {}, // will ignore toObject in global usecase
toJSON: {}, // will ignore toJSON in global usecase
})
#### Typescript
`ts`
import { mongoosePluginObjectIdToString } from '@cylution/mongoose-plugin-object-id-to-string'
import { plugin, Schema } from 'mongoose'
#### Javascript
`js`
const { mongoosePluginObjectIdToString } = require('@cylution/mongoose-plugin-object-id-to-string')
const { plugin } = require('mongoose')
`js
// don't forget this
const pluginObjectIdToString = mongoosePluginObjectIdToString(
v => v == null ? null : v.toString(), // convert function, can be simple: v => v.toString() if you ensure v is never null
[
'toObject',
'toJSON',
] // options
)
// global
plugin(pluginObjectIdToString)
// or schema
const userSchema = new Schema({
//
})
userSchema.plugin(pluginObjectIdToString)
``
This project is licensed under the MIT License.