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.6.2 to 1.0.0

CHANGELOG.md

6

index.js

@@ -6,6 +6,6 @@ 'use strict';

const path = require('path');
const storage = require(path.join(__dirname, 'lib', 'storage'));
const bucket = require(path.join(__dirname, 'lib', 'bucket'));
/* export gridfs storage */
module.exports = storage;
/* export gridfs bucket */
module.exports = bucket;

@@ -6,5 +6,6 @@ 'use strict';

const _ = require('lodash');
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const Mixed = Schema.Types.Mixed;
const { Schema, Mixed, copyInstance } = require('@lykmapipo/mongoose-common');
/* constants */
const WRITABLES = ['_id', 'filename', 'contentType', 'aliases', 'metadata'];

@@ -14,54 +15,77 @@

/**
* @description gridfs compactible schema
* @type {Function}
* @return {Schema} valid mongoose schema
* @function createSchema
* @name createSchema
* @description Create mongoose schema compactible with gridfs files collection.
* @return {Schema} valid mongoose schema to access specific gridfs
* files collection.
* @see {@link https://docs.mongodb.com/manual/core/gridfs/}
* @see {@link https://docs.mongodb.com/manual/core/gridfs/#the-files-collection}
* @author lally elias <lallyelias87@mail.com>
* @license MIT
* @since 1.0.0
* @version 0.1.0
* @private
*/
module.exports = function (gridFSStorage) {
//prepare collection
const collection = [gridFSStorage.options.collection, 'files'].join('.');
function createSchema(bucket) {
// obtain collection name from bucket
const collection = bucket.collectionName;
// 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] },
// gridfs files collection compactible schema
// see https://docs.mongodb.com/manual/core/gridfs/#the-files-collection
const FileSchema = new Schema({
length: { type: Number, index: true },
chunkSize: { type: Number, index: true },
uploadDate: { type: Date, index: true },
md5: { type: String, trim: true, index: true, searchable: true },
filename: { type: String, trim: true, index: true, searchable: true },
contentType: { type: String, trim: true, index: true, searchable: true },
aliases: { type: [String], sort: true, index: true, searchable: true },
metadata: { type: Mixed },
}, { collection: collection });
}, { collection });
//attach GridFSStorage instance
GridFSSchema.statics.gridfs = gridFSStorage;
// attach bucket instance
FileSchema.statics.bucket = bucket;
//instance methods
/* instances */
/**
* @function
* @function write
* @name write
* @param {stream.Readable} stream readable stream
* @param {Function} [done] a callback to invoke in success or error
* @private
* @description Write provided file into MongoDB GridFS
* @param {stream.Readable} stream readable stream
* @param {Function} [done] a callback to invoke in success or error
* @return {Model} valid instance of mongoose model.
* @see {@link https://docs.mongodb.com/manual/core/gridfs/}
* @see {@link http://mongodb.github.io/node-mongodb-native}
* @see {@link https://nodejs.org/api/stream.html#stream_writable_streams}
* @see {@link https://nodejs.org/api/stream.html#stream_readable_streams}
* @author lally elias <lallyelias87@mail.com>
* @license MIT
* @since 1.0.0
* @version 0.1.0
* @instance
* @example
* const attachment = new Attachment({filename:<filename>});
* attachment.write(<readableStream>, function(error,createdAttachment){
* ...
*
* const attachment = new Attachment({ filename });
* attachment.write(readablestream, (error, attached) => {
* //=> {_id: ..., filename: ..., ... }
* });
*
*/
GridFSSchema.methods.write = function (stream, done) {
//obtain writable file details
const fileDetails = _.pick(this.toObject(), WRITABLES);
FileSchema.methods.write = function write(stream, done) {
// obtain file writable details
const file = _.pick(copyInstance(this), WRITABLES);
//stream file to gridfs
return gridFSStorage.write(fileDetails, stream, function (error,
createdFile) {
// stream file to gridfs
bucket.writeFile(file, stream, function afterWriteFile(error, created) {
// back-off error
if (error) {
done(error);
} else {
//read file details
gridFSStorage.model.findById(createdFile._id, done);
}
});
//read file details
else {
this.constructor.findById(created._id, done);
}
}.bind(this));
};

@@ -71,47 +95,48 @@

/**
* @function
* @function read
* @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){
* ...
* });
* });
* @description Read file from MongoDB GridFS
* @param {Number} [options.revision=-1] The revision number relative to the
* oldest file with the given filename. 0 gets you the oldest file, 1 gets you
* the 2nd oldest, -1 gets you the newest.
* @param {Number} [optns.start] Optional 0-based offset in bytes to start
* streaming from.
* @param {Number} [optns.end] Optional 0-based offset in bytes to stop
* streaming before.
* @param {Function} [done] a callback to invoke on success or error.
*
* or for larger file size use stream
* Warn!: Pass callback if filesize is small enough.
* Otherwise consider using stream instead.
*
* Attachment.findById(<objectid>).exec(function(error, attachment){
* const stream = attachment.read();
* stream.on('error', fn);
* stream.on('data', fn);
* stream.on('close', fn);
* });
*/
GridFSSchema.methods.read = function (done) {
//stream file out of gridfs
return gridFSStorage.readById(this._id, done);
};
/**
* @function
* @name readByFileName
* @param {String} filename valid file name
* @param {Function} done a callback to invoke on success or error
* @return {stream.Readable}
* @public
* @return {GridFSBucketReadStream} a GridFSBucketReadStream instance.
* @see {@link https://docs.mongodb.com/manual/core/gridfs/}
* @see {@link http://mongodb.github.io/node-mongodb-native}
* @see {@link https://nodejs.org/api/stream.html#stream_writable_streams}
* @see {@link https://nodejs.org/api/stream.html#stream_readable_streams}
* @author lally elias <lallyelias87@mail.com>
* @license MIT
* @since 1.0.0
* @version 0.1.0
* @instance
* @example
* Attachment.readByFileName(<filename>,function(error, fileContent){
* ...
*
* // small file
* Attachment.findById(_id, (error, attachment) => {
* attachment.read((error, content) => { ... });
* });
*
* or for larger file size
* // large file
* Attachment.findById(_id, (error, attachment) => {
* const readstream = attachment.read();
* stream.on('error', fn);
* stream.on('data', fn);
* stream.on('close', fn);
* });
*
* const stream = Attachment.readByFileName(<filename>);
*/
GridFSSchema.statics.readByFileName = function (filename, done) {
return gridFSStorage.readByFileName(filename, done);
FileSchema.methods.read = function read(done) {
// stream file out of gridfs
const _id = this._id;
const filename = this.filename;
return bucket.readFile({ _id, filename }, done);
};

@@ -121,22 +146,31 @@

/**
* @function
* @function unlink
* @name unlink
* @description remove file from gridfs and file collection
* @param {Function} done a callback to invoke on success or error
* @private
* @alias unlink
* @description Remove an existing file and its chunks.
* @param {Function} done a callback to invoke on success or error
* @return {Model} mongoose model instance
* @author lally elias <lallyelias87@mail.com>
* @license MIT
* @since 1.0.0
* @version 0.1.0
* @instance
* @example
* attachment.unlink(function(error, unlinkedAttachment){
* ...
*
* attachment.unlink((error, unlinked) => {
* //=> {_id: ..., filename: ..., ... }
* });
*
*/
GridFSSchema.methods.unlink = function (done) {
//obtain file details
gridFSStorage.model.findById(this._id, function (error, fileDetails) {
FileSchema.methods.unlink = function unlink(done) {
// obtain file details
this.constructor.findById(this._id, function afterFindFile(error, file) {
// back-off error
if (error) {
done(error);
} else {
//remove file from gridfs
gridFSStorage.unlinkById(fileDetails._id, function (_error /*, id*/ ) {
done(_error, fileDetails);
}
//remove file from gridfs
else {
bucket.deleteFile(file._id, function afterDeleteFile(error /*, id*/ ) {
done(error, file);
});

@@ -148,20 +182,36 @@ }

//static methods
/* statics */
/**
* @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
* @description Write provided file into MongoDB GridFS
* @param {Object} file valid file details
* @param {stream.Readable} stream readable stream
* @param {Function} [done] a callback to invoke in success or error
* @return {GridFSBucketWriteStream} a GridFSBucketWriteStream instance.
* @see {@link https://docs.mongodb.com/manual/core/gridfs/}
* @see {@link http://mongodb.github.io/node-mongodb-native}
* @see {@link https://nodejs.org/api/stream.html#stream_writable_streams}
* @see {@link https://nodejs.org/api/stream.html#stream_readable_streams}
* @author lally elias <lallyelias87@mail.com>
* @license MIT
* @since 1.0.0
* @version 0.1.0
* @static
* @public
* @example
* Attachment.write({filename:<filename>}, <readableStream>, function(error, createdAttachment){
* ...
*
* // large file
* const writeStream = Attachment.write({ filename }, readStream);
*
* // small file
* Attachment.write({ filename }, readstream, (error, file) => {
* //=> {_id: ..., filename: ..., ... }
* });
*
*/
GridFSSchema.statics.write = function (fileDetails, stream, done) {
const $new = new this(fileDetails);
$new.write(stream, done);
FileSchema.statics.write = function write(file, stream, done) {
const _file = new this(file);
_file.write(stream, done);
};

@@ -171,19 +221,46 @@

/**
* @function
* @name readById
* @param {ObjectId} id valid object id
* @param {Function} done a callback to invoke on success or error
* @return {stream.Readable}
* @function read
* @name read
* @description Read file from MongoDB GridFS
* @param {Object} optns valid criteria for read existing file.
* @param {ObjectId} optns._id The id of the file doc
* @param {String} [optns.filename] The name of the file doc to stream
* @param {Number} [options.revision=-1] The revision number relative to the
* oldest file with the given filename. 0 gets you the oldest file, 1 gets you
* the 2nd oldest, -1 gets you the newest.
* @param {Number} [optns.start] Optional 0-based offset in bytes to start
* streaming from.
* @param {Number} [optns.end] Optional 0-based offset in bytes to stop
* streaming before.
* @param {Function} [done] a callback to invoke on success or error.
*
* Warn!: Pass callback if filesize is small enough.
* Otherwise consider using stream instead.
*
* @param {Function} [done] a callback to invoke on success or error
* @return {GridFSBucketReadStream} a GridFSBucketReadStream instance.
* @see {@link https://docs.mongodb.com/manual/core/gridfs/}
* @see {@link http://mongodb.github.io/node-mongodb-native}
* @see {@link https://nodejs.org/api/stream.html#stream_writable_streams}
* @see {@link https://nodejs.org/api/stream.html#stream_readable_streams}
* @author lally elias <lallyelias87@mail.com>
* @license MIT
* @since 1.0.0
* @version 0.1.0
* @static
* @public
* @example
* Attachment.readById(<objectid>,function(error, fileContent){
* ...
* });
*
* or for larger file size
* // small file
* Attachment.read({ _id }, (error, content) => { ... });
*
* const stream = Attachment.readById(<id>);
* // large file
* const readstream = Attachment.read({ _id });
* stream.on('error', fn);
* stream.on('data', fn);
* stream.on('close', fn);
*
*/
GridFSSchema.statics.readById = function (id, done) {
return gridFSStorage.readById(id, done);
FileSchema.statics.read = function read(optns, done) {
return bucket.readFile(optns, done);
};

@@ -193,37 +270,45 @@

/**
* @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
* @function unlink
* @name unlink
* @alias unlink
* @description Remove an existing file and its chunks.
* @param {ObjectId} _id The id of the file doc
* @param {Function} done a callback to invoke on success or error
* @return {Model} mongoose model instance
* @author lally elias <lallyelias87@mail.com>
* @license MIT
* @since 1.0.0
* @version 0.1.0
* @static
* @public
* @example
* Attachment.unlinkById(<id>, function(error, unlinkedFileDetails){
* ...
*
* Attachment.unlink(_id, (error, unlinked) => {
* //=> {_id: ..., filename: ..., ... }
* });
*
* or
*
* Attachment.unlink(<id>, function(error, unlinkedFileDetails){
* ...
* });
*/
GridFSSchema.statics.unlinkById =
GridFSSchema.statics.unlink = function (id, done) {
gridFSStorage.model.findById(id, function (error, foundInstance) {
if (error) {
return done(error);
}
FileSchema.statics.unlink = function unlink(_id, done) {
this.findById(_id, function (error, file) {
// back-off error
if (error) {
return done(error);
}
if (!foundInstance) {
return done(new Error('not found'));
}
if (!file) {
return done(new Error('not found'));
}
foundInstance.unlink(done);
});
};
//remove file from gridfs
file.unlink(done);
});
};
//return grifs schema
return GridFSSchema;
// return grifs schema
return FileSchema;
};
}
/* export bucket schema creator */
module.exports = exports = createSchema;
{
"name": "mongoose-gridfs",
"version": "0.6.2",
"version": "1.0.0",
"description": "mongoose gridfs on top of gridfs-stream",
"main": "index.js",
"scripts": {
"test": "grunt test"
"test": "grunt test",
"docs": "doxdox 'lib/**/*.js' -p package.json -l markdown -o DOCUMENTATION.md"
},

@@ -24,3 +25,6 @@ "repository": {

"storage",
"bucket"
"bucket",
"upload",
"download",
"chuck"
],

@@ -33,2 +37,3 @@ "author": "lykmapipo <lallyelias87@gmail.com>",

"dependencies": {
"@lykmapipo/mongoose-common": ">=0.20.0",
"lodash": ">=4.17.11",

@@ -38,18 +43,24 @@ "stream-read": ">=1.1.2"

"peerDependencies": {
"mongoose": ">=5.4.1"
"mongoose": ">=5.5.4"
},
"devDependencies": {
"@lykmapipo/mongoose-test-helpers": ">=0.2.4",
"async": ">=2.6.1",
"@lykmapipo/mongoose-test-helpers": ">=0.5.2",
"async": ">=2.6.2",
"body-parser": "^1.19.0",
"chai": "^4.2.0",
"grunt": "^1.0.3",
"grunt-contrib-jshint": "^2.0.0",
"doxdox": "^3.0.0",
"express": "^4.16.4",
"form-data": "^2.3.3",
"grunt": "^1.0.4",
"grunt-contrib-jshint": "^2.1.0",
"grunt-contrib-watch": "^1.1.0",
"grunt-mocha-test": "^0.13.3",
"is-stream": "^1.1.0",
"is-stream": "^2.0.0",
"jshint-stylish": "^2.2.1",
"load-grunt-tasks": "^4.0.0",
"mime": "^2.4.0",
"mocha": "^5.2.0",
"mongoose": ">=5.4.1"
"mime": "^2.4.2",
"mocha": "^6.1.4",
"mongoose": ">=5.5.4",
"multer": "^1.4.1",
"on-finished": "^2.3.0"
},

@@ -56,0 +67,0 @@ "engines": {

@@ -12,3 +12,3 @@ mongoose-gridfs

```sh
$ npm install --save mongoose mongoose-gridfs
$ npm install --save mongoose-gridfs
```

@@ -18,144 +18,35 @@

```js
// dependencies
const fs = require('fs');
const mongoose = require('mongoose');
const gridfs = require('mongoose-gridfs');
const { createReadStream } = require('fs');
const { createModel } = require('mongoose-gridfs');
// ensure mongoose connect
mongoose.connect('mongodb://localhost/test');
// use default bucket
const Attachment = createModel();
// instantiate mongoose-gridfs
const { model: Attachment } = gridfs({
collection: 'attachments',
model: 'Attachment',
mongooseConnection: mongoose.connection
// or create custom bucket with custom options
const Attachment = createModel({
modelName: 'Attachment',
connection: connection
});
// create or save a file to gridfs
const readStream = fs.createReadStream('/some/path/sample.txt');
// write file to gridfs
const readStream = createReadStream('sample.txt');
const options = ({ filename: 'sample.txt', contentType: 'text/plain' });
Attachment.write(options, readStream, (error, file) => { ... });
Attachment.write(options, readStream, (error, file) => {
//=> {_id: ..., filename: ..., ...}
});
// for larger file size, read a file and receive a readable stream
// read larger file
const readStream = Attachment.readById(objectid);
// for smaller file size, read a file and receive a buffer
// read smaller file
Attachment.readById(objectid, (error, buffer) => { ... });
// remove file details and its content from gridfs
// remove file and its content
Attachment.unlinkById(objectid, (error) => { ... });
```
## API
`mongoose-gridfs` wrap [new gridfs api](http://mongodb.github.io/node-mongodb-native/3.1/tutorials/gridfs/) to provide valid mongoose `schema` and `model` to use with [MongoDB GridFS](https://docs.mongodb.org/manual/core/gridfs/).
Each instance of `mongoose-gridfs` is binded to a specific `GridFS collection` and `mongoose model` or `schema` by using options.
**[Read Documentation](DOCUMENTATION.md)**
### Options
- `collection` a root collection to use in GridFS. default to `fs`
- `model` a model name to use in mongoose. default to `File`
Example
```js
const gridfs = require('mongoose-gridfs')({
collection: 'attachments',
model: 'Attachment'
});
```
### Schema & Model
To obtain underlying model use
```js
const Attachment = gridfs.model
```
To obtain underlying schema for self model registration use
```js
const AttachmentSchema = gridfs.schema;
// attach plugins
// ensure indexes
// register and export a model
module.export = mongoose.model('Attachment', AttachmentSchema);
```
### Static Methods
#### `write(fileDetails:Object, stream:Readable, done(error, createdFile))`
Write a readable stream into gridfs storage
##### Example
```js
const readStream = fs.createReadStream('/some/path/sample.txt');
const options = ({ filename: 'sample.txt', contentType: 'text/plain' });
Attachment.write(options, readStream, (error, attachment) => { ... });
```
#### `readById(objectid:ObjectId, [done(error, fileContent)]):Stream`
Read a file content from gridfs storage.
##### Example for smaller file size
```js
Attachment.readById(objectid, (error, content) => { ... });
```
##### Example for larger file size
```js
const readStream = Attachment.readById(objectid);
readStream.on('error', fn);
readStream.on('data', fn);
readStream.on('close', fn);
```
#### `unlinkById(objectid:ObjectId, done(error, unlinkedFile))`
Remove file details and its content from underlying gridfs collection.
##### Example
```js
Attachment.unlinkById(objectid, (error, unlinkedAttachment) => { ... });
Attachment.unlink(objectid, (error, unlinkedAttachment) => { ... });
```
### Instance Methods
#### `write(stream:Readable, done(error, createdFile))`
Write a readable stream into gridfs storage
##### Example
```js
const readStream = fs.createReadStream('/some/path/sample.txt');
const attachment = new Attachment({
filename: 'sample.txt',
contentType: 'text/plain'
});
attachment.write(readStream, function (error, attachment) => { ... });
```
#### `read([done(error, fileContent)]):Stream`
Read a file content from gridfs storage.
##### Example for smaller file size
```js
attachment.read((error, content) => { ... });
```
##### Example for larger file size
```js
const readStream = attachment.read();
readStream.on('error', fn);
readStream.on('data', fn);
readStream.on('close', fn);
```
#### `unlink(done(error, unlinkedFile))`
Remove file details and its content from underlying gridfs collection.
##### Example
```js
attachment.unlink((error, unlinkedAttachment) => { ... });
```
## Literature Reviewed

@@ -162,0 +53,0 @@ - [MongoDB GridFS](https://docs.mongodb.org/manual/core/gridfs/)

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