mongoose-snake-to-camel

Mongoose plugin that creates camelCase aliases for snake_case properties in Mongoose models

Usage no npm install needed!

<script type="module">
  import mongooseSnakeToCamel from 'https://cdn.skypack.dev/mongoose-snake-to-camel';
</script>

README

mongoose-snake-to-camel

Build Status NPM version

A Mongoose plugin that creates camelCase aliases (virtuals) for snake_case properties. This was inspired by/refactored from mongoose-convention by Vadim Tyokov.

Installation

npm install mongoose-snake-to-camel

Usage

var mongoose = require('mongoose'),
    Schema = mongoose.Schema,
    snakeToCamel = require('mongoose-snake-to-camel');

var schema = new Schema({
    _hello: { type: Schema.Types.Mixed },
    doNotChange: { type: Schema.Types.Mixed },
    foo_bar: { type: Schema.Types.Mixed },
    what__is___this: { type: Schema.Types.Mixed }
});

schema.plugin(snakeToCamel);

var Thing = mongoose.model('Thing', schema);
var thing = new Thing({
    fooBar: 'foo bar'
});

console.log(thing.fooBar); //"foo bar"
console.log(thing.foo_bar); //"foo bar"

thing.whatIsThis = 'snakes';

console.log(thing.what__is___this); //"snakes"
console.log(thing.whatIsThis); //"snakes"

console.log('hello' in thing); //true

console.dir(thing.toCleanObject());
/*
{
    id: ..., //_id is converted to id automatically
    hello: ...,
    fooBar: ...,
    doNotChange: ...,
    whatIsThis: ...
}
*/