Socket
Socket
Sign inDemoInstall

mg-to-csv

Package Overview
Dependencies
3
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    mg-to-csv

Export mongoose querys as csv streams.


Version published
Maintainers
1
Created

Readme

Source

mongoose-to-csv

MongooseToCsv is a mongoose plugin that creates a CsvBuilder instance for your Schema.

Usage

var UserSchema = new mongoose.Schema({
  fullname: {type: String},
  email: {type: String},
  age: {type: Number},
  username: {type: String}
});

UserSchema.plugin(mongooseToCsv, {
  headers: 'Firstname Lastname Username Email Age',
  constraints: {
    'Username': 'username',
    'Email': 'email',
    'Age': 'age'
  },
  virtuals: {
    'Firstname': function(doc) {
      return doc.fullname.split(' ')[0];
    },
    'Lastname': function(doc) {
      return doc.fullname.split(' ')[1];
    }
  }
});

var User = mongoose.model('Users', UserSchema);

// Query and stream
User.findAndStreamCsv({age: {$lt: 40}})
  .pipe(fs.createWriteStream('users_under_40.csv'));

// Create stream from query results
User.find({}).exec()
  .then(function(docs) {
    User.csvReadStream(docs)
      .pipe(fs.createWriteStream('users.csv'));
  });

// Transform mongoose streams
User.find({})
  .where('age').gt(20).lt(30)
  .limit(10)
  .sort('age')
  .stream()
  .pipe(User.csvTransformStream())
  .pipe(fs.createWriteStream('users.csv'));

Installation

$ npm install mongoose-to-csv

Testing

Running tests requires a local mongodb server, and mocha. While most likely not a namespace issue, the test script will create a database __mongoose_to_csv_test__, and drop the database when finished. You have been warned.

$ npm test 

API

Schema.plugin(mongooseToCsv, options)

The options argument is passed to the CsvBuilder instance, please refer to the Docs for more in-depth details. The only aditional property that can be included is the virutals property. The virtuals have nothing to do with mongoose virtuals.

Schema.csvReadStream([docs])

Creates a csv formated read stream from query results.

  • docs Array

Schema.csvTransformStream()

Transforms mongoose querystreams to csv formated streams.

Schema.findAndStreamCsv(query)

  • query Object Mongoose query This is just a convenience method for:
Schema.find(query).stream().pipe(Schema.csvTransformStream())

Keywords

FAQs

Last updated on 19 Dec 2016

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