mongoose-to-csv
Advanced tools
Comparing version 0.0.1 to 0.0.2
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 @@ |
{ | ||
"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(); | ||
}); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
13001
7
332
85