Last active
July 19, 2022 09:27
-
-
Save abskmj/2dafbf3296ef5dc0c7a2054110c75e53 to your computer and use it in GitHub Desktop.
Mongoose Query Population with GridFile Schema
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const mongoose = require('mongoose') | |
const schema = require('gridfile') | |
module.exports = mongoose.model('Attachment', schema, 'attachment.files') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const mongoose = require('mongoose') | |
const { Schema } = mongoose | |
const schema = new Schema({ | |
to: { | |
type: String, | |
required: true | |
}, | |
subject: { | |
type: String, | |
required: true | |
}, | |
html: { | |
type: String, | |
required: true | |
}, | |
attachments: [{ | |
type: Schema.Types.ObjectId, | |
ref: 'Attachment' | |
}] | |
}) | |
module.exports = mongoose.model('Email', schema) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const mongoose = require('mongoose') | |
const { MongoMemoryServer } = require('mongodb-memory-server') | |
const fs = require('fs') | |
const path = require('path') | |
const Email = require('./email.model') | |
const Attachment = require('./attachment.model') | |
const start = async () => { | |
// connect to a mongo database | |
const mongodb = new MongoMemoryServer() | |
const connectionUri = await mongodb.getUri() | |
await mongoose.connect( | |
connectionUri, { useNewUrlParser: true, useUnifiedTopology: true } | |
) | |
// upload an attachment from a local file | |
const fileStream = fs.createReadStream(path.join(__dirname, 'attachment.pdf')) | |
const attachment = new Attachment() | |
attachment.filename = 'attachment.pdf' | |
const uploadedAttachment = await attachment.upload(fileStream) | |
console.log(uploadedAttachment) | |
/* | |
{ | |
aliases: [], | |
_id: 5f3e8f06a37b5c4b1934a070, | |
length: 7945, | |
chunkSize: 261120, | |
uploadDate: 2020-08-20T14:56:06.925Z, | |
filename: 'attachment.pdf', | |
md5: 'fa7d7e650b2cec68f302b31ba28235d8' | |
} | |
*/ | |
// create a new email and link the uploaded attachment | |
const email = new Email() | |
email.to = 'abskmj@gmail.com' | |
email.subject = 'Test Mail' | |
email.html = 'Testing ...' | |
email.attachments.push(uploadedAttachment) | |
const savedEmail = await email.save() | |
// find an email and linked attachments | |
const foundEmail = await Email.findById(savedEmail).populate('attachments') | |
const foundAttachment = foundEmail.attachments[0] | |
// download file and save it locally | |
const downloadStream = fs.createWriteStream(path.join(__dirname, 'download.pdf')) | |
await foundAttachment.download(downloadStream) | |
// close mongo connection | |
await mongoose.connection.close() | |
await mongodb.stop() | |
} | |
start() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "gridfile-email-attachments", | |
"version": "1.0.0", | |
"description": "", | |
"main": "index.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1", | |
"start": "node ." | |
}, | |
"author": "abskmj@gmail.com", | |
"license": "MIT", | |
"devDependencies": { | |
"standard": "^14.3.4" | |
}, | |
"dependencies": { | |
"gridfile": "^1.0.1", | |
"mongodb-memory-server": "^6.8.0", | |
"mongoose": "^5.10.7" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment