Read and write files from MongoDb's GridFS using pull-streams.
files.write(id[, meta]) -> SinkStream() -> Promise()
files.read(id) -> SourceStream()
files.stat(id) -> Promise({ id, meta })
files.exists(id) -> Promise(Boolean)
npm install pull-mongo-files
const {GridFSBucket, MongoClient} = require('mongodb')
const client = new MongoClient('mongodb://localhost:27017/myproject')
const files = require('pull-mongo-files')(GridFSBucket, client.db())
const fs = require('node:fs')
const pull = require('pull-stream')
const toPull = require('stream-to-pull-stream')
pull(
toPull.source(fs.createReadStream('my_image.jpg'), {
name: 'image.jpg',
type:
}),
files.write(new ObjectId())
)
const pull = require('pull-stream')
const toPull = require('stream-to-pull-stream')
pull(files.read(id), toPull.sink(res))
Use write
to write any meta data and stat
to read it.
If you provide the keys name
and type
they are duplicated onto the file
document's root level as filename
and contentType
as those have special
status in other MongoDb GridFS implementations.
const uuid = require('mongo-uuid')
files.write(uuid())