https://github.com/abskmj/gridfile
GridFile is a reusable Mongoose schema for MongoDB GridFS
https://github.com/abskmj/gridfile
gridfs javascript mongodb mongoose mongoose-model mongoose-schema npm npm-package
Last synced: 9 months ago
JSON representation
GridFile is a reusable Mongoose schema for MongoDB GridFS
- Host: GitHub
- URL: https://github.com/abskmj/gridfile
- Owner: abskmj
- License: mit
- Created: 2020-08-22T18:29:10.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2025-02-01T12:05:28.000Z (over 1 year ago)
- Last Synced: 2025-08-22T01:30:41.567Z (10 months ago)
- Topics: gridfs, javascript, mongodb, mongoose, mongoose-model, mongoose-schema, npm, npm-package
- Language: JavaScript
- Homepage: https://npmjs.com/package/gridfile
- Size: 875 KB
- Stars: 10
- Watchers: 2
- Forks: 3
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
   [](https://coveralls.io/github/abskmj/gridfile?branch=master) [](https://github.com/abskmj/gridfile)
# GridFile - Mongoose Schema for MongoDB GridFS
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.
# Installation
```bash
npm install gridfile
```
# Usage
- 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)
```
## Examples
- [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
Documentation is available at [API.md](API.md)
# Fixes & Improvements
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.
# Sponsor / Support
If you find the project interesting or helpful, please consider sponsoring or supporting it at [github.com](https://github.com/abskmj/gridfile).