Skip to content

Commit

Permalink
Added support for root collection
Browse files Browse the repository at this point in the history
  • Loading branch information
devconcept committed May 26, 2016
1 parent 6d96b4f commit ebeecd2
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 3 deletions.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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**
Expand Down
4 changes: 3 additions & 1 deletion lib/gridfs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down
21 changes: 19 additions & 2 deletions test/options.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ describe('module usage', function () {
cb(null, Math.floor(Math.random() * 1000000));
},
chunkSize: 131072,
root: 'myfiles',
log: true,
logLevel: 'all'
});
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit ebeecd2

Please sign in to comment.