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

0.14.3 • Public • Published

Nest Logo

A progressive Node.js framework for building efficient and scalable server-side applications.

NPM Version Package License NPM Downloads CircleCI Coverage Discord Backers on Open Collective Sponsors on Open Collective Support us

Description

Nest framework TypeScript base storage module.

Installation

$ npm install --save @peersyst/storage-module

How to use it

Base storage module

  • Import and register StorageModule in AppModule
import { Module } from "@nestjs/common";
import { StorageModule, StorageType } from "@peersyst/storage-module";

@Module({
    imports: [
        ConfigModule.forRoot(...),
        StorageModule.register(ConfigModule, {
          storageType: StorageType.LOCAL,
        }),
        ...
    ],
    ...
})
export class AppModule {}
  • Add StorageErrorCode and StorageErrorBody to app ErrorCodes
import { HttpStatus } from "@nestjs/common";
import { StorageErrorCode, StorageErrorBody } from "@peersyst/storage-module";

// Define app error codes
enum AppErrorCode {}

export const ErrorCode = { ...AppErrorCode, ...StorageErrorCode };
export type ErrorCodeType = AppErrorCode | StorageErrorCode;

export const ErrorBody: { [code in ErrorCodeType]: { statusCode: HttpStatus; message: string } } = {
    // Define app error code bodies
    ...StorageErrorBody,
};
  • Import StorageModule in the modules you want to use it
import { Module } from "@nestjs/common";
import { StorageModule, StorageType } from "@peersyst/storage-module";

@Module({
    imports: [
        StorageModule.register(ConfigModule, {
          storageType: StorageType.LOCAL,
        }),
        ...
    ],
    ...
})
export class ResourceModule {}
  • Inject StorageService in the in the classes you want to use it
import { InjectRepository, Inject } from "@nestjs/common";
import { StorageServiceInterface } from "@peersyst/storage-module";

export class ResourceService {
  constructor(
    @InjectRepository(Resource) private readonly resourceRepository: Repository<Resource>,
    @Inject("StorageService") private readonly storageService: StorageServiceInterface,
  )
}

S3 Storage Type

  • Add configService configuration variables
export default (): any => ({
    aws: {
        region: process.env.AWS_REGION,
        accessKeyId: process.env.AWS_ACCESS_KEY_ID,
        secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
        bucketName: process.env.AWS_BUCKET_NAME,
    },
});

Local Storage Type

  • Add configService configuration variables
export default (): any => ({
    local: {
        rootPath: process.env.LOCAL_ROOT_PATH,
    },
});

License

Nest is MIT licensed.

Readme

Keywords

none

Package Sidebar

Install

npm i @peersyst/storage-module

Weekly Downloads

231

Version

0.14.3

License

none

Unpacked Size

196 kB

Total Files

32

Last publish

Collaborators

  • peersyst