https://github.com/sigalor/loopback-gridfs
Library for LoopBack 4 to access MongoDB GridFS via repositories.
https://github.com/sigalor/loopback-gridfs
gridfs loopback mongodb
Last synced: about 1 month ago
JSON representation
Library for LoopBack 4 to access MongoDB GridFS via repositories.
- Host: GitHub
- URL: https://github.com/sigalor/loopback-gridfs
- Owner: sigalor
- License: mit
- Created: 2022-12-09T13:46:03.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2023-10-09T12:44:41.000Z (over 2 years ago)
- Last Synced: 2025-12-03T03:40:24.481Z (6 months ago)
- Topics: gridfs, loopback, mongodb
- Language: TypeScript
- Homepage:
- Size: 274 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# loopback-gridfs
[](https://github.com/sigalor/loopback-gridfs/blob/main/LICENSE) [](https://www.npmjs.com/package/loopback-gridfs)
Uses MongoDB's [GridFS](https://www.mongodb.com/docs/manual/core/gridfs/) to manage binary contents of your LoopBack 4 application. Full TypeScript support. Works simply with Node.js `Buffer` objects.
## Installation
Add the loopback-gridfs dependency to your project:
```bash
npm install loopback-gridfs
```
## Usage
Simply create a new LoopBack 4 repository which inherits from `GridFSRepository`. The first parameter to `super` in the constructor is the bucket name, the second one is your existing Mongo data source. The following code would go into `src/repositories/document-contents.repository.ts` and that repository can then be injected into LoopBack 4 controllers, services etc. like any other.
```typescript
import { juggler } from '@loopback/repository';
import { GridFSRepository } from 'loopback-gridfs';
export class MyFilesRepository extends GridFSRepository {
constructor(@inject('datasources.mongo') dataSource: juggler.DataSource) {
super('MyFiles', dataSource);
}
}
```
## Reference
### GridFSRepository
| Method signature | Description |
| ----------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------- |
| `async upload(fileBuffer: Buffer, filename: string): Promise` | Uploads the specified buffer to the bucket using the specified filename and returns details about the uploaded file. |
| `async uploadIgnoreDuplicate(fileBuffer: Buffer, filename: string): Promise` | Same as `upload`, but does nothing and returns `undefined` if any file with the given name already exists. |
| `async download(filename: string): Promise` | Downloads a file using its filename. |
| `async exists(filename: string): Promise` | Returns a boolean indicating whether any file with the specified name exists. |
### GridFSFile
```typescript
interface GridFSFile {
_id: ObjectId;
length: number;
chunkSize: number;
uploadDate: Date;
filename: string;
md5?: string;
metadata?: {
container: string;
mimetype: string;
extension: string;
};
}
```
## License
MIT