Socket
Socket
Sign inDemoInstall

mongoose-gridfs

Package Overview
Dependencies
Maintainers
1
Versions
70
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mongoose-gridfs - npm Package Compare versions

Comparing version 0.5.0 to 0.6.0

38

lib/schema.js

@@ -22,32 +22,14 @@ 'use strict';

// fs schema
const GridFSSchema = new Schema({
length: {
type: Number
},
chunkSize: {
type: Number
},
uploadDate: {
type: Date
},
md5: {
type: String
},
filename: {
type: String
},
contentType: {
type: String
},
aliases: {
type: [String]
},
metadata: {
type: Mixed
},
}, {
collection: collection
});
length: { type: Number },
chunkSize: { type: Number },
uploadDate: { type: Date },
md5: { type: String },
filename: { type: String },
contentType: { type: String },
aliases: { type: [String] },
metadata: { type: Mixed },
}, { collection: collection });
//attach GridFSStorage instance

@@ -54,0 +36,0 @@ GridFSSchema.statics.gridfs = gridFSStorage;

@@ -8,3 +8,2 @@ 'use strict';

const mongoose = require('mongoose');
const Grid = require('@lykmapipo/gridfs-stream');
const read = require('stream-read');

@@ -14,3 +13,3 @@

/* declarations */
Grid.mongo = mongoose.mongo; /* use mongoose mongo for grid */
const GridFSBucket = mongoose.mongo.GridFSBucket;
const schema = require(path.join(__dirname, 'schema'));

@@ -28,3 +27,3 @@

* @since 0.1.0
* @version 0.4.0
* @version 0.6.0
* @public

@@ -34,3 +33,3 @@ */

//normalize options
// normalize options
this.options = _.merge({}, {

@@ -42,6 +41,10 @@ model: 'File',

//get either option connection or mongoose connection
this.conn = this.options.mongooseConnection || mongoose.connection;
// ensure bucketName
this.options.bucketName =
(this.options.bucketName || this.options.collection);
//Check if mongoose is connected.
// get either option connection or default mongoose connection
this.conn = (this.options.mongooseConnection || mongoose.connection);
// check if mongoose is connected.
if (this.conn.readyState !== 1) {

@@ -51,4 +54,6 @@ throw new Error('Mongoose is not connected');

//initialize grid
this.storage = new Grid(this.conn.db);
// initialize grid
// TODO pass/support additional bucket options
// TODO rename grid to bucket
this.storage = new GridFSBucket(this.conn.db, this.options);

@@ -58,5 +63,5 @@ }

//getters
/**

@@ -68,3 +73,3 @@ * @name collection

* @since 0.1.0
* @version 0.4.0
* @version 0.6.0
* @instance

@@ -74,3 +79,3 @@ */

get: function () {
return this.storage.collection(this.options.collection);
return this.storage.s._filesCollection;
}

@@ -142,5 +147,5 @@ });

//writers
/**

@@ -150,3 +155,3 @@ * @function

* @param {Object} [fileDetails] valid mongodb GridFS file details
* @param {ReadableStream} stream valid nodejs ReadableStream
* @param {ReadableStream} readstream valid nodejs ReadableStream
* @param {Function} done a callback to invoke on success or error

@@ -158,3 +163,3 @@ * @returns {WritableStream}

* @since 0.1.0
* @version 0.4.0
* @version 0.6.0
* @instance

@@ -166,5 +171,6 @@ */

function write(fileDetails, stream, done) {
function write(fileDetails, readstream, done) {
//ensure filename
// ensure filename
//TODO if no filename generate new objectid for saving
if (!fileDetails || !fileDetails.filename) {

@@ -174,38 +180,30 @@ throw new Error('Missing filename');

//obtain collection
const collection = this.options.collection;
// prepare file details
//TODO force required options check openUploadStream(filename, options)
//TODO ensure id is valid object id
fileDetails = _.merge({}, fileDetails);
const { _id, id, filename } = fileDetails;
const __id = (_id || id);
//prepare file details
fileDetails = _.merge({
root: collection, //root collection to use with GridFS for storage
}, fileDetails);
// initialize gridfs bucket write stream
const writestream = (
__id ?
this.storage.openUploadStreamWithId(__id, filename, fileDetails) :
this.storage.openUploadStream(filename, fileDetails)
);
//normalize contentType field
if (fileDetails.contentType) {
// stream file into mongodb gridfs bucket
readstream.pipe(writestream);
// jshint camelcase:false
fileDetails.content_type = fileDetails.contentType;
// jshint camelcase:true
delete fileDetails.contentType;
}
// initialize gridfs write stream
const writestream = this.storage.createWriteStream(fileDetails);
//stream file into mongodb GridFS
stream.pipe(writestream);
//work on the stream
// work on the stream
if (done && _.isFunction(done)) {
//handle errors
// handle errors
writestream.on('error', function (error) {
done(error);
return done(error);
});
//finalize write
writestream.on('close', function (file) {
done(null, file);
// finalize write
writestream.on('finish', function (file) {
return done(null, file);
});

@@ -223,5 +221,5 @@

//removers
/**

@@ -235,3 +233,3 @@ * @function

* @since 0.1.0
* @version 0.4.0
* @version 0.6.0
* @instance

@@ -241,14 +239,10 @@ */

//obtain collection
const collection = this.options.collection;
//prepare file remove options
options = _.merge({
root: collection
}, options);
options = _.merge({}, options);
//TODO ensure filename or _id exists in options
//TODO support options
//delete(id, callback)
//remove file from storage
this.storage.remove(options, done);
this.storage.delete(options._id, done);

@@ -265,10 +259,11 @@ };

* @since 0.1.0
* @version 0.4.0
* @version 0.6.0
* @instance
*/
GridFSStorage.prototype.unlinkById = function unlinkById(id, done) {
//TODO support options
//delete(id, callback)
//remove file from storage
this.unlink({
_id: id
}, function (error) {
this.unlink({ _id: id }, function (error) {
if (error) {

@@ -283,5 +278,5 @@ done(error);

//finders
/**

@@ -296,17 +291,23 @@ * @function

* @since 0.1.0
* @version 0.4.0
* @version 0.6.0
* @instance
*/
GridFSStorage.prototype.findOne = function (options, done) {
GridFSStorage.prototype.findOne = function findOne(options, done) {
//obtain collection
const collection = this.options.collection;
// TODO support options
// find(filter, options)
//prepare file remove options
options = _.merge({
root: collection
}, options);
// prepare file remove options
options = _.merge({}, options);
//find one existing file
this.storage.findOne(options, done);
// find one existing file
try {
const cursor = this.storage.find(options);
if (!cursor) { return done(new Error('Collection not found')); }
cursor.next(done);
}
// catch find errors
catch (error) {
done(error);
}

@@ -325,15 +326,15 @@ };

* @since 0.1.0
* @version 0.4.0
* @version 0.6.0
* @instance
*/
GridFSStorage.prototype.findById = function (id, done) {
this.findOne({
_id: id
}, done);
GridFSStorage.prototype.findById = function findById(id, done) {
// TODO support options
// find(filter, options)
this.findOne({ _id: id }, done);
};
//readers
/**

@@ -351,3 +352,3 @@ * @function

* @since 0.1.0
* @version 0.4.0
* @version 0.6.0
* @instance

@@ -362,15 +363,9 @@ * @example

*/
GridFSStorage.prototype.readById = function (id, done) {
GridFSStorage.prototype.readById = function readById(id, done) {
//obtain collection
const collection = this.options.collection;
// TODO support options
// openDownloadStream(id, options)
//prepare file remove options
const options = {
_id: id,
root: collection
};
//create a readable stream for a specific file
const stream = this.storage.createReadStream(options);
const stream = this.storage.openDownloadStream(id);

@@ -402,3 +397,3 @@ //pipe the whole stream into buffer if callback provided

* @since 0.1.0
* @version 0.4.0
* @version 0.6.0
* @instance

@@ -413,31 +408,27 @@ * @example

*/
GridFSStorage.prototype.readByFileName = function (filename, done) {
//obtain collection
const collection = this.options.collection;
GridFSStorage.prototype.readByFileName =
GridFSStorage.prototype.readByName = function readByName(filename, done) {
//prepare file remove options
const options = {
filename: filename,
root: collection
};
// TODO support options
// openDownloadStreamByName(filename, options)
//create a readable stream for a specific file
const stream = this.storage.createReadStream(options);
//create a readable stream for a specific file
const stream = this.storage.openDownloadStreamByName(filename);
//pipe the whole stream into buffer if callback provided
if (done && _.isFunction(done)) {
read(stream, done);
}
//pipe the whole stream into buffer if callback provided
if (done && _.isFunction(done)) {
read(stream, done);
}
//return stream
else {
return stream;
}
//return stream
else {
return stream;
}
};
};
//multer GridFS storage implementation
/**

@@ -453,17 +444,18 @@ * @function

*/
GridFSStorage.prototype._handleFile = function _handleFile(request, file, done) {
GridFSStorage.prototype._handleFile =
function _handleFile(request, file, done) {
//obtain file readable stream
const stream = file.stream;
//obtain file readable stream
const stream = file.stream;
//prepare file details
const fileDetails = _.merge({}, {
filename: file.originalname,
contentType: file.mimetype
});
//prepare file details
const fileDetails = _.merge({}, {
filename: file.originalname,
contentType: file.mimetype
});
//write stream
this.write(fileDetails, stream, done);
//write stream
this.write(fileDetails, stream, done);
};
};

@@ -477,19 +469,20 @@

* @since 0.1.0
* @version 0.4.0
* @version 0.6.0
* @instance
* @see {@link https://github.com/expressjs/multer/blob/master/StorageEngine.md|StorageEngine}
*/
GridFSStorage.prototype._removeFile = function _removeFile(request, file, done) {
GridFSStorage.prototype._removeFile =
function _removeFile(request, file, done) {
//remove file
if (file._id) {
this.removeById(file._id, done);
}
//remove file
if (file._id) {
this.unlinkById(file._id, done);
}
//no operation
else {
done(null, null);
}
//no operation
else {
done(null, null);
}
};
};

@@ -496,0 +489,0 @@

{
"name": "mongoose-gridfs",
"version": "0.5.0",
"version": "0.6.0",
"description": "mongoose gridfs on top of gridfs-stream",

@@ -18,3 +18,2 @@ "main": "index.js",

"gridfs",
"gridfs-stream",
"fs",

@@ -25,3 +24,4 @@ "stream",

"file",
"storage"
"storage",
"bucket"
],

@@ -34,11 +34,14 @@ "author": "lykmapipo <lallyelias87@gmail.com>",

"dependencies": {
"@lykmapipo/gridfs-stream": "^1.2.0",
"lodash": "^4.17.10",
"stream-read": "^1.1.2"
"lodash": ">=4.17.11",
"stream-read": ">=1.1.2"
},
"peerDependencies": {
"mongoose": ">=5.4.1"
},
"devDependencies": {
"async": "^2.6.1",
"chai": "^4.1.2",
"@lykmapipo/mongoose-test-helpers": ">=0.2.4",
"async": ">=2.6.1",
"chai": "^4.2.0",
"grunt": "^1.0.3",
"grunt-contrib-jshint": "^1.1.0",
"grunt-contrib-jshint": "^2.0.0",
"grunt-contrib-watch": "^1.1.0",

@@ -49,5 +52,5 @@ "grunt-mocha-test": "^0.13.3",

"load-grunt-tasks": "^4.0.0",
"mime": "^2.3.1",
"mime": "^2.4.0",
"mocha": "^5.2.0",
"mongoose": "^5.2.5"
"mongoose": ">=5.4.1"
},

@@ -54,0 +57,0 @@ "engines": {

@@ -6,3 +6,3 @@ mongoose-gridfs

mongoose [gridfs](https://docs.mongodb.com/manual/core/gridfs/) on top of [gridfs-stream](https://github.com/aheckmann/gridfs-stream)
mongoose [gridfs](https://docs.mongodb.com/manual/core/gridfs/) on top of [new gridfs api](http://mongodb.github.io/node-mongodb-native/3.1/tutorials/gridfs/)

@@ -201,3 +201,6 @@ *Note!: Ensure mongoose connection before use*

- [MongoDB GridFS](https://docs.mongodb.org/manual/core/gridfs/)
- [Node MongoDB GridFS](http://mongodb.github.io/node-mongodb-native/3.1/tutorials/gridfs/)
- [gridfs-stream](https://github.com/aheckmann/gridfs-stream)
- [New Streaming GridFS API](https://thecodebarbarian.com/mongodb-gridfs-stream)
- [New GridFS API](http://mongodb.github.io/node-mongodb-native/3.1/tutorials/gridfs/streaming/)

@@ -204,0 +207,0 @@ ## Licence

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