mongoose-gridfs
Advanced tools
Comparing version 0.1.0 to 0.2.0
@@ -8,2 +8,2 @@ 'use strict'; | ||
//export gridfs storage | ||
module.exports = storage; | ||
module.exports = storage; |
@@ -8,3 +8,3 @@ 'use strict'; | ||
var Mixed = Schema.Types.Mixed; | ||
var WRITABLES = ['filename', 'contentType', 'aliases', 'metadata']; | ||
var WRITABLES = ['_id', 'filename', 'contentType', 'aliases', 'metadata']; | ||
@@ -17,203 +17,211 @@ /** | ||
module.exports = function(gridFSStorage) { | ||
//prepare collection | ||
var collection = [gridFSStorage.options.collection, 'files'].join('.'); | ||
var model = gridFSStorage.options.model; | ||
//prepare collection | ||
var collection = [gridFSStorage.options.collection, 'files'].join('.'); | ||
var model = gridFSStorage.options.model; | ||
var 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 | ||
}); | ||
var 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 | ||
}); | ||
//attach GridFSStorage instance | ||
GridFSSchema.statics.gridfs = gridFSStorage; | ||
//attach GridFSStorage instance | ||
GridFSSchema.statics.gridfs = gridFSStorage; | ||
//instance methods | ||
//instance methods | ||
/** | ||
* @function | ||
* @name write | ||
* @param {stream.Readable} stream readable stream | ||
* @param {Function} [done] a callback to invoke in success or error | ||
* @private | ||
* @example | ||
* var attachment = new Attachment({filename:<filename>}); | ||
* attachment.write(<readableStream>, function(error,createdAttachment){ | ||
* ... | ||
* }); | ||
*/ | ||
GridFSSchema.methods.write = function(stream, done) { | ||
//obtain writable file details | ||
var fileDetails = _.pick(this.toObject(), WRITABLES); | ||
/** | ||
* @function | ||
* @name write | ||
* @param {stream.Readable} stream readable stream | ||
* @param {Function} [done] a callback to invoke in success or error | ||
* @private | ||
* @example | ||
* var attachment = new Attachment({filename:<filename>}); | ||
* attachment.write(<readableStream>, function(error,createdAttachment){ | ||
* ... | ||
* }); | ||
*/ | ||
GridFSSchema.methods.write = function(stream, done) { | ||
//obtain writable file details | ||
var fileDetails = _.pick(this.toObject(), WRITABLES); | ||
//grab gridfs | ||
var gridfs = mongoose.model(model).gridfs; | ||
//grab gridfs | ||
var gridfs = mongoose.model(model).gridfs; | ||
//stream file to gridfs | ||
gridfs.write(fileDetails, stream, function(error, createdFile) { | ||
if (error) { | ||
done(error); | ||
} else { | ||
//read file details | ||
mongoose.model(model).findById(createdFile._id, done); | ||
} | ||
}); | ||
}; | ||
//stream file to gridfs | ||
gridfs.write(fileDetails, stream, function(error, createdFile) { | ||
if (error) { | ||
done(error); | ||
} else { | ||
//read file details | ||
mongoose.model(model).findById(createdFile._id, done); | ||
} | ||
}); | ||
}; | ||
/** | ||
* @function | ||
* @name read | ||
* @param {Function} [done] a callback to invoke on success or error | ||
* @return {stream.Readable} | ||
* @private | ||
* @example | ||
* Attachment.findById(<objectid>).exec(function(error, attachment){ | ||
* attachment.read(function(error, fileContent){ | ||
* ... | ||
* }); | ||
* }); | ||
* | ||
* or for larger file size use stream | ||
* | ||
* Attachment.findById(<objectid>).exec(function(error, attachment){ | ||
* var stream = attachment.read(); | ||
* stream.on('error', fn); | ||
* stream.on('data', fn); | ||
* stream.on('close', fn); | ||
* }); | ||
*/ | ||
GridFSSchema.methods.read = function(done) { | ||
//grab gridfs | ||
var gridfs = mongoose.model(model).gridfs; | ||
/** | ||
* @function | ||
* @name read | ||
* @param {Function} [done] a callback to invoke on success or error | ||
* @return {stream.Readable} | ||
* @private | ||
* @example | ||
* Attachment.findById(<objectid>).exec(function(error, attachment){ | ||
* attachment.read(function(error, fileContent){ | ||
* ... | ||
* }); | ||
* }); | ||
* | ||
* or for larger file size use stream | ||
* | ||
* Attachment.findById(<objectid>).exec(function(error, attachment){ | ||
* var stream = attachment.read(); | ||
* stream.on('error', fn); | ||
* stream.on('data', fn); | ||
* stream.on('close', fn); | ||
* }); | ||
*/ | ||
GridFSSchema.methods.read = function(done) { | ||
//grab gridfs | ||
var gridfs = mongoose.model(model).gridfs; | ||
//stream file out of gridfs | ||
return gridfs.readById(this._id, done); | ||
}; | ||
//stream file out of gridfs | ||
return gridfs.readById(this._id, done); | ||
}; | ||
/** | ||
* @function | ||
* @name unlink | ||
* @description remove file from gridfs and file collection | ||
* @param {Function} done a callback to invoke on success or error | ||
* @private | ||
* @return {Model} mongoose model instance | ||
* @example | ||
* attachment.unlink(function(error, unlinkedAttachment){ | ||
* ... | ||
* }); | ||
*/ | ||
GridFSSchema.methods.unlink = function(done) { | ||
//grab gridfs | ||
var gridfs = mongoose.model(model).gridfs; | ||
/** | ||
* @function | ||
* @name unlink | ||
* @description remove file from gridfs and file collection | ||
* @param {Function} done a callback to invoke on success or error | ||
* @private | ||
* @return {Model} mongoose model instance | ||
* @example | ||
* attachment.unlink(function(error, unlinkedAttachment){ | ||
* ... | ||
* }); | ||
*/ | ||
GridFSSchema.methods.unlink = function(done) { | ||
//grab gridfs | ||
var gridfs = mongoose.model(model).gridfs; | ||
//obtain file details | ||
mongoose.model(model).findById(this._id, function(error, fileDetails) { | ||
if (error) { | ||
done(error); | ||
} else { | ||
//remove file from gridfs | ||
gridfs.unlinkById(fileDetails._id, function(_error /*, id*/ ) { | ||
done(_error, fileDetails); | ||
}); | ||
} | ||
}); | ||
}; | ||
//obtain file details | ||
mongoose.model(model).findById(this._id, function(error, fileDetails) { | ||
if (error) { | ||
done(error); | ||
} else { | ||
//remove file from gridfs | ||
gridfs.unlinkById(fileDetails._id, function(_error /*, id*/ ) { | ||
done(_error, fileDetails); | ||
}); | ||
} | ||
}); | ||
}; | ||
//static methods | ||
//static methods | ||
/** | ||
* @function | ||
* @name write | ||
* @param {Object} fileDetails details of the file | ||
* @param {stream.Readable} stream readable stream | ||
* @param {Function} [done] a callback to invoke in success or error | ||
* @public | ||
* @example | ||
* Attachment.write({filename:<filename>}, <readableStream>, function(error, createdAttachment){ | ||
* ... | ||
* }); | ||
*/ | ||
GridFSSchema.statics.write = function(fileDetails, stream, done) { | ||
var $new = new this(fileDetails); | ||
$new.write(stream, done); | ||
}; | ||
/** | ||
* @function | ||
* @name write | ||
* @param {Object} fileDetails details of the file | ||
* @param {stream.Readable} stream readable stream | ||
* @param {Function} [done] a callback to invoke in success or error | ||
* @public | ||
* @example | ||
* Attachment.write({filename:<filename>}, <readableStream>, function(error, createdAttachment){ | ||
* ... | ||
* }); | ||
*/ | ||
GridFSSchema.statics.write = function(fileDetails, stream, done) { | ||
var $new = new this(fileDetails); | ||
$new.write(stream, done); | ||
}; | ||
/** | ||
* @function | ||
* @name readById | ||
* @param {ObjectId} id valid object id | ||
* @param {Function} done a callback to invoke on success or error | ||
* @return {stream.Readable} | ||
* @public | ||
* @example | ||
* Attachment.readById(<objectid>,function(error, fileContent){ | ||
* ... | ||
* }); | ||
* | ||
* or for larger file size | ||
* | ||
* var stream = Attachment.readById(<id>); | ||
*/ | ||
GridFSSchema.statics.readById = function(id, done) { | ||
return this.gridfs.readById(id, done); | ||
}; | ||
/** | ||
* @function | ||
* @name readById | ||
* @param {ObjectId} id valid object id | ||
* @param {Function} done a callback to invoke on success or error | ||
* @return {stream.Readable} | ||
* @public | ||
* @example | ||
* Attachment.readById(<objectid>,function(error, fileContent){ | ||
* ... | ||
* }); | ||
* | ||
* or for larger file size | ||
* | ||
* var stream = Attachment.readById(<id>); | ||
*/ | ||
GridFSSchema.statics.readById = function(id, done) { | ||
return this.gridfs.readById(id, done); | ||
}; | ||
/** | ||
* @function | ||
* @name unlinkById | ||
* @description remove a file from gridfs using specified objectid | ||
* @param {ObjectId} id valid object id | ||
* @param {Function} done a callback to invoke on success or error | ||
* @public | ||
* @example | ||
* Attachment.unlinkById(<id>, function(error, unlinkedFileDetails){ | ||
* ... | ||
* }); | ||
* | ||
* or | ||
* | ||
* Attachment.unlink(<id>, function(error, unlinkedFileDetails){ | ||
* ... | ||
* }); | ||
*/ | ||
GridFSSchema.statics.unlinkById = | ||
GridFSSchema.statics.unlink = function(id, done) { | ||
this.findById(id).exec(function(error, foundInstance) { | ||
foundInstance.unlink(done); | ||
}); | ||
}; | ||
/** | ||
* @function | ||
* @name unlinkById | ||
* @description remove a file from gridfs using specified objectid | ||
* @param {ObjectId} id valid object id | ||
* @param {Function} done a callback to invoke on success or error | ||
* @public | ||
* @example | ||
* Attachment.unlinkById(<id>, function(error, unlinkedFileDetails){ | ||
* ... | ||
* }); | ||
* | ||
* or | ||
* | ||
* Attachment.unlink(<id>, function(error, unlinkedFileDetails){ | ||
* ... | ||
* }); | ||
*/ | ||
GridFSSchema.statics.unlinkById = | ||
GridFSSchema.statics.unlink = function(id, done) { | ||
this.findById(id).exec(function (error, foundInstance) { | ||
if (error) { | ||
return done(error); | ||
} | ||
//return grifs schema | ||
return GridFSSchema; | ||
}; | ||
if (!foundInstance) { | ||
return done(new Error('not found')); | ||
} | ||
foundInstance.unlink(done); | ||
}); | ||
}; | ||
//return grifs schema | ||
return GridFSSchema; | ||
}; |
@@ -23,7 +23,8 @@ 'use strict'; | ||
model: 'File', | ||
collection: 'fs' | ||
collection: 'fs', | ||
mongooseConnection: null | ||
}, options); | ||
//initialize grid fs database | ||
var db = mongoose.connection.db; | ||
var db = this.options.mongooseConnection || mongoose.connection.db; | ||
@@ -334,2 +335,2 @@ //TODO ensure mongoose connection | ||
return gridfs; | ||
}; | ||
}; |
{ | ||
"name": "mongoose-gridfs", | ||
"version": "0.1.0", | ||
"description": "mongoose gridfs on top of gridfs-stream", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "grunt test" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/lykmapipo/mongoose-gridfs" | ||
}, | ||
"keywords": [ | ||
"mongoose-plugin", | ||
"mongoose", | ||
"mongodb", | ||
"gridfs", | ||
"gridfs-stream", | ||
"fs", | ||
"stream" | ||
], | ||
"author": "lykmapipo <lallyelias87@gmail.com>", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/lykmapipo/mongoose-gridfs/issues" | ||
}, | ||
"dependencies": { | ||
"gridfs-stream": "^1.1.1", | ||
"lodash": "^4.11.1", | ||
"stream-read": "^1.1.2" | ||
}, | ||
"devDependencies": { | ||
"async": "^1.5.2", | ||
"chai": "^3.5.0", | ||
"grunt": "^0.4.5", | ||
"grunt-contrib-jshint": "^1.0.0", | ||
"grunt-contrib-watch": "^1.0.0", | ||
"grunt-mocha-test": "^0.12.7", | ||
"is-stream": "^1.1.0", | ||
"jshint-stylish": "^2.1.0", | ||
"load-grunt-tasks": "^3.5.0", | ||
"mime": "^1.3.4", | ||
"mocha": "^2.4.5", | ||
"mongoose": "^4.4.12" | ||
} | ||
} | ||
"name": "mongoose-gridfs", | ||
"version": "0.2.0", | ||
"description": "mongoose gridfs on top of gridfs-stream", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "grunt test" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/lykmapipo/mongoose-gridfs" | ||
}, | ||
"keywords": [ | ||
"mongoose-plugin", | ||
"mongoose", | ||
"mongodb", | ||
"gridfs", | ||
"gridfs-stream", | ||
"fs", | ||
"stream" | ||
], | ||
"author": "lykmapipo <lallyelias87@gmail.com>", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/lykmapipo/mongoose-gridfs/issues" | ||
}, | ||
"dependencies": { | ||
"gridfs-stream": "^1.1.1", | ||
"lodash": "^4.17.4", | ||
"stream-read": "^1.1.2" | ||
}, | ||
"devDependencies": { | ||
"async": "^2.3.0", | ||
"chai": "^3.5.0", | ||
"grunt": "^1.0.1", | ||
"grunt-contrib-jshint": "^1.1.0", | ||
"grunt-contrib-watch": "^1.0.0", | ||
"grunt-mocha-test": "^0.13.2", | ||
"is-stream": "^1.1.0", | ||
"jshint-stylish": "^2.2.1", | ||
"load-grunt-tasks": "^3.5.2", | ||
"mime": "^1.3.4", | ||
"mocha": "^3.2.0", | ||
"mongoose": "^4.9.3" | ||
} | ||
} |
@@ -21,3 +21,3 @@ mongoose-gridfs | ||
//mongoose connect | ||
mongoose.connect('mongodb://localhost/test'); | ||
var connection = mongoose.connect('mongodb://localhost/test'); | ||
@@ -27,3 +27,4 @@ //instantiate mongoose-gridfs | ||
collection:'attachments', | ||
model:'Attachment' | ||
model:'Attachment', | ||
mongooseConnection: connection | ||
}); | ||
@@ -30,0 +31,0 @@ |
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
21228
479
213
Updatedlodash@^4.17.4