mongoose-snake-to-camel

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

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
mongoose-snake-to-camel
000.3.05 years ago8 years agoMinified + gzip package size for mongoose-snake-to-camel in KB

Readme

mongoose-snake-to-camel
Build Status NPM version
A Mongoose plugin that creates camelCase aliases (virtuals) for snakecase 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: ...
}
*/