Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

mongoose-format

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

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.

  • 2.0.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
15
increased by1400%
Maintainers
1
Weekly downloads
 
Created
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

Package last updated on 05 Sep 2013

Did you know?

Socket

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc