mongoose-snake-to-camel

0.3.0 • Public • Published

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: ...
}
*/

Package Sidebar

Install

npm i mongoose-snake-to-camel

Weekly Downloads

171

Version

0.3.0

License

MIT

Unpacked Size

5.2 kB

Total Files

4

Last publish

Collaborators

  • tmont