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

mongoose-to-csv

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mongoose-to-csv - npm Package Compare versions

Comparing version 0.0.1 to 0.0.2

test/expected.csv

39

index.js

@@ -106,3 +106,3 @@ /**

* @param {Object|Function} [options = {}] Any valid options for `fs.createWriteStream`
*
* @api public
* @return {Stream}

@@ -114,3 +114,3 @@ * ###Ex

* .data(myQuery)
* .run()
* .save()
* .on('finish', function() {

@@ -121,6 +121,6 @@ * // done

MongooseToCsv.prototype.run = function (options) {
MongooseToCsv.prototype.save = function (options) {
options = (options || {});
if (!this._filename || !this._data || !this._model) {
throw new Error('Must have model, filename and data to run');
throw new Error('Must have model, filename and data to save');
}

@@ -144,2 +144,32 @@

/**
* Just Data, in csv format. Async callback.
*
* @param {Function} next callback(err, data)
*
*/
MongooseToCsv.prototype.run = function (next) {
var headersMap = this._createHeaders()
, headers = headersMap.map(function (obj){return obj.header;}).join(', ')
, self = this;
if (!self._data || !self._model) {
throw new Error('Must have model, and data to run');
}
return process.nextTick(function () {
var data = [];
try {
data.push(headers);
self._data.forEach(function (obj) {
data.push(objToRow(headersMap, obj));
});
return next(null, data.join('\n'));
} catch (e) {
return next(e);
}
});
};
/**
* Create the headers Obj

@@ -152,2 +182,3 @@ *

* and header is the csv header that values of the `actual` property will correspond to.
* @api private
*/

@@ -154,0 +185,0 @@

2

package.json
{
"name": "mongoose-to-csv",
"version": "0.0.1",
"version": "0.0.2",
"author": "Nick Pisacane <pisacanen@gmail.com> (http://nickpisacane.com)",

@@ -5,0 +5,0 @@ "description": "Export mongoose querys to csv.",

@@ -43,5 +43,8 @@ # MongooseToCsv

If function, then it should be a one to all transformative function, that takes a single property as an argument.
* <h3>MongooseToCsv#run()
* returns a WritableStream
## Example
* <h3>MongooseToCsv#run(next)
* next Function (err, data)
* <h3>MongooseToCsv#save()</h3>
* returns writeable stream
## Example Saving
```js

@@ -59,3 +62,3 @@

})
.run()
.save()
.on('finish', function () {

@@ -65,2 +68,21 @@ // export.csv is ready

});
```
## Example Running
```js
MyModel.find({}, function (err, doc) {
// handler err
mongooseToCsv()
.filename('export.csv')
.model(MyModel)
.exclude('_id')
.order('name email phone street')
.use(function (prop) {
return (prop.slice(0, 1).toUpperCase() + prop.slice(1))
})
.run(function (err, data) {
// data is ready
})
});
```

@@ -43,21 +43,23 @@ // mocha

var expected = [
'Name, Email, BadgeNumber\n',
'Nick, pisacanen@gmail.com, 1\n',
'John, john@gmail.com, 2\n',
'Joe, joe@gmail.com, 3\n',
'Sally, sally@gmail.com, 4\n',
'Sarah, sarah@gmail.com, 5\n',
'Smith, smith@gmail.com, 6\n',
'Josh, josh@gmail.com, 7\n',
'John, john@gmail.com, 8\n',
'Mike, mike@gmail.com, 9\n',
'Tammy, tammy@gmail.com, 10\n',
'Wendy, wendy@gmail.com, 11\n'
].join('');
var expectedData = [
'Name, Email, BadgeNumber',
'Nick, pisacanen@gmail.com, 1',
'John, john@gmail.com, 2',
'Joe, joe@gmail.com, 3',
'Sally, sally@gmail.com, 4',
'Sarah, sarah@gmail.com, 5',
'Smith, smith@gmail.com, 6',
'Josh, josh@gmail.com, 7',
'John, john@gmail.com, 8',
'Mike, mike@gmail.com, 9',
'Tammy, tammy@gmail.com, 10',
'Wendy, wendy@gmail.com, 11'
].join('\n');
var expectedCsv = fs.readFileSync(__dirname + '/expected.csv').toString();
describe('Mongoose To Csv', function () {
it('should create a csv file with custom headers', function (done) {
mongooseToCsv()
.filename('test.csv')
var test = mongooseToCsv()
.filename(__dirname + '/test.csv')
.model(TestModel)

@@ -72,4 +74,7 @@ .data(data)

'IdNumber': 'BadgeNumber'
})
.run()
});
it('should create a csv file with custom headers', function (done) {
test.save()
.on('finish', function () {

@@ -80,4 +85,3 @@ fs.readFile(__dirname + '/test.csv', function (err, data) {

}
data = String(data);
assert.equal(expected);
assert.equal(data, expectedCsv);
done();

@@ -87,2 +91,13 @@ })

});
it('should create csv formated data without saving a file', function (done) {
test.run(function (err, data) {
if (err) {
return done(err);
}
assert.equal(data, expectedData);
done();
});
});
});
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