Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@neoxia-js/nestjs-storage

Package Overview
Dependencies
Maintainers
9
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@neoxia-js/nestjs-storage

nestjs module allawing communication with different kind of local and cloud storage

  • 0.0.5
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
decreased by-100%
Maintainers
9
Weekly downloads
 
Created
Source

Nestjs storage

This package allow the management of your projects uploaded files using differents strategies

Installation

yarn add @neoxia-js/nestjs-storage

Example of usage

Create an entity which extends the package storage.entity.ts

import { Entity, OneToMany } from 'typeorm';

import {
  Storage as StorageBase,
  StorageInterface,
} from '@neoxia-js/nestjs-storage';

import { User } from '../user/user.entity';

@Entity()
export class Storage extends StorageBase implements StorageInterface {
  // the oneToMany is optional, it's just for the example
  @OneToMany(() => User, (user) => user.thumbnail)
  users: User[];
}

this entity is the one where all your files informations will be stored, you have to put a relation between the original entity that needs the file and the storage entity

// user.entity.ts
import { Entity, Column, PrimaryGeneratedColumn, ManyToOne } from 'typeorm';
import { Storage } from '../storage/storage.entity';

@Entity()
export class User {
  // ...
  @ManyToOne(() => Storage, (stoarge) => storage.users, {
    eager: true,
  })
  thumbnail: Storage;
}

import the storage module in app.module.ts, you can use the exported multer config from StorageService to initialize the MulterModule

import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { MulterModule } from '@nestjs/platform-express';
import {
  StorageModule,
  StorageModuleConfig,
  StorageService,
} from '@neoxia-js/nestjs-storage';
import { MulterOptions } from '@nestjs/platform-express/multer/interfaces/multer-options.interface';

@Module({
  imports: [
    ConfigModule.forRoot({
      isGlobal: true,
      load: [databaseConfig, storageConfig],
      envFilePath: !ENV ? '.env' : `.env.${ENV}`,
    }),
    TypeOrmModule.forRootAsync({
      imports: [ConfigModule],
      useFactory: async (configService: ConfigService) =>
        configService.get('database'),
      inject: [ConfigService],
    }),
    StorageModule.registerAsync({
      imports: [ConfigModule],
      useFactory: (configService: ConfigService): StorageModuleConfig =>
        configService.get('storage'),
      inject: [ConfigService],
    }),
    MulterModule.registerAsync({
      imports: [StorageModule],
      useFactory: (storageService: StorageService): MulterOptions =>
        storageService.getMulterConfig(),
      inject: [StorageService],
    }),

do not forget to import the ServeStaticModule if the used strategy is local (and if you need it)

// app.moduke.ts

@Module({
  imports: [
    // ...
    ServeStaticModule.forRoot({
      rootPath: join(process.cwd(), '.', 'public'),
    }),
})

here is un example of StorageModuleConfig

// src/config/storage.config.ts

import { registerAs } from '@nestjs/config';
import { StorageModuleConfig, STORAGE_KINDS } from '@neoxia-js/nestjs-storage';

export default registerAs('storage', (): StorageModuleConfig => ({
  fileSystem: process.env.FILE_SYSTEM || STORAGE_KINDS.LOCAL,
  maxFileSize: process.env.MAX_FILE_SIZE,
  local: {
    baseUrl: process.env.BASE_URL || 'http://localhost',
    publicDir: process.env.PUBLIC_DIR || 'public',
    destinationDir: process.env.STORAGE_DIR || 'storage',
  },
  azure: {
    connectionString: process.env.AZURE_STORAGE_CONNECTION_STRING,
    container: process.env.AZURE_STORAGE_CONTAINER,
    sasTime: process.env.AZURE_SAS_TIME,
  },
}));

now the multer configuration used by the FileInterceptor will be the one retrived by the storageService.getMulterConfig() and every eagered storage entity will have at least the properties

fieldname
originalname
mimetype
path
url

where url is the autogenerated (not in database) url of the media

this url is also generated using the shaed access signature (only for azure strategy for now), allowing the url to be public for a short period of time

Config description


{
  fileSystem: "local", // strategies kind, for now only "local" and "azure", default "local"
  maxFileSize: "8000000", // max file size iin bytes, default "8000000"
  local: { // options for local strategy
    baseUrl: 'http://localhost', // url of the server
    publicDir: 'public', // server public directory, default 'publiic'
    destinationDir: 'storage', // server image directory inside publiic directory, default 'storage'
  },
  azure: { // optiions for azurre strategy
    connectionString: "", // azure bucket connection string
    container: "", // azure bucket container name
    sasTime: "", // azue public sas url time validity (in minutes) default "100"
  },
}

TO DO

  • tests
  • improve documentation
  • optional sas for image url
  • implement sas for local strategy
  • custom compute filename functiion for all strategies
  • ...

Keywords

FAQs

Package last updated on 20 Jul 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc