GridFile is a reusable Mongoose schema for MongoDB GridFS. No separate database setup is needed as it uses the Mongoose connection for interacting with GridFS.
npm install gridfile
- Import the schema
const schema = require('gridfile')
- Create a Mongoose model from the schema
const GridFile = mongoose.model('GridFile', schema)
- Upload a file
const fileStream = fs.createReadStream('/path/to/file.ext')
const gridFile = new GridFile()
gridFile.filename = 'file.ext'
await gridFile.upload(fileStream)
- Download a file
const fileStream = fs.createWriteStream('/path/to/file.ext')
const gridFile = GridFile.findById('id')
await gridFile.download(fileStream)
- Upload files to MongoDB GridFS with Express - Create express based APIs to upload and download files to and from MongoDB GridFS
- Mongoose Query Population with GridFile Schema - Store email attachments on GridFS and link them to an email to support Mongoose query population
Documentation is available at API.md
Head over to the issues tab at github.com 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.