An open API service indexing awesome lists of open source software.

https://github.com/the-software-compagny/nestjs_module_factorydrive

Factory drive module for NestJS framework
https://github.com/the-software-compagny/nestjs_module_factorydrive

abstract abstraction async bucket disk drive driver factory factorydrive file filesystem nestjs node nodejs npm promise spaces storage

Last synced: 4 months ago
JSON representation

Factory drive module for NestJS framework

Awesome Lists containing this project

README

          



Nest Logo


Factory drive module for NestJS framework


NPM Version
Package License
Publish Package to npmjs



# NestJS Factory drive Module
Factory drive module for NestJS Framework

## Install dependencies
```bash
yarn add @the-software-compagny/nestjs_module_factorydrive
```
## Instanciate
```ts
// app.module.ts
import { FactorydriveModule, FactorydriveService } from '@the-software-compagny/nestjs_module_factorydrive'

@Module({
imports: [
// ...
FactorydriveModule.forRootAsync({
imports: [ConfigModule],
inject: [ConfigService],
useFactory: async (config: ConfigService) => ({
...config.get('factorydrive.options'),
}),
}),
// ...
],
})
export class AppModule {
public constructor(storage: FactorydriveService) {
// If you want to add a new driver you can use the registerDriver method
storage.registerDriver('s3', AwsS3Storage)
}
}

//config.ts
export default {
// ...
factorydrive: {
options: {
default: 'local',
disks: {
local: {
driver: 'local',
config: {
root: process.cwd() + '/storage',
},
},
s3: {
driver: 's3',
config: {
credentials: {
accessKeyId: '******',
secretAccessKey: '******',
},
endpoint: 'http://minio:9000/',
region: 'us-east-1',
bucket: 'example',
forcePathStyle: true,
},
},
},
},
},
// ...
}
```
## Usage
```ts
// filestorage.service.ts
import { FactorydriveService } from '@the-software-compagny/nestjs_module_factorydrive'

@Injectable()
export class FileStorageService {
public constructor(
@InjectFactorydrive() private readonly factorydrive: FactorydriveService,
) {}

public async uploadFile(path: string, file: Express.Multer.File): Promise {
const res = await this.factorydrive.getDisk('s3').put(path, file.buffer)
return res.raw
}

public async deleteFile(path: string): Promise {
await this.factorydrive.getDisk('s3').delete(path)
}

public async moveFile(path: string, target: string): Promise {
await this.factorydrive.getDisk('s3').move(path, target)
}

public async copyFile(path: string, target: string): Promise {
await this.factorydrive.getDisk('s3').copy(path, target)
}

public async listFiles(path: string): Promise {
await this.factorydrive.getDisk('s3').flatList(path)
}
}
```