@nestjs-land/storage
TypeScript icon, indicating that this package has built-in type declarations

1.0.3 • Public • Published

NestJS Land: Storage

Files storage module with S3 for NestJS applications.

Features

  • [x] Connect to AWS S3 compatible services.
  • [x] Provides a File TypeORM entity that is stored in a database and can be used for relations

Installation

$ npm install @nestjs-land/storage

Setup

Import the StorageModule into your application module:

...
import { StorageModule } from '@nestjs-land/storage';

@Module({
  imports: [
    StorageModule.forRoot({
      endpoint: 's3.us-west-2.amazonaws.com',
      region: 'us-west-2',
      bucket: 'my-bucket',
      accessKey: 'access-key',
      secretKey: 'secret-key',
    }),
  ]
})
export class AppModule {}

Usage

// 1. Set relations to the File entity where needed
import { File } from '@nestjs-land/storage';

@Entity()
export class User {
  // ...

  @OneToOne(() => File, { nullable: true, eager: true })
  @JoinColumn()
  profilePicture: File;
}

// 2. Read files from HTTP request
@Controller('users')
export class UsersController {
  constructor(private readonly usersService: UsersService) {}

  // ...

  @Post(':id/profilePicture')
  @UseInterceptors(FileInterceptor('file'))
  uploadProfilePicture(@UploadedFile() file, @Param('id') id: string) {
    this.usersService.saveProfilePicture(+id, file.buffer);
  }
}

// 3. Use the StorageService to save or delete files
import { StorageService } from '../../libs/storage/src';

@Injectable()
export class UsersService {
  constructor(
    // ...
    private readonly storageService: StorageService,
  ) {}

  // ...

  async saveProfilePicture(userId: number, buffer: Buffer) {
    const user = await this.findOne(userId);
    const file = await this.storageService.saveFile(buffer);
    user.profilePicture = file;
    this.entityManager.save(User, user);
  }
}

Package Sidebar

Install

npm i @nestjs-land/storage

Weekly Downloads

1

Version

1.0.3

License

ISC

Unpacked Size

527 kB

Total Files

24

Last publish

Collaborators

  • jeserodz