lykmapipo / mongoose-gridfs

mongoose gridfs on top of new gridfs api
MIT License
91 stars 39 forks source link

Question: How to Update the File Buffer? #43

Closed 360disrupt closed 5 years ago

360disrupt commented 5 years ago

The two cases writing a new file and updating the metadata are pretty straight forward. But what's the way to update the file buffer.

const fileBuffer = file.buffer;
delete file.buffer;       
const readStream = new Readable();
readStream.push(fileBuffer);
readStream.push(null);

Writing a new file:

Attachment.write(file, readStream, callback)

But as soon as the _id already is in the DB it throws an err. My current approach is to unlink the file first but it looks smelly.

Attachment.unlink({_id: file._id}, (err) => {
    if (err) {return callback(err)}
    Attachment.write(file, readStream, callback)
});

I know I can update the metadata with Attachment.updateOne but is there a solution to update the actual file buffer?

Didn't find anything here regarding updates: https://docs.mongodb.com/manual/core/gridfs/

lykmapipo commented 5 years ago

@360disrupt I may say the only way to update the file would be creating new one.

How: