@the-software-compagny/nestjs_module_factorydrive
TypeScript icon, indicating that this package has built-in type declarations

1.1.5 • Public • Published

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

yarn add @the-software-compagny/nestjs_module_factorydrive

Instanciate

// 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

// 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<string> {
    const res = await this.factorydrive.getDisk('s3').put(path, file.buffer)
    return res.raw
  }

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

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

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

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

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
1.1.536latest

Version History

VersionDownloads (Last 7 Days)Published
1.1.536
1.1.40

Package Sidebar

Install

npm i @the-software-compagny/nestjs_module_factorydrive

Weekly Downloads

35

Version

1.1.5

License

MIT

Unpacked Size

180 kB

Total Files

107

Last publish

Collaborators

  • tacxticx88