You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
				
					
					
						
							373 lines
						
					
					
						
							6.8 KiB
						
					
					
				
			
		
		
	
	
							373 lines
						
					
					
						
							6.8 KiB
						
					
					
				<!-- Generated by documentation.js. Update this documentation by updating the source code. -->
 | 
						|
 | 
						|
### Table of Contents
 | 
						|
 | 
						|
-   [GridFile][1]
 | 
						|
-   [GridFile#length][2]
 | 
						|
-   [GridFile#chunkSize][3]
 | 
						|
-   [GridFile#uploadDate][4]
 | 
						|
-   [GridFile#md5][5]
 | 
						|
-   [GridFile#filename][6]
 | 
						|
-   [GridFile#contentType][7]
 | 
						|
-   [GridFile#metadata][8]
 | 
						|
-   [GridFile#aliases][9]
 | 
						|
-   [GridFile#createdAt][10]
 | 
						|
-   [GridFile#chunkSizeBytes][11]
 | 
						|
-   [GridFile.getBucket][12]
 | 
						|
    -   [Examples][13]
 | 
						|
-   [GridFile.findOneAndDelete][14]
 | 
						|
    -   [Examples][15]
 | 
						|
-   [GridFile.findByIdAndDelete][16]
 | 
						|
    -   [Examples][17]
 | 
						|
-   [GridFile#getUploadStream][18]
 | 
						|
    -   [Examples][19]
 | 
						|
-   [GridFile#getDownloadStream][20]
 | 
						|
    -   [Examples][21]
 | 
						|
-   [GridFile#uploadStream][22]
 | 
						|
    -   [Parameters][23]
 | 
						|
    -   [Examples][24]
 | 
						|
-   [GridFile#downloadStream][25]
 | 
						|
    -   [Parameters][26]
 | 
						|
    -   [Examples][27]
 | 
						|
-   [GridFile#upload][28]
 | 
						|
    -   [Parameters][29]
 | 
						|
    -   [Examples][30]
 | 
						|
-   [GridFile#download][31]
 | 
						|
    -   [Parameters][32]
 | 
						|
    -   [Examples][33]
 | 
						|
 | 
						|
## GridFile
 | 
						|
 | 
						|
Mongoose schema for MongoDB GridFS
 | 
						|
 | 
						|
```javascript
 | 
						|
const mongoose = require('mongoose')
 | 
						|
const schema = require('gridfile')
 | 
						|
 | 
						|
const GridFile = mongoose.model('GridFile', schema)
 | 
						|
 | 
						|
const gridFile = new GridFile()
 | 
						|
```
 | 
						|
 | 
						|
## GridFile#length
 | 
						|
 | 
						|
Type: [Number][34]
 | 
						|
 | 
						|
## GridFile#chunkSize
 | 
						|
 | 
						|
Type: [Number][34]
 | 
						|
 | 
						|
## GridFile#uploadDate
 | 
						|
 | 
						|
Type: [Date][35]
 | 
						|
 | 
						|
## GridFile#md5
 | 
						|
 | 
						|
Type: [String][36]
 | 
						|
 | 
						|
## GridFile#filename
 | 
						|
 | 
						|
A MD5 hash is auto-generated when a file is uploaded
 | 
						|
 | 
						|
Type: [String][36]
 | 
						|
 | 
						|
## GridFile#contentType
 | 
						|
 | 
						|
Value is be used as `contentType` option when opening an upload stream: [GridFSBucket#openUploadStream][37]
 | 
						|
 | 
						|
Type: [String][36]
 | 
						|
 | 
						|
## GridFile#metadata
 | 
						|
 | 
						|
Value is be used as `metadata` option when opening an upload stream: [GridFSBucket#openUploadStream][37]
 | 
						|
 | 
						|
Type: Any
 | 
						|
 | 
						|
## GridFile#aliases
 | 
						|
 | 
						|
Value is be used as `aliases` option when opening an upload stream: [GridFSBucket#openUploadStream][37]
 | 
						|
 | 
						|
Type: \[[String][36]]
 | 
						|
 | 
						|
## GridFile#createdAt
 | 
						|
 | 
						|
Alias for GridFile#uploadDate
 | 
						|
 | 
						|
Type: [Date][35]
 | 
						|
 | 
						|
## GridFile#chunkSizeBytes
 | 
						|
 | 
						|
Value is be used as `chunkSizeBytes` option when opening an upload stream: [GridFSBucket#openUploadStream][37]
 | 
						|
 | 
						|
Type: [Number][34]
 | 
						|
 | 
						|
## GridFile.getBucket
 | 
						|
 | 
						|
Get the GridFS bucket created from the Mongoose connection
 | 
						|
 | 
						|
Type: [Function][38]
 | 
						|
 | 
						|
### Examples
 | 
						|
 | 
						|
```javascript
 | 
						|
const bucket = GridFile.getBucket()
 | 
						|
```
 | 
						|
 | 
						|
Returns **GridFSBucket** GridFS Bucket
 | 
						|
 | 
						|
## GridFile.findOneAndDelete
 | 
						|
 | 
						|
Delete a file from GridFS using [GridFSBucket#delete][39]
 | 
						|
 | 
						|
Type: [Function][38]
 | 
						|
 | 
						|
### Examples
 | 
						|
 | 
						|
```javascript
 | 
						|
const deletedFile = await GridFile.findOneAndDelete({ filename: 'image.png' })
 | 
						|
```
 | 
						|
 | 
						|
Returns **[Promise][40]<[GridFile][41]>** Deleted GridFile as a Promise
 | 
						|
 | 
						|
## GridFile.findByIdAndDelete
 | 
						|
 | 
						|
Delete a file from GridFS using [GridFSBucket#delete][39]
 | 
						|
 | 
						|
Type: [Function][38]
 | 
						|
 | 
						|
### Examples
 | 
						|
 | 
						|
```javascript
 | 
						|
const deletedFile = await GridFile.findByIdAndDelete('some-id')
 | 
						|
```
 | 
						|
 | 
						|
Returns **[Promise][40]<[GridFile][41]>** Deleted GridFile as a Promise
 | 
						|
 | 
						|
## GridFile#getUploadStream
 | 
						|
 | 
						|
Get a GridFS stream to upload a file
 | 
						|
 | 
						|
Type: [Function][38]
 | 
						|
 | 
						|
### Examples
 | 
						|
 | 
						|
```javascript
 | 
						|
const uploadStream = gridFile.getUploadStream()
 | 
						|
```
 | 
						|
 | 
						|
Returns **GridFSBucketWriteStream** Upload Stream
 | 
						|
 | 
						|
## GridFile#getDownloadStream
 | 
						|
 | 
						|
Get a GridFS stream to download a file
 | 
						|
 | 
						|
Type: [Function][38]
 | 
						|
 | 
						|
### Examples
 | 
						|
 | 
						|
```javascript
 | 
						|
const downloadStream = gridFile.getDownloadStream()
 | 
						|
```
 | 
						|
 | 
						|
Returns **GridFSBucketReadStream** Download Stream
 | 
						|
 | 
						|
## GridFile#uploadStream
 | 
						|
 | 
						|
Upload a file to GridFS
 | 
						|
 | 
						|
Type: [Function][38]
 | 
						|
 | 
						|
### Parameters
 | 
						|
 | 
						|
-   `FileStream` **[Stream][42]** Read stream of file to upload
 | 
						|
 | 
						|
### Examples
 | 
						|
 | 
						|
```javascript
 | 
						|
const fs = require('fs')
 | 
						|
 | 
						|
const fileStream = fs.createReadStream('/path/to/file')
 | 
						|
const uploadStream = gridFile.uploadStream(fileStream)
 | 
						|
 | 
						|
uploadStream.on('finish', (file) => {
 | 
						|
 console.log(file)
 | 
						|
})
 | 
						|
```
 | 
						|
 | 
						|
Returns **GridFSBucketWriteStream** Upload Stream
 | 
						|
 | 
						|
## GridFile#downloadStream
 | 
						|
 | 
						|
Download a file from GridFS
 | 
						|
 | 
						|
Type: [Function][38]
 | 
						|
 | 
						|
### Parameters
 | 
						|
 | 
						|
-   `FileStream` **[Stream][42]** Write stream of file to download into
 | 
						|
 | 
						|
### Examples
 | 
						|
 | 
						|
```javascript
 | 
						|
const fs = require('fs')
 | 
						|
 | 
						|
const fileStream = fs.createWriteStream('/path/to/file')
 | 
						|
const DownloadStream = gridFile.downloadStream(fileStream)
 | 
						|
 | 
						|
fileStream.on('finish', () => {
 | 
						|
 console.log('File downloaded successfully')
 | 
						|
})
 | 
						|
```
 | 
						|
 | 
						|
Returns **GridFSBucketWriteStream** Download Stream
 | 
						|
 | 
						|
## GridFile#upload
 | 
						|
 | 
						|
Upload a file to GridFS
 | 
						|
 | 
						|
Type: [Function][38]
 | 
						|
 | 
						|
### Parameters
 | 
						|
 | 
						|
-   `FileStream` **[Stream][42]** Read stream of file to upload
 | 
						|
-   `Callback` **[Function][38]** Callback function
 | 
						|
 | 
						|
### Examples
 | 
						|
 | 
						|
```javascript
 | 
						|
const fs = require('fs')
 | 
						|
 | 
						|
const fileStream = fs.createReadStream('/path/to/file')
 | 
						|
const uploadedFile = await gridFile.upload(fileStream)
 | 
						|
```
 | 
						|
 | 
						|
```javascript
 | 
						|
// callback
 | 
						|
gridFile.upload(filestream, (err, uploadedFile) => {
 | 
						|
 if(err){
 | 
						|
   console.error(err)
 | 
						|
 } else {
 | 
						|
   console.log(uploadedFile)
 | 
						|
 }
 | 
						|
})
 | 
						|
```
 | 
						|
 | 
						|
Returns **[Promise][40]<[GridFile][41]>** GridFile as a Promise
 | 
						|
 | 
						|
## GridFile#download
 | 
						|
 | 
						|
Download a file from GridFS
 | 
						|
 | 
						|
Type: [Function][38]
 | 
						|
 | 
						|
### Parameters
 | 
						|
 | 
						|
-   `FileStream` **[Stream][42]** Write stream of file to download into
 | 
						|
-   `Callback` **[Function][38]** Callback function
 | 
						|
 | 
						|
### Examples
 | 
						|
 | 
						|
```javascript
 | 
						|
const fs = require('fs')
 | 
						|
 | 
						|
const fileStream = fs.createWriteStream('/path/to/file')
 | 
						|
await gridFile.download(fileStream)
 | 
						|
```
 | 
						|
 | 
						|
```javascript
 | 
						|
// callback
 | 
						|
gridFile.download(fileStream, (err){
 | 
						|
 if(err){
 | 
						|
   console.error(err)
 | 
						|
 } else {
 | 
						|
   console.log('File downloaded successfully')
 | 
						|
 }
 | 
						|
})
 | 
						|
```
 | 
						|
 | 
						|
Returns **[Promise][40]<Void>** Promise
 | 
						|
 | 
						|
[1]: #gridfile
 | 
						|
 | 
						|
[2]: #gridfilelength
 | 
						|
 | 
						|
[3]: #gridfilechunksize
 | 
						|
 | 
						|
[4]: #gridfileuploaddate
 | 
						|
 | 
						|
[5]: #gridfilemd5
 | 
						|
 | 
						|
[6]: #gridfilefilename
 | 
						|
 | 
						|
[7]: #gridfilecontenttype
 | 
						|
 | 
						|
[8]: #gridfilemetadata
 | 
						|
 | 
						|
[9]: #gridfilealiases
 | 
						|
 | 
						|
[10]: #gridfilecreatedat
 | 
						|
 | 
						|
[11]: #gridfilechunksizebytes
 | 
						|
 | 
						|
[12]: #gridfilegetbucket
 | 
						|
 | 
						|
[13]: #examples
 | 
						|
 | 
						|
[14]: #gridfilefindoneanddelete
 | 
						|
 | 
						|
[15]: #examples-1
 | 
						|
 | 
						|
[16]: #gridfilefindbyidanddelete
 | 
						|
 | 
						|
[17]: #examples-2
 | 
						|
 | 
						|
[18]: #gridfilegetuploadstream
 | 
						|
 | 
						|
[19]: #examples-3
 | 
						|
 | 
						|
[20]: #gridfilegetdownloadstream
 | 
						|
 | 
						|
[21]: #examples-4
 | 
						|
 | 
						|
[22]: #gridfileuploadstream
 | 
						|
 | 
						|
[23]: #parameters
 | 
						|
 | 
						|
[24]: #examples-5
 | 
						|
 | 
						|
[25]: #gridfiledownloadstream
 | 
						|
 | 
						|
[26]: #parameters-1
 | 
						|
 | 
						|
[27]: #examples-6
 | 
						|
 | 
						|
[28]: #gridfileupload
 | 
						|
 | 
						|
[29]: #parameters-2
 | 
						|
 | 
						|
[30]: #examples-7
 | 
						|
 | 
						|
[31]: #gridfiledownload
 | 
						|
 | 
						|
[32]: #parameters-3
 | 
						|
 | 
						|
[33]: #examples-8
 | 
						|
 | 
						|
[34]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number
 | 
						|
 | 
						|
[35]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Date
 | 
						|
 | 
						|
[36]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String
 | 
						|
 | 
						|
[37]: https://mongodb.github.io/node-mongodb-native/3.6/api/GridFSBucket.html#openUploadStream
 | 
						|
 | 
						|
[38]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function
 | 
						|
 | 
						|
[39]: https://mongodb.github.io/node-mongodb-native/3.6/api/GridFSBucket.html#delete
 | 
						|
 | 
						|
[40]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise
 | 
						|
 | 
						|
[41]: #gridfile
 | 
						|
 | 
						|
[42]: https://nodejs.org/api/stream.html
 |