Socket
Socket
Sign inDemoInstall

mongoose-format

Package Overview
Dependencies
13
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    mongoose-format

A Mongoose plugin that enables you to produce formatted output of your instantiated models.


Version published
Weekly downloads
0
Maintainers
1
Install size
9.24 MB
Created
Weekly downloads
 

Readme

Source

mongoose-format

A Mongoose plugin that enables you to produce formatted output of your instantiated models.

Introduction

When using Mongoose, it is also likely that you are looking for different ways to output the contents of instances of your models.

But it is not always certain that you require all fields of your object, or maybe you wish them to be arranged in alternative manner.

You may even want to populate database references differently.

This plugin for Mongoose can solve all of those problems for you.

API


  // schema.js
  var formatPlugin = require('mongoose-format');

  // schema definition
  var schema = mongoose.schema({ ... });

  schema.plugin(formatPlugin);
  
  schema.methods.someMethod = function () {
    return this.value;
  };
  ...
  
  // model.js
  // we require the schema from above
  var schema = require(...);
  
  // create a model based on that schema
  var Model = mongoose.model('Schema', schema);
  
  // let's add a format
  Model.addFormat('api_v1', function (done) {
    var format = {
      fieldA: this.value,
      fieldB: this.some_method()
    };
    done(null, format);
  });
  
  // instantiate our model
  var model = new Model({ ... });
  
  model.format('api_v1', function (err, output) {
    console.log(output);
  });
  ...

I hope that piece of code is good enough to illustrate. I recommend using some kind of async flow control to create more advanced formats.

License

Copyright (C) 2012 Christoffer Hallas christoffer.hallas@forsvikgroup.com

Distributed under the MIT License, the same as Node.js.

Keywords

FAQs

Last updated on 05 Sep 2013

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc