From ebeecd246721b051b163430df02766cd5c2c5e07 Mon Sep 17 00:00:00 2001 From: devconcept Date: Thu, 26 May 2016 00:12:20 -0400 Subject: [PATCH] Added support for root collection --- README.md | 25 +++++++++++++++++++++++++ lib/gridfs.js | 4 +++- test/options.spec.js | 21 +++++++++++++++++++-- 3 files changed, 47 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index e3ca4e29..70b904b6 100644 --- a/README.md +++ b/README.md @@ -260,6 +260,31 @@ var storage = require('multer-gridfs-storage')({ var upload = multer({ storage: storage }); ``` +#### root + +Type: **String** + +Not required + +The root collection to store the files. By default this value is `null`. + +Example: + +```javascript +var storage = require('multer-gridfs-storage')({ + url: 'mongodb://localhost:27017/database', + root: 'myfiles' +}); +var upload = multer({ storage: storage }); +``` + +Later on you can query the GridFS collection using + +```javascript +db.collection('myfiles.files')//... +db.collection('myfiles.chunks')//... +``` + #### log Type: **Boolean** diff --git a/lib/gridfs.js b/lib/gridfs.js index 7a683615..11122fb1 100644 --- a/lib/gridfs.js +++ b/lib/gridfs.js @@ -18,7 +18,8 @@ Storage.prototype._handleFile = function _handleFile(req, file, cb) { var self = this; var streamOpts = { content_type: file.mimetype, - chunkSize: self.chunkSize + chunkSize: self.chunkSize, + root: self.root }; self.getIdentifier(req, file, function (err, id) { if (err) { @@ -93,6 +94,7 @@ function GridFSStorage(opts) { self.getFilename = (opts.filename || getFilename); self.getMetadata = (opts.metadata || noop); self.chunkSize = (opts.chunkSize || 261120); + self.root = (opts.root || null); if (!opts.gfs) { self.gfs = null; diff --git a/test/options.spec.js b/test/options.spec.js index 66d9d51e..e7270ea3 100644 --- a/test/options.spec.js +++ b/test/options.spec.js @@ -33,6 +33,7 @@ describe('module usage', function () { cb(null, Math.floor(Math.random() * 1000000)); }, chunkSize: 131072, + root: 'myfiles', log: true, logLevel: 'all' }); @@ -129,10 +130,26 @@ describe('module usage', function () { done(); }); + it('should be stored under a different root', function (done) { + db.collection('myfiles.files', {strict:true}, function (err, col) { + expect(err).to.be.equal(null); + db.collection('myfiles.chunks', {strict:true}, function (err, col) { + expect(err).to.be.equal(null); + db.collection('fs.files', {strict:true}, function (err, col) { + expect(err).not.to.be.equal(null); + db.collection('fs.chunks', {strict:true}, function (err, col) { + expect(err).not.to.be.equal(null); + done(); + }); + }); + }); + }); + }); + after(function (done) { - db.collection('fs.files').deleteMany({}) + db.collection('myfiles.files').deleteMany({}) .then(function () { - return db.collection('fs.chunks').deleteMany({}); + return db.collection('myfiles.chunks').deleteMany({}); }) .then(function () { done();