gridfile
Version:
GridFile is a reusable Mongoose schema for MongoDB GridFS
50 lines (38 loc) • 2.27 kB
Markdown
   [](https://coveralls.io/github/abskmj/gridfile?branch=master) [](https://github.com/abskmj/gridfile)
GridFile is a reusable [Mongoose](https://mongoosejs.com/) schema for [MongoDB GridFS](https://docs.mongodb.com/manual/core/gridfs/). No separate database setup is needed as it uses the Mongoose connection for interacting with GridFS.
```bash
npm install gridfile
```
- Import the schema
```javascript
const schema = require('gridfile')
```
- Create a Mongoose model from the schema
```javascript
const GridFile = mongoose.model('GridFile', schema)
```
- Upload a file
```javascript
const fileStream = fs.createReadStream('/path/to/file.ext')
const gridFile = new GridFile()
gridFile.filename = 'file.ext'
await gridFile.upload(fileStream)
```
- Download a file
```javascript
const fileStream = fs.createWriteStream('/path/to/file.ext')
const gridFile = GridFile.findById('id')
await gridFile.download(fileStream)
```
- [Upload files to MongoDB GridFS with Express](https://abskmj.github.io/notes/posts/express/express-multer-mongoose-gridfile/) - Create express based APIs to upload and download files to and from MongoDB GridFS
- [Mongoose Query Population with GridFile Schema](https://gist.github.com/abskmj/2dafbf3296ef5dc0c7a2054110c75e53) - Store email attachments on GridFS and link them to an email to support Mongoose query population
Documentation is available at [API.md](API.md)
Head over to the issues tab at [github.com](https://github.com/abskmj/gridfile/issues) to report a bug or suggest an improvement. Feel free to contribute to the code or documentation by creating a pull request.
If you find the project interesting or helpful, please consider sponsoring or supporting it at [github.com](https://github.com/abskmj/gridfile).